// This is a CUDA program that does the following:
//
// 1. On the host, fill the A and B arrays with random numbers
// 2. On the host, print the initial values of the A and B arrays
// 3. Copy the A and B arrays from the host to the device
// 4. On the device, add the A and B vectors and store the result in C
// 5. Copy the C array from the device to the host
// 6. On the host, print the result
//
// Author: Aaron Weeden, Shodor, 2016

// Import library so we can call printf()
#include <stdio.h>

// Import library so we can call exit(), malloc(), free(), random(), etc.
#include <stdlib.h>

// Import library so we can call time()
#include <time.h>

// Define the number of numbers in each array
#define NUM_COUNT 10

// Define the number of bytes in each array
#define BYTE_COUNT ((NUM_COUNT) * sizeof(int))

// Declare functions that will be defined later
void TryMalloc(void * const err);

// Start the program
int main()
{
  // Declare variables for the host and device arrays
  int * hostA;
  int * hostB;
  int * hostC;
  int * deviceA;
  int * deviceB;
  int * deviceC;

  // Allocate memory for the host arrays

  // Allocate memory for the device arrays

  // Initialize the random number generator
  srandom(time(NULL));

  // On the host, fill the A and B arrays with random numbers
  printf("Expected Result:\n");
  for (int i = 0; i < NUM_COUNT; i++)
  {
    hostA[i] = 100 * random() / RAND_MAX;
    hostB[i] = 100 * random() / RAND_MAX;
    printf("\thostC[%d] should be %d + %d\n", i, hostA[i], hostB[i]);
  }

  // Copy the A and B arrays from the host to the device

  // On the device, add the A and B vectors and store the result in C

  // Copy the C array from the device to the host

  // On the host, print the result
  printf("Result:\n");
  for (int i = 0; i < NUM_COUNT; i++)
  {
    printf("\thostC[%d] = %d\n", i, hostC[i]);
  }

  // De-allocate memory for the device arrays

  // De-allocate memory for the host arrays

  return 0;
}

// Define a function to check whether a malloc() call was successful
void TryMalloc(void * const err)
{
  if (err == NULL)
  {
    fprintf(stderr, "malloc error\n");
    exit(EXIT_FAILURE);
  }
}


	.file	"tmpxft_0031b5fc_00000000-6_add-vectors.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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Expected Result:\n"
.LC1:
	.string	"\thostC[%d] should be %d + %d\n"
.LC2:
	.string	"Result:\n"
.LC3:
	.string	"\thostC[%d] = %d\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	xorl	%edi, %edi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	leaq	.LC1(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rcx
	.cfi_def_cfa_offset 48
	call	time@PLT
	movl	%eax, %edi
	call	srandom@PLT
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L3:
	call	random@PLT
	movl	$2147483647, %r13d
	imulq	$100, %rax, %rax
	cqto
	idivq	%r13
	movl	%eax, 0(,%rbx,4)
	movl	%eax, %r12d
	call	random@PLT
	movl	%r12d, %ecx
	movq	%rbp, %rsi
	movl	$2, %edi
	imulq	$100, %rax, %rax
	cqto
	idivq	%r13
	movl	%ebx, %edx
	movl	%eax, 0(,%rbx,4)
	movl	%eax, %r8d
	incq	%rbx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	cmpq	$10, %rbx
	jne	.L3
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%ebx, %ebx
	call	__printf_chk@PLT
	leaq	.LC3(%rip), %rbp
.L4:
	movl	0(,%rbx,4), %ecx
	movl	%ebx, %edx
	movq	%rbp, %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	incq	%rbx
	call	__printf_chk@PLT
	cmpq	$10, %rbx
	jne	.L4
	popq	%rdx
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	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
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC4:
	.string	"malloc error\n"
	.text
	.globl	_Z9TryMallocPv
	.type	_Z9TryMallocPv, @function
_Z9TryMallocPv:
.LFB2028:
	.cfi_startproc
	endbr64
	testq	%rdi, %rdi
	jne	.L9
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC4(%rip), %rdx
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L9:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z9TryMallocPv, .-_Z9TryMallocPv
	.section	.text.startup
	.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
	.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:

// This is a CUDA program that does the following:
//
// 1. On the host, fill the A and B arrays with random numbers
// 2. On the host, print the initial values of the A and B arrays
// 3. Copy the A and B arrays from the host to the device
// 4. On the device, add the A and B vectors and store the result in C
// 5. Copy the C array from the device to the host
// 6. On the host, print the result
//
// Author: Aaron Weeden, Shodor, 2016

// Import library so we can call printf()

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

// Import library so we can call exit(), malloc(), free(), random(), etc.
#include <stdlib.h>

// Import library so we can call time()
#include <time.h>

// Define the number of numbers in each array
#define NUM_COUNT 10

// Define the number of bytes in each array
#define BYTE_COUNT ((NUM_COUNT) * sizeof(int))

// Declare functions that will be defined later
void TryMalloc(void * const err);

// Start the program
int main()
{
  // Declare variables for the host and device arrays
  int * hostA;
  int * hostB;
  int * hostC;
  int * deviceA;
  int * deviceB;
  int * deviceC;

  // Allocate memory for the host arrays

  // Allocate memory for the device arrays

  // Initialize the random number generator
  srandom(time(NULL));

  // On the host, fill the A and B arrays with random numbers
  printf("Expected Result:\n");
  for (int i = 0; i < NUM_COUNT; i++)
  {
    hostA[i] = 100 * random() / RAND_MAX;
    hostB[i] = 100 * random() / RAND_MAX;
    printf("\thostC[%d] should be %d + %d\n", i, hostA[i], hostB[i]);
  }

  // Copy the A and B arrays from the host to the device

  // On the device, add the A and B vectors and store the result in C

  // Copy the C array from the device to the host

  // On the host, print the result
  printf("Result:\n");
  for (int i = 0; i < NUM_COUNT; i++)
  {
    printf("\thostC[%d] = %d\n", i, hostC[i]);
  }

  // De-allocate memory for the device arrays

  // De-allocate memory for the host arrays

  return 0;
}

// Define a function to check whether a malloc() call was successful
void TryMalloc(void * const err)
{
  if (err == NULL)
  {
    fprintf(stderr, "malloc error\n");
    exit(EXIT_FAILURE);
  }
}


	.text
	.file	"add-vectors.hip"
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srandom
	movl	$.Lstr, %edi
	callq	puts@PLT
	callq	random
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z9TryMallocPv                  # -- Begin function _Z9TryMallocPv
	.type	_Z9TryMallocPv,@function
_Z9TryMallocPv:                         # @_Z9TryMallocPv
	.cfi_startproc
# %bb.0:
	testq	%rdi, %rdi
	je	.LBB1_2
# %bb.1:
	retq
.LBB1_2:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rcx
	movl	$.L.str.4, %edi
	movl	$13, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z9TryMallocPv, .Lfunc_end1-_Z9TryMallocPv
	.cfi_endproc
                                        # -- End function
	.type	.L.str.4,@object                # @.str.4
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.4:
	.asciz	"malloc error\n"
	.size	.L.str.4, 14

	.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	"Expected Result:"
	.size	.Lstr, 17

	.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
