#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime> 

using namespace std;

void matrix_multiplication(float* M, float* N, float* P, int witdh) {
    for (int i = 0; i < witdh; i++) {
        for (int j = 0; j < witdh; j++) {
            int sum = 0;
            for (int k = 0; k < witdh; k++) {
                float a = M[i * witdh + k];
                float b = N[k * witdh + j];
                
                sum += a * b;
            }
            P[i * witdh + j] = sum;
        }
    }
}

int main(int argc, char **argv) {
    int witdh = 200;

    float* A;
    float* B;
    float* C;

    A = (float*) malloc(witdh * witdh * sizeof(float));
    B = (float*) malloc(witdh * witdh * sizeof(float));
    C = (float*) malloc(witdh * witdh * sizeof(float));
    srand (time(NULL));

    // for (int i = 0; i < witdh; i++) {
    //     for (int j = 0; j < witdh; j++) {
    //         A[i * witdh + j] = (rand() % 100) + 1;
    //         B[i * witdh + j] = (rand() % 200) + 1;
    //     }
    // }
    // cout << endl;
    // for (int i = 0; i < witdh; i++) {
    //     for (int j = 0; j < witdh; j++) {
    //         cout << A[i * witdh + j] << "\t";
    //     }
    //     cout << endl;
    // }

    // cout << endl;

    // for (int i = 0; i < witdh; i++) {
    //     for (int j = 0; j < witdh; j++) {
    //         cout << B[i * witdh + j] << "\t";
    //     }
    //     cout << endl;
    // }

    matrix_multiplication(A, B, C, witdh);

    /*for (int i = 0; i < witdh; i++) {
        for (int j = 0; j < witdh; j++) {
            cout << C[i * witdh + j] << "\t";
        }
        cout << endl;
    }*/

    return 0;
}

	.file	"tmpxft_0036cfee_00000000-6_matrix_mul_host.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3639:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3639:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z21matrix_multiplicationPfS_S_i
	.type	_Z21matrix_multiplicationPfS_S_i, @function
_Z21matrix_multiplicationPfS_S_i:
.LFB3635:
	.cfi_startproc
	endbr64
	movslq	%ecx, %rcx
	xorl	%r8d, %r8d
	movq	%rsi, %r10
	movq	%rdi, %r9
	movq	%rcx, %rax
	movq	%rdx, %r11
	salq	$2, %rcx
	xorl	%esi, %esi
	cmpl	%eax, %r8d
	jl	.L11
	ret
.L11:
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
.L7:
	xorl	%edx, %edx
	leaq	(%r9,%rsi), %r12
	leaq	(%r11,%rsi), %r13
.L6:
	leaq	(%r10,%rdx,4), %rbp
	xorl	%edi, %edi
	xorl	%ebx, %ebx
.L4:
	movss	(%r12,%rdi,4), %xmm0
	mulss	0(%rbp), %xmm0
	cvtsi2ssl	%ebx, %xmm1
	incq	%rdi
	addq	%rcx, %rbp
	addss	%xmm1, %xmm0
	cvttss2sil	%xmm0, %ebx
	cmpl	%edi, %eax
	jg	.L4
	cvtsi2ssl	%ebx, %xmm0
	movss	%xmm0, 0(%r13,%rdx,4)
	incq	%rdx
	cmpl	%edx, %eax
	jg	.L6
	incl	%r8d
	addq	%rcx, %rsi
	cmpl	%eax, %r8d
	jl	.L7
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3635:
	.size	_Z21matrix_multiplicationPfS_S_i, .-_Z21matrix_multiplicationPfS_S_i
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	$160000, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	call	malloc@PLT
	movl	$160000, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	movl	$160000, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	xorl	%edi, %edi
	movq	%rax, %r12
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
	movq	%r12, %rdx
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	movl	$200, %ecx
	call	_Z21matrix_multiplicationPfS_S_i
	popq	%rbx
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3636:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3662:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3662:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime> 

using namespace std;

void matrix_multiplication(float* M, float* N, float* P, int witdh) {
    for (int i = 0; i < witdh; i++) {
        for (int j = 0; j < witdh; j++) {
            int sum = 0;
            for (int k = 0; k < witdh; k++) {
                float a = M[i * witdh + k];
                float b = N[k * witdh + j];
                
                sum += a * b;
            }
            P[i * witdh + j] = sum;
        }
    }
}

int main(int argc, char **argv) {
    int witdh = 200;

    float* A;
    float* B;
    float* C;

    A = (float*) malloc(witdh * witdh * sizeof(float));
    B = (float*) malloc(witdh * witdh * sizeof(float));
    C = (float*) malloc(witdh * witdh * sizeof(float));
    srand (time(NULL));

    // for (int i = 0; i < witdh; i++) {
    //     for (int j = 0; j < witdh; j++) {
    //         A[i * witdh + j] = (rand() % 100) + 1;
    //         B[i * witdh + j] = (rand() % 200) + 1;
    //     }
    // }
    // cout << endl;
    // for (int i = 0; i < witdh; i++) {
    //     for (int j = 0; j < witdh; j++) {
    //         cout << A[i * witdh + j] << "\t";
    //     }
    //     cout << endl;
    // }

    // cout << endl;

    // for (int i = 0; i < witdh; i++) {
    //     for (int j = 0; j < witdh; j++) {
    //         cout << B[i * witdh + j] << "\t";
    //     }
    //     cout << endl;
    // }

    matrix_multiplication(A, B, C, witdh);

    /*for (int i = 0; i < witdh; i++) {
        for (int j = 0; j < witdh; j++) {
            cout << C[i * witdh + j] << "\t";
        }
        cout << endl;
    }*/

    return 0;
}

	.text
	.file	"matrix_mul_host.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z21matrix_multiplicationPfS_S_i # -- Begin function _Z21matrix_multiplicationPfS_S_i
	.type	_Z21matrix_multiplicationPfS_S_i,@function
_Z21matrix_multiplicationPfS_S_i:       # @_Z21matrix_multiplicationPfS_S_i
	.cfi_startproc
# %bb.0:
	testl	%ecx, %ecx
	jle	.LBB0_8
# %bb.1:                                # %.preheader28.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
	movl	%ecx, %eax
	leaq	(,%rax,4), %r8
	xorl	%r9d, %r9d
	xorl	%r10d, %r10d
.LBB0_2:                                # %.preheader.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_3 Depth 2
                                        #       Child Loop BB0_4 Depth 3
	movl	%r9d, %r11d
	leaq	(%rdi,%r11,4), %r11
	movq	%r10, %rbx
	imulq	%rax, %rbx
	leaq	(%rdx,%rbx,4), %rbx
	movq	%rsi, %r14
	xorl	%r15d, %r15d
.LBB0_3:                                # %.preheader
                                        #   Parent Loop BB0_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB0_4 Depth 3
	xorl	%r12d, %r12d
	movq	%r14, %r13
	xorl	%ebp, %ebp
.LBB0_4:                                #   Parent Loop BB0_2 Depth=1
                                        #     Parent Loop BB0_3 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%r11,%r12,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	mulss	(%r13), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2ss	%ebp, %xmm1
	addss	%xmm0, %xmm1
	cvttss2si	%xmm1, %ebp
	incq	%r12
	addq	%r8, %r13
	cmpq	%r12, %rax
	jne	.LBB0_4
# %bb.5:                                # %._crit_edge
                                        #   in Loop: Header=BB0_3 Depth=2
	xorps	%xmm0, %xmm0
	cvtsi2ss	%ebp, %xmm0
	movss	%xmm0, (%rbx,%r15,4)
	incq	%r15
	addq	$4, %r14
	cmpq	%rax, %r15
	jne	.LBB0_3
# %bb.6:                                # %._crit_edge32
                                        #   in Loop: Header=BB0_2 Depth=1
	incq	%r10
	addl	%ecx, %r9d
	cmpq	%rax, %r10
	jne	.LBB0_2
# %bb.7:
	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
.LBB0_8:                                # %._crit_edge34
	retq
.Lfunc_end0:
	.size	_Z21matrix_multiplicationPfS_S_i, .Lfunc_end0-_Z21matrix_multiplicationPfS_S_i
	.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
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$160000, %edi                   # imm = 0x27100
	callq	malloc
	movq	%rax, %rbx
	movl	$160000, %edi                   # imm = 0x27100
	callq	malloc
	movq	%rax, %r14
	movl	$160000, %edi                   # imm = 0x27100
	callq	malloc
	movq	%rax, %r15
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	movq	%rbx, %rdi
	movq	%r14, %rsi
	movq	%r15, %rdx
	movl	$200, %ecx
	callq	_Z21matrix_multiplicationPfS_S_i
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
