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

extern int HI;      // the total number of point patterns
extern int HN;      // the total number of points
extern int HK_star; // the total number of covariates
extern int Hd;      // the dimensionality of the problem
extern int HV;      // the total number of elements in the grid

void read_files(int ** foci , float ** Z , int * foci_counts , int * brain , int* author) {
    int i , j; // those are for the for loops
    
    // read the file with the points
    FILE *file;
    file = fopen("./inputs/foci.txt","r");
    for (i=0 ; i<HN ; i++) {
        for (j=0 ; j<(Hd+1) ; j++) {
            if(!fscanf(file,"%d",&foci[i][j]))
                break;
        }
    }
    fclose(file);
    
    // read the design matrix
    file = fopen("./inputs/Z.txt","r");
    for (i=0 ; i<HI ; i++) {
        for (j=0 ; j<HK_star ; j++) {
            if(!fscanf(file,"%f",&Z[i][j]))
                break;
        }
    }
    fclose(file);

    // read the total number of points per pattern
    file = fopen("./inputs/counts.txt","r");
    for (i=0 ; i<HI ; i++) {
        if(!fscanf(file,"%d",&foci_counts[i]))
            break;
    }
    fclose(file);

    /* Binary vector indicating which voxels in the grid are within the brain mask */ 
    file = fopen("mask.txt","r");
    for (i=0 ; i<HV ; i++) {
        if(!fscanf(file,"%d",&brain[i]))
            break;
    }
    fclose(file);
    
    /* Read the publication identifier. Studies from the same paper must appear consecutive */
    file = fopen("./inputs/paper.txt","r");
    for (i=0 ; i<HI ; i++) {
        if(!fscanf(file,"%d",&author[i]))
            break;
    }
    fclose(file);

}
	.file	"tmpxft_002d8433_00000000-6_read_files.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	"./inputs/foci.txt"
.LC2:
	.string	"%d"
.LC3:
	.string	"./inputs/Z.txt"
.LC4:
	.string	"%f"
.LC5:
	.string	"./inputs/counts.txt"
.LC6:
	.string	"mask.txt"
.LC7:
	.string	"./inputs/paper.txt"
	.text
	.globl	_Z10read_filesPPiPPfS_S_S_
	.type	_Z10read_filesPPiPPfS_S_S_, @function
_Z10read_filesPPiPPfS_S_S_:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%r15d, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rdi, %r14
	leaq	.LC1(%rip), %rdi
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rsi, %r13
	leaq	.LC0(%rip), %rsi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rcx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%r8, %rbx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	call	fopen@PLT
	movq	%rax, %rdi
.L3:
	cmpl	%r15d, HN(%rip)
	jle	.L29
	xorl	%r8d, %r8d
.L4:
	cmpl	%r8d, Hd(%rip)
	jl	.L6
	movq	(%r14,%r15,8), %rax
	leaq	.LC2(%rip), %rsi
	movq	%r8, 8(%rsp)
	movq	%rdi, (%rsp)
	leaq	(%rax,%r8,4), %rdx
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movq	8(%rsp), %r8
	movq	(%rsp), %rdi
	incq	%r8
	testl	%eax, %eax
	jne	.L4
.L6:
	incq	%r15
	jmp	.L3
.L29:
	call	fclose@PLT
	leaq	.LC3(%rip), %rdi
	leaq	.LC0(%rip), %rsi
	xorl	%r14d, %r14d
	call	fopen@PLT
	movq	%rax, %rdi
.L7:
	cmpl	%r14d, HI(%rip)
	jle	.L30
	xorl	%r15d, %r15d
.L8:
	cmpl	%r15d, HK_star(%rip)
	jle	.L10
	movq	0(%r13,%r14,8), %rax
	leaq	.LC4(%rip), %rsi
	movq	%rdi, (%rsp)
	leaq	(%rax,%r15,4), %rdx
	xorl	%eax, %eax
	incq	%r15
	call	__isoc23_fscanf@PLT
	movq	(%rsp), %rdi
	testl	%eax, %eax
	jne	.L8
.L10:
	incq	%r14
	jmp	.L7
.L30:
	call	fclose@PLT
	leaq	.LC0(%rip), %rsi
	xorl	%r15d, %r15d
	leaq	.LC5(%rip), %rdi
	call	fopen@PLT
	leaq	.LC2(%rip), %r14
	movq	%rax, %r13
.L11:
	cmpl	%r15d, HI(%rip)
	jle	.L15
	movq	%r12, %rdx
	movq	%r14, %rsi
	movq	%r13, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	addq	$4, %r12
	testl	%eax, %eax
	jne	.L12
.L15:
	movq	%r13, %rdi
	leaq	.LC2(%rip), %r14
	xorl	%r13d, %r13d
	call	fclose@PLT
	leaq	.LC0(%rip), %rsi
	leaq	.LC6(%rip), %rdi
	call	fopen@PLT
	movq	%rax, %r12
	jmp	.L13
.L12:
	incl	%r15d
	jmp	.L11
.L18:
	movq	%rbp, %rdx
	movq	%r14, %rsi
	movq	%r12, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	addq	$4, %rbp
	testl	%eax, %eax
	je	.L19
	incl	%r13d
.L13:
	cmpl	%r13d, HV(%rip)
	jg	.L18
.L19:
	movq	%r12, %rdi
	leaq	.LC2(%rip), %r13
	xorl	%r12d, %r12d
	call	fclose@PLT
	leaq	.LC0(%rip), %rsi
	leaq	.LC7(%rip), %rdi
	call	fopen@PLT
	movq	%rax, %rbp
	jmp	.L17
.L21:
	movq	%rbx, %rdx
	movq	%r13, %rsi
	movq	%rbp, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	addq	$4, %rbx
	testl	%eax, %eax
	je	.L22
	incl	%r12d
.L17:
	cmpl	%r12d, HI(%rip)
	jg	.L21
.L22:
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	movq	%rbp, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	fclose@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z10read_filesPPiPPfS_S_S_, .-_Z10read_filesPPiPPfS_S_S_
	.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 <hip/hip_runtime.h>
#include <stdlib.h>
#include <stdio.h>

extern int HI;      // the total number of point patterns
extern int HN;      // the total number of points
extern int HK_star; // the total number of covariates
extern int Hd;      // the dimensionality of the problem
extern int HV;      // the total number of elements in the grid

void read_files(int ** foci , float ** Z , int * foci_counts , int * brain , int* author) {
    int i , j; // those are for the for loops
    
    // read the file with the points
    FILE *file;
    file = fopen("./inputs/foci.txt","r");
    for (i=0 ; i<HN ; i++) {
        for (j=0 ; j<(Hd+1) ; j++) {
            if(!fscanf(file,"%d",&foci[i][j]))
                break;
        }
    }
    fclose(file);
    
    // read the design matrix
    file = fopen("./inputs/Z.txt","r");
    for (i=0 ; i<HI ; i++) {
        for (j=0 ; j<HK_star ; j++) {
            if(!fscanf(file,"%f",&Z[i][j]))
                break;
        }
    }
    fclose(file);

    // read the total number of points per pattern
    file = fopen("./inputs/counts.txt","r");
    for (i=0 ; i<HI ; i++) {
        if(!fscanf(file,"%d",&foci_counts[i]))
            break;
    }
    fclose(file);

    /* Binary vector indicating which voxels in the grid are within the brain mask */ 
    file = fopen("mask.txt","r");
    for (i=0 ; i<HV ; i++) {
        if(!fscanf(file,"%d",&brain[i]))
            break;
    }
    fclose(file);
    
    /* Read the publication identifier. Studies from the same paper must appear consecutive */
    file = fopen("./inputs/paper.txt","r");
    for (i=0 ; i<HI ; i++) {
        if(!fscanf(file,"%d",&author[i]))
            break;
    }
    fclose(file);

}
	.text
	.file	"read_files.hip"
	.globl	_Z10read_filesPPiPPfS_S_S_      # -- Begin function _Z10read_filesPPiPPfS_S_S_
	.type	_Z10read_filesPPiPPfS_S_S_,@function
_Z10read_filesPPiPPfS_S_S_:             # @_Z10read_filesPPiPPfS_S_S_
	.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	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%r8, 16(%rsp)                   # 8-byte Spill
	movq	%rcx, 8(%rsp)                   # 8-byte Spill
	movq	%rdx, (%rsp)                    # 8-byte Spill
	movq	%rsi, %r12
	movq	%rdi, %r13
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %rbp
	movl	HN(%rip), %eax
	testl	%eax, %eax
	jle	.LBB0_8
# %bb.1:                                # %.preheader42.preheader
	movl	Hd(%rip), %ecx
	xorl	%ebx, %ebx
.LBB0_2:                                # %.preheader42
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
	testl	%ecx, %ecx
	js	.LBB0_7
# %bb.3:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB0_2 Depth=1
	xorl	%r14d, %r14d
	xorl	%r15d, %r15d
.LBB0_4:                                # %.lr.ph
                                        #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	(%r13,%rbx,8), %rdx
	addq	%r14, %rdx
	movl	$.L.str.2, %esi
	movq	%rbp, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	Hd(%rip), %ecx
	testl	%eax, %eax
	je	.LBB0_6
# %bb.5:                                # %.lr.ph
                                        #   in Loop: Header=BB0_4 Depth=2
	leaq	1(%r15), %rax
	addq	$4, %r14
	movslq	%ecx, %rdx
	cmpq	%rdx, %r15
	movq	%rax, %r15
	jl	.LBB0_4
.LBB0_6:                                # %._crit_edge.loopexit
                                        #   in Loop: Header=BB0_2 Depth=1
	movl	HN(%rip), %eax
.LBB0_7:                                # %._crit_edge
                                        #   in Loop: Header=BB0_2 Depth=1
	incq	%rbx
	movslq	%eax, %rdx
	cmpq	%rdx, %rbx
	jl	.LBB0_2
.LBB0_8:                                # %._crit_edge47
	movq	%rbp, %rdi
	callq	fclose
	movl	$.L.str.3, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %r13
	movl	HI(%rip), %eax
	testl	%eax, %eax
	jle	.LBB0_16
# %bb.9:                                # %.preheader.preheader
	movl	HK_star(%rip), %ecx
	xorl	%ebp, %ebp
.LBB0_10:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_12 Depth 2
	testl	%ecx, %ecx
	jle	.LBB0_15
# %bb.11:                               # %.lr.ph49.preheader
                                        #   in Loop: Header=BB0_10 Depth=1
	movl	$1, %ebx
	xorl	%r14d, %r14d
.LBB0_12:                               # %.lr.ph49
                                        #   Parent Loop BB0_10 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	(%r12,%rbp,8), %rdx
	addq	%r14, %rdx
	movl	$.L.str.4, %esi
	movq	%r13, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	HK_star(%rip), %ecx
	testl	%eax, %eax
	je	.LBB0_14
# %bb.13:                               # %.lr.ph49
                                        #   in Loop: Header=BB0_12 Depth=2
	addq	$4, %r14
	leaq	1(%rbx), %rax
	movslq	%ecx, %rdx
	cmpq	%rdx, %rbx
	movq	%rax, %rbx
	jl	.LBB0_12
.LBB0_14:                               # %._crit_edge50.loopexit
                                        #   in Loop: Header=BB0_10 Depth=1
	movl	HI(%rip), %eax
.LBB0_15:                               # %._crit_edge50
                                        #   in Loop: Header=BB0_10 Depth=1
	incq	%rbp
	movslq	%eax, %rdx
	cmpq	%rdx, %rbp
	jl	.LBB0_10
.LBB0_16:                               # %._crit_edge53
	movq	%r13, %rdi
	callq	fclose
	movl	$.L.str.5, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %r12
	cmpl	$0, HI(%rip)
	movq	(%rsp), %r14                    # 8-byte Reload
	jle	.LBB0_20
# %bb.17:                               # %.lr.ph56.preheader
	xorl	%ebx, %ebx
.LBB0_19:                               # %.lr.ph56
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.2, %esi
	movq	%r12, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	testl	%eax, %eax
	je	.LBB0_20
# %bb.18:                               #   in Loop: Header=BB0_19 Depth=1
	incq	%rbx
	movslq	HI(%rip), %rax
	addq	$4, %r14
	cmpq	%rax, %rbx
	jl	.LBB0_19
.LBB0_20:                               # %._crit_edge57
	movq	%r12, %rdi
	callq	fclose
	movl	$.L.str.6, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %r15
	cmpl	$0, HV(%rip)
	movq	8(%rsp), %r14                   # 8-byte Reload
	jle	.LBB0_24
# %bb.21:                               # %.lr.ph61.preheader
	xorl	%ebx, %ebx
.LBB0_23:                               # %.lr.ph61
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.2, %esi
	movq	%r15, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	testl	%eax, %eax
	je	.LBB0_24
# %bb.22:                               #   in Loop: Header=BB0_23 Depth=1
	incq	%rbx
	movslq	HV(%rip), %rax
	addq	$4, %r14
	cmpq	%rax, %rbx
	jl	.LBB0_23
.LBB0_24:                               # %._crit_edge62
	movq	%r15, %rdi
	callq	fclose
	movl	$.L.str.7, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %r14
	cmpl	$0, HI(%rip)
	movq	16(%rsp), %r15                  # 8-byte Reload
	jle	.LBB0_28
# %bb.25:                               # %.lr.ph66.preheader
	xorl	%ebx, %ebx
.LBB0_27:                               # %.lr.ph66
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.2, %esi
	movq	%r14, %rdi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	testl	%eax, %eax
	je	.LBB0_28
# %bb.26:                               #   in Loop: Header=BB0_27 Depth=1
	incq	%rbx
	movslq	HI(%rip), %rax
	addq	$4, %r15
	cmpq	%rax, %rbx
	jl	.LBB0_27
.LBB0_28:                               # %._crit_edge67
	movq	%r14, %rdi
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.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
	jmp	fclose                          # TAILCALL
.Lfunc_end0:
	.size	_Z10read_filesPPiPPfS_S_S_, .Lfunc_end0-_Z10read_filesPPiPPfS_S_S_
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"./inputs/foci.txt"
	.size	.L.str, 18

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"r"
	.size	.L.str.1, 2

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%d"
	.size	.L.str.2, 3

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"./inputs/Z.txt"
	.size	.L.str.3, 15

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"%f"
	.size	.L.str.4, 3

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"./inputs/counts.txt"
	.size	.L.str.5, 20

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"mask.txt"
	.size	.L.str.6, 9

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"./inputs/paper.txt"
	.size	.L.str.7, 19

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.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_

	.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
