#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <cuda.h>

//#define DBUG

float* loadSpins(const char *filename, int *Nbits) {
    
    FILE *fp = fopen(filename, "r");
    int errnum = 0;
    if (fp == NULL){
        errnum = errno;
	fprintf(stderr, "Error opening file: %s\n", strerror(errnum));
	return NULL;
    }
    else{

        int ch = 0;
        int Nbytes=-1;
       
        while(!feof(fp)){
            ch = fgetc(fp);
    	    Nbytes++;
        };
    
#ifdef DBUG
        printf("Loaded %d bytes\n", Nbytes);
#endif 

        //float *spins = (float *)malloc(Nbytes * 8 * sizeof(int));
	float *h_spins;
        cudaError_t status = cudaMallocHost((void **)&h_spins, Nbytes*8*sizeof(float));
	if (status != cudaSuccess){
	    printf("Error allocating pinned host memory\n");
	    h_spins = (float *)malloc(Nbytes * 8 * sizeof(float));
	}	
	rewind(fp);
        *Nbits = Nbytes * 8;//Total sample size

        for (int t=0; t < Nbytes; t++){
            ch = fgetc(fp);
            for (int i=0; i < 8; i++){
	        int bit = (ch & (1 << (7-i))) >> (7-i);
                h_spins[8*t + i] = 2 * bit - 1; 
            }
        }
        fclose(fp);
        return h_spins;
    }
}

	.file	"tmpxft_00200e52_00000000-6_loadSpins.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"r"
.LC1:
	.string	"Error opening file: %s\n"
.LC2:
	.string	"Error allocating pinned host memory\n"
	.text
	.globl	_Z9loadSpinsPKcPi
	.type	_Z9loadSpinsPKcPi, @function
_Z9loadSpinsPKcPi:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rsi, %r13
	leaq	.LC0(%rip), %rsi
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	orl	$-1, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	jne	.L3
	call	__errno_location@PLT
	movl	(%rax), %edi
	call	strerror@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC1(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L2
.L3:
	movq	%rbx, %rdi
	call	feof@PLT
	testl	%eax, %eax
	jne	.L20
	movq	%rbx, %rdi
	incl	%ebp
	call	fgetc@PLT
	jmp	.L3
.L20:
	leal	0(,%rbp,8), %r14d
	movq	%rsp, %rdi
	movslq	%r14d, %r12
	salq	$2, %r12
	movq	%r12, %rsi
	call	cudaMallocHost@PLT
	testl	%eax, %eax
	je	.L6
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r12, %rdi
	call	malloc@PLT
	movq	%rax, (%rsp)
.L6:
	movq	%rbx, %rdi
	movl	$7, %r12d
	call	rewind@PLT
	movl	%r14d, 0(%r13)
	xorl	%r13d, %r13d
.L7:
	movq	%rbx, %rdi
	cmpl	%r13d, %ebp
	jle	.L21
	call	fgetc@PLT
	movq	%r13, %rsi
	movl	$1, %r8d
	movl	%eax, %edi
	salq	$5, %rsi
	xorl	%eax, %eax
	addq	(%rsp), %rsi
.L8:
	movl	%r12d, %ecx
	movl	%r8d, %edx
	subl	%eax, %ecx
	sall	%cl, %edx
	andl	%edi, %edx
	sarl	%cl, %edx
	leal	-1(%rdx,%rdx), %edx
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, (%rsi,%rax,4)
	incq	%rax
	cmpq	$8, %rax
	jne	.L8
	incq	%r13
	jmp	.L7
.L21:
	call	fclose@PLT
	movq	(%rsp), %rbx
.L2:
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	movq	%rbx, %rax
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9loadSpinsPKcPi, .-_Z9loadSpinsPKcPi
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2053:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2053:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <hip/hip_runtime.h>

//#define DBUG

float* loadSpins(const char *filename, int *Nbits) {
    
    FILE *fp = fopen(filename, "r");
    int errnum = 0;
    if (fp == NULL){
        errnum = errno;
	fprintf(stderr, "Error opening file: %s\n", strerror(errnum));
	return NULL;
    }
    else{

        int ch = 0;
        int Nbytes=-1;
       
        while(!feof(fp)){
            ch = fgetc(fp);
    	    Nbytes++;
        };
    
#ifdef DBUG
        printf("Loaded %d bytes\n", Nbytes);
#endif 

        //float *spins = (float *)malloc(Nbytes * 8 * sizeof(int));
	float *h_spins;
        hipError_t status = hipHostMalloc((void **)&h_spins, Nbytes*8*sizeof(float), hipHostMallocDefault);
	if (status != hipSuccess){
	    printf("Error allocating pinned host memory\n");
	    h_spins = (float *)malloc(Nbytes * 8 * sizeof(float));
	}	
	rewind(fp);
        *Nbits = Nbytes * 8;//Total sample size

        for (int t=0; t < Nbytes; t++){
            ch = fgetc(fp);
            for (int i=0; i < 8; i++){
	        int bit = (ch & (1 << (7-i))) >> (7-i);
                h_spins[8*t + i] = 2 * bit - 1; 
            }
        }
        fclose(fp);
        return h_spins;
    }
}

	.text
	.file	"loadSpins.hip"
	.globl	_Z9loadSpinsPKcPi               # -- Begin function _Z9loadSpinsPKcPi
	.type	_Z9loadSpinsPKcPi,@function
_Z9loadSpinsPKcPi:                      # @_Z9loadSpinsPKcPi
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %r14
	movl	$.L.str, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB0_12
# %bb.1:                                # %.preheader
	movq	%rax, %rbx
	movq	%rax, %rdi
	callq	feof
	movl	$-1, %r12d
	testl	%eax, %eax
	jne	.LBB0_4
# %bb.2:                                # %.lr.ph.preheader
	movl	$-1, %r12d
.LBB0_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%rbx, %rdi
	callq	fgetc
	incl	%r12d
	movq	%rbx, %rdi
	callq	feof
	testl	%eax, %eax
	je	.LBB0_3
.LBB0_4:                                # %._crit_edge
	leal	(,%r12,8), %ebp
	movslq	%ebp, %r15
	shlq	$2, %r15
	leaq	8(%rsp), %rdi
	movq	%r15, %rsi
	xorl	%edx, %edx
	callq	hipHostMalloc
	testl	%eax, %eax
	je	.LBB0_6
# %bb.5:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, 8(%rsp)
.LBB0_6:
	movq	%rbx, %rdi
	callq	rewind
	movl	%ebp, (%r14)
	testl	%r12d, %r12d
	jle	.LBB0_11
# %bb.7:                                # %.lr.ph35.preheader
	movl	%r12d, %r14d
	xorl	%r15d, %r15d
.LBB0_8:                                # %.lr.ph35
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_9 Depth 2
	movl	%r15d, %r12d
	andl	$536870911, %r12d               # imm = 0x1FFFFFFF
	shlq	$5, %r12
	movq	%rbx, %rdi
	callq	fgetc
	addq	8(%rsp), %r12
	xorl	%edx, %edx
.LBB0_9:                                #   Parent Loop BB0_8 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%edx, %ecx
	xorb	$7, %cl
	movl	$1, %esi
	shll	%cl, %esi
	andl	%eax, %esi
	shrl	%cl, %esi
	leal	-1(,%rsi,2), %ecx
	xorps	%xmm0, %xmm0
	cvtsi2ss	%ecx, %xmm0
	movss	%xmm0, (%r12,%rdx,4)
	incq	%rdx
	cmpq	$8, %rdx
	jne	.LBB0_9
# %bb.10:                               #   in Loop: Header=BB0_8 Depth=1
	incq	%r15
	cmpq	%r14, %r15
	jne	.LBB0_8
.LBB0_11:                               # %._crit_edge36
	movq	%rbx, %rdi
	callq	fclose
	movq	8(%rsp), %rbx
.LBB0_13:
	movq	%rbx, %rax
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB0_12:
	.cfi_def_cfa_offset 64
	callq	__errno_location
	movl	(%rax), %edi
	movq	stderr(%rip), %r14
	callq	strerror
	xorl	%ebx, %ebx
	movl	$.L.str.1, %esi
	movq	%r14, %rdi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB0_13
.Lfunc_end0:
	.size	_Z9loadSpinsPKcPi, .Lfunc_end0-_Z9loadSpinsPKcPi
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"r"
	.size	.L.str, 2

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Error opening file: %s\n"
	.size	.L.str.1, 24

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Error allocating pinned host memory"
	.size	.Lstr, 36

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
