#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
extern int errno;
/* Function that saves the Image  */
void
writeImg(char *in_filename,float *data, int dataRows,int attributes){
	FILE *fp;
	int counter=1;
	char delim = ',';
	char *filename;
	filename = strtok(in_filename,".");
	filename = strcat(filename,"_out.csv"); 
	/* Check if the out file existsi and find an appropriate name that is unique*/
	while (access(filename,F_OK) == 0){
		filename = strtok(filename,"_");
		filename = strcat(filename,"_out");
		char num[100];
		sprintf(num,"%d",counter);
		counter++;
		filename = strcat(filename, num);
		filename = strcat(filename,".csv");
	}
	/* Open File */	
	fp = fopen(filename,"w");
	/* Write contents */
	for (int i=0;i<dataRows;i++){
		for (int j=0;j<attributes;j++){
			/* If writing the last element of the row skip the delimiter and add new line instead*/
			if ( j == attributes-1)
				fprintf(fp,"%f\n",data[i * attributes +j]);
			else
				fprintf(fp,"%f%c",data[i * attributes +j],delim);
		}

		
	}
	/* Close File*/
	fclose(fp);
}

/* Function that checks the number of arguments */
void
checkArgsNum(int num,int argc){
	num++; // Add 1 to offset
	if (num ==  argc ) return;
	else if (argc == 1 ){
		fprintf(stderr, "Line:%d No argument was given\n",__LINE__);
		exit(EXIT_FAILURE);
	}
	else if (argc < num && argc > 1){
		fprintf(stderr, "Line:%d Less arguments was given\n",__LINE__);
		exit(EXIT_FAILURE);
	}
	else if (argc > num){
		fprintf(stderr, "Line:%d More args given\n",__LINE__);
		exit(EXIT_FAILURE);
	}
}
/* Check if patch size given as an argument is an odd number */
void checkPatchSize(int patchSize){
	if ( (patchSize % 2)!=1){
		fprintf(stderr, "Line:%d Patch Size is not odd number\n",__LINE__);
		exit(EXIT_FAILURE);
	}
}
/* Helper function that gets the number of attributes in the csv file */
int
getAttributes(FILE *fp){
	/* Variables */
	char buf[8192];
	int attributes=0;
	char delim=',';
	/* Get first line from file */
	/* and check if file is empty */
	if (fgets(buf,8192,fp)==NULL){
		fprintf(stderr,"Line %d: Input file empty\n",__LINE__ );
		exit(EXIT_FAILURE);
	}
	/* Count the occurences of the delimiters */
	for(int i=0;i<strlen(buf);i++){
		if (buf[i]==delim) attributes++;
	}
	/* Add 1 to the occurrences of the delimiter */
	attributes++;
	/* Rewind fp */
	rewind(fp);
	return attributes;
}
/* Function that reads csv files and returns a 2d array  */
float **
readCSV(FILE *fp, float **data, int *dataRows,int *attributes){
	/* Variables */
	*attributes = getAttributes(fp);
	const char delim[] = ",";
	int row=0;
	char buf[8192];
	/* Read Lines one by one and split them on the delimiter */
	while(fgets(buf,8192,fp)){
		/* realloc the data array to fill another row */
		data = (float **)realloc(data,(row+1)*sizeof(float *));
		data[row] = (float *)malloc(*attributes*(sizeof(float)));
		/* Split the buf on the delimiter and fill the row */
		char *token;
		for (int i = 0; i< *attributes; i++){
			if (i==0)
				token = strtok(buf,delim); 
			else
				token = strtok(NULL,delim);
			/* If token NULL no more lines exist (Maybe there is no need for this) */
			if (token==NULL) break;
			/* Covert str to float */
			sscanf(token, "%f",&data[row][i]);
		}
		row++;
	}
	/* Return dataRows */
	*dataRows=row;
	rewind(fp);
	return data;
}
/* Wrapper function that calls the pre-existing readCSV and returns the data as a 1D array */
float *
getImg( char *filename, int *dataRows, int *attributes ){

	FILE *fp;
	/* Open file */
	fp= fopen (filename,"r");
	if (fp == NULL){
		fprintf(stderr, "Line %d: Error opening file %s\n",__LINE__,strerror(errno));
		exit (EXIT_FAILURE);
	}
	/* Read csv */
	float **data2D= (float **)malloc(0);
	data2D=readCSV(fp,data2D,dataRows,attributes);
	/* Make 2D array to 1D */
	float *data1D = (float *)malloc(*attributes * (*dataRows) * sizeof(float));
	for (int i=0;i<(*dataRows);i++)
		for (int j=0;j<(*attributes);j++)
			data1D[i*(*attributes) + j] = data2D[i][j];

	/* Free  data memory*/
	int RowsToFree=*dataRows;
	while(RowsToFree) free(data2D[-- RowsToFree]);
	free(data2D);
	/* Close file */
	fclose(fp);
	return data1D;
}


	.file	"tmpxft_002ddc63_00000000-6_iofun.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2048:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2048:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"."
.LC1:
	.string	"_out.csv"
.LC2:
	.string	"_"
.LC3:
	.string	"_out"
.LC4:
	.string	"%d"
.LC5:
	.string	".csv"
.LC6:
	.string	"w"
.LC7:
	.string	"%f\n"
.LC8:
	.string	"%f%c"
	.text
	.globl	_Z8writeImgPcPfii
	.type	_Z8writeImgPcPfii, @function
_Z8writeImgPcPfii:
.LFB2040:
	.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
	movl	$1, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%edx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	leaq	.LC2(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%ecx, %ebx
	subq	$136, %rsp
	.cfi_def_cfa_offset 192
	movq	%rsi, 8(%rsp)
	leaq	.LC0(%rip), %rsi
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	call	strtok@PLT
	leaq	.LC1(%rip), %rsi
	movq	%rax, %rdi
	call	strcat@PLT
	movq	%rax, %rdi
.L3:
	xorl	%esi, %esi
	movq	%rdi, (%rsp)
	call	access@PLT
	movq	(%rsp), %rdi
	testl	%eax, %eax
	jne	.L15
	movq	%rbp, %rsi
	leaq	20(%rsp), %r15
	call	strtok@PLT
	leaq	.LC3(%rip), %rsi
	movq	%rax, %rdi
	call	strcat@PLT
	movl	%r13d, %r8d
	movl	$100, %edx
	movq	%r15, %rdi
	movq	%rax, %r14
	leaq	.LC4(%rip), %rcx
	xorl	%eax, %eax
	incl	%r13d
	movl	$2, %esi
	call	__sprintf_chk@PLT
	movq	%r15, %rsi
	movq	%r14, %rdi
	call	strcat@PLT
	movq	%r14, %rdi
	leaq	.LC5(%rip), %rsi
	call	strcat@PLT
	movq	%r14, %rdi
	jmp	.L3
.L15:
	leaq	.LC6(%rip), %rsi
	xorl	%r13d, %r13d
	xorl	%r14d, %r14d
	call	fopen@PLT
	movq	%rax, (%rsp)
.L5:
	cmpl	%r12d, %r14d
	jge	.L9
	movq	8(%rsp), %rcx
	movslq	%r13d, %rax
	xorl	%ebp, %ebp
	leaq	(%rcx,%rax,4), %r15
.L10:
	cmpl	%ebx, %ebp
	jge	.L16
	leal	-1(%rbx), %eax
	cvtss2sd	(%r15,%rbp,4), %xmm0
	cmpl	%ebp, %eax
	jne	.L6
	movq	(%rsp), %rdi
	leaq	.LC7(%rip), %rdx
	movl	$2, %esi
	movb	$1, %al
	call	__fprintf_chk@PLT
	jmp	.L7
.L6:
	movq	(%rsp), %rdi
	movl	$44, %ecx
	movl	$2, %esi
	movb	$1, %al
	leaq	.LC8(%rip), %rdx
	call	__fprintf_chk@PLT
.L7:
	incq	%rbp
	jmp	.L10
.L16:
	incl	%r14d
	addl	%ebx, %r13d
	jmp	.L5
.L9:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	movq	(%rsp), %rdi
	addq	$136, %rsp
	.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
	jmp	fclose@PLT
	.cfi_endproc
.LFE2040:
	.size	_Z8writeImgPcPfii, .-_Z8writeImgPcPfii
	.section	.rodata.str1.1
.LC9:
	.string	"Line:%d No argument was given\n"
.LC10:
	.string	"Line:%d Less arguments was given\n"
.LC11:
	.string	"Line:%d More args given\n"
	.text
	.globl	_Z12checkArgsNumii
	.type	_Z12checkArgsNumii, @function
_Z12checkArgsNumii:
.LFB2041:
	.cfi_startproc
	endbr64
	incl	%edi
	cmpl	%esi, %edi
	je	.L28
	pushq	%rdx
	.cfi_def_cfa_offset 16
	cmpl	$1, %esi
	jne	.L19
	movq	stderr(%rip), %rdi
	movl	$49, %ecx
	leaq	.LC9(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L19:
	cmpl	%esi, %edi
	jle	.L20
	cmpl	$1, %esi
	jle	.L20
	movq	stderr(%rip), %rdi
	movl	$53, %ecx
	leaq	.LC10(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L20:
	cmpl	%esi, %edi
	jge	.L17
	movq	stderr(%rip), %rdi
	movl	$57, %ecx
	leaq	.LC11(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L17:
	popq	%rax
	.cfi_def_cfa_offset 8
	ret
.L28:
	ret
	.cfi_endproc
.LFE2041:
	.size	_Z12checkArgsNumii, .-_Z12checkArgsNumii
	.section	.rodata.str1.1
.LC12:
	.string	"Line:%d Patch Size is not odd number\n"
	.text
	.globl	_Z14checkPatchSizei
	.type	_Z14checkPatchSizei, @function
_Z14checkPatchSizei:
.LFB2042:
	.cfi_startproc
	endbr64
	movl	%edi, %eax
	movl	$2, %ecx
	cltd
	idivl	%ecx
	decl	%edx
	je	.L31
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rdi
	movl	$64, %ecx
	xorl	%eax, %eax
	leaq	.LC12(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L31:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2042:
	.size	_Z14checkPatchSizei, .-_Z14checkPatchSizei
	.section	.rodata.str1.1
.LC13:
	.string	"Line %d: Input file empty\n"
	.text
	.globl	_Z13getAttributesP8_IO_FILE
	.type	_Z13getAttributesP8_IO_FILE, @function
_Z13getAttributesP8_IO_FILE:
.LFB2043:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$4096, %rsp
	.cfi_def_cfa_offset 4128
	orq	$0, (%rsp)
	subq	$4096, %rsp
	.cfi_def_cfa_offset 8224
	orq	$0, (%rsp)
	subq	$16, %rsp
	.cfi_def_cfa_offset 8240
	movl	$8192, %edx
	movl	$8192, %esi
	movq	%fs:40, %rax
	movq	%rax, 8200(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %r12
	movq	%rdi, %rbp
	movq	%rdi, %rcx
	movq	%r12, %rdi
	call	__fgets_chk@PLT
	testq	%rax, %rax
	je	.L37
	movq	%r12, %rdi
	call	strlen@PLT
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	jmp	.L38
.L37:
	movq	stderr(%rip), %rdi
	movl	$78, %ecx
	leaq	.LC13(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L38:
	leal	1(%rcx), %ebx
	cmpq	%rdx, %rax
	je	.L43
	cmpb	$44, (%r12,%rdx)
	cmovne	%ecx, %ebx
	incq	%rdx
	movl	%ebx, %ecx
	jmp	.L38
.L43:
	movq	%rbp, %rdi
	call	rewind@PLT
	movq	8200(%rsp), %rax
	subq	%fs:40, %rax
	je	.L41
	call	__stack_chk_fail@PLT
.L41:
	addq	$8208, %rsp
	.cfi_def_cfa_offset 32
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2043:
	.size	_Z13getAttributesP8_IO_FILE, .-_Z13getAttributesP8_IO_FILE
	.section	.rodata.str1.1
.LC14:
	.string	"%f"
	.text
	.globl	_Z7readCSVP8_IO_FILEPPfPiS3_
	.type	_Z7readCSVP8_IO_FILEPPfPiS3_, @function
_Z7readCSVP8_IO_FILEPPfPiS3_:
.LFB2044:
	.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
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$4096, %rsp
	.cfi_def_cfa_offset 4152
	orq	$0, (%rsp)
	subq	$4096, %rsp
	.cfi_def_cfa_offset 8248
	orq	$0, (%rsp)
	subq	$40, %rsp
	.cfi_def_cfa_offset 8288
	xorl	%r14d, %r14d
	movq	%rdi, (%rsp)
	movq	%rcx, %rbp
	movq	%rsi, %rbx
	movq	%rdx, %r15
	movq	%fs:40, %rax
	movq	%rax, 8216(%rsp)
	xorl	%eax, %eax
	call	_Z13getAttributesP8_IO_FILE
	movw	$44, 22(%rsp)
	movl	%eax, 0(%rbp)
.L45:
	leaq	24(%rsp), %rax
	movq	(%rsp), %rcx
	movl	$8192, %edx
	movl	$8192, %esi
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	__fgets_chk@PLT
	testq	%rax, %rax
	je	.L56
	leaq	0(,%r14,8), %r13
	movq	%rbx, %rdi
	xorl	%r12d, %r12d
	leaq	8(%r13), %rsi
	call	realloc@PLT
	movslq	0(%rbp), %rdi
	addq	%rax, %r13
	movq	%rax, %rbx
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rax, 0(%r13)
.L46:
	cmpl	%r12d, 0(%rbp)
	jle	.L51
	xorl	%edi, %edi
	testq	%r12, %r12
	leaq	22(%rsp), %rsi
	jne	.L55
	movq	8(%rsp), %rdi
.L55:
	call	strtok@PLT
	testq	%rax, %rax
	jne	.L49
.L51:
	incq	%r14
	jmp	.L45
.L49:
	movq	0(%r13), %rdx
	movq	%rax, %rdi
	leaq	.LC14(%rip), %rsi
	xorl	%eax, %eax
	leaq	(%rdx,%r12,4), %rdx
	incq	%r12
	call	__isoc23_sscanf@PLT
	jmp	.L46
.L56:
	movl	%r14d, (%r15)
	movq	(%rsp), %rdi
	call	rewind@PLT
	movq	8216(%rsp), %rax
	subq	%fs:40, %rax
	je	.L53
	call	__stack_chk_fail@PLT
.L53:
	addq	$8232, %rsp
	.cfi_def_cfa_offset 56
	movq	%rbx, %rax
	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
.LFE2044:
	.size	_Z7readCSVP8_IO_FILEPPfPiS3_, .-_Z7readCSVP8_IO_FILEPPfPiS3_
	.section	.rodata.str1.1
.LC15:
	.string	"r"
.LC16:
	.string	"Line %d: Error opening file %s\n"
	.text
	.globl	_Z6getImgPcPiS0_
	.type	_Z6getImgPcPiS0_, @function
_Z6getImgPcPiS0_:
.LFB2045:
	.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
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movq	%rsi, %rbx
	leaq	.LC15(%rip), %rsi
	call	fopen@PLT
	testq	%rax, %rax
	jne	.L58
	call	__errno_location@PLT
	movl	(%rax), %edi
	call	strerror@PLT
	movl	$131, %ecx
	movl	$2, %esi
	movq	stderr(%rip), %rdi
	movq	%rax, %r8
	leaq	.LC16(%rip), %rdx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L58:
	xorl	%edi, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movq	%rbx, %rdx
	movq	%r12, %rcx
	movq	%rbp, %rdi
	movq	%rax, %rsi
	call	_Z7readCSVP8_IO_FILEPPfPiS3_
	movl	(%r12), %edi
	imull	(%rbx), %edi
	movq	%rax, %r14
	movslq	%edi, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movslq	(%rbx), %rbx
	xorl	%edx, %edx
	movq	%rax, %r13
.L59:
	cmpl	%edx, %ebx
	jle	.L63
	movl	(%r12), %ecx
	movl	%ecx, %eax
	imull	%edx, %eax
	cltq
	leaq	0(%r13,%rax,4), %rsi
	xorl	%eax, %eax
.L62:
	cmpl	%eax, %ecx
	jle	.L66
	movq	(%r14,%rdx,8), %rdi
	movss	(%rdi,%rax,4), %xmm0
	movss	%xmm0, (%rsi,%rax,4)
	incq	%rax
	jmp	.L62
.L66:
	incq	%rdx
	jmp	.L59
.L63:
	testl	%ebx, %ebx
	je	.L67
	movq	-8(%r14,%rbx,8), %rdi
	decq	%rbx
	call	free@PLT
	jmp	.L63
.L67:
	movq	%r14, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	fclose@PLT
	popq	%rbx
	.cfi_def_cfa_offset 40
	movq	%r13, %rax
	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
.LFE2045:
	.size	_Z6getImgPcPiS0_, .-_Z6getImgPcPiS0_
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2071:
	.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
.LFE2071:
	.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 <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
extern int errno;
/* Function that saves the Image  */
void
writeImg(char *in_filename,float *data, int dataRows,int attributes){
	FILE *fp;
	int counter=1;
	char delim = ',';
	char *filename;
	filename = strtok(in_filename,".");
	filename = strcat(filename,"_out.csv"); 
	/* Check if the out file existsi and find an appropriate name that is unique*/
	while (access(filename,F_OK) == 0){
		filename = strtok(filename,"_");
		filename = strcat(filename,"_out");
		char num[100];
		sprintf(num,"%d",counter);
		counter++;
		filename = strcat(filename, num);
		filename = strcat(filename,".csv");
	}
	/* Open File */	
	fp = fopen(filename,"w");
	/* Write contents */
	for (int i=0;i<dataRows;i++){
		for (int j=0;j<attributes;j++){
			/* If writing the last element of the row skip the delimiter and add new line instead*/
			if ( j == attributes-1)
				fprintf(fp,"%f\n",data[i * attributes +j]);
			else
				fprintf(fp,"%f%c",data[i * attributes +j],delim);
		}

		
	}
	/* Close File*/
	fclose(fp);
}

/* Function that checks the number of arguments */
void
checkArgsNum(int num,int argc){
	num++; // Add 1 to offset
	if (num ==  argc ) return;
	else if (argc == 1 ){
		fprintf(stderr, "Line:%d No argument was given\n",__LINE__);
		exit(EXIT_FAILURE);
	}
	else if (argc < num && argc > 1){
		fprintf(stderr, "Line:%d Less arguments was given\n",__LINE__);
		exit(EXIT_FAILURE);
	}
	else if (argc > num){
		fprintf(stderr, "Line:%d More args given\n",__LINE__);
		exit(EXIT_FAILURE);
	}
}
/* Check if patch size given as an argument is an odd number */
void checkPatchSize(int patchSize){
	if ( (patchSize % 2)!=1){
		fprintf(stderr, "Line:%d Patch Size is not odd number\n",__LINE__);
		exit(EXIT_FAILURE);
	}
}
/* Helper function that gets the number of attributes in the csv file */
int
getAttributes(FILE *fp){
	/* Variables */
	char buf[8192];
	int attributes=0;
	char delim=',';
	/* Get first line from file */
	/* and check if file is empty */
	if (fgets(buf,8192,fp)==NULL){
		fprintf(stderr,"Line %d: Input file empty\n",__LINE__ );
		exit(EXIT_FAILURE);
	}
	/* Count the occurences of the delimiters */
	for(int i=0;i<strlen(buf);i++){
		if (buf[i]==delim) attributes++;
	}
	/* Add 1 to the occurrences of the delimiter */
	attributes++;
	/* Rewind fp */
	rewind(fp);
	return attributes;
}
/* Function that reads csv files and returns a 2d array  */
float **
readCSV(FILE *fp, float **data, int *dataRows,int *attributes){
	/* Variables */
	*attributes = getAttributes(fp);
	const char delim[] = ",";
	int row=0;
	char buf[8192];
	/* Read Lines one by one and split them on the delimiter */
	while(fgets(buf,8192,fp)){
		/* realloc the data array to fill another row */
		data = (float **)realloc(data,(row+1)*sizeof(float *));
		data[row] = (float *)malloc(*attributes*(sizeof(float)));
		/* Split the buf on the delimiter and fill the row */
		char *token;
		for (int i = 0; i< *attributes; i++){
			if (i==0)
				token = strtok(buf,delim); 
			else
				token = strtok(NULL,delim);
			/* If token NULL no more lines exist (Maybe there is no need for this) */
			if (token==NULL) break;
			/* Covert str to float */
			sscanf(token, "%f",&data[row][i]);
		}
		row++;
	}
	/* Return dataRows */
	*dataRows=row;
	rewind(fp);
	return data;
}
/* Wrapper function that calls the pre-existing readCSV and returns the data as a 1D array */
float *
getImg( char *filename, int *dataRows, int *attributes ){

	FILE *fp;
	/* Open file */
	fp= fopen (filename,"r");
	if (fp == NULL){
		fprintf(stderr, "Line %d: Error opening file %s\n",__LINE__,strerror(errno));
		exit (EXIT_FAILURE);
	}
	/* Read csv */
	float **data2D= (float **)malloc(0);
	data2D=readCSV(fp,data2D,dataRows,attributes);
	/* Make 2D array to 1D */
	float *data1D = (float *)malloc(*attributes * (*dataRows) * sizeof(float));
	for (int i=0;i<(*dataRows);i++)
		for (int j=0;j<(*attributes);j++)
			data1D[i*(*attributes) + j] = data2D[i][j];

	/* Free  data memory*/
	int RowsToFree=*dataRows;
	while(RowsToFree) free(data2D[-- RowsToFree]);
	free(data2D);
	/* Close file */
	fclose(fp);
	return data1D;
}


	.text
	.file	"iofun.hip"
	.globl	_Z8writeImgPcPfii               # -- Begin function _Z8writeImgPcPfii
	.type	_Z8writeImgPcPfii,@function
_Z8writeImgPcPfii:                      # @_Z8writeImgPcPfii
	.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	$152, %rsp
	.cfi_def_cfa_offset 208
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%ecx, %r14d
	movl	%edx, %ebp
	movq	%rsi, 16(%rsp)                  # 8-byte Spill
	movl	$.L.str, %esi
	callq	strtok
	movq	%rax, %r15
	movq	%rax, %rdi
	callq	strlen
	movabsq	$8535274769970720607, %rcx      # imm = 0x7673632E74756F5F
	movq	%rcx, (%r15,%rax)
	movb	$0, 8(%r15,%rax)
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	access
	testl	%eax, %eax
	jne	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	movl	$1, %r12d
	xorl	%ebx, %ebx
	leaq	48(%rsp), %r13
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.2, %esi
	movq	%r15, %rdi
	callq	strtok
	movq	%rax, %r15
	movq	%rax, %rdi
	callq	strlen
	movb	%bl, 4(%r15,%rax)
	movl	$1953853279, (%r15,%rax)        # imm = 0x74756F5F
	movl	$.L.str.4, %esi
	movq	%r13, %rdi
	movl	%r12d, %edx
	xorl	%eax, %eax
	callq	sprintf
	incl	%r12d
	movq	%r15, %rdi
	movq	%r13, %rsi
	callq	strcat
	movq	%r15, %rdi
	callq	strlen
	movb	%bl, 4(%r15,%rax)
	movl	$1987273518, (%r15,%rax)        # imm = 0x7673632E
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	access
	testl	%eax, %eax
	je	.LBB0_2
.LBB0_3:                                # %._crit_edge
	movl	$.L.str.6, %esi
	movq	%r15, %rdi
	callq	fopen
	movq	%rax, %r15
	testl	%ebp, %ebp
	jle	.LBB0_10
# %bb.4:                                # %.preheader.lr.ph
	leal	-1(%r14), %r12d
	movl	%ebp, %eax
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movl	%r14d, %ebp
	movq	16(%rsp), %rax                  # 8-byte Reload
	leaq	(%rax,%r12,4), %rax
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	xorl	%eax, %eax
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	xorl	%eax, %eax
	movq	%rax, (%rsp)                    # 8-byte Spill
	movq	%r14, 24(%rsp)                  # 8-byte Spill
.LBB0_5:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_7 Depth 2
	testl	%r14d, %r14d
	jle	.LBB0_9
# %bb.6:                                # %.lr.ph40
                                        #   in Loop: Header=BB0_5 Depth=1
	movl	8(%rsp), %eax                   # 4-byte Reload
	movq	16(%rsp), %rcx                  # 8-byte Reload
	leaq	(%rcx,%rax,4), %r13
	movq	(%rsp), %rbx                    # 8-byte Reload
                                        # kill: def $ebx killed $ebx killed $rbx def $rbx
	imull	24(%rsp), %ebx                  # 4-byte Folded Reload
	xorl	%r14d, %r14d
.LBB0_7:                                #   Parent Loop BB0_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpq	%r14, %r12
	jne	.LBB0_11
# %bb.8:                                #   in Loop: Header=BB0_7 Depth=2
	movq	40(%rsp), %rax                  # 8-byte Reload
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rax,%rbx,4), %xmm0
	movl	$.L.str.7, %esi
	movq	%r15, %rdi
	movb	$1, %al
	callq	fprintf
	jmp	.LBB0_12
.LBB0_11:                               #   in Loop: Header=BB0_7 Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r13,%r14,4), %xmm0
	movl	$.L.str.8, %esi
	movq	%r15, %rdi
	movl	$44, %edx
	movb	$1, %al
	callq	fprintf
.LBB0_12:                               #   in Loop: Header=BB0_7 Depth=2
	incq	%r14
	cmpq	%r14, %rbp
	jne	.LBB0_7
.LBB0_9:                                # %._crit_edge41
                                        #   in Loop: Header=BB0_5 Depth=1
	movq	(%rsp), %rcx                    # 8-byte Reload
	incq	%rcx
	movq	24(%rsp), %r14                  # 8-byte Reload
	movq	8(%rsp), %rax                   # 8-byte Reload
	addl	%r14d, %eax
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	movq	%rcx, %rax
	movq	%rcx, (%rsp)                    # 8-byte Spill
	cmpq	32(%rsp), %rcx                  # 8-byte Folded Reload
	jne	.LBB0_5
.LBB0_10:                               # %._crit_edge43
	movq	%r15, %rdi
	addq	$152, %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	_Z8writeImgPcPfii, .Lfunc_end0-_Z8writeImgPcPfii
	.cfi_endproc
                                        # -- End function
	.globl	_Z12checkArgsNumii              # -- Begin function _Z12checkArgsNumii
	.type	_Z12checkArgsNumii,@function
_Z12checkArgsNumii:                     # @_Z12checkArgsNumii
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
                                        # kill: def $edi killed $edi def $rdi
	leal	1(%rdi), %eax
	cmpl	%esi, %eax
	je	.LBB1_9
# %bb.1:
	cmpl	$1, %esi
	je	.LBB1_2
# %bb.4:
	cmpl	%esi, %edi
	jl	.LBB1_7
# %bb.5:
	cmpl	$2, %esi
	jge	.LBB1_6
.LBB1_7:
	cmpl	%esi, %eax
	jl	.LBB1_8
.LBB1_9:
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.LBB1_2:
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rdi
	movl	$.L.str.9, %esi
	movl	$51, %edx
	jmp	.LBB1_3
.LBB1_6:
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	movl	$55, %edx
	jmp	.LBB1_3
.LBB1_8:
	movq	stderr(%rip), %rdi
	movl	$.L.str.11, %esi
	movl	$59, %edx
.LBB1_3:
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z12checkArgsNumii, .Lfunc_end1-_Z12checkArgsNumii
	.cfi_endproc
                                        # -- End function
	.globl	_Z14checkPatchSizei             # -- Begin function _Z14checkPatchSizei
	.type	_Z14checkPatchSizei,@function
_Z14checkPatchSizei:                    # @_Z14checkPatchSizei
	.cfi_startproc
# %bb.0:
	andl	$-2147483647, %edi              # imm = 0x80000001
	cmpl	$1, %edi
	jne	.LBB2_2
# %bb.1:
	retq
.LBB2_2:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rdi
	movl	$.L.str.12, %esi
	movl	$66, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end2:
	.size	_Z14checkPatchSizei, .Lfunc_end2-_Z14checkPatchSizei
	.cfi_endproc
                                        # -- End function
	.globl	_Z13getAttributesP8_IO_FILE     # -- Begin function _Z13getAttributesP8_IO_FILE
	.type	_Z13getAttributesP8_IO_FILE,@function
_Z13getAttributesP8_IO_FILE:            # @_Z13getAttributesP8_IO_FILE
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$8200, %rsp                     # imm = 0x2008
	.cfi_def_cfa_offset 8224
	.cfi_offset %rbx, -24
	.cfi_offset %rbp, -16
	movq	%rdi, %rbx
	movq	%rsp, %rdi
	movl	$8192, %esi                     # imm = 0x2000
	movq	%rbx, %rdx
	callq	fgets
	testq	%rax, %rax
	je	.LBB3_3
# %bb.1:                                # %.preheader
	movq	%rsp, %rdi
	callq	strlen
	testq	%rax, %rax
	je	.LBB3_2
# %bb.6:                                # %.lr.ph.preheader
	xorl	%ecx, %ecx
	xorl	%ebp, %ebp
.LBB3_7:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	xorl	%edx, %edx
	cmpb	$44, (%rsp,%rcx)
	sete	%dl
	addl	%edx, %ebp
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB3_7
# %bb.4:                                # %._crit_edge.loopexit
	incl	%ebp
	jmp	.LBB3_5
.LBB3_2:
	movl	$1, %ebp
.LBB3_5:                                # %._crit_edge
	movq	%rbx, %rdi
	callq	rewind
	movl	%ebp, %eax
	addq	$8200, %rsp                     # imm = 0x2008
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB3_3:
	.cfi_def_cfa_offset 8224
	movq	stderr(%rip), %rdi
	movl	$.L.str.13, %esi
	movl	$80, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end3:
	.size	_Z13getAttributesP8_IO_FILE, .Lfunc_end3-_Z13getAttributesP8_IO_FILE
	.cfi_endproc
                                        # -- End function
	.globl	_Z7readCSVP8_IO_FILEPPfPiS3_    # -- Begin function _Z7readCSVP8_IO_FILEPPfPiS3_
	.type	_Z7readCSVP8_IO_FILEPPfPiS3_,@function
_Z7readCSVP8_IO_FILEPPfPiS3_:           # @_Z7readCSVP8_IO_FILEPPfPiS3_
	.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	$8232, %rsp                     # imm = 0x2028
	.cfi_def_cfa_offset 8288
	.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	%rcx, %r12
	movq	%rdx, %r14
	movq	%rsi, %r15
	movq	%rdi, %rbx
	callq	_Z13getAttributesP8_IO_FILE
	movl	%eax, (%r12)
	movw	$44, 6(%rsp)
	leaq	32(%rsp), %rdi
	movl	$8192, %esi                     # imm = 0x2000
	movq	%rbx, 8(%rsp)                   # 8-byte Spill
	movq	%rbx, %rdx
	callq	fgets
	testq	%rax, %rax
	je	.LBB4_1
# %bb.2:                                # %.lr.ph30.preheader
	movq	%r14, 16(%rsp)                  # 8-byte Spill
	leaq	32(%rsp), %r13
	xorl	%ebx, %ebx
.LBB4_3:                                # %.lr.ph30
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_5 Depth 2
	leaq	8(,%rbx,8), %rsi
	movq	%r15, %rdi
	callq	realloc
	movq	%r13, %rbp
	movq	%rax, %r13
	movslq	(%r12), %r15
	leaq	(,%r15,4), %rdi
	callq	malloc
	movq	%r13, 24(%rsp)                  # 8-byte Spill
	movq	%rax, (%r13,%rbx,8)
	movq	%rbp, %r13
	leaq	6(%rsp), %rbp
	testq	%r15, %r15
	jle	.LBB4_10
# %bb.4:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB4_3 Depth=1
	movq	%rax, %r14
	xorl	%r15d, %r15d
.LBB4_5:                                # %.lr.ph
                                        #   Parent Loop BB4_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	testq	%r15, %r15
	je	.LBB4_6
# %bb.7:                                #   in Loop: Header=BB4_5 Depth=2
	xorl	%edi, %edi
	jmp	.LBB4_8
.LBB4_6:                                #   in Loop: Header=BB4_5 Depth=2
	movq	%r13, %rdi
.LBB4_8:                                #   in Loop: Header=BB4_5 Depth=2
	movq	%rbp, %rsi
	callq	strtok
	testq	%rax, %rax
	je	.LBB4_10
# %bb.9:                                #   in Loop: Header=BB4_5 Depth=2
	movl	$.L.str.14, %esi
	movq	%rax, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_sscanf
	incq	%r15
	movslq	(%r12), %rax
	addq	$4, %r14
	cmpq	%rax, %r15
	jl	.LBB4_5
.LBB4_10:                               # %._crit_edge
                                        #   in Loop: Header=BB4_3 Depth=1
	incq	%rbx
	movq	%r13, %rdi
	movl	$8192, %esi                     # imm = 0x2000
	movq	8(%rsp), %rdx                   # 8-byte Reload
	callq	fgets
	testq	%rax, %rax
	movq	24(%rsp), %r15                  # 8-byte Reload
	jne	.LBB4_3
# %bb.11:                               # %._crit_edge31.loopexit
	movq	16(%rsp), %r14                  # 8-byte Reload
	jmp	.LBB4_12
.LBB4_1:
	xorl	%ebx, %ebx
.LBB4_12:                               # %._crit_edge31
	movl	%ebx, (%r14)
	movq	8(%rsp), %rdi                   # 8-byte Reload
	callq	rewind
	movq	%r15, %rax
	addq	$8232, %rsp                     # imm = 0x2028
	.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_end4:
	.size	_Z7readCSVP8_IO_FILEPPfPiS3_, .Lfunc_end4-_Z7readCSVP8_IO_FILEPPfPiS3_
	.cfi_endproc
                                        # -- End function
	.globl	_Z6getImgPcPiS0_                # -- Begin function _Z6getImgPcPiS0_
	.type	_Z6getImgPcPiS0_,@function
_Z6getImgPcPiS0_:                       # @_Z6getImgPcPiS0_
	.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	%rdx, %r12
	movq	%rsi, %r15
	movl	$.L.str.15, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB5_10
# %bb.1:
	movq	%rax, %rbx
	xorl	%r13d, %r13d
	xorl	%edi, %edi
	callq	malloc
	movq	%rbx, 16(%rsp)                  # 8-byte Spill
	movq	%rbx, %rdi
	movq	%rax, %rsi
	movq	%r15, %rdx
	movq	%r12, %rcx
	callq	_Z7readCSVP8_IO_FILEPPfPiS3_
	movq	%rax, %r14
	movl	(%r12), %ebx
	movl	(%r15), %ebp
	movl	%ebp, %eax
	imull	%ebx, %eax
	movslq	%eax, %rdi
	shlq	$2, %rdi
	callq	malloc
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	testl	%ebp, %ebp
	jle	.LBB5_6
# %bb.2:                                # %.preheader32.lr.ph
	leaq	(,%rbx,4), %r12
	xorl	%r15d, %r15d
.LBB5_3:                                # %.preheader32
                                        # =>This Inner Loop Header: Depth=1
	testl	%ebx, %ebx
	jle	.LBB5_5
# %bb.4:                                # %.lr.ph
                                        #   in Loop: Header=BB5_3 Depth=1
	movl	%r13d, %eax
	movq	8(%rsp), %rcx                   # 8-byte Reload
	leaq	(%rcx,%rax,4), %rdi
	movq	(%r14,%r15,8), %rsi
	movq	%r12, %rdx
	callq	memcpy@PLT
.LBB5_5:                                # %._crit_edge
                                        #   in Loop: Header=BB5_3 Depth=1
	incq	%r15
	addl	%ebx, %r13d
	cmpq	%r15, %rbp
	jne	.LBB5_3
.LBB5_6:                                # %.preheader
	testq	%rbp, %rbp
	je	.LBB5_9
# %bb.7:                                # %.lr.ph37.preheader
	movslq	%ebp, %rbx
	decq	%rbx
	movq	%rbx, %r15
.LBB5_8:                                # %.lr.ph37
                                        # =>This Inner Loop Header: Depth=1
	movq	(%r14,%rbx,8), %rdi
	callq	free
	decq	%r15
	testl	%ebx, %ebx
	movq	%r15, %rbx
	jne	.LBB5_8
.LBB5_9:                                # %._crit_edge38
	movq	%r14, %rdi
	callq	free
	movq	16(%rsp), %rdi                  # 8-byte Reload
	callq	fclose
	movq	8(%rsp), %rax                   # 8-byte Reload
	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
	retq
.LBB5_10:
	.cfi_def_cfa_offset 80
	movq	stderr(%rip), %rbx
	callq	__errno_location
	movl	(%rax), %edi
	callq	strerror
	movl	$.L.str.16, %esi
	movq	%rbx, %rdi
	movl	$133, %edx
	movq	%rax, %rcx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end5:
	.size	_Z6getImgPcPiS0_, .Lfunc_end5-_Z6getImgPcPiS0_
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"."
	.size	.L.str, 2

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"_out.csv"
	.size	.L.str.1, 9

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

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"_out"
	.size	.L.str.3, 5

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	".csv"
	.size	.L.str.5, 5

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"w"
	.size	.L.str.6, 2

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

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"%f%c"
	.size	.L.str.8, 5

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Line:%d No argument was given\n"
	.size	.L.str.9, 31

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Line:%d Less arguments was given\n"
	.size	.L.str.10, 34

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"Line:%d More args given\n"
	.size	.L.str.11, 25

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Line:%d Patch Size is not odd number\n"
	.size	.L.str.12, 38

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Line %d: Input file empty\n"
	.size	.L.str.13, 27

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

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

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"Line %d: Error opening file %s\n"
	.size	.L.str.16, 32

	.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
