#include <stdio.h>

extern "C"

// CUDA allocating function, with naive error handling
double* cuda_allocate(const size_t size) {

    double *dev_matrix;

    if (cudaMalloc((void**)&dev_matrix, size) != cudaSuccess) {
        printf("Error allocating\n");
        return 0;
    }
    else {
        return dev_matrix;
    }

}

// cuda copy with naive error handling (0 = host to device, 1 = device to host)
 void cuda_copy(double* dest, double* src, const size_t size, const size_t dir) {

    // copy inputs to/from device (0 is from device to host)
    if (dir == 0) {
      if (cudaMemcpy(dest, src, size, cudaMemcpyHostToDevice) != cudaSuccess) {
          printf("NOCOPY!\n\n");
      }
    }
    else{
      if (cudaMemcpy(dest, src, size, cudaMemcpyDeviceToHost) != cudaSuccess) {
          printf("NOCOPY!\n\n");
      }
    }
}

// CUDA free with naive error handling
 void cuda_free(double* matrix){
  if (cudaFree(matrix) != cudaSuccess) {
      printf("NOFREE!\n\n");
  }
}

// populate function
void populate(double*  matrix_in, const size_t size){
  for (size_t i = 0; i < size*size; i++) {
    matrix_in[i] = i;
  }
}

void print_matrix(const double* matrix_in, const size_t n) {
    for (size_t i = 0; i < n; i++) {
        for (size_t j = 0; j < n; j++) {
            printf("%0.f ", matrix_in[i*n+j]);
        }
        printf("\n");
    }
}

	.file	"tmpxft_002f7e1a_00000000-6_utils.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Error allocating\n"
	.text
	.globl	cuda_allocate
	.type	cuda_allocate, @function
cuda_allocate:
.LFB2027:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	%rdi, %rsi
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rdi
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L3
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
	jmp	.L2
.L3:
	movq	(%rsp), %rax
.L2:
	movq	8(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L5
	call	__stack_chk_fail@PLT
.L5:
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	cuda_allocate, .-cuda_allocate
	.section	.rodata.str1.1
.LC1:
	.string	"NOCOPY!\n\n"
	.text
	.globl	_Z9cuda_copyPdS_mm
	.type	_Z9cuda_copyPdS_mm, @function
_Z9cuda_copyPdS_mm:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r8
	.cfi_def_cfa_offset 16
	testq	%rcx, %rcx
	jne	.L9
	movl	$1, %ecx
.L14:
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L8
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	popq	%rdx
	.cfi_remember_state
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
.L9:
	.cfi_restore_state
	movl	$2, %ecx
	jmp	.L14
.L8:
	popq	%rax
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z9cuda_copyPdS_mm, .-_Z9cuda_copyPdS_mm
	.section	.rodata.str1.1
.LC2:
	.string	"NOFREE!\n\n"
	.text
	.globl	_Z9cuda_freePd
	.type	_Z9cuda_freePd, @function
_Z9cuda_freePd:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rcx
	.cfi_def_cfa_offset 16
	call	cudaFree@PLT
	testl	%eax, %eax
	je	.L15
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	popq	%rdx
	.cfi_remember_state
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
.L15:
	.cfi_restore_state
	popq	%rax
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z9cuda_freePd, .-_Z9cuda_freePd
	.globl	_Z8populatePdm
	.type	_Z8populatePdm, @function
_Z8populatePdm:
.LFB2030:
	.cfi_startproc
	endbr64
	imulq	%rsi, %rsi
	xorl	%eax, %eax
.L19:
	cmpq	%rsi, %rax
	je	.L21
	cvtsi2sdq	%rax, %xmm0
	movsd	%xmm0, (%rdi,%rax,8)
	incq	%rax
	jmp	.L19
.L21:
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z8populatePdm, .-_Z8populatePdm
	.section	.rodata.str1.1
.LC3:
	.string	"%0.f "
.LC4:
	.string	"\n"
	.text
	.globl	_Z12print_matrixPKdm
	.type	_Z12print_matrixPKdm, @function
_Z12print_matrixPKdm:
.LFB2031:
	.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
	movq	%rdi, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	xorl	%r13d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	pushq	%rdx
	.cfi_def_cfa_offset 64
.L23:
	cmpq	%rbx, %r12
	je	.L28
	leaq	(%r14,%r13,8), %r15
	xorl	%ebp, %ebp
.L24:
	movsd	(%r15,%rbp,8), %xmm0
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	incq	%rbp
	call	__printf_chk@PLT
	cmpq	%rbp, %rbx
	jne	.L24
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	incq	%r12
	addq	%rbx, %r13
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L23
.L28:
	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
.LFE2031:
	.size	_Z12print_matrixPKdm, .-_Z12print_matrixPKdm
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.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
.LFE2057:
	.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 <stdio.h>

extern "C"

// CUDA allocating function, with naive error handling
double* cuda_allocate(const size_t size) {

    double *dev_matrix;

    if (hipMalloc((void**)&dev_matrix, size) != hipSuccess) {
        printf("Error allocating\n");
        return 0;
    }
    else {
        return dev_matrix;
    }

}

// cuda copy with naive error handling (0 = host to device, 1 = device to host)
 void cuda_copy(double* dest, double* src, const size_t size, const size_t dir) {

    // copy inputs to/from device (0 is from device to host)
    if (dir == 0) {
      if (hipMemcpy(dest, src, size, hipMemcpyHostToDevice) != hipSuccess) {
          printf("NOCOPY!\n\n");
      }
    }
    else{
      if (hipMemcpy(dest, src, size, hipMemcpyDeviceToHost) != hipSuccess) {
          printf("NOCOPY!\n\n");
      }
    }
}

// CUDA free with naive error handling
 void cuda_free(double* matrix){
  if (hipFree(matrix) != hipSuccess) {
      printf("NOFREE!\n\n");
  }
}

// populate function
void populate(double*  matrix_in, const size_t size){
  for (size_t i = 0; i < size*size; i++) {
    matrix_in[i] = i;
  }
}

void print_matrix(const double* matrix_in, const size_t n) {
    for (size_t i = 0; i < n; i++) {
        for (size_t j = 0; j < n; j++) {
            printf("%0.f ", matrix_in[i*n+j]);
        }
        printf("\n");
    }
}

	.text
	.file	"utils.hip"
	.globl	cuda_allocate                   # -- Begin function cuda_allocate
	.type	cuda_allocate,@function
cuda_allocate:                          # @cuda_allocate
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdi, %rsi
	movq	%rsp, %rdi
	callq	hipMalloc
	testl	%eax, %eax
	je	.LBB0_2
# %bb.1:
	movl	$.Lstr, %edi
	callq	puts@PLT
	xorl	%eax, %eax
	jmp	.LBB0_3
.LBB0_2:
	movq	(%rsp), %rax
.LBB0_3:
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	cuda_allocate, .Lfunc_end0-cuda_allocate
	.cfi_endproc
                                        # -- End function
	.globl	_Z9cuda_copyPdS_mm              # -- Begin function _Z9cuda_copyPdS_mm
	.type	_Z9cuda_copyPdS_mm,@function
_Z9cuda_copyPdS_mm:                     # @_Z9cuda_copyPdS_mm
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	testq	%rcx, %rcx
	je	.LBB1_1
# %bb.2:
	movl	$2, %ecx
	jmp	.LBB1_3
.LBB1_1:
	movl	$1, %ecx
.LBB1_3:
	callq	hipMemcpy
	testl	%eax, %eax
	je	.LBB1_4
# %bb.5:                                # %.sink.split
	movl	$.Lstr.2, %edi
	popq	%rax
	.cfi_def_cfa_offset 8
	jmp	puts@PLT                        # TAILCALL
.LBB1_4:
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z9cuda_copyPdS_mm, .Lfunc_end1-_Z9cuda_copyPdS_mm
	.cfi_endproc
                                        # -- End function
	.globl	_Z9cuda_freePd                  # -- Begin function _Z9cuda_freePd
	.type	_Z9cuda_freePd,@function
_Z9cuda_freePd:                         # @_Z9cuda_freePd
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	hipFree
	testl	%eax, %eax
	je	.LBB2_1
# %bb.2:
	movl	$.Lstr.3, %edi
	popq	%rax
	.cfi_def_cfa_offset 8
	jmp	puts@PLT                        # TAILCALL
.LBB2_1:
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z9cuda_freePd, .Lfunc_end2-_Z9cuda_freePd
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z8populatePdm
.LCPI3_0:
	.long	1127219200                      # 0x43300000
	.long	1160773632                      # 0x45300000
	.long	0                               # 0x0
	.long	0                               # 0x0
.LCPI3_1:
	.quad	0x4330000000000000              # double 4503599627370496
	.quad	0x4530000000000000              # double 1.9342813113834067E+25
	.text
	.globl	_Z8populatePdm
	.type	_Z8populatePdm,@function
_Z8populatePdm:                         # @_Z8populatePdm
	.cfi_startproc
# %bb.0:
	imulq	%rsi, %rsi
	testq	%rsi, %rsi
	je	.LBB3_3
# %bb.1:                                # %.lr.ph.preheader
	xorl	%eax, %eax
	movdqa	.LCPI3_0(%rip), %xmm0           # xmm0 = [1127219200,1160773632,0,0]
	movapd	.LCPI3_1(%rip), %xmm1           # xmm1 = [4.503599627370496E+15,1.9342813113834067E+25]
.LBB3_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%rax, %xmm2
	punpckldq	%xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
	subpd	%xmm1, %xmm2
	movapd	%xmm2, %xmm3
	unpckhpd	%xmm2, %xmm3                    # xmm3 = xmm3[1],xmm2[1]
	addsd	%xmm2, %xmm3
	movsd	%xmm3, (%rdi,%rax,8)
	incq	%rax
	cmpq	%rax, %rsi
	jne	.LBB3_2
.LBB3_3:                                # %._crit_edge
	retq
.Lfunc_end3:
	.size	_Z8populatePdm, .Lfunc_end3-_Z8populatePdm
	.cfi_endproc
                                        # -- End function
	.globl	_Z12print_matrixPKdm            # -- Begin function _Z12print_matrixPKdm
	.type	_Z12print_matrixPKdm,@function
_Z12print_matrixPKdm:                   # @_Z12print_matrixPKdm
	.cfi_startproc
# %bb.0:
	testq	%rsi, %rsi
	je	.LBB4_6
# %bb.1:                                # %.preheader.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	%rsi, %rbx
	movq	%rdi, %r14
	leaq	(,%rsi,8), %r15
	xorl	%r12d, %r12d
.LBB4_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_3 Depth 2
	xorl	%r13d, %r13d
.LBB4_3:                                #   Parent Loop BB4_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movsd	(%r14,%r13,8), %xmm0            # xmm0 = mem[0],zero
	movl	$.L.str.3, %edi
	movb	$1, %al
	callq	printf
	incq	%r13
	cmpq	%r13, %rbx
	jne	.LBB4_3
# %bb.4:                                #   in Loop: Header=BB4_2 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r12
	addq	%r15, %r14
	cmpq	%rbx, %r12
	jne	.LBB4_2
# %bb.5:
	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
.LBB4_6:                                # %._crit_edge
	retq
.Lfunc_end4:
	.size	_Z12print_matrixPKdm, .Lfunc_end4-_Z12print_matrixPKdm
	.cfi_endproc
                                        # -- End function
	.type	.L.str.3,@object                # @.str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.3:
	.asciz	"%0.f "
	.size	.L.str.3, 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	"Error allocating"
	.size	.Lstr, 17

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"NOCOPY!\n"
	.size	.Lstr.2, 9

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"NOFREE!\n"
	.size	.Lstr.3, 9

	.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
