#include <iostream>
#include <stdlib.h>
#include "load_matrix.cuh"

int load_matrix(double ***matrix, int *size, char* filename) {

	FILE* ip;
	int i, j;

	if ((ip = fopen(filename, "r")) == NULL) {
		return 1;
	}
	fscanf(ip, "%d\n\n", size);
	(*matrix) = alloc_mem(*size, (*size) + 1);
	for (i = 0; i < *size; ++i) {
		for (j = 0; j < *size; ++j)
			fscanf(ip, "%lf\t", &(*matrix)[i][j]);
		fscanf(ip, "\n");
	}
	fscanf(ip, "\n");
	for (i = 0; i < *size; ++i)
		fscanf(ip, "%lf\n", &(*matrix)[i][(*size - 1) + 1]);
	fclose(ip);
	return 0;
}


double **alloc_mem(int rows_count, int cols_count) {
	int i;
	double ** mem;
	mem = (double **)malloc(rows_count * sizeof(double*));
	for (i = 0; i < rows_count; ++i) {
		mem[i] = (double *)malloc(cols_count * sizeof(double));
	}
	return mem;
}


void dealloc_mem(double** matrix, int rows_count) {
	int i;
	for (i = 0; i < rows_count; ++i) {
		free(matrix[i]);
	}
	free(matrix);
}
	.file	"tmpxft_0027f0a9_00000000-6_load_matrix.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3640:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3640:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z9alloc_memii
	.type	_Z9alloc_memii, @function
_Z9alloc_memii:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movslq	%edi, %rdi
	xorl	%r13d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	salq	$3, %rdi
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movslq	%esi, %rbx
	pushq	%rcx
	.cfi_def_cfa_offset 48
	salq	$3, %rbx
	call	malloc@PLT
	movq	%rax, %rbp
.L3:
	cmpl	%r13d, %r12d
	jle	.L7
	movq	%rbx, %rdi
	call	malloc@PLT
	movq	%rax, 0(%rbp,%r13,8)
	incq	%r13
	jmp	.L3
.L7:
	popq	%rdx
	.cfi_def_cfa_offset 40
	movq	%rbp, %rax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3636:
	.size	_Z9alloc_memii, .-_Z9alloc_memii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"r"
.LC1:
	.string	"%d\n\n"
.LC2:
	.string	"%lf\t"
.LC3:
	.string	"\n"
.LC4:
	.string	"%lf\n"
	.text
	.globl	_Z11load_matrixPPPdPiPc
	.type	_Z11load_matrixPPPdPiPc, @function
_Z11load_matrixPPPdPiPc:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdi, %r12
	movq	%rdx, %rdi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rsi, %rbp
	leaq	.LC0(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	pushq	%rcx
	.cfi_def_cfa_offset 64
	call	fopen@PLT
	movq	%rax, %rbx
	movl	$1, %eax
	testq	%rbx, %rbx
	je	.L8
	movq	%rbp, %rdx
	leaq	.LC1(%rip), %rsi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movl	0(%rbp), %edi
	xorl	%r14d, %r14d
	leal	1(%rdi), %esi
	call	_Z9alloc_memii
	movq	%rax, (%r12)
.L10:
	cmpl	%r14d, 0(%rbp)
	jle	.L12
	leaq	0(,%r14,8), %r15
	xorl	%r13d, %r13d
.L13:
	cmpl	%r13d, 0(%rbp)
	jle	.L19
	movq	(%r12), %rax
	leaq	.LC2(%rip), %rsi
	movq	%rbx, %rdi
	movq	(%rax,%r15), %rax
	leaq	(%rax,%r13,8), %rdx
	xorl	%eax, %eax
	incq	%r13
	call	__isoc23_fscanf@PLT
	jmp	.L13
.L19:
	leaq	.LC3(%rip), %rsi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	incq	%r14
	call	__isoc23_fscanf@PLT
	jmp	.L10
.L12:
	leaq	.LC3(%rip), %rsi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	xorl	%r13d, %r13d
	call	__isoc23_fscanf@PLT
	leaq	.LC4(%rip), %r14
.L14:
	movslq	0(%rbp), %rax
	cmpl	%r13d, %eax
	jle	.L20
	movq	(%r12), %rdx
	movq	%r14, %rsi
	movq	%rbx, %rdi
	movq	(%rdx,%r13,8), %rdx
	incq	%r13
	leaq	(%rdx,%rax,8), %rdx
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	jmp	.L14
.L20:
	movq	%rbx, %rdi
	call	fclose@PLT
	xorl	%eax, %eax
.L8:
	popq	%rdx
	.cfi_def_cfa_offset 56
	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
	ret
	.cfi_endproc
.LFE3635:
	.size	_Z11load_matrixPPPdPiPc, .-_Z11load_matrixPPPdPiPc
	.globl	_Z11dealloc_memPPdi
	.type	_Z11dealloc_memPPdi, @function
_Z11dealloc_memPPdi:
.LFB3637:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	%esi, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%rdi, %rbx
.L22:
	cmpl	%ebp, %r12d
	jle	.L25
	movq	(%rbx,%rbp,8), %rdi
	incq	%rbp
	call	free@PLT
	jmp	.L22
.L25:
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	free@PLT
	.cfi_endproc
.LFE3637:
	.size	_Z11dealloc_memPPdi, .-_Z11dealloc_memPPdi
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3663:
	.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
.LFE3663:
	.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 <iostream>
#include <stdlib.h>
#include "load_matrix.cuh"

int load_matrix(double ***matrix, int *size, char* filename) {

	FILE* ip;
	int i, j;

	if ((ip = fopen(filename, "r")) == NULL) {
		return 1;
	}
	fscanf(ip, "%d\n\n", size);
	(*matrix) = alloc_mem(*size, (*size) + 1);
	for (i = 0; i < *size; ++i) {
		for (j = 0; j < *size; ++j)
			fscanf(ip, "%lf\t", &(*matrix)[i][j]);
		fscanf(ip, "\n");
	}
	fscanf(ip, "\n");
	for (i = 0; i < *size; ++i)
		fscanf(ip, "%lf\n", &(*matrix)[i][(*size - 1) + 1]);
	fclose(ip);
	return 0;
}


double **alloc_mem(int rows_count, int cols_count) {
	int i;
	double ** mem;
	mem = (double **)malloc(rows_count * sizeof(double*));
	for (i = 0; i < rows_count; ++i) {
		mem[i] = (double *)malloc(cols_count * sizeof(double));
	}
	return mem;
}


void dealloc_mem(double** matrix, int rows_count) {
	int i;
	for (i = 0; i < rows_count; ++i) {
		free(matrix[i]);
	}
	free(matrix);
}
	.text
	.file	"load_matrix.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z11load_matrixPPPdPiPc         # -- Begin function _Z11load_matrixPPPdPiPc
	.type	_Z11load_matrixPPPdPiPc,@function
_Z11load_matrixPPPdPiPc:                # @_Z11load_matrixPPPdPiPc
	.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
	pushq	%rax
	.cfi_def_cfa_offset 64
	.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	%rsi, %rbx
	movq	%rdi, %r14
	movl	$.L.str, %esi
	movq	%rdx, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB0_1
# %bb.2:
	movq	%rax, %r15
	movl	$.L.str.1, %esi
	movq	%rax, %rdi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	(%rbx), %edi
	leal	1(%rdi), %esi
                                        # kill: def $edi killed $edi killed $rdi
	callq	_Z9alloc_memii
	movq	%rax, (%r14)
	movl	(%rbx), %eax
	testl	%eax, %eax
	jle	.LBB0_8
# %bb.3:                                # %.preheader.preheader
	xorl	%r12d, %r12d
.LBB0_4:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_6 Depth 2
	testl	%eax, %eax
	jle	.LBB0_7
# %bb.5:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB0_4 Depth=1
	xorl	%r13d, %r13d
	xorl	%ebp, %ebp
.LBB0_6:                                # %.lr.ph
                                        #   Parent Loop BB0_4 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	(%r14), %rax
	movq	(%rax,%r12,8), %rdx
	addq	%r13, %rdx
	movl	$.L.str.2, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%rbp
	movslq	(%rbx), %rax
	addq	$8, %r13
	cmpq	%rax, %rbp
	jl	.LBB0_6
.LBB0_7:                                # %._crit_edge
                                        #   in Loop: Header=BB0_4 Depth=1
	movl	$.L.str.3, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r12
	movslq	(%rbx), %rax
	cmpq	%rax, %r12
	jl	.LBB0_4
.LBB0_8:                                # %._crit_edge32
	xorl	%ebp, %ebp
	movl	$.L.str.3, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	(%rbx), %eax
	testl	%eax, %eax
	jle	.LBB0_11
# %bb.9:                                # %.lr.ph35.preheader
	xorl	%r12d, %r12d
.LBB0_10:                               # %.lr.ph35
                                        # =>This Inner Loop Header: Depth=1
	movq	(%r14), %rcx
	movslq	%eax, %rdx
	shlq	$3, %rdx
	addq	(%rcx,%r12,8), %rdx
	movl	$.L.str.4, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r12
	movslq	(%rbx), %rax
	cmpq	%rax, %r12
	jl	.LBB0_10
.LBB0_11:                               # %._crit_edge36
	movq	%r15, %rdi
	callq	fclose
	jmp	.LBB0_12
.LBB0_1:
	movl	$1, %ebp
.LBB0_12:
	movl	%ebp, %eax
	addq	$8, %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
	retq
.Lfunc_end0:
	.size	_Z11load_matrixPPPdPiPc, .Lfunc_end0-_Z11load_matrixPPPdPiPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z9alloc_memii                  # -- Begin function _Z9alloc_memii
	.type	_Z9alloc_memii,@function
_Z9alloc_memii:                         # @_Z9alloc_memii
	.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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %r14d
	movl	%edi, %ebp
	movslq	%edi, %r15
	leaq	(,%r15,8), %rdi
	callq	malloc
	movq	%rax, %rbx
	testl	%r15d, %r15d
	jle	.LBB1_3
# %bb.1:                                # %.lr.ph
	movslq	%r14d, %r14
	shlq	$3, %r14
	movl	%ebp, %r15d
	xorl	%r12d, %r12d
.LBB1_2:                                # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, (%rbx,%r12,8)
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB1_2
.LBB1_3:                                # %._crit_edge
	movq	%rbx, %rax
	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
.Lfunc_end1:
	.size	_Z9alloc_memii, .Lfunc_end1-_Z9alloc_memii
	.cfi_endproc
                                        # -- End function
	.globl	_Z11dealloc_memPPdi             # -- Begin function _Z11dealloc_memPPdi
	.type	_Z11dealloc_memPPdi,@function
_Z11dealloc_memPPdi:                    # @_Z11dealloc_memPPdi
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	testl	%esi, %esi
	jle	.LBB2_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %r14d
	xorl	%r15d, %r15d
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	(%rbx,%r15,8), %rdi
	callq	free
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB2_2
.LBB2_3:                                # %._crit_edge
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	free                            # TAILCALL
.Lfunc_end2:
	.size	_Z11dealloc_memPPdi, .Lfunc_end2-_Z11dealloc_memPPdi
	.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	"%d\n\n"
	.size	.L.str.1, 5

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%lf\t"
	.size	.L.str.2, 5

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"\n"
	.size	.L.str.3, 2

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"%lf\n"
	.size	.L.str.4, 5

	.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
