/* Using a dataset consisting of three arrays: A, B and C, the operation
Cx = Ax + Bx is performed on each element.

This code is executed on the host using the CPU. The result compute time is
displayed at the end of the computing.*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void host_add(int *a, int *b, int *c, int size){
    /* Add two numbers and put them in an array of C (Host computation)
        a, b and c must be arrays */
    for(int i=0; i<size; i++)
        c[i] = a[i] + b[i];
}

void fill_array(int *data, int size){
    /* Fill an array with the index */
    for(int i=0; i<size; i++)
        data[i] = i;
}

void print_output(int *a, int *b, int *c, int size){
    for(int i=0; i<size; i++)
        printf("\n %d + %d = %d", a[i], b[i], c[i]);
}
int main(int argc, char *argv[]) {
    int sizeOfArray = 512;
    if(argc > 1)
        sizeOfArray = atoi(argv[1]);

    int *a, *b, *c;
    int size = sizeOfArray * sizeof(int);
    struct timespec start, finish;
    clock_gettime(CLOCK_REALTIME, &start);

    /* Alloc space for host copies of a, b and c. Setup with input values */
    a = (int*)malloc(size); fill_array(a, sizeOfArray);
    b = (int*)malloc(size); fill_array(b, sizeOfArray);
    c = (int*)malloc(size);
    host_add(a, b, c, sizeOfArray);

    /* Get compute time */
    clock_gettime(CLOCK_REALTIME, &finish);
    long ns = finish.tv_nsec - start.tv_nsec;

    /* Print output */
    print_output(a, b, c, sizeOfArray);
    printf("\n\nTime to compute: %ld ns \n\n", ns);

    free(a);
    free(b);
    free(c);
    return 0;
}
	.file	"tmpxft_0024a1ec_00000000-6_arr_add_host.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2033:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2033:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z8host_addPiS_S_i
	.type	_Z8host_addPiS_S_i, @function
_Z8host_addPiS_S_i:
.LFB2027:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L3:
	cmpl	%eax, %ecx
	jle	.L6
	movl	(%rsi,%rax,4), %r8d
	addl	(%rdi,%rax,4), %r8d
	movl	%r8d, (%rdx,%rax,4)
	incq	%rax
	jmp	.L3
.L6:
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z8host_addPiS_S_i, .-_Z8host_addPiS_S_i
	.globl	_Z10fill_arrayPii
	.type	_Z10fill_arrayPii, @function
_Z10fill_arrayPii:
.LFB2028:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L8:
	cmpl	%eax, %esi
	jle	.L10
	movl	%eax, (%rdi,%rax,4)
	incq	%rax
	jmp	.L8
.L10:
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z10fill_arrayPii, .-_Z10fill_arrayPii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"\n %d + %d = %d"
	.text
	.globl	_Z12print_outputPiS_S_i
	.type	_Z12print_outputPiS_S_i, @function
_Z12print_outputPiS_S_i:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC0(%rip), %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rdx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rsi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	pushq	%r8
	.cfi_def_cfa_offset 64
.L12:
	cmpl	%ebx, %ebp
	jle	.L15
	movl	0(%r13,%rbx,4), %ecx
	movl	(%r12,%rbx,4), %edx
	movq	%r15, %rsi
	xorl	%eax, %eax
	movl	(%r14,%rbx,4), %r8d
	movl	$2, %edi
	incq	%rbx
	call	__printf_chk@PLT
	jmp	.L12
.L15:
	popq	%rax
	.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
.LFE2029:
	.size	_Z12print_outputPiS_S_i, .-_Z12print_outputPiS_S_i
	.section	.rodata.str1.1
.LC1:
	.string	"\n\nTime to compute: %ld ns \n\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	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
	movl	$512, %ebx
	subq	$48, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	decl	%edi
	jle	.L17
	movq	8(%rsi), %rdi
	call	atoi@PLT
	movl	%eax, %ebx
.L17:
	leal	0(,%rbx,4), %r13d
	leaq	8(%rsp), %rsi
	xorl	%edi, %edi
	call	clock_gettime@PLT
	movslq	%r13d, %r13
	movq	%r13, %rdi
	call	malloc@PLT
	movl	%ebx, %esi
	movq	%rax, %rdi
	movq	%rax, %r12
	call	_Z10fill_arrayPii
	movq	%r13, %rdi
	call	malloc@PLT
	movl	%ebx, %esi
	movq	%rax, %rdi
	movq	%rax, %rbp
	call	_Z10fill_arrayPii
	movq	%r13, %rdi
	call	malloc@PLT
	movl	%ebx, %ecx
	movq	%rbp, %rsi
	movq	%r12, %rdi
	movq	%rax, %rdx
	movq	%rax, %r13
	call	_Z8host_addPiS_S_i
	xorl	%edi, %edi
	leaq	24(%rsp), %rsi
	call	clock_gettime@PLT
	movl	%ebx, %ecx
	movq	32(%rsp), %r14
	movq	%r13, %rdx
	subq	16(%rsp), %r14
	movq	%rbp, %rsi
	movq	%r12, %rdi
	call	_Z12print_outputPiS_S_i
	movq	%r14, %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC1(%rip), %rsi
	call	__printf_chk@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L18
	call	__stack_chk_fail@PLT
.L18:
	addq	$48, %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
.LFE2030:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2056:
	.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
.LFE2056:
	.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:

/* Using a dataset consisting of three arrays: A, B and C, the operation
Cx = Ax + Bx is performed on each element.

This code is executed on the host using the CPU. The result compute time is
displayed at the end of the computing.*/


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void host_add(int *a, int *b, int *c, int size){
    /* Add two numbers and put them in an array of C (Host computation)
        a, b and c must be arrays */
    for(int i=0; i<size; i++)
        c[i] = a[i] + b[i];
}

void fill_array(int *data, int size){
    /* Fill an array with the index */
    for(int i=0; i<size; i++)
        data[i] = i;
}

void print_output(int *a, int *b, int *c, int size){
    for(int i=0; i<size; i++)
        printf("\n %d + %d = %d", a[i], b[i], c[i]);
}
int main(int argc, char *argv[]) {
    int sizeOfArray = 512;
    if(argc > 1)
        sizeOfArray = atoi(argv[1]);

    int *a, *b, *c;
    int size = sizeOfArray * sizeof(int);
    struct timespec start, finish;
    clock_gettime(CLOCK_REALTIME, &start);

    /* Alloc space for host copies of a, b and c. Setup with input values */
    a = (int*)malloc(size); fill_array(a, sizeOfArray);
    b = (int*)malloc(size); fill_array(b, sizeOfArray);
    c = (int*)malloc(size);
    host_add(a, b, c, sizeOfArray);

    /* Get compute time */
    clock_gettime(CLOCK_REALTIME, &finish);
    long ns = finish.tv_nsec - start.tv_nsec;

    /* Print output */
    print_output(a, b, c, sizeOfArray);
    printf("\n\nTime to compute: %ld ns \n\n", ns);

    free(a);
    free(b);
    free(c);
    return 0;
}
	.text
	.file	"arr_add_host.hip"
	.globl	_Z8host_addPiS_S_i              # -- Begin function _Z8host_addPiS_S_i
	.type	_Z8host_addPiS_S_i,@function
_Z8host_addPiS_S_i:                     # @_Z8host_addPiS_S_i
	.cfi_startproc
# %bb.0:
	testl	%ecx, %ecx
	jle	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%ecx, %eax
	xorl	%ecx, %ecx
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rsi,%rcx,4), %r8d
	addl	(%rdi,%rcx,4), %r8d
	movl	%r8d, (%rdx,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z8host_addPiS_S_i, .Lfunc_end0-_Z8host_addPiS_S_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z10fill_arrayPii               # -- Begin function _Z10fill_arrayPii
	.type	_Z10fill_arrayPii,@function
_Z10fill_arrayPii:                      # @_Z10fill_arrayPii
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB1_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %eax
	xorl	%ecx, %ecx
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%ecx, (%rdi,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB1_2
.LBB1_3:                                # %._crit_edge
	retq
.Lfunc_end1:
	.size	_Z10fill_arrayPii, .Lfunc_end1-_Z10fill_arrayPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z12print_outputPiS_S_i         # -- Begin function _Z12print_outputPiS_S_i
	.type	_Z12print_outputPiS_S_i,@function
_Z12print_outputPiS_S_i:                # @_Z12print_outputPiS_S_i
	.cfi_startproc
# %bb.0:
	testl	%ecx, %ecx
	jle	.LBB2_4
# %bb.1:                                # %.lr.ph.preheader
	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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdx, %rbx
	movq	%rsi, %r14
	movq	%rdi, %r15
	movl	%ecx, %r12d
	xorl	%r13d, %r13d
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%r15,%r13,4), %esi
	movl	(%r14,%r13,4), %edx
	movl	(%rbx,%r13,4), %ecx
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r13
	cmpq	%r13, %r12
	jne	.LBB2_2
# %bb.3:
	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
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r13
	.cfi_restore %r14
	.cfi_restore %r15
.LBB2_4:                                # %._crit_edge
	retq
.Lfunc_end2:
	.size	_Z12print_outputPiS_S_i, .Lfunc_end2-_Z12print_outputPiS_S_i
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	.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	$512, %r12d                     # imm = 0x200
	cmpl	$2, %edi
	jl	.LBB3_2
# %bb.1:
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %r12d
.LBB3_2:
	leal	(,%r12,4), %ebx
	leaq	8(%rsp), %rsi
	xorl	%edi, %edi
	callq	clock_gettime
	movslq	%ebx, %r15
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %rbx
	testl	%r12d, %r12d
	jle	.LBB3_3
# %bb.4:                                # %.lr.ph.preheader.i
	movl	%r12d, %ebp
	xorl	%eax, %eax
.LBB3_5:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movl	%eax, (%rbx,%rax,4)
	incq	%rax
	cmpq	%rax, %rbp
	jne	.LBB3_5
# %bb.6:                                # %_Z10fill_arrayPii.exit
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r14
	xorl	%eax, %eax
.LBB3_7:                                # %.lr.ph.i23
                                        # =>This Inner Loop Header: Depth=1
	movl	%eax, (%r14,%rax,4)
	incq	%rax
	cmpq	%rax, %rbp
	jne	.LBB3_7
# %bb.8:                                # %_Z10fill_arrayPii.exit27
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r15
	xorl	%eax, %eax
.LBB3_9:                                # %.lr.ph.i30
                                        # =>This Inner Loop Header: Depth=1
	movl	(%r14,%rax,4), %ecx
	addl	(%rbx,%rax,4), %ecx
	movl	%ecx, (%r15,%rax,4)
	incq	%rax
	cmpq	%rax, %rbp
	jne	.LBB3_9
# %bb.10:                               # %_Z8host_addPiS_S_i.exit
	leaq	24(%rsp), %r13
	xorl	%edi, %edi
	movq	%r13, %rsi
	callq	clock_gettime
	movq	8(%r13), %r13
	movq	16(%rsp), %rax
	testl	%r12d, %r12d
	jle	.LBB3_14
# %bb.11:                               # %.lr.ph.preheader.i34
	movq	%rax, (%rsp)                    # 8-byte Spill
	xorl	%r12d, %r12d
.LBB3_12:                               # %.lr.ph.i36
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%r12,4), %esi
	movl	(%r14,%r12,4), %edx
	movl	(%r15,%r12,4), %ecx
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r12
	cmpq	%r12, %rbp
	jne	.LBB3_12
# %bb.13:
	movq	(%rsp), %rax                    # 8-byte Reload
	jmp	.LBB3_14
.LBB3_3:                                # %_Z8host_addPiS_S_i.exit.thread
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r14
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r15
	leaq	24(%rsp), %r12
	xorl	%edi, %edi
	movq	%r12, %rsi
	callq	clock_gettime
	movq	8(%r12), %r13
	movq	16(%rsp), %rax
.LBB3_14:                               # %_Z12print_outputPiS_S_i.exit
	subq	%rax, %r13
	movl	$.L.str.1, %edi
	movq	%r13, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$40, %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_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"\n %d + %d = %d"
	.size	.L.str, 15

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"\n\nTime to compute: %ld ns \n\n"
	.size	.L.str.1, 29

	.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
