#include "stdio.h"
#include "time.h"
#include <iostream>

// Defining Number of elements in Array
#define N	10000000

// Defining vector addition function for CPU
void cpuAdd(int *h_a, int *h_b, int *h_c)
{
	int tid = 0;
	while (tid < N)
	{
		h_c[tid] = h_a[tid] + h_b[tid];
		tid += 1;
	}
}

int main(void)
{
	int *h_a, *h_b, *h_c;
	h_a = (int *)malloc(sizeof(int) * N);
	h_b = (int *)malloc(sizeof(int) * N);
	h_c = (int *)malloc(sizeof(int) * N);
	// Initializing two arrays for addition
	for (int i = 0; i < N; i++)
	{
		h_a[i] = i + 1;
		h_b[i] = i - 1;
	}
	// Start Time
	clock_t start_h = clock();
	// Calling CPU function for vector addition
	cpuAdd(h_a, h_b, h_c);
	// End Time
	clock_t end_h = clock();
	// Printing Answer
	printf("Vector addition on CPU\n");
	// Execute Time
	double time_h = (double)(end_h - start_h) / CLOCKS_PER_SEC;
	printf("N = %d \n", N);
	printf("Execute time: %f seconds \n", time_h);
	
	for (int i = 9999990; i < N; i++)
	{
	printf("The sum of %d element is %d + %d = %d\n", i, h_a[i], h_b[i], h_c[i]);
	}
	
	free(h_a);
	free(h_b);
	free(h_c);
	system("pause");
	return 0;
}
	.file	"tmpxft_00394dce_00000000-6_Add_cpu.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	_Z6cpuAddPiS_S_
	.type	_Z6cpuAddPiS_S_, @function
_Z6cpuAddPiS_S_:
.LFB3635:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L3:
	movl	(%rsi,%rax), %ecx
	addl	(%rdi,%rax), %ecx
	movl	%ecx, (%rdx,%rax)
	addq	$4, %rax
	cmpq	$40000000, %rax
	jne	.L3
	ret
	.cfi_endproc
.LFE3635:
	.size	_Z6cpuAddPiS_S_, .-_Z6cpuAddPiS_S_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Vector addition on CPU\n"
.LC2:
	.string	"N = %d \n"
.LC3:
	.string	"Execute time: %f seconds \n"
.LC4:
	.string	"The sum of %d element is %d + %d = %d\n"
.LC5:
	.string	"pause"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	$40000000, %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	$16, %rsp
	.cfi_def_cfa_offset 64
	call	malloc@PLT
	movl	$40000000, %edi
	movq	%rax, %r12
	call	malloc@PLT
	movl	$40000000, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movq	%rax, %r13
	movl	$1, %eax
.L7:
	leal	-2(%rax), %edx
	movl	%eax, -4(%r12,%rax,4)
	movl	%edx, -4(%rbp,%rax,4)
	incq	%rax
	cmpq	$10000001, %rax
	jne	.L7
	call	clock@PLT
	movq	%r13, %rdx
	movq	%rbp, %rsi
	movq	%r12, %rdi
	movq	%rax, %r14
	call	_Z6cpuAddPiS_S_
	call	clock@PLT
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rbx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	subq	%r14, %rbx
	movl	$10000000, %edx
	xorl	%eax, %eax
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	leaq	.LC4(%rip), %r14
	cvtsi2sdq	%rbx, %xmm0
	divsd	.LC1(%rip), %xmm0
	movsd	%xmm0, 8(%rsp)
	movl	$9999990, %ebx
	call	__printf_chk@PLT
	movsd	8(%rsp), %xmm0
	leaq	.LC3(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
.L8:
	movl	(%r12,%rbx,4), %ecx
	movl	0(%r13,%rbx,4), %r9d
	movl	%ebx, %edx
	movq	%r14, %rsi
	movl	0(%rbp,%rbx,4), %r8d
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbx
	call	__printf_chk@PLT
	cmpq	$10000000, %rbx
	jne	.L8
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	%r13, %rdi
	call	free@PLT
	leaq	.LC5(%rip), %rdi
	call	system@PLT
	addq	$16, %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
.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
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	0
	.long	1093567616
	.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 "time.h"
#include <iostream>

// Defining Number of elements in Array
#define N	10000000

// Defining vector addition function for CPU
void cpuAdd(int *h_a, int *h_b, int *h_c)
{
	int tid = 0;
	while (tid < N)
	{
		h_c[tid] = h_a[tid] + h_b[tid];
		tid += 1;
	}
}

int main(void)
{
	int *h_a, *h_b, *h_c;
	h_a = (int *)malloc(sizeof(int) * N);
	h_b = (int *)malloc(sizeof(int) * N);
	h_c = (int *)malloc(sizeof(int) * N);
	// Initializing two arrays for addition
	for (int i = 0; i < N; i++)
	{
		h_a[i] = i + 1;
		h_b[i] = i - 1;
	}
	// Start Time
	clock_t start_h = clock();
	// Calling CPU function for vector addition
	cpuAdd(h_a, h_b, h_c);
	// End Time
	clock_t end_h = clock();
	// Printing Answer
	printf("Vector addition on CPU\n");
	// Execute Time
	double time_h = (double)(end_h - start_h) / CLOCKS_PER_SEC;
	printf("N = %d \n", N);
	printf("Execute time: %f seconds \n", time_h);
	
	for (int i = 9999990; i < N; i++)
	{
	printf("The sum of %d element is %d + %d = %d\n", i, h_a[i], h_b[i], h_c[i]);
	}
	
	free(h_a);
	free(h_b);
	free(h_c);
	system("pause");
	return 0;
}
	.text
	.file	"Add_cpu.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z6cpuAddPiS_S_                 # -- Begin function _Z6cpuAddPiS_S_
	.type	_Z6cpuAddPiS_S_,@function
_Z6cpuAddPiS_S_:                        # @_Z6cpuAddPiS_S_
	.cfi_startproc
# %bb.0:
	xorl	%eax, %eax
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	movl	(%rsi,%rax,4), %ecx
	addl	(%rdi,%rax,4), %ecx
	movl	%ecx, (%rdx,%rax,4)
	incq	%rax
	cmpq	$10000000, %rax                 # imm = 0x989680
	jne	.LBB0_1
# %bb.2:
	retq
.Lfunc_end0:
	.size	_Z6cpuAddPiS_S_, .Lfunc_end0-_Z6cpuAddPiS_S_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	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	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$40000000, %edi                 # imm = 0x2625A00
	callq	malloc
	movq	%rax, %rbx
	movl	$40000000, %edi                 # imm = 0x2625A00
	callq	malloc
	movq	%rax, %r14
	movl	$40000000, %edi                 # imm = 0x2625A00
	callq	malloc
	movq	%rax, %r15
	xorl	%eax, %eax
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	leaq	1(%rax), %rcx
	movl	%ecx, (%rbx,%rax,4)
	leal	-1(%rax), %edx
	movl	%edx, (%r14,%rax,4)
	movq	%rcx, %rax
	cmpq	$10000000, %rcx                 # imm = 0x989680
	jne	.LBB1_1
# %bb.2:
	xorl	%r13d, %r13d
	callq	clock
	movq	%rax, %r12
.LBB1_3:                                # =>This Inner Loop Header: Depth=1
	movl	(%r14,%r13,4), %eax
	addl	(%rbx,%r13,4), %eax
	movl	%eax, (%r15,%r13,4)
	incq	%r13
	cmpq	$10000000, %r13                 # imm = 0x989680
	jne	.LBB1_3
# %bb.4:                                # %_Z6cpuAddPiS_S_.exit
	callq	clock
	movq	%rax, %r13
	movl	$.Lstr, %edi
	callq	puts@PLT
	subq	%r12, %r13
	cvtsi2sd	%r13, %xmm0
	divsd	.LCPI1_0(%rip), %xmm0
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movl	$.L.str.1, %edi
	movl	$10000000, %esi                 # imm = 0x989680
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.2, %edi
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	xorl	%r12d, %r12d
.LBB1_5:                                # =>This Inner Loop Header: Depth=1
	movl	39999960(%rbx,%r12,4), %edx
	movl	39999960(%r14,%r12,4), %ecx
	movl	39999960(%r15,%r12,4), %r8d
	leal	9999990(%r12), %esi
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r12
	cmpq	$10, %r12
	jne	.LBB1_5
# %bb.6:
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movl	$.L.str.4, %edi
	callq	system
	xorl	%eax, %eax
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	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	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"N = %d \n"
	.size	.L.str.1, 9

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Execute time: %f seconds \n"
	.size	.L.str.2, 27

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"The sum of %d element is %d + %d = %d\n"
	.size	.L.str.3, 39

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

	.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	"Vector addition on CPU"
	.size	.Lstr, 23

	.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
