#include <stdio.h>
#include <vector>

__global__ void add_kernel(int *a, int *b, int *c) {
    *c = *a + *b;
}

//template <typename T>
//struct Matrix {
//    int width;
//    int height;
//    std::vector<T> data;
//};
//
//template <>
//struct Matrix {
//    int width;
//    int height;
//    std::vector<float> data;
//};

#define BS 2

typedef struct {
    int width;
	int height;
	int stride;
    float *data;
} Matrix;

// run inner GPU threads
__device__ float GetElement(Matrix mat, int row, int col) {
    return mat.data[row * mat.width + col];
}

__device__ void SetElement(Matrix mat, int row, int col, float val) {
    mat.data[row * mat.stride + col] = val;
}

__device__ Matrix GetSubMatrix(Matrix mat, int row, int col) {
    Matrix subx;
	subx.width = subx.height = BS;
	subx.stride = mat.stride;
	subx.data = &mat.data[row * BS * mat.width + col * BS];
    return subx;
}

// kernel
__global__ void KDot(Matrix A, Matrix B, Matrix C) {
    int block_row = blockIdx.y;
    int block_col = blockIdx.x;
    // in RK(inner register in each thread)
    float res = 0;
    // b * b sub matrix
    Matrix csub = GetSubMatrix(C, block_row, block_col);
    int row = threadIdx.y;
    int col = threadIdx.x;
    for (int k = 0; k < A.width / BS; k++) {
        Matrix asub = GetSubMatrix(A, block_row, k);
        Matrix bsub = GetSubMatrix(B, k, block_col);
        // shm set
        __shared__ float a_shm[BS][BS];
        __shared__ float b_shm[BS][BS];
        // each thread load an element from global memory only once
        a_shm[row][col] = GetElement(asub, row, col);
        b_shm[row][col] = GetElement(bsub, row, col);
		__syncthreads();
		// sync all threads ensure all elements in sub matrix are stored to shm
        // compute this op
        for (int e = 0; e < BS; e++) {
            res += a_shm[row][e] * b_shm[e][col];
        }
		__syncthreads();
    }
    SetElement(csub, row, col, res);
}

// call the kernel
void MatMul(const Matrix A, const Matrix B, Matrix C) {
    dim3 dim_block(BS, BS); // block internal
    dim3 dim_grid(A.height / BS, B.width / BS);
    Matrix d_A, d_B, d_C;
    d_A.width = A.width; d_A.height = A.height; d_A.stride = A.stride;
    // only zcopy 'data' field to GPU memory
    size_t size = d_A.width * d_A.height * sizeof(float);
    cudaError_t err = cudaMalloc(&d_A.data, size);
    if (err) {
        printf("Error while allocate matrix A ==> %s\n", cudaGetErrorString(err));
        exit(-1);
    }
    // pointer to dest, pointer to src, size, type
    cudaMemcpy(d_A.data, A.data, size, cudaMemcpyHostToDevice);

    d_B.width = B.width; d_B.height = B.height; d_B.stride = B.stride;
    // only zcopy 'data' field to GPU memory
    size = d_B.width * d_B.height * sizeof(float);
    err = cudaMalloc(&d_B.data, size);
    if (err) {
        printf("Error while allocate matrix B ==> %s\n", cudaGetErrorString(err));
        exit(-1);
    }
    // pointer to dest, pointer to src, size, type
    cudaMemcpy(d_B.data, B.data, size, cudaMemcpyHostToDevice);

    d_C.width = C.width; d_C.height = C.height; d_C.stride = C.stride;
    // only zcopy 'data' field to GPU memory
    size = d_C.width * d_C.height * sizeof(float);
    err = cudaMalloc(&d_C.data, size);
    if (err) {
        printf("Error while allocate matrix C ==> %s\n", cudaGetErrorString(err));
        exit(-1);
    }
	  // invoke kernel
    KDot<<<dim_grid, dim_block>>>(d_A, d_B, d_C);
    err = cudaThreadSynchronize();
    if (err) {
        printf("Error while launch kernel ==> %s\n", cudaGetErrorString(err));
    }
    // zcopy result to host when computation done
    err = cudaMemcpy(C.data, d_C.data, size, cudaMemcpyDeviceToHost);
    if (err) {
        printf("Error while copy result to host memory ==> %s\n", cudaGetErrorString(err));
    }

    // free all these memory finally
    cudaFree(d_A.data);
    cudaFree(d_B.data);
	cudaFree(d_C.data);
}

int main()
{
	Matrix A, B, C;
	// A[6 * 5] B[5 * 6] C[6 * 6]
	A.width = A.stride = B.height = 4;
	A.height = B.stride = B.width = 6;
	C.width = C.height = C.stride = 6;
	// allocate memory for these matrices
	A.data = (float*)malloc(A.width * A.height * sizeof(float));
	B.data = (float*)malloc(B.width * B.height * sizeof(float));
	C.data = (float*)malloc(C.width * C.height * sizeof(float));
	// initialize A B to I[M * N]
	for (int i = 0; i < A.height; i++) {
		for (int j = 0; j < A.width; j++) {
			A.data[i * A.width + j] = 1;
		}
	}
	for (int i = 0; i < B.height; i++) {
		for (int j = 0; j < B.width; j++) {
			B.data[i * B.width + j] = 1;
		}
	}
	MatMul(A, B, C);
	printf("res: \n");
	// print out the result
	for (int i = 0; i < C.height; i++) {
		for (int j = 0; j < C.width; j++) {
			printf("%.1f ", C.data[i * C.width + j]);
		}
		printf("\n");
	}
    return 0;
}

	.file	"tmpxft_0024a099_00000000-6_kernel.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2903:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2903:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z10GetElement6Matrixii
	.type	_Z10GetElement6Matrixii, @function
_Z10GetElement6Matrixii:
.LFB2896:
	.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
.LFE2896:
	.size	_Z10GetElement6Matrixii, .-_Z10GetElement6Matrixii
	.globl	_Z10SetElement6Matrixiif
	.type	_Z10SetElement6Matrixiif, @function
_Z10SetElement6Matrixiif:
.LFB2897:
	.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
.LFE2897:
	.size	_Z10SetElement6Matrixiif, .-_Z10SetElement6Matrixiif
	.globl	_Z12GetSubMatrix6Matrixii
	.type	_Z12GetSubMatrix6Matrixii, @function
_Z12GetSubMatrix6Matrixii:
.LFB2898:
	.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
.LFE2898:
	.size	_Z12GetSubMatrix6Matrixii, .-_Z12GetSubMatrix6Matrixii
	.globl	_Z34__device_stub__Z10add_kernelPiS_S_PiS_S_
	.type	_Z34__device_stub__Z10add_kernelPiS_S_PiS_S_, @function
_Z34__device_stub__Z10add_kernelPiS_S_PiS_S_:
.LFB2925:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L8
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z10add_kernelPiS_S_(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L8:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2925:
	.size	_Z34__device_stub__Z10add_kernelPiS_S_PiS_S_, .-_Z34__device_stub__Z10add_kernelPiS_S_PiS_S_
	.globl	_Z10add_kernelPiS_S_
	.type	_Z10add_kernelPiS_S_, @function
_Z10add_kernelPiS_S_:
.LFB2926:
	.cfi_startproc
	endbr64
	jmp	_Z34__device_stub__Z10add_kernelPiS_S_PiS_S_
	.cfi_endproc
.LFE2926:
	.size	_Z10add_kernelPiS_S_, .-_Z10add_kernelPiS_S_
	.globl	_Z32__device_stub__Z4KDot6MatrixS_S_R6MatrixS0_S0_
	.type	_Z32__device_stub__Z4KDot6MatrixS_S_R6MatrixS0_S0_, @function
_Z32__device_stub__Z4KDot6MatrixS_S_R6MatrixS0_S0_:
.LFB2927:
	.cfi_startproc
	endbr64
	subq	$104, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rcx
	movq	%rdi, 64(%rsp)
	movabsq	$4294967297, %rax
	movq	%rsi, 72(%rsp)
	leaq	16(%rsp), %rdi
	leaq	28(%rsp), %rsi
	movq	%rdx, 80(%rsp)
	movq	%rsp, %rdx
	movq	%rax, 16(%rsp)
	movl	$1, 24(%rsp)
	movq	%rax, 28(%rsp)
	movl	$1, 36(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L14
	pushq	8(%rsp)
	.cfi_def_cfa_offset 120
	leaq	_Z4KDot6MatrixS_S_(%rip), %rdi
	pushq	8(%rsp)
	.cfi_def_cfa_offset 128
	movq	44(%rsp), %rcx
	movl	52(%rsp), %r8d
	movq	32(%rsp), %rsi
	movl	40(%rsp), %edx
	leaq	80(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 120
	popq	%rdx
	.cfi_def_cfa_offset 112
.L14:
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L16
	call	__stack_chk_fail@PLT
.L16:
	addq	$104, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2927:
	.size	_Z32__device_stub__Z4KDot6MatrixS_S_R6MatrixS0_S0_, .-_Z32__device_stub__Z4KDot6MatrixS_S_R6MatrixS0_S0_
	.globl	_Z4KDot6MatrixS_S_
	.type	_Z4KDot6MatrixS_S_, @function
_Z4KDot6MatrixS_S_:
.LFB2928:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	64(%rsp), %rdx
	leaq	40(%rsp), %rsi
	leaq	16(%rsp), %rdi
	call	_Z32__device_stub__Z4KDot6MatrixS_S_R6MatrixS0_S0_
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2928:
	.size	_Z4KDot6MatrixS_S_, .-_Z4KDot6MatrixS_S_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Error while allocate matrix A ==> %s\n"
.LC1:
	.string	"Error while allocate matrix B ==> %s\n"
.LC2:
	.string	"Error while allocate matrix C ==> %s\n"
.LC3:
	.string	"Error while launch kernel ==> %s\n"
.LC4:
	.string	"Error while copy result to host memory ==> %s\n"
	.text
	.globl	_Z6MatMul6MatrixS_S_
	.type	_Z6MatMul6MatrixS_S_, @function
_Z6MatMul6MatrixS_S_:
.LFB2899:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	$2, %esi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	subq	$184, %rsp
	.cfi_def_cfa_offset 208
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	movl	212(%rsp), %ecx
	movabsq	$8589934594, %rax
	movl	232(%rsp), %ebx
	movq	%rax, (%rsp)
	movl	208(%rsp), %ebp
	leaq	40(%rsp), %rdi
	movl	%ecx, %eax
	movl	$1, 8(%rsp)
	cltd
	movl	%ebp, 24(%rsp)
	imull	%ecx, %ebp
	idivl	%esi
	movl	$1, 20(%rsp)
	movl	%ecx, 28(%rsp)
	movslq	%ebp, %rbp
	salq	$2, %rbp
	movl	%eax, 12(%rsp)
	movl	%ebx, %eax
	cltd
	idivl	%esi
	movq	%rbp, %rsi
	movl	%eax, 16(%rsp)
	movl	216(%rsp), %eax
	movl	%eax, 32(%rsp)
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L21
	movl	%eax, %edi
	call	cudaGetErrorString@PLT
	leaq	.LC0(%rip), %rsi
	movq	%rax, %rdx
	jmp	.L35
.L21:
	movq	224(%rsp), %rsi
	movq	40(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rdx
	call	cudaMemcpy@PLT
	movl	%ebx, 48(%rsp)
	leaq	64(%rsp), %rdi
	movq	236(%rsp), %rax
	imull	236(%rsp), %ebx
	movq	%rax, 52(%rsp)
	movslq	%ebx, %rbx
	salq	$2, %rbx
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L22
	call	cudaGetErrorString@PLT
	leaq	.LC1(%rip), %rsi
	movq	%rax, %rdx
.L35:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L22:
	movq	248(%rsp), %rsi
	movq	64(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	256(%rsp), %ebx
	leaq	88(%rsp), %rdi
	movl	260(%rsp), %eax
	movl	264(%rsp), %edx
	movl	%ebx, 72(%rsp)
	imull	%eax, %ebx
	movl	%eax, 76(%rsp)
	movl	%edx, 80(%rsp)
	movslq	%ebx, %rbx
	salq	$2, %rbx
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L23
	call	cudaGetErrorString@PLT
	leaq	.LC2(%rip), %rsi
	movq	%rax, %rdx
	jmp	.L35
.L23:
	movl	8(%rsp), %ecx
	movq	(%rsp), %rdx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	12(%rsp), %rdi
	movl	20(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	movq	40(%rsp), %rax
	movups	24(%rsp), %xmm0
	leaq	144(%rsp), %rdx
	leaq	120(%rsp), %rsi
	movups	48(%rsp), %xmm1
	movups	72(%rsp), %xmm2
	leaq	96(%rsp), %rdi
	movq	%rax, 112(%rsp)
	movq	64(%rsp), %rax
	movups	%xmm0, 96(%rsp)
	movq	%rax, 136(%rsp)
	movq	88(%rsp), %rax
	movups	%xmm1, 120(%rsp)
	movq	%rax, 160(%rsp)
	movups	%xmm2, 144(%rsp)
	call	_Z32__device_stub__Z4KDot6MatrixS_S_R6MatrixS0_S0_
.L24:
	call	cudaThreadSynchronize@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L25
	call	cudaGetErrorString@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L25:
	movq	272(%rsp), %rdi
	movq	88(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L26
	call	cudaGetErrorString@PLT
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L26:
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	addq	$184, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2899:
	.size	_Z6MatMul6MatrixS_S_, .-_Z6MatMul6MatrixS_S_
	.section	.rodata.str1.1
.LC6:
	.string	"res: \n"
.LC7:
	.string	"%.1f "
.LC8:
	.string	"\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2900:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	$96, %edi
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$80, %rsp
	.cfi_def_cfa_offset 128
	call	malloc@PLT
	movl	$96, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	movl	$144, %edi
	movq	%rax, %r12
	call	malloc@PLT
	movss	.LC5(%rip), %xmm0
	movq	%rbx, %rcx
	xorl	%edx, %edx
	movq	%rax, %rbp
.L37:
	xorl	%eax, %eax
.L38:
	movss	%xmm0, (%rcx,%rax,4)
	incq	%rax
	cmpq	$4, %rax
	jne	.L38
	addl	$4, %edx
	addq	$16, %rcx
	cmpl	$24, %edx
	jne	.L37
	movq	%r12, %rcx
	xorl	%edx, %edx
.L39:
	xorl	%eax, %eax
.L40:
	movss	%xmm0, (%rcx,%rax,4)
	incq	%rax
	cmpq	$6, %rax
	jne	.L40
	addl	$6, %edx
	addq	$24, %rcx
	cmpl	$24, %edx
	jne	.L39
	movabsq	$25769803780, %rax
	movl	$4, 16(%rsp)
	subq	$80, %rsp
	.cfi_def_cfa_offset 208
	leaq	.LC7(%rip), %r14
	movq	%rax, 88(%rsp)
	movabsq	$17179869190, %rax
	movq	%rax, 112(%rsp)
	movabsq	$25769803782, %rax
	movq	%rax, 136(%rsp)
	movl	$6, 120(%rsp)
	movl	$6, 144(%rsp)
	movups	112(%rsp), %xmm2
	movups	136(%rsp), %xmm1
	movups	88(%rsp), %xmm3
	movq	%r12, 40(%rsp)
	xorl	%r12d, %r12d
	movups	%xmm2, 24(%rsp)
	movups	%xmm1, 48(%rsp)
	movups	%xmm3, (%rsp)
	movq	%rbp, 64(%rsp)
	movq	%rbx, 16(%rsp)
	call	_Z6MatMul6MatrixS_S_
	addq	$80, %rsp
	.cfi_def_cfa_offset 128
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC6(%rip), %rsi
	call	__printf_chk@PLT
.L42:
	imulq	$24, %r12, %r13
	xorl	%ebx, %ebx
	addq	%rbp, %r13
.L43:
	cvtss2sd	0(%r13,%rbx,4), %xmm0
	movq	%r14, %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	incq	%rbx
	cmpq	$6, %rbx
	jne	.L43
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%r12
	call	__printf_chk@PLT
	cmpq	$6, %r12
	jne	.L42
	addq	$80, %rsp
	.cfi_def_cfa_offset 48
	xorl	%eax, %eax
	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
.LFE2900:
	.size	main, .-main
	.section	.rodata.str1.1
.LC9:
	.string	"_Z4KDot6MatrixS_S_"
.LC10:
	.string	"_Z10add_kernelPiS_S_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2930:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -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	.LC9(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z4KDot6MatrixS_S_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC10(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z10add_kernelPiS_S_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	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	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2930:
	.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
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC5:
	.long	1065353216
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <vector>

__global__ void add_kernel(int *a, int *b, int *c) {
    *c = *a + *b;
}

//template <typename T>
//struct Matrix {
//    int width;
//    int height;
//    std::vector<T> data;
//};
//
//template <>
//struct Matrix {
//    int width;
//    int height;
//    std::vector<float> data;
//};

#define BS 2

typedef struct {
    int width;
	int height;
	int stride;
    float *data;
} Matrix;

// run inner GPU threads
__device__ float GetElement(Matrix mat, int row, int col) {
    return mat.data[row * mat.width + col];
}

__device__ void SetElement(Matrix mat, int row, int col, float val) {
    mat.data[row * mat.stride + col] = val;
}

__device__ Matrix GetSubMatrix(Matrix mat, int row, int col) {
    Matrix subx;
	subx.width = subx.height = BS;
	subx.stride = mat.stride;
	subx.data = &mat.data[row * BS * mat.width + col * BS];
    return subx;
}

// kernel
__global__ void KDot(Matrix A, Matrix B, Matrix C) {
    int block_row = blockIdx.y;
    int block_col = blockIdx.x;
    // in RK(inner register in each thread)
    float res = 0;
    // b * b sub matrix
    Matrix csub = GetSubMatrix(C, block_row, block_col);
    int row = threadIdx.y;
    int col = threadIdx.x;
    for (int k = 0; k < A.width / BS; k++) {
        Matrix asub = GetSubMatrix(A, block_row, k);
        Matrix bsub = GetSubMatrix(B, k, block_col);
        // shm set
        __shared__ float a_shm[BS][BS];
        __shared__ float b_shm[BS][BS];
        // each thread load an element from global memory only once
        a_shm[row][col] = GetElement(asub, row, col);
        b_shm[row][col] = GetElement(bsub, row, col);
		__syncthreads();
		// sync all threads ensure all elements in sub matrix are stored to shm
        // compute this op
        for (int e = 0; e < BS; e++) {
            res += a_shm[row][e] * b_shm[e][col];
        }
		__syncthreads();
    }
    SetElement(csub, row, col, res);
}

// call the kernel
void MatMul(const Matrix A, const Matrix B, Matrix C) {
    dim3 dim_block(BS, BS); // block internal
    dim3 dim_grid(A.height / BS, B.width / BS);
    Matrix d_A, d_B, d_C;
    d_A.width = A.width; d_A.height = A.height; d_A.stride = A.stride;
    // only zcopy 'data' field to GPU memory
    size_t size = d_A.width * d_A.height * sizeof(float);
    hipError_t err = hipMalloc(&d_A.data, size);
    if (err) {
        printf("Error while allocate matrix A ==> %s\n", hipGetErrorString(err));
        exit(-1);
    }
    // pointer to dest, pointer to src, size, type
    hipMemcpy(d_A.data, A.data, size, hipMemcpyHostToDevice);

    d_B.width = B.width; d_B.height = B.height; d_B.stride = B.stride;
    // only zcopy 'data' field to GPU memory
    size = d_B.width * d_B.height * sizeof(float);
    err = hipMalloc(&d_B.data, size);
    if (err) {
        printf("Error while allocate matrix B ==> %s\n", hipGetErrorString(err));
        exit(-1);
    }
    // pointer to dest, pointer to src, size, type
    hipMemcpy(d_B.data, B.data, size, hipMemcpyHostToDevice);

    d_C.width = C.width; d_C.height = C.height; d_C.stride = C.stride;
    // only zcopy 'data' field to GPU memory
    size = d_C.width * d_C.height * sizeof(float);
    err = hipMalloc(&d_C.data, size);
    if (err) {
        printf("Error while allocate matrix C ==> %s\n", hipGetErrorString(err));
        exit(-1);
    }
	  // invoke kernel
    KDot<<<dim_grid, dim_block>>>(d_A, d_B, d_C);
    err = hipDeviceSynchronize();
    if (err) {
        printf("Error while launch kernel ==> %s\n", hipGetErrorString(err));
    }
    // zcopy result to host when computation done
    err = hipMemcpy(C.data, d_C.data, size, hipMemcpyDeviceToHost);
    if (err) {
        printf("Error while copy result to host memory ==> %s\n", hipGetErrorString(err));
    }

    // free all these memory finally
    hipFree(d_A.data);
    hipFree(d_B.data);
	hipFree(d_C.data);
}

int main()
{
	Matrix A, B, C;
	// A[6 * 5] B[5 * 6] C[6 * 6]
	A.width = A.stride = B.height = 4;
	A.height = B.stride = B.width = 6;
	C.width = C.height = C.stride = 6;
	// allocate memory for these matrices
	A.data = (float*)malloc(A.width * A.height * sizeof(float));
	B.data = (float*)malloc(B.width * B.height * sizeof(float));
	C.data = (float*)malloc(C.width * C.height * sizeof(float));
	// initialize A B to I[M * N]
	for (int i = 0; i < A.height; i++) {
		for (int j = 0; j < A.width; j++) {
			A.data[i * A.width + j] = 1;
		}
	}
	for (int i = 0; i < B.height; i++) {
		for (int j = 0; j < B.width; j++) {
			B.data[i * B.width + j] = 1;
		}
	}
	MatMul(A, B, C);
	printf("res: \n");
	// print out the result
	for (int i = 0; i < C.height; i++) {
		for (int j = 0; j < C.width; j++) {
			printf("%.1f ", C.data[i * C.width + j]);
		}
		printf("\n");
	}
    return 0;
}

	.text
	.file	"kernel.hip"
	.globl	_Z25__device_stub__add_kernelPiS_S_ # -- Begin function _Z25__device_stub__add_kernelPiS_S_
	.type	_Z25__device_stub__add_kernelPiS_S_,@function
_Z25__device_stub__add_kernelPiS_S_:    # @_Z25__device_stub__add_kernelPiS_S_
	.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), %rcx
	movq	%rsi, (%rcx)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%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	$_Z10add_kernelPiS_S_, %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	_Z25__device_stub__add_kernelPiS_S_, .Lfunc_end0-_Z25__device_stub__add_kernelPiS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z19__device_stub__KDot6MatrixS_S_ # -- Begin function _Z19__device_stub__KDot6MatrixS_S_
	.type	_Z19__device_stub__KDot6MatrixS_S_,@function
_Z19__device_stub__KDot6MatrixS_S_:     # @_Z19__device_stub__KDot6MatrixS_S_
	.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	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	128(%rsp), %rax
	leaq	48(%rsp), %rbx
	movq	%rax, (%rbx)
	leaq	152(%rsp), %rax
	movq	%rax, 8(%rbx)
	leaq	176(%rsp), %rax
	movq	%rax, 16(%rbx)
	leaq	32(%rsp), %r14
	leaq	16(%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	$_Z4KDot6MatrixS_S_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	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_end1:
	.size	_Z19__device_stub__KDot6MatrixS_S_, .Lfunc_end1-_Z19__device_stub__KDot6MatrixS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z6MatMul6MatrixS_S_            # -- Begin function _Z6MatMul6MatrixS_S_
	.type	_Z6MatMul6MatrixS_S_,@function
_Z6MatMul6MatrixS_S_:                   # @_Z6MatMul6MatrixS_S_
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$248, %rsp
	.cfi_def_cfa_offset 304
	.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	304(%rsp), %eax
	movl	308(%rsp), %r14d
	movl	328(%rsp), %ebp
	leaq	80(%rsp), %rdi
	movl	%eax, -16(%rdi)
	movl	%r14d, -12(%rdi)
	movl	312(%rsp), %ecx
	movl	%ecx, -8(%rdi)
	imull	%r14d, %eax
	movslq	%eax, %rbx
	shlq	$2, %rbx
	movq	%rbx, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB2_1
# %bb.3:
	leaq	328(%rsp), %r15
	leaq	304(%rsp), %rax
	movq	80(%rsp), %rdi
	movq	16(%rax), %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	56(%rsp), %rdi
	movl	%ebp, -16(%rdi)
	movl	4(%r15), %eax
	movl	%eax, -12(%rdi)
	movl	8(%r15), %ecx
	movl	%ecx, -8(%rdi)
	imull	%ebp, %eax
	movslq	%eax, %rbx
	shlq	$2, %rbx
	movq	%rbx, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB2_4
# %bb.5:
	leaq	352(%rsp), %r13
	movq	56(%rsp), %rdi
	movq	16(%r15), %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	(%r13), %eax
	leaq	32(%rsp), %rdi
	movl	%eax, -16(%rdi)
	movl	4(%r13), %ecx
	movl	%ecx, -12(%rdi)
	movl	8(%r13), %edx
	movl	%edx, -8(%rdi)
	imull	%eax, %ecx
	movslq	%ecx, %rbx
	shlq	$2, %rbx
	movq	%rbx, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB2_6
# %bb.7:
	movl	%r14d, %eax
	shrl	$31, %eax
	addl	%r14d, %eax
	sarl	%eax
	movl	%ebp, %edi
	shrl	$31, %edi
	addl	%ebp, %edi
	sarl	%edi
	shlq	$32, %rdi
	orq	%rax, %rdi
	movabsq	$8589934594, %rdx               # imm = 0x200000002
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_9
# %bb.8:
	movq	80(%rsp), %rax
	leaq	128(%rsp), %rcx
	movq	%rax, 16(%rcx)
	movups	64(%rsp), %xmm0
	movaps	%xmm0, (%rcx)
	movq	56(%rsp), %rax
	leaq	160(%rsp), %rdx
	movq	%rax, 16(%rdx)
	movups	40(%rsp), %xmm0
	movaps	%xmm0, (%rdx)
	movq	32(%rsp), %rax
	leaq	192(%rsp), %rsi
	movq	%rax, 16(%rsi)
	movups	16(%rsp), %xmm0
	movaps	%xmm0, (%rsi)
	leaq	224(%rsp), %r14
	movq	%rcx, (%r14)
	movq	%rdx, 8(%r14)
	movq	%rsi, 16(%r14)
	leaq	112(%rsp), %r15
	leaq	96(%rsp), %r12
	leaq	8(%rsp), %rdx
	leaq	88(%rsp), %rbp
	movq	%r15, %rdi
	movq	%r12, %rsi
	movq	%rbp, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r15), %rsi
	movl	8(%r15), %edx
	movq	(%r12), %rcx
	movl	8(%r12), %r8d
	movl	$_Z4KDot6MatrixS_S_, %edi
	movq	%r14, %r9
	pushq	(%rbp)
	.cfi_adjust_cfa_offset 8
	leaq	16(%rsp), %rax
	pushq	(%rax)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$16, %rsp
	.cfi_adjust_cfa_offset -16
.LBB2_9:
	callq	hipDeviceSynchronize
	testl	%eax, %eax
	je	.LBB2_11
# %bb.10:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.3, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
.LBB2_11:
	movq	16(%r13), %rdi
	movq	32(%rsp), %rsi
	movq	%rbx, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	je	.LBB2_13
# %bb.12:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.4, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
.LBB2_13:
	movq	80(%rsp), %rdi
	callq	hipFree
	movq	56(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	addq	$248, %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
.LBB2_1:
	.cfi_def_cfa_offset 304
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	jmp	.LBB2_2
.LBB2_4:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.1, %edi
	jmp	.LBB2_2
.LBB2_6:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.2, %edi
.LBB2_2:
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$-1, %edi
	callq	exit
.Lfunc_end2:
	.size	_Z6MatMul6MatrixS_S_, .Lfunc_end2-_Z6MatMul6MatrixS_S_
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.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
	subq	$144, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$96, %edi
	callq	malloc
	movq	%rax, %r14
	movl	$96, %edi
	callq	malloc
	movq	%rax, %r15
	movl	$144, %edi
	callq	malloc
	movq	%rax, %rbx
	xorl	%eax, %eax
	movq	%r14, %rcx
.LBB3_1:                                # %.preheader46
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_2 Depth 2
	xorl	%edx, %edx
.LBB3_2:                                #   Parent Loop BB3_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	$1065353216, (%rcx,%rdx,4)      # imm = 0x3F800000
	incq	%rdx
	cmpq	$4, %rdx
	jne	.LBB3_2
# %bb.3:                                #   in Loop: Header=BB3_1 Depth=1
	incq	%rax
	addq	$16, %rcx
	cmpq	$6, %rax
	jne	.LBB3_1
# %bb.4:                                # %.preheader44.preheader
	xorl	%eax, %eax
	movq	%r15, %rcx
.LBB3_5:                                # %.preheader44
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_6 Depth 2
	xorl	%edx, %edx
.LBB3_6:                                #   Parent Loop BB3_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	$1065353216, (%rcx,%rdx,4)      # imm = 0x3F800000
	incq	%rdx
	cmpq	$6, %rdx
	jne	.LBB3_6
# %bb.7:                                #   in Loop: Header=BB3_5 Depth=1
	incq	%rax
	addq	$24, %rcx
	cmpq	$4, %rax
	jne	.LBB3_5
# %bb.8:
	movabsq	$25769803780, %rax              # imm = 0x600000004
	movq	%rax, 96(%rsp)
	movl	$4, 104(%rsp)
	movq	%r14, 112(%rsp)
	movabsq	$17179869190, %rax              # imm = 0x400000006
	movq	%rax, 72(%rsp)
	movl	$6, %eax
	movl	%eax, 80(%rsp)
	movq	%r15, 88(%rsp)
	movabsq	$25769803782, %rcx              # imm = 0x600000006
	movq	%rcx, 120(%rsp)
	movl	%eax, 128(%rsp)
	movq	%rbx, 136(%rsp)
	movq	%rbx, 64(%rsp)
	movups	120(%rsp), %xmm0
	movups	%xmm0, 48(%rsp)
	movq	88(%rsp), %rax
	movq	%rax, 40(%rsp)
	movups	72(%rsp), %xmm0
	movups	%xmm0, 24(%rsp)
	movq	112(%rsp), %rax
	movq	%rax, 16(%rsp)
	movups	96(%rsp), %xmm0
	movups	%xmm0, (%rsp)
	callq	_Z6MatMul6MatrixS_S_
	movl	$.Lstr, %edi
	callq	puts@PLT
	xorl	%r14d, %r14d
.LBB3_9:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_10 Depth 2
	xorl	%r15d, %r15d
.LBB3_10:                               #   Parent Loop BB3_9 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx,%r15,4), %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	incq	%r15
	cmpq	$6, %r15
	jne	.LBB3_10
# %bb.11:                               #   in Loop: Header=BB3_9 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r14
	addq	$24, %rbx
	cmpq	$6, %r14
	jne	.LBB3_9
# %bb.12:
	xorl	%eax, %eax
	addq	$144, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	main, .Lfunc_end3-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:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z10add_kernelPiS_S_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z4KDot6MatrixS_S_, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__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	.LBB5_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
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z10add_kernelPiS_S_,@object    # @_Z10add_kernelPiS_S_
	.section	.rodata,"a",@progbits
	.globl	_Z10add_kernelPiS_S_
	.p2align	3, 0x0
_Z10add_kernelPiS_S_:
	.quad	_Z25__device_stub__add_kernelPiS_S_
	.size	_Z10add_kernelPiS_S_, 8

	.type	_Z4KDot6MatrixS_S_,@object      # @_Z4KDot6MatrixS_S_
	.globl	_Z4KDot6MatrixS_S_
	.p2align	3, 0x0
_Z4KDot6MatrixS_S_:
	.quad	_Z19__device_stub__KDot6MatrixS_S_
	.size	_Z4KDot6MatrixS_S_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Error while allocate matrix A ==> %s\n"
	.size	.L.str, 38

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Error while allocate matrix B ==> %s\n"
	.size	.L.str.1, 38

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Error while allocate matrix C ==> %s\n"
	.size	.L.str.2, 38

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Error while launch kernel ==> %s\n"
	.size	.L.str.3, 34

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Error while copy result to host memory ==> %s\n"
	.size	.L.str.4, 47

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

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

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z4KDot6MatrixS_S_"
	.size	.L__unnamed_2, 19

	.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

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"res: "
	.size	.Lstr, 6

	.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 _Z25__device_stub__add_kernelPiS_S_
	.addrsig_sym _Z19__device_stub__KDot6MatrixS_S_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z10add_kernelPiS_S_
	.addrsig_sym _Z4KDot6MatrixS_S_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z10add_kernelPiS_S_    ; -- Begin function _Z10add_kernelPiS_S_
	.globl	_Z10add_kernelPiS_S_
	.p2align	8
	.type	_Z10add_kernelPiS_S_,@function
_Z10add_kernelPiS_S_:                   ; @_Z10add_kernelPiS_S_
; %bb.0:
	s_clause 0x1
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[0:1], s[0:1], 0x10
	s_waitcnt lgkmcnt(0)
	s_load_b32 s2, s[4:5], 0x0
	s_load_b32 s3, s[6:7], 0x0
	s_waitcnt lgkmcnt(0)
	s_add_i32 s2, s3, s2
	s_delay_alu instid0(SALU_CYCLE_1)
	v_dual_mov_b32 v0, 0 :: v_dual_mov_b32 v1, s2
	global_store_b32 v0, v1, s[0:1]
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z10add_kernelPiS_S_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 24
		.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 0
		.amdhsa_next_free_vgpr 2
		.amdhsa_next_free_sgpr 8
		.amdhsa_reserve_vcc 0
		.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	_Z10add_kernelPiS_S_, .Lfunc_end0-_Z10add_kernelPiS_S_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 80
; NumSgprs: 8
; NumVgprs: 2
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 0
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 8
; NumVGPRsForWavesPerEU: 2
; 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: 0
	.text
	.protected	_Z4KDot6MatrixS_S_      ; -- Begin function _Z4KDot6MatrixS_S_
	.globl	_Z4KDot6MatrixS_S_
	.p2align	8
	.type	_Z4KDot6MatrixS_S_,@function
_Z4KDot6MatrixS_S_:                     ; @_Z4KDot6MatrixS_S_
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x0
	s_load_b64 s[2:3], s[0:1], 0x40
	v_bfe_u32 v1, v0, 10, 10
	v_and_b32_e32 v0, 0x3ff, v0
	v_mov_b32_e32 v2, 0
	s_waitcnt lgkmcnt(0)
	s_cmp_lt_i32 s4, 2
	s_cbranch_scc1 .LBB1_3
; %bb.1:                                ; %.lr.ph
	s_clause 0x2
	s_load_b64 s[6:7], s[0:1], 0x10
	s_load_b64 s[10:11], s[0:1], 0x28
	s_load_b32 s5, s[0:1], 0x18
	v_lshlrev_b32_e32 v4, 1, v1
	v_dual_mov_b32 v2, 0 :: v_dual_lshlrev_b32 v5, 2, v0
	v_lshlrev_b32_e32 v3, 3, v1
	s_lshr_b32 s8, s4, 31
	s_delay_alu instid0(VALU_DEP_3)
	v_add_lshl_u32 v8, v4, v0, 2
	s_add_i32 s8, s4, s8
	s_mul_i32 s4, s15, s4
	v_add_nc_u32_e32 v4, v3, v5
	v_add_nc_u32_e32 v5, 16, v5
	s_ashr_i32 s8, s8, 1
	s_lshl_b32 s4, s4, 1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v10, v5, v3
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v6, s6, s6, v8
	v_add_co_ci_u32_e64 v7, null, s7, 0, s6
	v_add_co_u32 v8, s6, s10, v8
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v9, null, s11, 0, s6
	s_lshl_b32 s6, s14, 1
	s_lshl_b32 s9, s5, 1
.LBB1_2:                                ; =>This Inner Loop Header: Depth=1
	s_ashr_i32 s5, s4, 31
	s_ashr_i32 s7, s6, 31
	s_lshl_b64 s[10:11], s[4:5], 2
	s_add_i32 s8, s8, -1
	v_add_co_u32 v11, vcc_lo, v6, s10
	v_add_co_ci_u32_e32 v12, vcc_lo, s11, v7, vcc_lo
	s_lshl_b64 s[10:11], s[6:7], 2
	s_add_i32 s4, s4, 2
	v_add_co_u32 v13, vcc_lo, v8, s10
	v_add_co_ci_u32_e32 v14, vcc_lo, s11, v9, vcc_lo
	global_load_b32 v11, v[11:12], off
	global_load_b32 v12, v[13:14], off
	s_add_i32 s6, s6, s9
	s_cmp_eq_u32 s8, 0
	s_waitcnt vmcnt(1)
	ds_store_b32 v4, v11
	s_waitcnt vmcnt(0)
	ds_store_b32 v10, v12
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_b64 v[11:12], v3
	ds_load_2addr_b32 v[13:14], v5 offset1:2
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_fmac_f32_e32 v2, v11, v13
	s_delay_alu instid0(VALU_DEP_1)
	v_fmac_f32_e32 v2, v12, v14
	s_cbranch_scc0 .LBB1_2
.LBB1_3:                                ; %Flow102
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x30
	s_load_b32 s0, s[0:1], 0x38
	s_waitcnt lgkmcnt(0)
	v_mad_u64_u32 v[3:4], null, s0, v1, v[0:1]
	s_mul_i32 s0, s4, s15
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s0, s0, s14
	s_lshl_b32 s0, s0, 1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_ashr_i32 s1, s0, 31
	v_ashrrev_i32_e32 v4, 31, v3
	s_lshl_b64 s[0:1], s[0:1], 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	s_add_u32 s0, s2, s0
	s_addc_u32 s1, s3, s1
	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, s0, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_store_b32 v[0:1], v2, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z4KDot6MatrixS_S_
		.amdhsa_group_segment_fixed_size 32
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 72
		.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 15
		.amdhsa_next_free_sgpr 16
		.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_end1:
	.size	_Z4KDot6MatrixS_S_, .Lfunc_end1-_Z4KDot6MatrixS_S_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 472
; NumSgprs: 18
; NumVgprs: 15
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 32 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 15
; 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
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 24
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z10add_kernelPiS_S_
    .private_segment_fixed_size: 0
    .sgpr_count:     8
    .sgpr_spill_count: 0
    .symbol:         _Z10add_kernelPiS_S_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     2
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
  - .args:
      - .offset:         0
        .size:           24
        .value_kind:     by_value
      - .offset:         24
        .size:           24
        .value_kind:     by_value
      - .offset:         48
        .size:           24
        .value_kind:     by_value
    .group_segment_fixed_size: 32
    .kernarg_segment_align: 8
    .kernarg_segment_size: 72
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z4KDot6MatrixS_S_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z4KDot6MatrixS_S_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     15
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
