#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>

/* change dimension size as needed */
const int dimension = 32 ;
struct timeval tv; 

double timestamp()
{
        double t;
        gettimeofday(&tv, NULL);
        t = tv.tv_sec + (tv.tv_usec/1000000.0);
        return t;
}

int main(int argc, char *argv[])
{
	cudaEvent_t start1, stop1;
	float time;

	cudaEventCreate(&start1);
	cudaEventCreate(&stop1);

        int i, j, k;
        double *A, *B, *C; 
	double  start, end;

        A = (double*)malloc(dimension*dimension*sizeof(double));
        B = (double*)malloc(dimension*dimension*sizeof(double));
        C = (double*)malloc(dimension*dimension*sizeof(double));

        srand(292);

        for(i = 0; i < dimension; i++)
                for(j = 0; j < dimension; j++)
                {   
                        A[dimension*i+j] = (rand()/(RAND_MAX + 1.0));
                        B[dimension*i+j] = (rand()/(RAND_MAX + 1.0));
                        C[dimension*i+j] = 0.0;
                }   

	cudaEventRecord( start1, 0 );


        start = timestamp();
        for(i = 0; i < dimension; i++)
                for(j = 0; j < dimension; j++)
                        for(k = 0; k < dimension; k++)
                                C[dimension*i+j] += A[dimension*i+k] *
                                        B[dimension*k+j];

        end = timestamp();
		
	cudaEventRecord( stop1, 0 );
	cudaEventSynchronize( stop1 );
	
	cudaEventElapsedTime( &time, start1, stop1 );
	printf("\nsecs:%f\n", time ) ; // cudaEventElapsedTime( &time, start1, stop1 ));
	cudaEventDestroy( start1 );
	cudaEventDestroy( stop1 );

        printf("\nsecs:%f\n", end-start);

        free(A);
        free(B);
        free(C);

        return 0;

}

	.file	"tmpxft_00354624_00000000-6_matMul.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2031:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2031:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z9timestampv
	.type	_Z9timestampv, @function
_Z9timestampv:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	xorl	%esi, %esi
	leaq	tv(%rip), %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	tv(%rip), %xmm1
	cvtsi2sdq	8+tv(%rip), %xmm0
	divsd	.LC0(%rip), %xmm0
	popq	%rdx
	.cfi_def_cfa_offset 8
	addsd	%xmm1, %xmm0
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9timestampv, .-_Z9timestampv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"\nsecs:%f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.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
	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	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	32(%rsp), %rdi
	call	cudaEventCreate@PLT
	movl	$8192, %edi
	call	malloc@PLT
	movl	$8192, %edi
	movq	%rax, %r12
	call	malloc@PLT
	movl	$8192, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movl	$292, %edi
	movq	%rax, %rbx
	call	srand@PLT
.L5:
	movq	%r14, %r13
	xorl	%r15d, %r15d
	salq	$8, %r13
.L6:
	call	rand@PLT
	incl	%r15d
	cvtsi2sdl	%eax, %xmm0
	mulsd	.LC1(%rip), %xmm0
	movsd	%xmm0, (%r12,%r13)
	call	rand@PLT
	movq	$0x000000000, (%rbx,%r13)
	cvtsi2sdl	%eax, %xmm0
	mulsd	.LC1(%rip), %xmm0
	movsd	%xmm0, 0(%rbp,%r13)
	addq	$8, %r13
	cmpl	$32, %r15d
	jne	.L6
	incq	%r14
	cmpq	$32, %r14
	jne	.L5
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	call	_Z9timestampv
	xorl	%eax, %eax
	movsd	%xmm0, (%rsp)
.L8:
	leaq	(%rbx,%rax), %rcx
	xorl	%edx, %edx
	leaq	(%r12,%rax), %r8
.L12:
	movsd	(%rcx), %xmm0
	leaq	0(%rbp,%rdx,8), %rdi
	xorl	%esi, %esi
.L9:
	movsd	(%r8,%rsi,8), %xmm1
	mulsd	(%rdi), %xmm1
	incq	%rsi
	addq	$256, %rdi
	addsd	%xmm1, %xmm0
	cmpq	$32, %rsi
	jne	.L9
	incq	%rdx
	movsd	%xmm0, (%rcx)
	addq	$8, %rcx
	cmpq	$32, %rdx
	jne	.L12
	addq	$256, %rax
	cmpq	$8192, %rax
	jne	.L8
	call	_Z9timestampv
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	leaq	.LC3(%rip), %r13
	movsd	%xmm0, 8(%rsp)
	call	cudaEventRecord@PLT
	movq	32(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	32(%rsp), %rdx
	movq	24(%rsp), %rsi
	leaq	20(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	%r13, %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	20(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	24(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	32(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movsd	8(%rsp), %xmm1
	movq	%r13, %rsi
	movb	$1, %al
	subsd	(%rsp), %xmm1
	movl	$2, %edi
	movaps	%xmm1, %xmm0
	call	__printf_chk@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$56, %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
.LFE2028:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2054:
	.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
.LFE2054:
	.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
	.globl	tv
	.bss
	.align 16
	.type	tv, @object
	.size	tv, 16
tv:
	.zero	16
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	0
	.long	1093567616
	.align 8
.LC1:
	.long	0
	.long	1040187392
	.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 <stdlib.h>
#include <stdio.h>
#include <sys/time.h>

/* change dimension size as needed */
const int dimension = 32 ;
struct timeval tv; 

double timestamp()
{
        double t;
        gettimeofday(&tv, NULL);
        t = tv.tv_sec + (tv.tv_usec/1000000.0);
        return t;
}

int main(int argc, char *argv[])
{
	hipEvent_t start1, stop1;
	float time;

	hipEventCreate(&start1);
	hipEventCreate(&stop1);

        int i, j, k;
        double *A, *B, *C; 
	double  start, end;

        A = (double*)malloc(dimension*dimension*sizeof(double));
        B = (double*)malloc(dimension*dimension*sizeof(double));
        C = (double*)malloc(dimension*dimension*sizeof(double));

        srand(292);

        for(i = 0; i < dimension; i++)
                for(j = 0; j < dimension; j++)
                {   
                        A[dimension*i+j] = (rand()/(RAND_MAX + 1.0));
                        B[dimension*i+j] = (rand()/(RAND_MAX + 1.0));
                        C[dimension*i+j] = 0.0;
                }   

	hipEventRecord( start1, 0 );


        start = timestamp();
        for(i = 0; i < dimension; i++)
                for(j = 0; j < dimension; j++)
                        for(k = 0; k < dimension; k++)
                                C[dimension*i+j] += A[dimension*i+k] *
                                        B[dimension*k+j];

        end = timestamp();
		
	hipEventRecord( stop1, 0 );
	hipEventSynchronize( stop1 );
	
	hipEventElapsedTime( &time, start1, stop1 );
	printf("\nsecs:%f\n", time ) ; // cudaEventElapsedTime( &time, start1, stop1 ));
	hipEventDestroy( start1 );
	hipEventDestroy( stop1 );

        printf("\nsecs:%f\n", end-start);

        free(A);
        free(B);
        free(C);

        return 0;

}

	.text
	.file	"matMul.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z9timestampv
.LCPI0_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	_Z9timestampv
	.type	_Z9timestampv,@function
_Z9timestampv:                          # @_Z9timestampv
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movl	$tv, %edi
	xorl	%esi, %esi
	callq	gettimeofday
	cvtsi2sdq	tv(%rip), %xmm1
	cvtsi2sdq	tv+8(%rip), %xmm0
	divsd	.LCPI0_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z9timestampv, .Lfunc_end0-_Z9timestampv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0x3e00000000000000              # double 4.6566128730773926E-10
.LCPI1_1:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	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	$56, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	movl	$8192, %edi                     # imm = 0x2000
	callq	malloc
	movq	%rax, %rbp
	movl	$8192, %edi                     # imm = 0x2000
	callq	malloc
	movq	%rax, %r14
	movl	$8192, %edi                     # imm = 0x2000
	callq	malloc
	movq	%rax, %rbx
	movl	$292, %edi                      # imm = 0x124
	callq	srand
	xorl	%r12d, %r12d
	movq	%rbp, 40(%rsp)                  # 8-byte Spill
	movq	%r14, 32(%rsp)                  # 8-byte Spill
	movq	%rbx, %r15
.LBB1_1:                                # %.preheader36
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_2 Depth 2
	xorl	%r13d, %r13d
.LBB1_2:                                #   Parent Loop BB1_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	mulsd	.LCPI1_0(%rip), %xmm0
	movsd	%xmm0, (%rbp,%r13,8)
	callq	rand
	movsd	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	mulsd	%xmm1, %xmm0
	movsd	%xmm0, (%r14,%r13,8)
	movq	$0, (%r15,%r13,8)
	incq	%r13
	cmpq	$32, %r13
	jne	.LBB1_2
# %bb.3:                                #   in Loop: Header=BB1_1 Depth=1
	incq	%r12
	movl	$256, %eax                      # imm = 0x100
	addq	%rax, %r15
	addq	%rax, %r14
	addq	%rax, %rbp
	cmpq	$32, %r12
	jne	.LBB1_1
# %bb.4:
	movq	24(%rsp), %rdi
	xorl	%r12d, %r12d
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	$tv, %edi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	tv(%rip), %rax
	movq	tv+8(%rip), %rcx
	movq	40(%rsp), %r14                  # 8-byte Reload
	movq	%r14, %rdx
	movq	32(%rsp), %r15                  # 8-byte Reload
.LBB1_5:                                # %.preheader35
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_6 Depth 2
                                        #       Child Loop BB1_7 Depth 3
	movq	%r12, %rsi
	shlq	$5, %rsi
	movq	%r15, %rdi
	xorl	%r8d, %r8d
.LBB1_6:                                # %.preheader
                                        #   Parent Loop BB1_5 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_7 Depth 3
	movq	%r8, %r9
	orq	%rsi, %r9
	movsd	(%rbx,%r9,8), %xmm0             # xmm0 = mem[0],zero
	movq	%rdi, %r10
	xorl	%r11d, %r11d
.LBB1_7:                                #   Parent Loop BB1_5 Depth=1
                                        #     Parent Loop BB1_6 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movsd	(%rdx,%r11,8), %xmm1            # xmm1 = mem[0],zero
	mulsd	(%r10), %xmm1
	addsd	%xmm1, %xmm0
	incq	%r11
	addq	$256, %r10                      # imm = 0x100
	cmpq	$32, %r11
	jne	.LBB1_7
# %bb.8:                                #   in Loop: Header=BB1_6 Depth=2
	movsd	%xmm0, (%rbx,%r9,8)
	incq	%r8
	addq	$8, %rdi
	cmpq	$32, %r8
	jne	.LBB1_6
# %bb.9:                                #   in Loop: Header=BB1_5 Depth=1
	incq	%r12
	addq	$256, %rdx                      # imm = 0x100
	cmpq	$32, %r12
	jne	.LBB1_5
# %bb.10:
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rcx, %xmm0
	divsd	.LCPI1_1(%rip), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, 16(%rsp)                 # 8-byte Spill
	movl	$tv, %edi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	tv(%rip), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	tv+8(%rip), %xmm1
	divsd	.LCPI1_1(%rip), %xmm1
	addsd	%xmm0, %xmm1
	subsd	16(%rsp), %xmm1                 # 8-byte Folded Reload
	movsd	%xmm1, 16(%rsp)                 # 8-byte Spill
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	24(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	52(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventElapsedTime
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r12), %xmm0
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	movq	24(%rsp), %rdi
	callq	hipEventDestroy
	movq	8(%rsp), %rdi
	callq	hipEventDestroy
	movl	$.L.str, %edi
	movsd	16(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	%rbx, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	tv,@object                      # @tv
	.bss
	.globl	tv
	.p2align	3, 0x0
tv:
	.zero	16
	.size	tv, 16

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"\nsecs:%f\n"
	.size	.L.str, 10

	.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 tv
	.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
