#include <cuda.h>

extern "C" {
  int cudaPyHostToDevice(void*, void*, size_t, size_t);
  int cudaPyDeviceToHost(void*, void*, size_t, size_t);
  void* cudaPyAllocArray(size_t, size_t);
  int cudaPyFree(void*);
}


int cudaPyHostToDevice(void* dst, void* src, size_t N, size_t tsize) {
  return cudaMemcpy(dst, src, N * tsize, cudaMemcpyHostToDevice);
}


int cudaPyDeviceToHost(void* dst, void* src, size_t N, size_t tsize) {
  return cudaMemcpy(dst, src, N * tsize, cudaMemcpyDeviceToHost);
}


void* cudaPyAllocArray(size_t N, size_t tsize) {
  void* arr;
  size_t arraySize = 2 * sizeof(size_t) + N * tsize;

  if (cudaMalloc(&arr, arraySize))
    return NULL;
  cudaMemset(&arr, 0, arraySize);

  size_t header[2] = {tsize, N};
  cudaMemcpy(arr, &header, sizeof(size_t) * sizeof(header), cudaMemcpyHostToDevice);

  return (void*)((size_t*)arr + 2);
}


int cudaPyFree(void* input) {
  return cudaFree((void*)((size_t*)input - 2));
}

	.file	"tmpxft_00389257_00000000-6_libCudaPy.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2014:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2014:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	cudaPyHostToDevice
	.type	cudaPyHostToDevice, @function
cudaPyHostToDevice:
.LFB2008:
	.cfi_startproc
	endbr64
	imulq	%rcx, %rdx
	movl	$1, %ecx
	jmp	cudaMemcpy@PLT
	.cfi_endproc
.LFE2008:
	.size	cudaPyHostToDevice, .-cudaPyHostToDevice
	.globl	cudaPyDeviceToHost
	.type	cudaPyDeviceToHost, @function
cudaPyDeviceToHost:
.LFB2009:
	.cfi_startproc
	endbr64
	imulq	%rcx, %rdx
	movl	$2, %ecx
	jmp	cudaMemcpy@PLT
	.cfi_endproc
.LFE2009:
	.size	cudaPyDeviceToHost, .-cudaPyDeviceToHost
	.globl	cudaPyAllocArray
	.type	cudaPyAllocArray, @function
cudaPyAllocArray:
.LFB2010:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	imulq	%rsi, %r12
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rdi, %rbx
	addq	$16, %r12
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	movq	%r12, %rsi
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %r13
	movq	%r13, %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edx
	xorl	%eax, %eax
	testl	%edx, %edx
	jne	.L4
	movq	%r12, %rdx
	xorl	%esi, %esi
	movq	%r13, %rdi
	call	cudaMemset@PLT
	movq	(%rsp), %rdi
	leaq	8(%rsp), %rsi
	movl	$1, %ecx
	movl	$128, %edx
	movq	%rbp, 8(%rsp)
	movq	%rbx, 16(%rsp)
	call	cudaMemcpy@PLT
	movq	(%rsp), %rax
	addq	$16, %rax
.L4:
	movq	24(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L6
	call	__stack_chk_fail@PLT
.L6:
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	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
.LFE2010:
	.size	cudaPyAllocArray, .-cudaPyAllocArray
	.globl	cudaPyFree
	.type	cudaPyFree, @function
cudaPyFree:
.LFB2011:
	.cfi_startproc
	endbr64
	subq	$16, %rdi
	jmp	cudaFree@PLT
	.cfi_endproc
.LFE2011:
	.size	cudaPyFree, .-cudaPyFree
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2037:
	.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
.LFE2037:
	.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>

extern "C" {
  int cudaPyHostToDevice(void*, void*, size_t, size_t);
  int cudaPyDeviceToHost(void*, void*, size_t, size_t);
  void* cudaPyAllocArray(size_t, size_t);
  int cudaPyFree(void*);
}


int cudaPyHostToDevice(void* dst, void* src, size_t N, size_t tsize) {
  return hipMemcpy(dst, src, N * tsize, hipMemcpyHostToDevice);
}


int cudaPyDeviceToHost(void* dst, void* src, size_t N, size_t tsize) {
  return hipMemcpy(dst, src, N * tsize, hipMemcpyDeviceToHost);
}


void* cudaPyAllocArray(size_t N, size_t tsize) {
  void* arr;
  size_t arraySize = 2 * sizeof(size_t) + N * tsize;

  if (hipMalloc(&arr, arraySize))
    return NULL;
  hipMemset(&arr, 0, arraySize);

  size_t header[2] = {tsize, N};
  hipMemcpy(arr, &header, sizeof(size_t) * sizeof(header), hipMemcpyHostToDevice);

  return (void*)((size_t*)arr + 2);
}


int cudaPyFree(void* input) {
  return hipFree((void*)((size_t*)input - 2));
}

	.text
	.file	"libCudaPy.hip"
	.globl	cudaPyHostToDevice              # -- Begin function cudaPyHostToDevice
	.type	cudaPyHostToDevice,@function
cudaPyHostToDevice:                     # @cudaPyHostToDevice
	.cfi_startproc
# %bb.0:
	imulq	%rcx, %rdx
	movl	$1, %ecx
	jmp	hipMemcpy                       # TAILCALL
.Lfunc_end0:
	.size	cudaPyHostToDevice, .Lfunc_end0-cudaPyHostToDevice
	.cfi_endproc
                                        # -- End function
	.globl	cudaPyDeviceToHost              # -- Begin function cudaPyDeviceToHost
	.type	cudaPyDeviceToHost,@function
cudaPyDeviceToHost:                     # @cudaPyDeviceToHost
	.cfi_startproc
# %bb.0:
	imulq	%rcx, %rdx
	movl	$2, %ecx
	jmp	hipMemcpy                       # TAILCALL
.Lfunc_end1:
	.size	cudaPyDeviceToHost, .Lfunc_end1-cudaPyDeviceToHost
	.cfi_endproc
                                        # -- End function
	.globl	cudaPyAllocArray                # -- Begin function cudaPyAllocArray
	.type	cudaPyAllocArray,@function
cudaPyAllocArray:                       # @cudaPyAllocArray
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %r14
	movq	%rdi, %rbx
	movq	%rsi, %r15
	imulq	%rdi, %r15
	addq	$16, %r15
	leaq	8(%rsp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	je	.LBB2_2
# %bb.1:
	xorl	%eax, %eax
	jmp	.LBB2_3
.LBB2_2:
	leaq	8(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	movq	%r15, %rdx
	callq	hipMemset
	leaq	16(%rsp), %rsi
	movq	%r14, (%rsi)
	movq	%rbx, 8(%rsi)
	movq	(%r12), %rdi
	movl	$128, %edx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r12), %rax
	addq	$16, %rax
.LBB2_3:
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	cudaPyAllocArray, .Lfunc_end2-cudaPyAllocArray
	.cfi_endproc
                                        # -- End function
	.globl	cudaPyFree                      # -- Begin function cudaPyFree
	.type	cudaPyFree,@function
cudaPyFree:                             # @cudaPyFree
	.cfi_startproc
# %bb.0:
	addq	$-16, %rdi
	jmp	hipFree                         # TAILCALL
.Lfunc_end3:
	.size	cudaPyFree, .Lfunc_end3-cudaPyFree
	.cfi_endproc
                                        # -- End function
	.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
