#include <stdio.h>
#include <cuda.h>
#include "cuda_runtime_api.h"
#include <stdint.h>
#include <stdlib.h>

//This is the working matrix multiplication code - very basic
/*
Done:
- printing of matrix in a more pleasant manner using printMatrix function
- command line arguments
- opens matrix files and reads the matrix successfully
*/


// START of Auxiliary functions

//Start of kernel multiplication
__global__ void MatrixMulKernel ( int  *Md, int *Nd, int *Pd, int Width ){
	int tx = threadIdx.x;
	int ty = threadIdx.y;

	int Pvalue = 0;
	for ( int k = 0; k < Width; ++k ){
		int Mdelement = Md[ ty * Width + k ];
		int Ndelement = Nd[ k * Width + tx ];
		Pvalue += Mdelement * Ndelement;
	}

	Pd[ ty * Width + tx  ] = Pvalue;
}
//End of kernel multiplication

//Start of matrix multiplication host function
void MatrixMul( int *M, int *N, int  *P, int Width ){
	int size = Width * Width * sizeof( int );
	int *Md, *Nd, *Pd;

	cudaMalloc( (void**) &Md, size );
	cudaMemcpy( Md, M, size, cudaMemcpyHostToDevice );
	cudaMalloc( (void**) &Nd, size );
        cudaMemcpy( Nd, N, size, cudaMemcpyHostToDevice );
	cudaMalloc( (void**) &Pd, size );
	
	dim3 dimBlock( Width, Width );
	dim3 dimGrid( 1, 1 );
	
	MatrixMulKernel<<< dimGrid, dimBlock >>>( Md, Nd, Pd, Width );

	cudaMemcpy( P, Pd, size, cudaMemcpyDeviceToHost );

	cudaFree( Md ); cudaFree( Nd ); cudaFree ( Pd );
}
//End of Matrix multiplication function

//Start of getMatWidth => Get width i.e. # of columns
int getMatWidth( char *filename ){
	int width;
	//assumes space separate integer values e.g. -1 23 4 -56 6 77
	//assumes first integer in file is row, 2nd integer is column
	FILE *ptr = fopen( filename, "r" );
	if ( ptr == 0 ){
		printf( "\n could not open file %s \n", filename );
		width = 0;
	}
	else{
		fscanf( ptr, "%d", &width  );
	}
	fclose( ptr );
	return width;
}//end of getMatWidth function

//Start of getMatHeight => Get height i.e. # of rows
int getMatHeight( char *filename ){
	int height, dummy;
	//assumes space separate integer values e.g. -1 23 4 -56 6 77
	//assumes first integer in file is row, 2nd integer is column
	FILE *ptr = fopen( filename, "r" );
	if ( ptr == 0 ){
		printf( "\n could not open file %s \n", filename );
		height = 0;
	}
	else{
		for ( int count = 1; count < 3; count++ ){
			if ( count != 2 )
				fscanf( ptr, "%d", &dummy );
		else
			fscanf( ptr, "%d", &dummy  );
			height = dummy;
		}
	}
	fclose( ptr );
	return height;
}//end of getMatHeight function


//function to print matrix
void printMatrix ( int *M, int rows, int columns ){
	//assumes matrix is in row-major format
	printf ( "\n %s: \n", "M" );
	for ( int v = 0; v < rows; v++  ){
	//assumes a square matrix
		for ( int w = 0; w < columns; w++   ) {
			printf ( " %03d ", M[ v * columns + w ]  );
		}
		printf ( " \n " );
	}
}//End of printMatrix function

//END of Auxiliary functions


//START of Main function
int main ( int argc, char *argv[ ] ) {
	
	if ( argc != 3 ) {
		printf( "\nusage: %s matrixFile1 matrixFile2 \n\n", argv [ 0 ] );
	}
	else {
		char *filename1 = argv[ 1 ];
		char *filename2 = argv[ 2 ];
		int *matA; //holds 1st matrix
		int *matB; //holds 2nd matrix
		
		matA = ( int * ) malloc( sizeof ( int ) );
		matB = ( int * ) malloc( sizeof ( int ) );

		printf( "you have entered files %s and %s \n", filename1, filename2 );
		//load matrices from files
		FILE *ptr1 = fopen( filename1, "r" );
		FILE *ptr2 = fopen( filename2, "r" );

		if ( ptr1 == 0 && ptr2 == 0 )
			printf( "\n could not open one of the following files: %s %s \n", argv[ 1 ], argv[ 2 ] );
		else {
		//load matrices from files
			//get heigh/rows and width/columns of matrices
			int matWidthA = getMatWidth ( filename1  );
			int matHeightA = getMatHeight ( filename1  );
			
			int matWidthB = getMatWidth ( filename2  );
			int matHeightB = getMatHeight ( filename2  );

			int y = 1;
			int x;
			int offset = 2;
			int z[ ( matWidthA * matHeightA ) + offset ] ;
			fscanf( ptr1, " %d", &x  );
			while( !feof( ptr1 ) && y < ( matWidthA * matHeightA ) + offset ){
				if ( y > offset ){
                                   	fscanf( ptr1, " %d", &z[ y - offset ]  );
					printf( " B: z[ %d ]: %d \n", y, z[ y - offset ] );
					//fscanf( ptr1, " %d", &x );
					//printf( "\n A: y: %d MatEl: %d \n", y, x );
				} /*
				else{
					fscanf( ptr1, " %d", &z[ y - offset ]  );
					printf( " B: z[ %d ]: %d \n", y, z[ y - offset ] );
				} */
				y++;
			}
		}

		free( matA ); free( matB );
	}

	int Width = 4;
	
	int A[ Width * Width ];

	for ( int x = 0; x < Width * Width; x++ ){
		A[ x ] = 2;
	}
	
	int B[ Width * Width ];
	for ( int z = 0; z < Width * Width; z++ ){
		B[ z ] = 2;
	}
	
	int C[ Width * Width ];

	//MatrixMul( A, B, C, Width );
	//printMatrix( C, Width, Width );
}
//END of Main function

	.file	"tmpxft_0039c8f3_00000000-6_mulMatrix-v1.3-test.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"r"
.LC1:
	.string	"\n could not open file %s \n"
.LC2:
	.string	"%d"
	.text
	.globl	_Z11getMatWidthPc
	.type	_Z11getMatWidthPc, @function
_Z11getMatWidthPc:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC0(%rip), %rsi
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	jne	.L3
	movq	%rbp, %rdx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
	movl	%eax, 4(%rsp)
	jmp	.L4
.L3:
	movq	%rax, %rdi
	leaq	4(%rsp), %rdx
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
.L4:
	movq	%rbx, %rdi
	call	fclose@PLT
	movl	4(%rsp), %eax
	movq	8(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L5
	call	__stack_chk_fail@PLT
.L5:
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z11getMatWidthPc, .-_Z11getMatWidthPc
	.globl	_Z12getMatHeightPc
	.type	_Z12getMatHeightPc, @function
_Z12getMatHeightPc:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC0(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$16, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	je	.L9
	leaq	.LC2(%rip), %rbp
	leaq	4(%rsp), %r12
	movq	%rax, %rdi
	xorl	%eax, %eax
	movq	%rbp, %rsi
	movq	%r12, %rdx
	call	__isoc23_fscanf@PLT
	movq	%rbp, %rsi
	movq	%r12, %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movl	4(%rsp), %ebp
	jmp	.L10
.L9:
	movq	%rbp, %rdx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%ebp, %ebp
.L10:
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	addq	$16, %rsp
	.cfi_def_cfa_offset 32
	movl	%ebp, %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
.LFE2029:
	.size	_Z12getMatHeightPc, .-_Z12getMatHeightPc
	.section	.rodata.str1.1
.LC3:
	.string	"M"
.LC4:
	.string	"\n %s: \n"
.LC5:
	.string	" %03d "
.LC6:
	.string	" \n "
	.text
	.globl	_Z11printMatrixPiii
	.type	_Z11printMatrixPiii, @function
_Z11printMatrixPiii:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%eax, %eax
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movl	%esi, %r14d
	leaq	.LC4(%rip), %rsi
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	xorl	%r13d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rdi, %rbp
	movl	$2, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edx, %ebx
	leaq	.LC3(%rip), %rdx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	call	__printf_chk@PLT
.L14:
	cmpl	%r14d, %r13d
	jge	.L13
	movslq	%r12d, %rax
	xorl	%r15d, %r15d
	leaq	0(%rbp,%rax,4), %r8
.L17:
	cmpl	%r15d, %ebx
	jle	.L19
	movl	(%r8,%r15,4), %edx
	leaq	.LC5(%rip), %rsi
	xorl	%eax, %eax
	incq	%r15
	movl	$2, %edi
	movq	%r8, 8(%rsp)
	call	__printf_chk@PLT
	movq	8(%rsp), %r8
	jmp	.L17
.L19:
	leaq	.LC6(%rip), %rsi
	xorl	%eax, %eax
	incl	%r13d
	addl	%ebx, %r12d
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L14
.L13:
	addq	$24, %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
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z11printMatrixPiii, .-_Z11printMatrixPiii
	.section	.rodata.str1.1
.LC7:
	.string	"\nusage: %s matrixFile1 matrixFile2 \n\n"
.LC8:
	.string	"you have entered files %s and %s \n"
.LC9:
	.string	"\n could not open one of the following files: %s %s \n"
.LC10:
	.string	" %d"
.LC11:
	.string	" B: z[ %d ]: %d \n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	.cfi_offset 15, -24
	.cfi_offset 14, -32
	.cfi_offset 13, -40
	.cfi_offset 12, -48
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	subq	$40, %rsp
	movq	%fs:40, %rax
	movq	%rax, -56(%rbp)
	xorl	%eax, %eax
	cmpl	$3, %edi
	je	.L21
	movq	(%rsi), %rdx
	movl	$2, %edi
	leaq	.LC7(%rip), %rsi
	call	__printf_chk@PLT
	jmp	.L22
.L21:
	movq	8(%rsi), %r14
	movq	16(%rsi), %r13
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC8(%rip), %rsi
	leaq	.LC0(%rip), %r15
	movq	%r13, %rcx
	movq	%r14, %rdx
	call	__printf_chk@PLT
	movq	%r15, %rsi
	movq	%r14, %rdi
	call	fopen@PLT
	movq	%r15, %rsi
	movq	%r13, %rdi
	movq	%rax, %r12
	call	fopen@PLT
	orq	%r12, %rax
	jne	.L23
	movq	16(%rbx), %rcx
	movq	8(%rbx), %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC9(%rip), %rsi
	call	__printf_chk@PLT
	jmp	.L22
.L23:
	movq	%r14, %rdi
	movq	%rsp, -72(%rbp)
	call	_Z11getMatWidthPc
	movq	%r14, %rdi
	movl	%eax, %ebx
	call	_Z12getMatHeightPc
	movq	%r13, %rdi
	movl	%eax, %r14d
	call	_Z11getMatWidthPc
	imull	%r14d, %ebx
	movq	%r13, %rdi
	call	_Z12getMatHeightPc
	movq	%rsp, %rcx
	addl	$2, %ebx
	movslq	%ebx, %rax
	leaq	15(,%rax,4), %rdx
	movq	%rdx, %rax
	andq	$-4096, %rdx
	andq	$-16, %rax
	subq	%rdx, %rcx
.L24:
	cmpq	%rcx, %rsp
	je	.L25
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L24
.L25:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L26
	orq	$0, -8(%rsp,%rax)
.L26:
	movq	%rsp, %r13
	leaq	-60(%rbp), %rdx
	leaq	.LC10(%rip), %rsi
	xorl	%eax, %eax
	movq	%r12, %rdi
	subq	$4, %r13
	movl	$1, %r14d
	call	__isoc23_fscanf@PLT
	leaq	.LC11(%rip), %r15
.L27:
	movq	%r12, %rdi
	call	feof@PLT
	cmpl	%ebx, %r14d
	jge	.L28
	testl	%eax, %eax
	jne	.L28
	cmpl	$2, %r14d
	jle	.L30
	movq	%r13, %rdx
	leaq	.LC10(%rip), %rsi
	movq	%r12, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movl	0(%r13), %ecx
	movl	%r14d, %edx
	movq	%r15, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L30:
	incl	%r14d
	addq	$4, %r13
	jmp	.L27
.L28:
	movq	-72(%rbp), %rsp
.L22:
	movq	-56(%rbp), %rax
	subq	%fs:40, %rax
	je	.L31
	call	__stack_chk_fail@PLT
.L31:
	leaq	-40(%rbp), %rsp
	xorl	%eax, %eax
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE2031:
	.size	main, .-main
	.text
	.globl	_Z40__device_stub__Z15MatrixMulKernelPiS_S_iPiS_S_i
	.type	_Z40__device_stub__Z15MatrixMulKernelPiS_S_iPiS_S_i, @function
_Z40__device_stub__Z15MatrixMulKernelPiS_S_iPiS_S_i:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 4(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L42
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z15MatrixMulKernelPiS_S_i(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L42:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L44
	call	__stack_chk_fail@PLT
.L44:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z40__device_stub__Z15MatrixMulKernelPiS_S_iPiS_S_i, .-_Z40__device_stub__Z15MatrixMulKernelPiS_S_iPiS_S_i
	.globl	_Z15MatrixMulKernelPiS_S_i
	.type	_Z15MatrixMulKernelPiS_S_i, @function
_Z15MatrixMulKernelPiS_S_i:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z40__device_stub__Z15MatrixMulKernelPiS_S_iPiS_S_i
	.cfi_endproc
.LFE2057:
	.size	_Z15MatrixMulKernelPiS_S_i, .-_Z15MatrixMulKernelPiS_S_i
	.globl	_Z9MatrixMulPiS_S_i
	.type	_Z9MatrixMulPiS_S_i, @function
_Z9MatrixMulPiS_S_i:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	%ecx, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movq	%rdx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movl	%ecx, %ebx
	imull	%ecx, %ebx
	subq	$64, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	sall	$2, %ebx
	leaq	8(%rsp), %rdi
	movslq	%ebx, %rbx
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r13, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	16(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	24(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movl	%r14d, 32(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	%r14d, 36(%rsp)
	movq	32(%rsp), %rdx
	movl	$1, %ecx
	movabsq	$4294967297, %rdi
	movl	$1, %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L48
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rsi
	movl	%r14d, %ecx
	movq	8(%rsp), %rdi
	call	_Z40__device_stub__Z15MatrixMulKernelPiS_S_iPiS_S_i
.L48:
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdx
	movq	%rbp, %rdi
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L49
	call	__stack_chk_fail@PLT
.L49:
	addq	$64, %rsp
	.cfi_def_cfa_offset 48
	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	_Z9MatrixMulPiS_S_i, .-_Z9MatrixMulPiS_S_i
	.section	.rodata.str1.1
.LC12:
	.string	"_Z15MatrixMulKernelPiS_S_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC12(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z15MatrixMulKernelPiS_S_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2059:
	.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 <hip/hip_runtime.h>
#include "hip/hip_runtime_api.h"
#include <stdint.h>
#include <stdlib.h>

//This is the working matrix multiplication code - very basic
/*
Done:
- printing of matrix in a more pleasant manner using printMatrix function
- command line arguments
- opens matrix files and reads the matrix successfully
*/


// START of Auxiliary functions

//Start of kernel multiplication
__global__ void MatrixMulKernel ( int  *Md, int *Nd, int *Pd, int Width ){
	int tx = threadIdx.x;
	int ty = threadIdx.y;

	int Pvalue = 0;
	for ( int k = 0; k < Width; ++k ){
		int Mdelement = Md[ ty * Width + k ];
		int Ndelement = Nd[ k * Width + tx ];
		Pvalue += Mdelement * Ndelement;
	}

	Pd[ ty * Width + tx  ] = Pvalue;
}
//End of kernel multiplication

//Start of matrix multiplication host function
void MatrixMul( int *M, int *N, int  *P, int Width ){
	int size = Width * Width * sizeof( int );
	int *Md, *Nd, *Pd;

	hipMalloc( (void**) &Md, size );
	hipMemcpy( Md, M, size, hipMemcpyHostToDevice );
	hipMalloc( (void**) &Nd, size );
        hipMemcpy( Nd, N, size, hipMemcpyHostToDevice );
	hipMalloc( (void**) &Pd, size );
	
	dim3 dimBlock( Width, Width );
	dim3 dimGrid( 1, 1 );
	
	MatrixMulKernel<<< dimGrid, dimBlock >>>( Md, Nd, Pd, Width );

	hipMemcpy( P, Pd, size, hipMemcpyDeviceToHost );

	hipFree( Md ); hipFree( Nd ); hipFree ( Pd );
}
//End of Matrix multiplication function

//Start of getMatWidth => Get width i.e. # of columns
int getMatWidth( char *filename ){
	int width;
	//assumes space separate integer values e.g. -1 23 4 -56 6 77
	//assumes first integer in file is row, 2nd integer is column
	FILE *ptr = fopen( filename, "r" );
	if ( ptr == 0 ){
		printf( "\n could not open file %s \n", filename );
		width = 0;
	}
	else{
		fscanf( ptr, "%d", &width  );
	}
	fclose( ptr );
	return width;
}//end of getMatWidth function

//Start of getMatHeight => Get height i.e. # of rows
int getMatHeight( char *filename ){
	int height, dummy;
	//assumes space separate integer values e.g. -1 23 4 -56 6 77
	//assumes first integer in file is row, 2nd integer is column
	FILE *ptr = fopen( filename, "r" );
	if ( ptr == 0 ){
		printf( "\n could not open file %s \n", filename );
		height = 0;
	}
	else{
		for ( int count = 1; count < 3; count++ ){
			if ( count != 2 )
				fscanf( ptr, "%d", &dummy );
		else
			fscanf( ptr, "%d", &dummy  );
			height = dummy;
		}
	}
	fclose( ptr );
	return height;
}//end of getMatHeight function


//function to print matrix
void printMatrix ( int *M, int rows, int columns ){
	//assumes matrix is in row-major format
	printf ( "\n %s: \n", "M" );
	for ( int v = 0; v < rows; v++  ){
	//assumes a square matrix
		for ( int w = 0; w < columns; w++   ) {
			printf ( " %03d ", M[ v * columns + w ]  );
		}
		printf ( " \n " );
	}
}//End of printMatrix function

//END of Auxiliary functions


//START of Main function
int main ( int argc, char *argv[ ] ) {
	
	if ( argc != 3 ) {
		printf( "\nusage: %s matrixFile1 matrixFile2 \n\n", argv [ 0 ] );
	}
	else {
		char *filename1 = argv[ 1 ];
		char *filename2 = argv[ 2 ];
		int *matA; //holds 1st matrix
		int *matB; //holds 2nd matrix
		
		matA = ( int * ) malloc( sizeof ( int ) );
		matB = ( int * ) malloc( sizeof ( int ) );

		printf( "you have entered files %s and %s \n", filename1, filename2 );
		//load matrices from files
		FILE *ptr1 = fopen( filename1, "r" );
		FILE *ptr2 = fopen( filename2, "r" );

		if ( ptr1 == 0 && ptr2 == 0 )
			printf( "\n could not open one of the following files: %s %s \n", argv[ 1 ], argv[ 2 ] );
		else {
		//load matrices from files
			//get heigh/rows and width/columns of matrices
			int matWidthA = getMatWidth ( filename1  );
			int matHeightA = getMatHeight ( filename1  );
			
			int matWidthB = getMatWidth ( filename2  );
			int matHeightB = getMatHeight ( filename2  );

			int y = 1;
			int x;
			int offset = 2;
			int z[ ( matWidthA * matHeightA ) + offset ] ;
			fscanf( ptr1, " %d", &x  );
			while( !feof( ptr1 ) && y < ( matWidthA * matHeightA ) + offset ){
				if ( y > offset ){
                                   	fscanf( ptr1, " %d", &z[ y - offset ]  );
					printf( " B: z[ %d ]: %d \n", y, z[ y - offset ] );
					//fscanf( ptr1, " %d", &x );
					//printf( "\n A: y: %d MatEl: %d \n", y, x );
				} /*
				else{
					fscanf( ptr1, " %d", &z[ y - offset ]  );
					printf( " B: z[ %d ]: %d \n", y, z[ y - offset ] );
				} */
				y++;
			}
		}

		free( matA ); free( matB );
	}

	int Width = 4;
	
	int A[ Width * Width ];

	for ( int x = 0; x < Width * Width; x++ ){
		A[ x ] = 2;
	}
	
	int B[ Width * Width ];
	for ( int z = 0; z < Width * Width; z++ ){
		B[ z ] = 2;
	}
	
	int C[ Width * Width ];

	//MatrixMul( A, B, C, Width );
	//printMatrix( C, Width, Width );
}
//END of Main function

	.text
	.file	"mulMatrix-v1.3-test.hip"
	.globl	_Z30__device_stub__MatrixMulKernelPiS_S_i # -- Begin function _Z30__device_stub__MatrixMulKernelPiS_S_i
	.type	_Z30__device_stub__MatrixMulKernelPiS_S_i,@function
_Z30__device_stub__MatrixMulKernelPiS_S_i: # @_Z30__device_stub__MatrixMulKernelPiS_S_i
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z15MatrixMulKernelPiS_S_i, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.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
	retq
.Lfunc_end0:
	.size	_Z30__device_stub__MatrixMulKernelPiS_S_i, .Lfunc_end0-_Z30__device_stub__MatrixMulKernelPiS_S_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z9MatrixMulPiS_S_i             # -- Begin function _Z9MatrixMulPiS_S_i
	.type	_Z9MatrixMulPiS_S_i,@function
_Z9MatrixMulPiS_S_i:                    # @_Z9MatrixMulPiS_S_i
	.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
	movl	%ecx, %ebp
	movq	%rdx, %rbx
	movq	%rsi, %r15
	movq	%rdi, %r12
	movl	%ecx, %eax
	imull	%ecx, %eax
	shll	$2, %eax
	movslq	%eax, %r14
	leaq	16(%rsp), %r13
	movq	%r13, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	(%r13), %rdi
	movq	%r12, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	8(%rsp), %r12
	movq	%r12, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	(%r12), %rdi
	movq	%r15, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	%rsp, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movl	%ebp, %eax
	movq	%rax, %rdx
	shlq	$32, %rdx
	orq	%rax, %rdx
	movabsq	$4294967297, %rdi               # imm = 0x100000001
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_2
# %bb.1:
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdx
	movl	%ebp, %ecx
	callq	_Z30__device_stub__MatrixMulKernelPiS_S_i
.LBB1_2:
	movq	(%rsp), %rsi
	movq	%rbx, %rdi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	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
.Lfunc_end1:
	.size	_Z9MatrixMulPiS_S_i, .Lfunc_end1-_Z9MatrixMulPiS_S_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z11getMatWidthPc               # -- Begin function _Z11getMatWidthPc
	.type	_Z11getMatWidthPc,@function
_Z11getMatWidthPc:                      # @_Z11getMatWidthPc
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rdi, %r14
	movl	$.L.str, %esi
	callq	fopen
	movq	%rax, %rbx
	testq	%rax, %rax
	je	.LBB2_1
# %bb.2:
	leaq	4(%rsp), %rdx
	movl	$.L.str.2, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	jmp	.LBB2_3
.LBB2_1:
	movl	$.L.str.1, %edi
	movq	%r14, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$0, 4(%rsp)
.LBB2_3:
	movq	%rbx, %rdi
	callq	fclose
	movl	4(%rsp), %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z11getMatWidthPc, .Lfunc_end2-_Z11getMatWidthPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z12getMatHeightPc              # -- Begin function _Z12getMatHeightPc
	.type	_Z12getMatHeightPc,@function
_Z12getMatHeightPc:                     # @_Z12getMatHeightPc
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$16, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %rbp, -16
	movq	%rdi, %r14
	movl	$.L.str, %esi
	callq	fopen
	movq	%rax, %rbx
	testq	%rax, %rax
	je	.LBB3_5
# %bb.1:                                # %.preheader.preheader
	movl	$2, %ebp
	leaq	12(%rsp), %r14
.LBB3_2:                                # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.2, %esi
	movq	%rbx, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	decl	%ebp
	jne	.LBB3_2
# %bb.3:                                # %.loopexit.loopexit
	movl	12(%rsp), %ebp
	jmp	.LBB3_4
.LBB3_5:
	xorl	%ebp, %ebp
	movl	$.L.str.1, %edi
	movq	%r14, %rsi
	xorl	%eax, %eax
	callq	printf
.LBB3_4:                                # %.loopexit
	movq	%rbx, %rdi
	callq	fclose
	movl	%ebp, %eax
	addq	$16, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z12getMatHeightPc, .Lfunc_end3-_Z12getMatHeightPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z11printMatrixPiii             # -- Begin function _Z11printMatrixPiii
	.type	_Z11printMatrixPiii,@function
_Z11printMatrixPiii:                    # @_Z11printMatrixPiii
	.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
	movl	%edx, %ebx
	movl	%esi, %ebp
	movq	%rdi, 8(%rsp)                   # 8-byte Spill
	movl	$.L.str.3, %edi
	movl	$.L.str.4, %esi
	xorl	%eax, %eax
	callq	printf
	testl	%ebp, %ebp
	jle	.LBB4_6
# %bb.1:                                # %.preheader.lr.ph
	movl	%ebp, %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	%ebx, %r12d
	xorl	%ebp, %ebp
	xorl	%r13d, %r13d
.LBB4_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_4 Depth 2
	testl	%ebx, %ebx
	jle	.LBB4_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB4_2 Depth=1
	movl	%ebp, %eax
	movq	8(%rsp), %rcx                   # 8-byte Reload
	leaq	(%rcx,%rax,4), %r14
	xorl	%r15d, %r15d
.LBB4_4:                                #   Parent Loop BB4_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%r14,%r15,4), %esi
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r15
	cmpq	%r15, %r12
	jne	.LBB4_4
.LBB4_5:                                # %._crit_edge
                                        #   in Loop: Header=BB4_2 Depth=1
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r13
	addl	%ebx, %ebp
	cmpq	16(%rsp), %r13                  # 8-byte Folded Reload
	jne	.LBB4_2
.LBB4_6:                                # %._crit_edge13
	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
.Lfunc_end4:
	.size	_Z11printMatrixPiii, .Lfunc_end4-_Z11printMatrixPiii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$24, %rsp
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	movq	%rsi, %r15
	cmpl	$3, %edi
	jne	.LBB5_1
# %bb.2:
	movq	8(%r15), %r12
	movq	16(%r15), %r14
	movl	$.L.str.8, %edi
	movq	%r12, %rsi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str, %esi
	movq	%r12, %rdi
	callq	fopen
	movq	%rax, %rbx
	movl	$.L.str, %esi
	movq	%r14, %rdi
	callq	fopen
	orq	%rbx, %rax
	jne	.LBB5_4
# %bb.3:
	movq	8(%r15), %rsi
	movq	16(%r15), %rdx
	movl	$.L.str.9, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB5_12
.LBB5_1:
	movq	(%r15), %rsi
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB5_12
.LBB5_4:
	movq	%r12, %rdi
	callq	_Z11getMatWidthPc
	movl	%eax, %r13d
	movq	%r12, %rdi
	callq	_Z12getMatHeightPc
	movl	%eax, %r15d
	movq	%r14, %rdi
	callq	_Z11getMatWidthPc
	movq	%r14, %rdi
	callq	_Z12getMatHeightPc
	movq	%rsp, -48(%rbp)                 # 8-byte Spill
	imull	%r13d, %r15d
	leal	2(%r15), %r12d
	leaq	15(,%r12,4), %rax
	andq	$-16, %rax
	movq	%rsp, %r14
	subq	%rax, %r14
	movq	%r14, %rsp
	leaq	-52(%rbp), %rdx
	movl	$.L.str.10, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movq	%rbx, %rdi
	callq	feof
	testl	%eax, %eax
	jne	.LBB5_11
# %bb.5:
	testl	%r15d, %r15d
	js	.LBB5_11
# %bb.6:                                # %.lr.ph.preheader
	movl	%r12d, %r13d
	addq	$-4, %r14
	movl	$1, %r15d
	xorl	%r12d, %r12d
.LBB5_7:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	cmpq	$3, %r15
	jb	.LBB5_9
# %bb.8:                                #   in Loop: Header=BB5_7 Depth=1
	movl	$.L.str.10, %esi
	movq	%rbx, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	(%r14), %edx
	movl	$.L.str.11, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
.LBB5_9:                                #   in Loop: Header=BB5_7 Depth=1
	movq	%rbx, %rdi
	callq	feof
	testl	%eax, %eax
	jne	.LBB5_11
# %bb.10:                               #   in Loop: Header=BB5_7 Depth=1
	incq	%r15
	leaq	2(%r12), %rax
	addq	$4, %r14
	incq	%r12
	cmpq	%r13, %rax
	jb	.LBB5_7
.LBB5_11:                               # %.critedge
	movq	-48(%rbp), %rsp                 # 8-byte Reload
.LBB5_12:                               # %.preheader.preheader
	xorl	%eax, %eax
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa %rsp, 8
	retq
.Lfunc_end5:
	.size	main, .Lfunc_end5-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z15MatrixMulKernelPiS_S_i, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB7_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z15MatrixMulKernelPiS_S_i,@object # @_Z15MatrixMulKernelPiS_S_i
	.section	.rodata,"a",@progbits
	.globl	_Z15MatrixMulKernelPiS_S_i
	.p2align	3, 0x0
_Z15MatrixMulKernelPiS_S_i:
	.quad	_Z30__device_stub__MatrixMulKernelPiS_S_i
	.size	_Z15MatrixMulKernelPiS_S_i, 8

	.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	"\n could not open file %s \n"
	.size	.L.str.1, 27

	.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	"\n %s: \n"
	.size	.L.str.3, 8

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	" %03d "
	.size	.L.str.5, 7

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	" \n "
	.size	.L.str.6, 4

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"\nusage: %s matrixFile1 matrixFile2 \n\n"
	.size	.L.str.7, 38

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"you have entered files %s and %s \n"
	.size	.L.str.8, 35

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"\n could not open one of the following files: %s %s \n"
	.size	.L.str.9, 53

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

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	" B: z[ %d ]: %d \n"
	.size	.L.str.11, 18

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z15MatrixMulKernelPiS_S_i"
	.size	.L__unnamed_1, 27

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.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 _Z30__device_stub__MatrixMulKernelPiS_S_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z15MatrixMulKernelPiS_S_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z15MatrixMulKernelPiS_S_i ; -- Begin function _Z15MatrixMulKernelPiS_S_i
	.globl	_Z15MatrixMulKernelPiS_S_i
	.p2align	8
	.type	_Z15MatrixMulKernelPiS_S_i,@function
_Z15MatrixMulKernelPiS_S_i:             ; @_Z15MatrixMulKernelPiS_S_i
; %bb.0:
	s_clause 0x2
	s_load_b32 s2, s[0:1], 0x18
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[0:1], s[0:1], 0x10
	v_bfe_u32 v1, v0, 10, 10
	v_and_b32_e32 v6, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_2)
	v_mul_lo_u32 v1, v1, s2
	s_cmp_lt_i32 s2, 1
	s_cbranch_scc1 .LBB0_3
; %bb.1:                                ; %.lr.ph.preheader
	v_mov_b32_e32 v3, 0
	s_mov_b32 s3, s2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mov_b32_e32 v2, v3
	v_lshlrev_b64 v[4:5], 2, v[1:2]
	v_mov_b32_e32 v2, v6
	v_mov_b32_e32 v0, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_u32 v4, vcc_lo, s4, v4
	v_add_co_ci_u32_e32 v5, vcc_lo, s5, v5, vcc_lo
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	v_lshlrev_b64 v[7:8], 2, v[2:3]
	v_add_nc_u32_e32 v2, s2, v2
	s_add_i32 s3, s3, -1
	s_cmp_eq_u32 s3, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_u32 v7, vcc_lo, s6, v7
	v_add_co_ci_u32_e32 v8, vcc_lo, s7, v8, vcc_lo
	global_load_b32 v9, v[4:5], off
	global_load_b32 v10, v[7:8], off
	v_add_co_u32 v4, vcc_lo, v4, 4
	v_add_co_ci_u32_e32 v5, vcc_lo, 0, v5, vcc_lo
	s_waitcnt vmcnt(0)
	v_mad_u64_u32 v[7:8], null, v10, v9, v[0:1]
	s_delay_alu instid0(VALU_DEP_1)
	v_mov_b32_e32 v0, v7
	s_cbranch_scc0 .LBB0_2
	s_branch .LBB0_4
.LBB0_3:
	v_mov_b32_e32 v0, 0
.LBB0_4:                                ; %Flow39
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v1, v1, v6
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[1:2], 2, v[1:2]
	v_add_co_u32 v1, vcc_lo, s0, v1
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v2, vcc_lo, s1, v2, vcc_lo
	global_store_b32 v[1:2], v0, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z15MatrixMulKernelPiS_S_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 28
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 11
		.amdhsa_next_free_sgpr 8
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z15MatrixMulKernelPiS_S_i, .Lfunc_end0-_Z15MatrixMulKernelPiS_S_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 276
; NumSgprs: 10
; NumVgprs: 11
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 1
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 10
; NumVGPRsForWavesPerEU: 11
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.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_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 28
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z15MatrixMulKernelPiS_S_i
    .private_segment_fixed_size: 0
    .sgpr_count:     10
    .sgpr_spill_count: 0
    .symbol:         _Z15MatrixMulKernelPiS_S_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     11
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
