/*
 * Russell Taylor(rtaylor)
 * Matt Crusse(macrusse)
 * CPE458-01 Lab 1 Winter 2013 
 */

#include <sys/stat.h>
#include <sys/mman.h> 
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdint.h>

#define TILE_SIZE 32

/* Compile-Time Declaration on double or float usage */
#ifdef DOUBLE
#define TYPEUSE double
#else
#define TYPEUSE float
#endif

/* 
 * Handles CUDA errors, taking from provided sample code on clupo site
 */
static void HandleError( cudaError_t err, const char * file, int line)
{
  if(err !=cudaSuccess){
    printf("%s in %s at line %d\n", cudaGetErrorString(err), file, line);
    exit(EXIT_FAILURE);
  }
}
#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))


/*
 * Reads Input File and Returns Buffer of Contents
 */
char* read_file(const char * file_name) 
{
  size_t size;
  char *buffer;
  FILE *fp;
  
  fp = fopen(file_name,"r");
  if(!fp) {
    fprintf (stderr, "Error opening input file.\n");
    exit (EXIT_FAILURE);    
  }

  fseek (fp, 0, SEEK_END);
  size = ftell(fp);
  rewind (fp);
  
  buffer = (char*) malloc (sizeof(char)*size);
  fread (buffer, 1, size, fp);
  fclose(fp);
  return buffer;
}

/* 
 * Calculate the Resultant Matrix from Multiplication
 */
void calc_matrix(TYPEUSE *A, TYPEUSE *B, TYPEUSE *C, int Arow, int Acol, int Brow, int Bcol)
{
  uint64_t i, j, k;
  TYPEUSE sum = 0;
  for(i = 0; i < Arow; i++)//Iterate through Matrix B columnwise
  {
    for(j = 0; j < Bcol; j++)//Iterate through Matrix A rowwise
    {
        for(k = 0; k < Acol; k++)//Acol = Brow on valid Matrices
        {
          if(i >475)
            printf("");
          sum+= A[ i* (Acol) + k] * B[k * (Bcol) + j];
          
        }
        C[i *Acol + j] = sum;
        sum = 0;
    }
  }
}

/* 
 * Print matrix values to a file outputfile 
 */
void output_matrix(const char * outputfile, TYPEUSE *matrix, int row, int col) 
{
  int i, j;

  FILE *ofp = fopen(outputfile, "w");
  if(!ofp){
    fprintf (stderr, "Error opening output file.\n");
    exit (EXIT_FAILURE);    
  }

  for(i = 0; i < row; i++) {
    for(j = 0; j < col; j++) {
      fprintf(ofp, "%.2f ",matrix[i*uint64_t(col) + j]);
    }  
    if(i < row-1){
      fprintf(ofp, "\n");
    }
  }
  fprintf(ofp, "\n");
  fclose(ofp);
}


/*
 * Simply prints out the matrix to screen 
 */
void print_matrix(TYPEUSE *matrix, int row, int col) 
{
  int i, j;
  for(i = 0; i < row; i++) {
    for(j = 0; j < col; j++) {
      //printf("(%d,%d)", i, j);
      printf("%.2f ",matrix[i*col +j]);
    }  
    if(i < row-1){
      printf("\n");
    }
  }
  printf("\n");

}

/*
 * Created a Matrix based on Buffered Input Information
 */
TYPEUSE * read_matrix(uint64_t * rowCnt, uint64_t * colCnt, char * mapped)
{
  TYPEUSE value;  
  const char *delim_space = " ";
  char *token = NULL;  
  char *unconverted;
  int i, j, len;
  TYPEUSE *matrix;
  uint64_t bigiter;
  *colCnt = 0;
  *rowCnt = 0;

  
  /* Determine Col Count */
  i = 0;
  while(mapped[i] != '\n'){
    if(mapped[i] == '.') {
     (*colCnt)++;
    }
    i++;
  }  

  /* Determine Row Count */
  bigiter = 0;//For large file sizes, an int is too small to iterate through
  len = strlen(mapped);
  while(bigiter < len && mapped[bigiter] != '\0'){
    if((mapped[bigiter] == '\n') && (mapped[bigiter+1] != '\0') ) {
     (*rowCnt)++;
    }
    bigiter+=1;
  }
  (*rowCnt)++;

  /* Malloc the Matrix */
  if (( matrix = (TYPEUSE *) malloc((*rowCnt) * (*colCnt) * sizeof(TYPEUSE))) == NULL ) {
    printf("malloc issue");
  }
    
  /* Read values into matrix */
  i = 0; j = 0;
  for (token = strtok(mapped, delim_space); token != NULL; token = strtok(NULL, delim_space)) {
    value = strtod(token, &unconverted);
    matrix[i*(*colCnt) +j] = value;
    j++;
    if(j == (*colCnt)) {
      j = 0;
      if(++i == (*rowCnt))
	      break;
    }
  }
  return matrix;

}

/*
 * Simply copies device matrix to shared memory 
 */
__device__ void copyMiniMatrix(TYPEUSE * M_device, TYPEUSE M_shared[TILE_SIZE][TILE_SIZE], uint64_t devOffset)
{
  if(threadIdx.y < TILE_SIZE && threadIdx.x < TILE_SIZE) {
    M_shared[threadIdx.y][threadIdx.x] = M_device[devOffset];  
  }
}


/*
 * Kernel called from main.
 */
__global__ void MMKernel(TYPEUSE *A_d, TYPEUSE *B_d, TYPEUSE * C_d, uint64_t depth, uint64_t Arow, uint64_t Bcol)
{
  TYPEUSE Cvalue = 0.0;
  __shared__ TYPEUSE A_shared[TILE_SIZE][TILE_SIZE], B_shared[TILE_SIZE][TILE_SIZE]; 
  int resultWidth = Bcol;
  int resultCol = blockIdx.x * blockDim.x + threadIdx.x;
  int resultRow = blockIdx.y * blockDim.y + threadIdx.y;  
  int resultIndex = resultRow * resultWidth + resultCol;
  uint64_t threadReadId;

  int validCalcThread = resultRow < Arow && resultCol < Bcol;

  for(int i = 0; i < (depth+TILE_SIZE-1)/TILE_SIZE; i++) {

    /* Copy device matrix A into shared memory */
    threadReadId = threadIdx.x + i * TILE_SIZE;
    if(threadReadId < depth){
      copyMiniMatrix(A_d, A_shared, resultRow*depth + threadReadId);
    }

    /* Copy device matrix B into shared memory */    
    threadReadId = threadIdx.y + i * TILE_SIZE;
    if(threadReadId < depth) {
      copyMiniMatrix(B_d, B_shared, threadReadId * Bcol + resultCol);
    }
    
    /* Wait for all threads to complete copy to shared memory */
    __syncthreads();
    
    
    /* Boundary Check*/
    int remaining = depth - (i * TILE_SIZE);
    int maxIter = (remaining <= TILE_SIZE) * (remaining) + 
                  (remaining > TILE_SIZE) * (TILE_SIZE);

    if(validCalcThread) {
      for(int k = 0; k < maxIter; k++) {
        TYPEUSE Aelem = A_shared[threadIdx.y][k];
        TYPEUSE Belem = B_shared[k][threadIdx.x];
        Cvalue += Aelem * Belem;
      }
    }

    /* Wait for all threads to finish */
    __syncthreads();
  }

  if(validCalcThread) {
    C_d[resultIndex] = Cvalue;
  }
  
}

int main (int argc, const char * argv[])
{
  const char * Cfile = "result.out";
  TYPEUSE * Amatrix, * Bmatrix, * Cmatrix;
  TYPEUSE * A_d, * B_d, * C_d;
  uint64_t Arow, Acol, Brow, Bcol;
  int size;
  int blockRow, blockCol;
  char * Amapped, * Bmapped;

  if(argc != 3) { 
    fprintf(stderr, "Usage: [Matrix A] [Matrix B]\n");
    exit(EXIT_FAILURE);
  }

  /* Device Properties */
  /*
  cudaDeviceProp prop;
  cudaGetDeviceProperties(&prop,0);
  printf("maxThreads: %d\n", prop.maxThreadsPerBlock);
  */

  /* Read and Map matrix */
  Amapped = read_file(argv[1]);
  Bmapped = read_file(argv[2]);
  Amatrix = read_matrix(&Arow, &Acol, Amapped); 
  Bmatrix = read_matrix(&Brow, &Bcol, Bmapped);
  
  if(Acol != Brow) {
    fprintf(stderr, "Matrices are not a compatible size to be multiplied, %dx%d and %dx%d\n", Arow, Acol, Brow, Bcol);
    exit(EXIT_FAILURE);
  }
  
  /* Malloc a New Matrix */
  if (( Cmatrix = (TYPEUSE *) malloc((Arow) * (Bcol) * sizeof(TYPEUSE))) == NULL ) {
    printf("malloc issue");
  }
  /* Malloc and Copy space on GPU */
  size = Arow * Acol * sizeof(TYPEUSE);
  HANDLE_ERROR(cudaMalloc(&A_d, size));
  HANDLE_ERROR(cudaMemcpy(A_d, Amatrix, size, cudaMemcpyHostToDevice));
  
  size = Brow * Bcol * sizeof(TYPEUSE);
  HANDLE_ERROR(cudaMalloc(&B_d, size));
  HANDLE_ERROR(cudaMemcpy(B_d, Bmatrix, size, cudaMemcpyHostToDevice));
  
  size = Arow * Bcol * sizeof(TYPEUSE);
  HANDLE_ERROR(cudaMalloc(&C_d, size));
        
  /* Kernel Setup */  
  blockRow = (Arow+31) / 32;
  blockCol = (Bcol+31) / 32;
  dim3 dimGrid(blockCol,blockRow);
  dim3 dimBlock(32,32);

  /* Kernel Call */  
  MMKernel<<<dimGrid,dimBlock>>>(A_d, B_d, C_d, Brow, Arow, Bcol);
  HANDLE_ERROR(cudaMemcpy(Cmatrix,C_d,size, cudaMemcpyDeviceToHost));

  output_matrix(Cfile, Cmatrix, Arow, Bcol);
  
  //print_matrix(Cmatrix, Arow, Bcol);
  
  /* Free Stuff */
  cudaFree(A_d);
  cudaFree(B_d);
  cudaFree(C_d);
  free(Amatrix);
  free(Bmatrix);
  free(Cmatrix);
  free(Amapped);
  free(Bmapped);

  return 0;
}

	.file	"tmpxft_00359175_00000000-6_main.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2041:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2041:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/russelltaylor05/458-lab1c/master/main.cu"
.LC1:
	.string	"%s in %s at line %d\n"
	.text
	.type	_ZL11HandleError9cudaErrorPKci.constprop.0, @function
_ZL11HandleError9cudaErrorPKci.constprop.0:
.LFB2184:
	.cfi_startproc
	testl	%edi, %edi
	je	.L2
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	%esi, 12(%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r8d
	movl	$2, %edi
	leaq	.LC0(%rip), %rcx
	movq	%rax, %rdx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L2:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2184:
	.size	_ZL11HandleError9cudaErrorPKci.constprop.0, .-_ZL11HandleError9cudaErrorPKci.constprop.0
	.section	.rodata.str1.1
.LC2:
	.string	"r"
.LC3:
	.string	"Error opening input file.\n"
	.text
	.globl	_Z9read_filePKc
	.type	_Z9read_filePKc, @function
_Z9read_filePKc:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC2(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	call	fopen@PLT
	testq	%rax, %rax
	jne	.L9
	movq	stderr(%rip), %rdi
	leaq	.LC3(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L9:
	movq	%rax, %rbx
	movl	$2, %edx
	xorl	%esi, %esi
	movq	%rax, %rdi
	call	fseek@PLT
	movq	%rbx, %rdi
	call	ftell@PLT
	movq	%rbx, %rdi
	movq	%rax, %rbp
	call	rewind@PLT
	movq	%rbp, %rdi
	call	malloc@PLT
	movq	%rbx, %r8
	movq	%rbp, %rcx
	movq	%rbp, %rsi
	movl	$1, %edx
	movq	%rax, %rdi
	movq	%rax, %r12
	call	__fread_chk@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	%r12, %rax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z9read_filePKc, .-_Z9read_filePKc
	.section	.rodata.str1.1
.LC5:
	.string	""
	.text
	.globl	_Z11calc_matrixPfS_S_iiii
	.type	_Z11calc_matrixPfS_S_iiii, @function
_Z11calc_matrixPfS_S_iiii:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rsi, %r9
	movq	%rdx, %r15
	movslq	%ecx, %rcx
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	xorl	%edx, %edx
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movslq	%r8d, %r13
	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
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movslq	144(%rsp), %r10
	movq	%rdi, 16(%rsp)
	leaq	0(,%r10,4), %r11
.L12:
	cmpq	%rcx, %r12
	jnb	.L11
	leaq	0(,%rdx,4), %rbp
	xorl	%ebx, %ebx
	leaq	(%r15,%rbp), %rax
	movq	%rax, 8(%rsp)
	movq	16(%rsp), %rax
	addq	%rax, %rbp
.L18:
	cmpq	%r10, %rbx
	jnb	.L15
	leaq	(%r9,%rbx,4), %r8
	xorps	%xmm0, %xmm0
	xorl	%r14d, %r14d
.L16:
	cmpq	%r13, %r14
	jnb	.L20
	cmpq	$475, %r12
	jbe	.L13
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	movq	%rcx, 72(%rsp)
	movq	%r9, 64(%rsp)
	movq	%rdx, 56(%rsp)
	movq	%r8, 48(%rsp)
	movq	%r10, 32(%rsp)
	movq	%r11, 24(%rsp)
	movss	%xmm0, 44(%rsp)
	call	__printf_chk@PLT
	movq	72(%rsp), %rcx
	movq	64(%rsp), %r9
	movq	56(%rsp), %rdx
	movq	48(%rsp), %r8
	movss	44(%rsp), %xmm0
	movq	32(%rsp), %r10
	movq	24(%rsp), %r11
.L13:
	movss	0(%rbp,%r14,4), %xmm1
	mulss	(%r8), %xmm1
	incq	%r14
	addq	%r11, %r8
	addss	%xmm1, %xmm0
	jmp	.L16
.L20:
	movq	8(%rsp), %rax
	movss	%xmm0, (%rax,%rbx,4)
	incq	%rbx
	jmp	.L18
.L15:
	incq	%r12
	addq	%r13, %rdx
	jmp	.L12
.L11:
	addq	$88, %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
.LFE2033:
	.size	_Z11calc_matrixPfS_S_iiii, .-_Z11calc_matrixPfS_S_iiii
	.section	.rodata.str1.1
.LC6:
	.string	"w"
.LC7:
	.string	"Error opening output file.\n"
.LC8:
	.string	"%.2f "
.LC9:
	.string	"\n"
	.text
	.globl	_Z13output_matrixPKcPfii
	.type	_Z13output_matrixPKcPfii, @function
_Z13output_matrixPKcPfii:
.LFB2034:
	.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
	movq	%rsi, %r13
	leaq	.LC6(%rip), %rsi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%ecx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%edx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	call	fopen@PLT
	movq	%rax, 8(%rsp)
	testq	%rax, %rax
	je	.L22
	movslq	%r12d, %r15
	xorl	%r14d, %r14d
	xorl	%ebx, %ebx
	jmp	.L23
.L22:
	movq	stderr(%rip), %rdi
	leaq	.LC7(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L29:
	leal	-1(%rbp), %eax
	cmpl	%ebx, %eax
	jle	.L25
	movq	8(%rsp), %rdi
	leaq	.LC9(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L25:
	incl	%ebx
	addq	%r15, %r14
.L23:
	cmpl	%ebp, %ebx
	jge	.L26
	leaq	0(%r13,%r14,4), %r8
	xorl	%ecx, %ecx
.L27:
	cmpl	%ecx, %r12d
	jle	.L29
	movq	8(%rsp), %rdi
	leaq	.LC8(%rip), %rdx
	movl	$2, %esi
	movb	$1, %al
	cvtss2sd	(%r8,%rcx,4), %xmm0
	movq	%r8, 24(%rsp)
	movq	%rcx, 16(%rsp)
	call	__fprintf_chk@PLT
	movq	16(%rsp), %rcx
	movq	24(%rsp), %r8
	incq	%rcx
	jmp	.L27
.L26:
	movq	8(%rsp), %rdi
	leaq	.LC9(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	8(%rsp), %rdi
	addq	$40, %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
.LFE2034:
	.size	_Z13output_matrixPKcPfii, .-_Z13output_matrixPKcPfii
	.globl	_Z12print_matrixPfii
	.type	_Z12print_matrixPfii, @function
_Z12print_matrixPfii:
.LFB2035:
	.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
	xorl	%r14d, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdi, %r13
	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
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
.L31:
	cmpl	%ebp, %ebx
	jge	.L34
	movslq	%r14d, %rax
	xorl	%r15d, %r15d
	leaq	0(%r13,%rax,4), %rcx
.L35:
	cmpl	%r15d, %r12d
	jle	.L37
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	(%rcx,%r15,4), %xmm0
	movq	%rcx, 8(%rsp)
	incq	%r15
	call	__printf_chk@PLT
	movq	8(%rsp), %rcx
	jmp	.L35
.L37:
	leal	-1(%rbp), %eax
	cmpl	%ebx, %eax
	jle	.L33
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L33:
	incl	%ebx
	addl	%r12d, %r14d
	jmp	.L31
.L34:
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	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	__printf_chk@PLT
	.cfi_endproc
.LFE2035:
	.size	_Z12print_matrixPfii, .-_Z12print_matrixPfii
	.section	.rodata.str1.1
.LC10:
	.string	"malloc issue"
.LC11:
	.string	" "
	.text
	.globl	_Z11read_matrixPmS_Pc
	.type	_Z11read_matrixPmS_Pc, @function
_Z11read_matrixPmS_Pc:
.LFB2036:
	.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	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rdi, %rbx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	xorl	%eax, %eax
	movq	%rax, (%rsi)
	movq	%rax, (%rdi)
	movq	%rdx, %rax
.L39:
	movb	(%rax), %dl
	cmpb	$10, %dl
	je	.L54
	cmpb	$46, %dl
	jne	.L40
	incq	0(%rbp)
.L40:
	incq	%rax
	jmp	.L39
.L54:
	movq	%r12, %rdi
	call	strlen@PLT
	xorl	%edx, %edx
	cltq
.L42:
	movq	(%rbx), %rcx
	leaq	1(%rcx), %rdi
	cmpq	%rax, %rdx
	jnb	.L44
	movb	(%r12,%rdx), %cl
	testb	%cl, %cl
	je	.L44
	incq	%rdx
	cmpb	$10, %cl
	jne	.L42
	cmpb	$0, (%r12,%rdx)
	je	.L42
	movq	%rdi, (%rbx)
	jmp	.L42
.L44:
	movq	%rdi, (%rbx)
	imulq	0(%rbp), %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rax, %r13
	testq	%rax, %rax
	jne	.L46
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L46:
	movq	%r12, %rdi
	leaq	.LC11(%rip), %rsi
	xorl	%r14d, %r14d
	xorl	%r12d, %r12d
	call	strtok@PLT
	movq	%rsp, %r15
.L47:
	testq	%rax, %rax
	je	.L38
	movq	%r15, %rsi
	movq	%rax, %rdi
	call	strtod@PLT
	movq	0(%rbp), %rcx
	movslq	%r12d, %rax
	movslq	%r14d, %rsi
	incl	%r14d
	cvtsd2ss	%xmm0, %xmm0
	imulq	%rcx, %rax
	addq	%rsi, %rax
	movss	%xmm0, 0(%r13,%rax,4)
	movslq	%r14d, %rax
	cmpq	%rax, %rcx
	jne	.L48
	incl	%r12d
	movslq	%r12d, %rax
	cmpq	(%rbx), %rax
	je	.L38
	xorl	%r14d, %r14d
.L48:
	leaq	.LC11(%rip), %rsi
	xorl	%edi, %edi
	call	strtok@PLT
	jmp	.L47
.L38:
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L51
	call	__stack_chk_fail@PLT
.L51:
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	movq	%r13, %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
.LFE2036:
	.size	_Z11read_matrixPmS_Pc, .-_Z11read_matrixPmS_Pc
	.globl	_Z14copyMiniMatrixPfPA32_fm
	.type	_Z14copyMiniMatrixPfPA32_fm, @function
_Z14copyMiniMatrixPfPA32_fm:
.LFB2037:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2037:
	.size	_Z14copyMiniMatrixPfPA32_fm, .-_Z14copyMiniMatrixPfPA32_fm
	.globl	_Z34__device_stub__Z8MMKernelPfS_S_mmmPfS_S_mmm
	.type	_Z34__device_stub__Z8MMKernelPfS_S_mmmPfS_S_mmm, @function
_Z34__device_stub__Z8MMKernelPfS_S_mmmPfS_S_mmm:
.LFB2063:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	84(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	64(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%r9, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	movq	%rsp, %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L57
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z8MMKernelPfS_S_mmm(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 208
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L57:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L59
	call	__stack_chk_fail@PLT
.L59:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2063:
	.size	_Z34__device_stub__Z8MMKernelPfS_S_mmmPfS_S_mmm, .-_Z34__device_stub__Z8MMKernelPfS_S_mmmPfS_S_mmm
	.globl	_Z8MMKernelPfS_S_mmm
	.type	_Z8MMKernelPfS_S_mmm, @function
_Z8MMKernelPfS_S_mmm:
.LFB2064:
	.cfi_startproc
	endbr64
	jmp	_Z34__device_stub__Z8MMKernelPfS_S_mmmPfS_S_mmm
	.cfi_endproc
.LFE2064:
	.size	_Z8MMKernelPfS_S_mmm, .-_Z8MMKernelPfS_S_mmm
	.section	.rodata.str1.1
.LC12:
	.string	"Usage: [Matrix A] [Matrix B]\n"
.LC13:
	.string	"Matrices are not a compatible size to be multiplied, %dx%d and %dx%d\n"
.LC14:
	.string	"result.out"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2038:
	.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	$136, %rsp
	.cfi_def_cfa_offset 192
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	cmpl	$3, %edi
	je	.L63
	movq	stderr(%rip), %rdi
	leaq	.LC12(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L63:
	movq	8(%rsi), %rdi
	movq	%rsi, %rbx
	call	_Z9read_filePKc
	movq	16(%rbx), %rdi
	movq	%rax, 8(%rsp)
	call	_Z9read_filePKc
	movq	8(%rsp), %rdx
	leaq	72(%rsp), %rsi
	leaq	64(%rsp), %rdi
	movq	%rax, 16(%rsp)
	call	_Z11read_matrixPmS_Pc
	movq	16(%rsp), %rdx
	leaq	88(%rsp), %rsi
	leaq	80(%rsp), %rdi
	movq	%rax, %r14
	call	_Z11read_matrixPmS_Pc
	movq	72(%rsp), %r13
	movq	80(%rsp), %r15
	movq	%rax, 24(%rsp)
	movq	64(%rsp), %rbx
	cmpq	%r15, %r13
	je	.L64
	pushq	%rax
	.cfi_remember_state
	.cfi_def_cfa_offset 200
	movq	stderr(%rip), %rdi
	movq	%r15, %r9
	movq	%r13, %r8
	pushq	96(%rsp)
	.cfi_def_cfa_offset 208
	movq	%rbx, %rcx
	leaq	.LC13(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L64:
	.cfi_restore_state
	movq	88(%rsp), %rbp
	movq	%rbx, %rdi
	imulq	%rbp, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rax, %r12
	testq	%rax, %rax
	jne	.L65
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L65:
	imull	%ebx, %r13d
	leaq	40(%rsp), %rdi
	sall	$2, %r13d
	movslq	%r13d, %r13
	movq	%r13, %rsi
	call	cudaMalloc@PLT
	movl	$297, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movq	%r13, %rdx
	movl	%ebp, %r13d
	movq	40(%rsp), %rdi
	imull	%r15d, %r13d
	movl	$1, %ecx
	movq	%r14, %rsi
	call	cudaMemcpy@PLT
	movl	$298, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	sall	$2, %r13d
	leaq	48(%rsp), %rdi
	movslq	%r13d, %r13
	movq	%r13, %rsi
	call	cudaMalloc@PLT
	movl	$301, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movq	%r13, %rdx
	movl	%ebp, %r13d
	movq	24(%rsp), %rsi
	imull	%ebx, %r13d
	movq	48(%rsp), %rdi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$302, %esi
	sall	$2, %r13d
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movslq	%r13d, %r13
	leaq	56(%rsp), %rdi
	movq	%r13, %rsi
	call	cudaMalloc@PLT
	movl	$305, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	leaq	31(%rbp), %rax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	shrq	$5, %rax
	movl	$1, %ecx
	movl	$1, %esi
	movabsq	$137438953504, %rdx
	movl	%eax, 96(%rsp)
	leaq	31(%rbx), %rax
	shrq	$5, %rax
	movl	%eax, 100(%rsp)
	movq	96(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L66
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	movq	%rbp, %r9
	movq	%rbx, %r8
	movq	40(%rsp), %rdi
	movq	%r15, %rcx
	call	_Z34__device_stub__Z8MMKernelPfS_S_mmmPfS_S_mmm
.L66:
	movq	56(%rsp), %rsi
	movl	$2, %ecx
	movq	%r13, %rdx
	movq	%r12, %rdi
	call	cudaMemcpy@PLT
	movl	$315, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movl	%ebp, %ecx
	movl	%ebx, %edx
	movq	%r12, %rsi
	leaq	.LC14(%rip), %rdi
	call	_Z13output_matrixPKcPfii
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	%r14, %rdi
	call	free@PLT
	movq	24(%rsp), %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	8(%rsp), %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	free@PLT
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L67
	call	__stack_chk_fail@PLT
.L67:
	addq	$136, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	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
.LFE2038:
	.size	main, .-main
	.section	.rodata.str1.1
.LC15:
	.string	"_Z8MMKernelPfS_S_mmm"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2066:
	.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	.LC15(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z8MMKernelPfS_S_mmm(%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
.LFE2066:
	.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:

/*
 * Russell Taylor(rtaylor)
 * Matt Crusse(macrusse)
 * CPE458-01 Lab 1 Winter 2013 
 */


#include <hip/hip_runtime.h>
#include <sys/stat.h>
#include <sys/mman.h> 
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdint.h>

#define TILE_SIZE 32

/* Compile-Time Declaration on double or float usage */
#ifdef DOUBLE
#define TYPEUSE double
#else
#define TYPEUSE float
#endif

/* 
 * Handles CUDA errors, taking from provided sample code on clupo site
 */
static void HandleError( hipError_t err, const char * file, int line)
{
  if(err !=hipSuccess){
    printf("%s in %s at line %d\n", hipGetErrorString(err), file, line);
    exit(EXIT_FAILURE);
  }
}
#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))


/*
 * Reads Input File and Returns Buffer of Contents
 */
char* read_file(const char * file_name) 
{
  size_t size;
  char *buffer;
  FILE *fp;
  
  fp = fopen(file_name,"r");
  if(!fp) {
    fprintf (stderr, "Error opening input file.\n");
    exit (EXIT_FAILURE);    
  }

  fseek (fp, 0, SEEK_END);
  size = ftell(fp);
  rewind (fp);
  
  buffer = (char*) malloc (sizeof(char)*size);
  fread (buffer, 1, size, fp);
  fclose(fp);
  return buffer;
}

/* 
 * Calculate the Resultant Matrix from Multiplication
 */
void calc_matrix(TYPEUSE *A, TYPEUSE *B, TYPEUSE *C, int Arow, int Acol, int Brow, int Bcol)
{
  uint64_t i, j, k;
  TYPEUSE sum = 0;
  for(i = 0; i < Arow; i++)//Iterate through Matrix B columnwise
  {
    for(j = 0; j < Bcol; j++)//Iterate through Matrix A rowwise
    {
        for(k = 0; k < Acol; k++)//Acol = Brow on valid Matrices
        {
          if(i >475)
            printf("");
          sum+= A[ i* (Acol) + k] * B[k * (Bcol) + j];
          
        }
        C[i *Acol + j] = sum;
        sum = 0;
    }
  }
}

/* 
 * Print matrix values to a file outputfile 
 */
void output_matrix(const char * outputfile, TYPEUSE *matrix, int row, int col) 
{
  int i, j;

  FILE *ofp = fopen(outputfile, "w");
  if(!ofp){
    fprintf (stderr, "Error opening output file.\n");
    exit (EXIT_FAILURE);    
  }

  for(i = 0; i < row; i++) {
    for(j = 0; j < col; j++) {
      fprintf(ofp, "%.2f ",matrix[i*uint64_t(col) + j]);
    }  
    if(i < row-1){
      fprintf(ofp, "\n");
    }
  }
  fprintf(ofp, "\n");
  fclose(ofp);
}


/*
 * Simply prints out the matrix to screen 
 */
void print_matrix(TYPEUSE *matrix, int row, int col) 
{
  int i, j;
  for(i = 0; i < row; i++) {
    for(j = 0; j < col; j++) {
      //printf("(%d,%d)", i, j);
      printf("%.2f ",matrix[i*col +j]);
    }  
    if(i < row-1){
      printf("\n");
    }
  }
  printf("\n");

}

/*
 * Created a Matrix based on Buffered Input Information
 */
TYPEUSE * read_matrix(uint64_t * rowCnt, uint64_t * colCnt, char * mapped)
{
  TYPEUSE value;  
  const char *delim_space = " ";
  char *token = NULL;  
  char *unconverted;
  int i, j, len;
  TYPEUSE *matrix;
  uint64_t bigiter;
  *colCnt = 0;
  *rowCnt = 0;

  
  /* Determine Col Count */
  i = 0;
  while(mapped[i] != '\n'){
    if(mapped[i] == '.') {
     (*colCnt)++;
    }
    i++;
  }  

  /* Determine Row Count */
  bigiter = 0;//For large file sizes, an int is too small to iterate through
  len = strlen(mapped);
  while(bigiter < len && mapped[bigiter] != '\0'){
    if((mapped[bigiter] == '\n') && (mapped[bigiter+1] != '\0') ) {
     (*rowCnt)++;
    }
    bigiter+=1;
  }
  (*rowCnt)++;

  /* Malloc the Matrix */
  if (( matrix = (TYPEUSE *) malloc((*rowCnt) * (*colCnt) * sizeof(TYPEUSE))) == NULL ) {
    printf("malloc issue");
  }
    
  /* Read values into matrix */
  i = 0; j = 0;
  for (token = strtok(mapped, delim_space); token != NULL; token = strtok(NULL, delim_space)) {
    value = strtod(token, &unconverted);
    matrix[i*(*colCnt) +j] = value;
    j++;
    if(j == (*colCnt)) {
      j = 0;
      if(++i == (*rowCnt))
	      break;
    }
  }
  return matrix;

}

/*
 * Simply copies device matrix to shared memory 
 */
__device__ void copyMiniMatrix(TYPEUSE * M_device, TYPEUSE M_shared[TILE_SIZE][TILE_SIZE], uint64_t devOffset)
{
  if(threadIdx.y < TILE_SIZE && threadIdx.x < TILE_SIZE) {
    M_shared[threadIdx.y][threadIdx.x] = M_device[devOffset];  
  }
}


/*
 * Kernel called from main.
 */
__global__ void MMKernel(TYPEUSE *A_d, TYPEUSE *B_d, TYPEUSE * C_d, uint64_t depth, uint64_t Arow, uint64_t Bcol)
{
  TYPEUSE Cvalue = 0.0;
  __shared__ TYPEUSE A_shared[TILE_SIZE][TILE_SIZE], B_shared[TILE_SIZE][TILE_SIZE]; 
  int resultWidth = Bcol;
  int resultCol = blockIdx.x * blockDim.x + threadIdx.x;
  int resultRow = blockIdx.y * blockDim.y + threadIdx.y;  
  int resultIndex = resultRow * resultWidth + resultCol;
  uint64_t threadReadId;

  int validCalcThread = resultRow < Arow && resultCol < Bcol;

  for(int i = 0; i < (depth+TILE_SIZE-1)/TILE_SIZE; i++) {

    /* Copy device matrix A into shared memory */
    threadReadId = threadIdx.x + i * TILE_SIZE;
    if(threadReadId < depth){
      copyMiniMatrix(A_d, A_shared, resultRow*depth + threadReadId);
    }

    /* Copy device matrix B into shared memory */    
    threadReadId = threadIdx.y + i * TILE_SIZE;
    if(threadReadId < depth) {
      copyMiniMatrix(B_d, B_shared, threadReadId * Bcol + resultCol);
    }
    
    /* Wait for all threads to complete copy to shared memory */
    __syncthreads();
    
    
    /* Boundary Check*/
    int remaining = depth - (i * TILE_SIZE);
    int maxIter = (remaining <= TILE_SIZE) * (remaining) + 
                  (remaining > TILE_SIZE) * (TILE_SIZE);

    if(validCalcThread) {
      for(int k = 0; k < maxIter; k++) {
        TYPEUSE Aelem = A_shared[threadIdx.y][k];
        TYPEUSE Belem = B_shared[k][threadIdx.x];
        Cvalue += Aelem * Belem;
      }
    }

    /* Wait for all threads to finish */
    __syncthreads();
  }

  if(validCalcThread) {
    C_d[resultIndex] = Cvalue;
  }
  
}

int main (int argc, const char * argv[])
{
  const char * Cfile = "result.out";
  TYPEUSE * Amatrix, * Bmatrix, * Cmatrix;
  TYPEUSE * A_d, * B_d, * C_d;
  uint64_t Arow, Acol, Brow, Bcol;
  int size;
  int blockRow, blockCol;
  char * Amapped, * Bmapped;

  if(argc != 3) { 
    fprintf(stderr, "Usage: [Matrix A] [Matrix B]\n");
    exit(EXIT_FAILURE);
  }

  /* Device Properties */
  /*
  cudaDeviceProp prop;
  cudaGetDeviceProperties(&prop,0);
  printf("maxThreads: %d\n", prop.maxThreadsPerBlock);
  */

  /* Read and Map matrix */
  Amapped = read_file(argv[1]);
  Bmapped = read_file(argv[2]);
  Amatrix = read_matrix(&Arow, &Acol, Amapped); 
  Bmatrix = read_matrix(&Brow, &Bcol, Bmapped);
  
  if(Acol != Brow) {
    fprintf(stderr, "Matrices are not a compatible size to be multiplied, %dx%d and %dx%d\n", Arow, Acol, Brow, Bcol);
    exit(EXIT_FAILURE);
  }
  
  /* Malloc a New Matrix */
  if (( Cmatrix = (TYPEUSE *) malloc((Arow) * (Bcol) * sizeof(TYPEUSE))) == NULL ) {
    printf("malloc issue");
  }
  /* Malloc and Copy space on GPU */
  size = Arow * Acol * sizeof(TYPEUSE);
  HANDLE_ERROR(hipMalloc(&A_d, size));
  HANDLE_ERROR(hipMemcpy(A_d, Amatrix, size, hipMemcpyHostToDevice));
  
  size = Brow * Bcol * sizeof(TYPEUSE);
  HANDLE_ERROR(hipMalloc(&B_d, size));
  HANDLE_ERROR(hipMemcpy(B_d, Bmatrix, size, hipMemcpyHostToDevice));
  
  size = Arow * Bcol * sizeof(TYPEUSE);
  HANDLE_ERROR(hipMalloc(&C_d, size));
        
  /* Kernel Setup */  
  blockRow = (Arow+31) / 32;
  blockCol = (Bcol+31) / 32;
  dim3 dimGrid(blockCol,blockRow);
  dim3 dimBlock(32,32);

  /* Kernel Call */  
  MMKernel<<<dimGrid,dimBlock>>>(A_d, B_d, C_d, Brow, Arow, Bcol);
  HANDLE_ERROR(hipMemcpy(Cmatrix,C_d,size, hipMemcpyDeviceToHost));

  output_matrix(Cfile, Cmatrix, Arow, Bcol);
  
  //print_matrix(Cmatrix, Arow, Bcol);
  
  /* Free Stuff */
  hipFree(A_d);
  hipFree(B_d);
  hipFree(C_d);
  free(Amatrix);
  free(Bmatrix);
  free(Cmatrix);
  free(Amapped);
  free(Bmapped);

  return 0;
}

	.text
	.file	"main.hip"
	.globl	_Z9read_filePKc                 # -- Begin function _Z9read_filePKc
	.type	_Z9read_filePKc,@function
_Z9read_filePKc:                        # @_Z9read_filePKc
	.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
	movl	$.L.str, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB0_2
# %bb.1:
	movq	%rax, %rbx
	movq	%rax, %rdi
	xorl	%esi, %esi
	movl	$2, %edx
	callq	fseek
	movq	%rbx, %rdi
	callq	ftell
	movq	%rax, %r14
	movq	%rbx, %rdi
	callq	rewind
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %r15
	movl	$1, %esi
	movq	%rax, %rdi
	movq	%r14, %rdx
	movq	%rbx, %rcx
	callq	fread
	movq	%rbx, %rdi
	callq	fclose
	movq	%r15, %rax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB0_2:
	.cfi_def_cfa_offset 32
	movq	stderr(%rip), %rcx
	movl	$.L.str.1, %edi
	movl	$26, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z9read_filePKc, .Lfunc_end0-_Z9read_filePKc
	.cfi_endproc
                                        # -- End function
	.globl	_Z11calc_matrixPfS_S_iiii       # -- Begin function _Z11calc_matrixPfS_S_iiii
	.type	_Z11calc_matrixPfS_S_iiii,@function
_Z11calc_matrixPfS_S_iiii:              # @_Z11calc_matrixPfS_S_iiii
	.cfi_startproc
# %bb.0:
	testl	%ecx, %ecx
	je	.LBB1_10
# %bb.1:                                # %.preheader29.lr.ph
	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
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movslq	%ecx, %rcx
	movslq	56(%rsp), %r9
	movslq	%r8d, %r10
	leaq	(,%r9,4), %r11
	leaq	(,%r10,4), %rbx
	xorl	%r14d, %r14d
.LBB1_2:                                # %.preheader29
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
                                        #       Child Loop BB1_6 Depth 3
	cmpl	$0, 56(%rsp)
	je	.LBB1_8
# %bb.3:                                # %.preheader.lr.ph
                                        #   in Loop: Header=BB1_2 Depth=1
	movq	%r14, %rax
	imulq	%r10, %rax
	leaq	(%rdx,%rax,4), %r15
	movq	%rsi, %r12
	xorl	%r13d, %r13d
.LBB1_4:                                # %.preheader
                                        #   Parent Loop BB1_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_6 Depth 3
	xorps	%xmm0, %xmm0
	testl	%r8d, %r8d
	je	.LBB1_7
# %bb.5:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB1_4 Depth=2
	movq	%r12, %rbp
	xorl	%eax, %eax
.LBB1_6:                                # %.lr.ph
                                        #   Parent Loop BB1_2 Depth=1
                                        #     Parent Loop BB1_4 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%rdi,%rax,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	mulss	(%rbp), %xmm1
	addss	%xmm1, %xmm0
	incq	%rax
	addq	%r11, %rbp
	cmpq	%rax, %r10
	jne	.LBB1_6
.LBB1_7:                                # %._crit_edge
                                        #   in Loop: Header=BB1_4 Depth=2
	movss	%xmm0, (%r15,%r13,4)
	incq	%r13
	addq	$4, %r12
	cmpq	%r9, %r13
	jne	.LBB1_4
.LBB1_8:                                # %._crit_edge34
                                        #   in Loop: Header=BB1_2 Depth=1
	incq	%r14
	addq	%rbx, %rdi
	cmpq	%rcx, %r14
	jne	.LBB1_2
# %bb.9:
	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
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r13
	.cfi_restore %r14
	.cfi_restore %r15
	.cfi_restore %rbp
.LBB1_10:                               # %._crit_edge36
	retq
.Lfunc_end1:
	.size	_Z11calc_matrixPfS_S_iiii, .Lfunc_end1-_Z11calc_matrixPfS_S_iiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z13output_matrixPKcPfii        # -- Begin function _Z13output_matrixPKcPfii
	.type	_Z13output_matrixPKcPfii,@function
_Z13output_matrixPKcPfii:               # @_Z13output_matrixPKcPfii
	.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, 4(%rsp)                   # 4-byte Spill
	movl	%edx, %r12d
	movq	%rsi, %r14
	movl	$.L.str.3, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB2_10
# %bb.1:                                # %.preheader22
	movq	%rax, %r15
	testl	%r12d, %r12d
	jle	.LBB2_9
# %bb.2:                                # %.preheader.lr.ph
	movslq	4(%rsp), %r13                   # 4-byte Folded Reload
	leal	-1(%r12), %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	%r12d, %eax
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	movl	%r13d, %ebx
	shlq	$2, %r13
	xorl	%ebp, %ebp
.LBB2_3:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_5 Depth 2
	cmpl	$0, 4(%rsp)                     # 4-byte Folded Reload
	jle	.LBB2_6
# %bb.4:                                # %.lr.ph
                                        #   in Loop: Header=BB2_3 Depth=1
	xorl	%r12d, %r12d
.LBB2_5:                                #   Parent Loop BB2_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r14,%r12,4), %xmm0
	movl	$.L.str.5, %esi
	movq	%r15, %rdi
	movb	$1, %al
	callq	fprintf
	incq	%r12
	cmpq	%r12, %rbx
	jne	.LBB2_5
.LBB2_6:                                # %._crit_edge
                                        #   in Loop: Header=BB2_3 Depth=1
	cmpq	16(%rsp), %rbp                  # 8-byte Folded Reload
	jae	.LBB2_8
# %bb.7:                                #   in Loop: Header=BB2_3 Depth=1
	movl	$10, %edi
	movq	%r15, %rsi
	callq	fputc@PLT
.LBB2_8:                                #   in Loop: Header=BB2_3 Depth=1
	incq	%rbp
	addq	%r13, %r14
	cmpq	8(%rsp), %rbp                   # 8-byte Folded Reload
	jne	.LBB2_3
.LBB2_9:                                # %._crit_edge25
	movl	$10, %edi
	movq	%r15, %rsi
	callq	fputc@PLT
	movq	%r15, %rdi
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.LBB2_10:
	.cfi_def_cfa_offset 80
	movq	stderr(%rip), %rcx
	movl	$.L.str.4, %edi
	movl	$27, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end2:
	.size	_Z13output_matrixPKcPfii, .Lfunc_end2-_Z13output_matrixPKcPfii
	.cfi_endproc
                                        # -- End function
	.globl	_Z12print_matrixPfii            # -- Begin function _Z12print_matrixPfii
	.type	_Z12print_matrixPfii,@function
_Z12print_matrixPfii:                   # @_Z12print_matrixPfii
	.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
                                        # kill: def $esi killed $esi def $rsi
	movq	%rdi, (%rsp)                    # 8-byte Spill
	testl	%esi, %esi
	jle	.LBB3_8
# %bb.1:                                # %.preheader.lr.ph
	movl	%edx, %ebx
	leal	-1(%rsi), %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	%esi, %eax
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	movl	%edx, %r13d
	xorl	%ebp, %ebp
	xorl	%r14d, %r14d
.LBB3_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_4 Depth 2
	testl	%ebx, %ebx
	jle	.LBB3_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB3_2 Depth=1
	movl	%ebp, %eax
	movq	(%rsp), %rcx                    # 8-byte Reload
	leaq	(%rcx,%rax,4), %r15
	xorl	%r12d, %r12d
.LBB3_4:                                #   Parent Loop BB3_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r15,%r12,4), %xmm0
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	incq	%r12
	cmpq	%r12, %r13
	jne	.LBB3_4
.LBB3_5:                                # %._crit_edge
                                        #   in Loop: Header=BB3_2 Depth=1
	cmpq	16(%rsp), %r14                  # 8-byte Folded Reload
	jae	.LBB3_7
# %bb.6:                                #   in Loop: Header=BB3_2 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
.LBB3_7:                                #   in Loop: Header=BB3_2 Depth=1
	incq	%r14
	addl	%ebx, %ebp
	cmpq	8(%rsp), %r14                   # 8-byte Folded Reload
	jne	.LBB3_2
.LBB3_8:                                # %._crit_edge17
	movl	$10, %edi
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	putchar@PLT                     # TAILCALL
.Lfunc_end3:
	.size	_Z12print_matrixPfii, .Lfunc_end3-_Z12print_matrixPfii
	.cfi_endproc
                                        # -- End function
	.globl	_Z11read_matrixPmS_Pc           # -- Begin function _Z11read_matrixPmS_Pc
	.type	_Z11read_matrixPmS_Pc,@function
_Z11read_matrixPmS_Pc:                  # @_Z11read_matrixPmS_Pc
	.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	%rdx, %r15
	movq	%rsi, %rbx
	movq	%rdi, %r14
	xorl	%eax, %eax
	movq	%rax, (%rsi)
	movq	%rax, (%rdi)
	movq	%rdx, %rax
.LBB4_1:                                # =>This Inner Loop Header: Depth=1
	movzbl	(%rax), %ecx
	cmpl	$46, %ecx
	je	.LBB4_7
# %bb.2:                                #   in Loop: Header=BB4_1 Depth=1
	cmpl	$10, %ecx
	jne	.LBB4_8
	jmp	.LBB4_3
.LBB4_7:                                #   in Loop: Header=BB4_1 Depth=1
	incq	(%rbx)
.LBB4_8:                                #   in Loop: Header=BB4_1 Depth=1
	incq	%rax
	jmp	.LBB4_1
.LBB4_3:
	movq	%r15, %rdi
	callq	strlen
	shlq	$32, %rax
	je	.LBB4_12
# %bb.4:                                # %.lr.ph.preheader
	sarq	$32, %rax
	cmpq	$1, %rax
	adcq	$0, %rax
	xorl	%ecx, %ecx
.LBB4_5:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movzbl	(%r15,%rcx), %edx
	cmpl	$10, %edx
	je	.LBB4_9
# %bb.6:                                # %.lr.ph
                                        #   in Loop: Header=BB4_5 Depth=1
	testl	%edx, %edx
	jne	.LBB4_11
	jmp	.LBB4_12
.LBB4_9:                                #   in Loop: Header=BB4_5 Depth=1
	cmpb	$0, 1(%r15,%rcx)
	je	.LBB4_11
# %bb.10:                               #   in Loop: Header=BB4_5 Depth=1
	incq	(%r14)
.LBB4_11:                               #   in Loop: Header=BB4_5 Depth=1
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB4_5
.LBB4_12:                               # %.critedge
	movq	(%r14), %rdi
	incq	%rdi
	movq	%rdi, (%r14)
	imulq	(%rbx), %rdi
	shlq	$2, %rdi
	callq	malloc
	movq	%rax, %r12
	testq	%rax, %rax
	jne	.LBB4_14
# %bb.13:
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
.LBB4_14:
	movl	$.L.str.7, %esi
	movq	%r15, %rdi
	callq	strtok
	testq	%rax, %rax
	je	.LBB4_19
# %bb.15:                               # %.lr.ph52.preheader
	xorl	%r13d, %r13d
	movq	%rsp, %r15
	xorl	%ebp, %ebp
.LBB4_16:                               # %.lr.ph52
                                        # =>This Inner Loop Header: Depth=1
	movq	%rax, %rdi
	movq	%r15, %rsi
	callq	strtod
	cvtsd2ss	%xmm0, %xmm0
	movslq	%r13d, %rax
	movq	(%rbx), %rcx
	imulq	%rcx, %rax
	movslq	%ebp, %rbp
	leaq	(%r12,%rax,4), %rax
	movss	%xmm0, (%rax,%rbp,4)
	incl	%ebp
	movslq	%ebp, %rax
	cmpq	%rax, %rcx
	jne	.LBB4_18
# %bb.17:                               #   in Loop: Header=BB4_16 Depth=1
	incl	%r13d
	movslq	%r13d, %rax
	xorl	%ebp, %ebp
	cmpq	%rax, (%r14)
	je	.LBB4_19
.LBB4_18:                               #   in Loop: Header=BB4_16 Depth=1
	movl	$.L.str.7, %esi
	xorl	%edi, %edi
	callq	strtok
	testq	%rax, %rax
	jne	.LBB4_16
.LBB4_19:                               # %._crit_edge
	movq	%r12, %rax
	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_end4:
	.size	_Z11read_matrixPmS_Pc, .Lfunc_end4-_Z11read_matrixPmS_Pc
	.cfi_endproc
                                        # -- End function
	.globl	_Z23__device_stub__MMKernelPfS_S_mmm # -- Begin function _Z23__device_stub__MMKernelPfS_S_mmm
	.type	_Z23__device_stub__MMKernelPfS_S_mmm,@function
_Z23__device_stub__MMKernelPfS_S_mmm:   # @_Z23__device_stub__MMKernelPfS_S_mmm
	.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	$144, %rsp
	.cfi_def_cfa_offset 192
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	24(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	16(%rsp), %r8
	movq	%r9, (%r8)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%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	$_Z8MMKernelPfS_S_mmm, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$160, %rsp
	.cfi_adjust_cfa_offset -160
	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_end5:
	.size	_Z23__device_stub__MMKernelPfS_S_mmm, .Lfunc_end5-_Z23__device_stub__MMKernelPfS_S_mmm
	.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
	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	$104, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$3, %edi
	jne	.LBB6_7
# %bb.1:
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	callq	_Z9read_filePKc
	movq	%rax, %r14
	movq	16(%rbx), %rdi
	callq	_Z9read_filePKc
	movq	%rax, %r12
	leaq	24(%rsp), %rdi
	leaq	96(%rsp), %rbx
	movq	%rbx, %rsi
	movq	%r14, %rdx
	callq	_Z11read_matrixPmS_Pc
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	leaq	88(%rsp), %r15
	leaq	16(%rsp), %rsi
	movq	%r15, %rdi
	movq	%r12, %rdx
	callq	_Z11read_matrixPmS_Pc
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	movq	(%rbx), %r13
	movq	(%r15), %r8
	cmpq	%r8, %r13
	jne	.LBB6_9
# %bb.2:
	movq	%r12, 64(%rsp)                  # 8-byte Spill
	movq	%r14, 72(%rsp)                  # 8-byte Spill
	movq	24(%rsp), %rbp
	movq	16(%rsp), %r14
	movq	%r14, %rbx
	imulq	%rbp, %rbx
	leaq	(,%rbx,4), %rdi
	callq	malloc
	movq	%rax, 56(%rsp)                  # 8-byte Spill
	testq	%rax, %rax
	jne	.LBB6_4
# %bb.3:
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
.LBB6_4:
	movl	%ebp, %eax
	imull	%r13d, %eax
	shll	$2, %eax
	movslq	%eax, %r15
	leaq	40(%rsp), %r12
	movq	%r12, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$299, %esi                      # imm = 0x12B
	callq	_ZL11HandleError10hipError_tPKci
	movq	(%r12), %rdi
	movq	8(%rsp), %rsi                   # 8-byte Reload
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$300, %esi                      # imm = 0x12C
	callq	_ZL11HandleError10hipError_tPKci
	movl	%r14d, %eax
	movq	%r13, 48(%rsp)                  # 8-byte Spill
	imull	%r13d, %eax
	shll	$2, %eax
	movslq	%eax, %r15
	leaq	32(%rsp), %r12
	movq	%r12, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$303, %esi                      # imm = 0x12F
	callq	_ZL11HandleError10hipError_tPKci
	movq	(%r12), %rdi
	movq	80(%rsp), %r12                  # 8-byte Reload
	movq	%r12, %rsi
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$304, %esi                      # imm = 0x130
	callq	_ZL11HandleError10hipError_tPKci
	shll	$2, %ebx
	movslq	%ebx, %r15
	movq	%rsp, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$307, %esi                      # imm = 0x133
	callq	_ZL11HandleError10hipError_tPKci
	leaq	31(%r14), %rax
	shrq	$5, %rax
	movl	%eax, %eax
	movq	%rbp, %rcx
	shlq	$27, %rcx
	movl	$4160749568, %edx               # imm = 0xF8000000
	addq	%rcx, %rdx
	movabsq	$-4294967296, %rdi              # imm = 0xFFFFFFFF00000000
	andq	%rdx, %rdi
	orq	%rax, %rdi
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB6_6
# %bb.5:
	movq	40(%rsp), %rdi
	movq	32(%rsp), %rsi
	movq	(%rsp), %rdx
	movq	48(%rsp), %rcx                  # 8-byte Reload
	movq	%rbp, %r8
	movq	%r14, %r9
	callq	_Z23__device_stub__MMKernelPfS_S_mmm
.LBB6_6:
	movq	(%rsp), %rsi
	movq	56(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rdi
	movq	%r15, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$317, %esi                      # imm = 0x13D
	callq	_ZL11HandleError10hipError_tPKci
	movl	$.L.str.9, %edi
	movq	%rbx, %rsi
	movl	%ebp, %edx
	movl	%r14d, %ecx
	callq	_Z13output_matrixPKcPfii
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi                   # 8-byte Reload
	callq	free
	movq	%r12, %rdi
	callq	free
	movq	%rbx, %rdi
	callq	free
	movq	72(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	64(%rsp), %rdi                  # 8-byte Reload
	callq	free
	xorl	%eax, %eax
	addq	$104, %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
.LBB6_7:
	.cfi_def_cfa_offset 160
	movq	stderr(%rip), %rcx
	movl	$.L.str.10, %edi
	movl	$29, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	jmp	.LBB6_8
.LBB6_9:
	movq	stderr(%rip), %rdi
	movq	24(%rsp), %rdx
	movq	16(%rsp), %r9
	movl	$.L.str.11, %esi
	movq	%r13, %rcx
	xorl	%eax, %eax
	callq	fprintf
.LBB6_8:
	movl	$1, %edi
	callq	exit
.Lfunc_end6:
	.size	main, .Lfunc_end6-main
	.cfi_endproc
                                        # -- End function
	.type	_ZL11HandleError10hipError_tPKci,@function # -- Begin function _ZL11HandleError10hipError_tPKci
_ZL11HandleError10hipError_tPKci:       # @_ZL11HandleError10hipError_tPKci
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jne	.LBB7_2
# %bb.1:
	retq
.LBB7_2:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%esi, %ebx
	callq	hipGetErrorString
	movl	$.L.str.13, %edi
	movl	$.L.str.12, %edx
	movq	%rax, %rsi
	movl	%ebx, %ecx
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end7:
	.size	_ZL11HandleError10hipError_tPKci, .Lfunc_end7-_ZL11HandleError10hipError_tPKci
	.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	.LBB8_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB8_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z8MMKernelPfS_S_mmm, %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_end8:
	.size	__hip_module_ctor, .Lfunc_end8-__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	.LBB9_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
.LBB9_2:
	retq
.Lfunc_end9:
	.size	__hip_module_dtor, .Lfunc_end9-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"r"
	.size	.L.str, 2

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Error opening input file.\n"
	.size	.L.str.1, 27

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Error opening output file.\n"
	.size	.L.str.4, 28

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%.2f "
	.size	.L.str.5, 6

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

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"malloc issue"
	.size	.L.str.8, 13

	.type	_Z8MMKernelPfS_S_mmm,@object    # @_Z8MMKernelPfS_S_mmm
	.section	.rodata,"a",@progbits
	.globl	_Z8MMKernelPfS_S_mmm
	.p2align	3, 0x0
_Z8MMKernelPfS_S_mmm:
	.quad	_Z23__device_stub__MMKernelPfS_S_mmm
	.size	_Z8MMKernelPfS_S_mmm, 8

	.type	.L.str.9,@object                # @.str.9
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.9:
	.asciz	"result.out"
	.size	.L.str.9, 11

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Usage: [Matrix A] [Matrix B]\n"
	.size	.L.str.10, 30

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"Matrices are not a compatible size to be multiplied, %dx%d and %dx%d\n"
	.size	.L.str.11, 70

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/russelltaylor05/458-lab1c/master/main.hip"
	.size	.L.str.12, 104

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"%s in %s at line %d\n"
	.size	.L.str.13, 21

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z8MMKernelPfS_S_mmm"
	.size	.L__unnamed_1, 21

	.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 _Z23__device_stub__MMKernelPfS_S_mmm
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z8MMKernelPfS_S_mmm
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z8MMKernelPfS_S_mmm    ; -- Begin function _Z8MMKernelPfS_S_mmm
	.globl	_Z8MMKernelPfS_S_mmm
	.p2align	8
	.type	_Z8MMKernelPfS_S_mmm,@function
_Z8MMKernelPfS_S_mmm:                   ; @_Z8MMKernelPfS_S_mmm
; %bb.0:
	s_clause 0x2
	s_load_b32 s2, s[0:1], 0x3c
	s_load_b128 s[16:19], s[0:1], 0x20
	s_load_b256 s[4:11], s[0:1], 0x0
	v_bfe_u32 v4, v0, 10, 10
	v_dual_mov_b32 v8, 0 :: v_dual_and_b32 v5, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s0, s2, 16
	s_and_b32 s1, s2, 0xffff
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mad_u64_u32 v[0:1], null, s15, s0, v[4:5]
	v_mad_u64_u32 v[2:3], null, s14, s1, v[5:6]
	v_ashrrev_i32_e32 v1, 31, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_ashrrev_i32_e32 v3, 31, v2
	v_cmp_gt_u64_e32 vcc_lo, s[16:17], v[0:1]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_gt_u64_e64 s0, s[18:19], v[2:3]
	s_and_b32 s1, s0, vcc_lo
	s_add_u32 s12, s10, 31
	s_addc_u32 s13, s11, 0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_lt_u64_e64 s0, s[12:13], 32
	s_and_b32 vcc_lo, exec_lo, s0
	s_cbranch_vccnz .LBB0_10
; %bb.1:                                ; %.lr.ph59
	v_mul_lo_u32 v8, v1, s10
	v_mul_lo_u32 v9, v0, s11
	v_mad_u64_u32 v[6:7], null, v0, s10, 0
	v_lshlrev_b64 v[10:11], 2, v[2:3]
	v_lshlrev_b32_e32 v12, 2, v5
	v_lshlrev_b32_e32 v1, 7, v4
	s_mov_b32 s3, 0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_4)
	s_mov_b32 s2, s3
	v_add3_u32 v7, v7, v9, v8
	v_or_b32_e32 v8, v5, v4
	v_or_b32_e32 v9, 0x1000, v12
	v_add_co_u32 v10, vcc_lo, s6, v10
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_3) | instid1(VALU_DEP_4)
	v_lshlrev_b64 v[6:7], 2, v[6:7]
	v_add_co_ci_u32_e32 v11, vcc_lo, s7, v11, vcc_lo
	v_cmp_gt_u32_e64 s0, 32, v8
	v_dual_mov_b32 v8, 0 :: v_dual_add_nc_u32 v3, v1, v12
	v_add_co_u32 v12, vcc_lo, s4, v6
	v_add_co_ci_u32_e32 v13, vcc_lo, s5, v7, vcc_lo
	v_dual_mov_b32 v7, 0 :: v_dual_add_nc_u32 v14, v9, v1
	s_lshr_b64 s[4:5], s[12:13], 5
.LBB0_2:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_8 Depth 2
	s_lshl_b32 s6, s2, 5
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v6, s6, v5
	v_cmp_gt_u64_e32 vcc_lo, s[10:11], v[6:7]
	s_and_b32 s12, s0, vcc_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s7, s12
	s_cbranch_execz .LBB0_4
; %bb.3:                                ;   in Loop: Header=BB0_2 Depth=1
	v_lshlrev_b64 v[15:16], 2, v[6:7]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v15, vcc_lo, v12, v15
	v_add_co_ci_u32_e32 v16, vcc_lo, v13, v16, vcc_lo
	global_load_b32 v6, v[15:16], off
	s_waitcnt vmcnt(0)
	ds_store_b32 v3, v6
.LBB0_4:                                ; %_Z14copyMiniMatrixPfPA32_fm.exit
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s7
	v_add_nc_u32_e32 v6, s6, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_cmp_gt_u64_e32 vcc_lo, s[10:11], v[6:7]
	s_and_b32 s12, s0, vcc_lo
	s_and_saveexec_b32 s7, s12
	s_cbranch_execz .LBB0_6
; %bb.5:                                ;   in Loop: Header=BB0_2 Depth=1
	v_mad_u64_u32 v[15:16], null, v6, s18, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[17:18], null, v6, s19, v[16:17]
	v_mov_b32_e32 v16, v17
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[15:16], 2, v[15:16]
	v_add_co_u32 v15, vcc_lo, v10, v15
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v16, vcc_lo, v11, v16, vcc_lo
	global_load_b32 v6, v[15:16], off
	s_waitcnt vmcnt(0)
	ds_store_b32 v14, v6
.LBB0_6:                                ; %_Z14copyMiniMatrixPfPA32_fm.exit51
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s7
	s_sub_i32 s6, s10, s6
	s_waitcnt lgkmcnt(0)
	s_cmp_lt_i32 s6, 33
	s_barrier
	s_cselect_b32 s7, s6, 0
	s_cmp_gt_i32 s6, 32
	buffer_gl0_inv
	s_cselect_b32 s6, -1, 0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e64 v6, 0, 1, s6
	v_readfirstlane_b32 s6, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b32 s6, s6, 5
	s_add_i32 s6, s7, s6
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	s_cmp_gt_i32 s6, 0
	s_cselect_b32 s7, -1, 0
	s_and_b32 s12, s1, s7
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s7, s12
	s_cbranch_execz .LBB0_9
; %bb.7:                                ; %.lr.ph.preheader
                                        ;   in Loop: Header=BB0_2 Depth=1
	v_mov_b32_e32 v6, v9
	v_mov_b32_e32 v15, v1
.LBB0_8:                                ; %.lr.ph
                                        ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	ds_load_b32 v16, v15
	ds_load_b32 v17, v6
	v_add_nc_u32_e32 v15, 4, v15
	v_add_nc_u32_e32 v6, 0x80, v6
	s_add_i32 s6, s6, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lg_u32 s6, 0
	s_waitcnt lgkmcnt(0)
	v_fmac_f32_e32 v8, v16, v17
	s_cbranch_scc1 .LBB0_8
.LBB0_9:                                ; %Flow86
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s7
	s_add_i32 s2, s2, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	v_cmp_le_u64_e64 s6, s[4:5], s[2:3]
	s_barrier
	buffer_gl0_inv
	s_and_b32 vcc_lo, exec_lo, s6
	s_cbranch_vccz .LBB0_2
.LBB0_10:                               ; %Flow88
	s_and_saveexec_b32 s0, s1
	s_cbranch_execz .LBB0_12
; %bb.11:
	v_mad_u64_u32 v[3:4], null, v0, s18, v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v4, 31, v3
	v_lshlrev_b64 v[0:1], 2, v[3:4]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v0, vcc_lo, s8, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s9, v1, vcc_lo
	global_store_b32 v[0:1], v8, off
.LBB0_12:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z8MMKernelPfS_S_mmm
		.amdhsa_group_segment_fixed_size 8192
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 304
		.amdhsa_user_sgpr_count 14
		.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 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 19
		.amdhsa_next_free_sgpr 20
		.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	_Z8MMKernelPfS_S_mmm, .Lfunc_end0-_Z8MMKernelPfS_S_mmm
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 720
; NumSgprs: 22
; NumVgprs: 19
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 8192 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 22
; NumVGPRsForWavesPerEU: 19
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; 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:           8
        .value_kind:     by_value
      - .offset:         32
        .size:           8
        .value_kind:     by_value
      - .offset:         40
        .size:           8
        .value_kind:     by_value
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         52
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         60
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         62
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         64
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         66
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         68
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         70
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         112
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 8192
    .kernarg_segment_align: 8
    .kernarg_segment_size: 304
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z8MMKernelPfS_S_mmm
    .private_segment_fixed_size: 0
    .sgpr_count:     22
    .sgpr_spill_count: 0
    .symbol:         _Z8MMKernelPfS_S_mmm.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     19
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
