#include <stdio.h>
#include <stdlib.h>

#define HOST_TO_DEVICE 0
#define DEVICE_TO_HOST 1

// Print the usage of the program
inline void usage(char *program)
{     
        fprintf(stderr, "usage: %s memsize iters [-r]\n", program);
        fprintf(stderr, "    memsize : memory transferred in bytes (>0)\n");
        fprintf(stderr, "    iters   : number of iterations (>0)\n");
        fprintf(stderr, "    -r      : re-allocate memory for each iteration\n");
}

// Allocate 'size' worth of bytes to host memory '*ptr'
inline void alloc_host_mem(void **ptr, int size, bool pinned)
{
        if (pinned) {
                cudaMallocHost(ptr, size);
        } else {
                *ptr = malloc(size);
        }
}

// Free host memory pointed by 'ptr'
inline void free_host_mem(void *ptr, bool pinned)
{
        if (pinned) {
                cudaFreeHost(ptr);
        } else {
                free(ptr);
        }
}

// Profile memory copy performance between GPU and CPU
// For each iteration, we copy 'size' worth of bytes in a direction 
// size        :   memory copy size for each iteration
// iters       :   number of iterations
// direction   :   HOST_TO_DEVICE / DEVICE_TO_HOST
// pinned      :   whether enable pinned memory allocation at host
// reallocate  :   whether re-allocate memory for each time
// This function prints the average throughput and transfer time result    
void profile_memcpy(int size, int iters, int direction, bool pinned, bool reallocate)
{
        void *h = NULL, *d = NULL;      // host and device memory
        cudaEvent_t start, stop;
        float time, avg_time, total_time, throughput;
        int i;
        cudaError_t result;

        // Allocate host and device memory only one time
        if (!reallocate) {
                alloc_host_mem(&h, size, pinned);
                cudaMalloc(&d, size);
        }

        cudaEventCreate(&start);
        cudaEventCreate(&stop);
        total_time = 0;

        for (i = 0; i < iters; i++) {
                // Re-allocate host and device memory for each time
                if (reallocate) {                        
                        alloc_host_mem(&h, size, pinned);
                        cudaMalloc((void**)&d, size);               
                }

                cudaEventRecord(start, 0);
                if (direction == HOST_TO_DEVICE) { 
                        result = cudaMemcpy(d, h, size, cudaMemcpyHostToDevice);
                } else {
                        result = cudaMemcpy(h, d, size, cudaMemcpyDeviceToHost);
                }
                cudaEventRecord(stop, 0);
                cudaEventSynchronize(stop);
                cudaEventElapsedTime(&time, start, stop);
                total_time += time;
                
                if (result != cudaSuccess) {
                        fprintf(stderr, "Error: memory copy\n");
                }

                if (reallocate) {
                        free_host_mem(h, pinned);
                        cudaFree(d);
                }
        }

        // Calculate host to device information
        avg_time = total_time / iters / 1000;  // time in second
        throughput = (float)size / avg_time / 1000000000; // throughput in GB/s

        if (direction == HOST_TO_DEVICE) {
                printf("  Host to Device Time: %.6f s\n", avg_time);
                printf("  Host to Device Throughput: %.6f GB/s\n", throughput);
        } else {
                printf("  Device to Host Time: %.6f s\n", avg_time);
                printf("  Device to Host Throughput: %.6f GB/s\n", throughput);
        }

        cudaEventDestroy(start);
        cudaEventDestroy(stop);

        if (!reallocate) {
                free_host_mem(h, pinned);
                cudaFree(d);
        }
}

int main(int argc, char **argv)
{
        int size, iters;
        bool reallocate = false;        // By default, we don't re-allocate memory for each test

        if (!(argc == 3 || (argc == 4 && strcmp(argv[3], "-r") == 0))) {
                usage(argv[0]);
                return EXIT_FAILURE;
        }

        size = atoi(argv[1]);
        iters = atoi(argv[2]);

        if (size <= 0 || iters <= 0) {
                usage(argv[0]);
                return EXIT_FAILURE;
        }

        if (argc == 4) {
                reallocate = true;
        }

        // warm up
        cudaFree(0);

        // Profile memory copy
        printf("Transfer size (MB): %f\n\n", (float)size / (1024 * 1024));
        
        printf("Pageable transfers\n");
        profile_memcpy(size, iters, HOST_TO_DEVICE, false, reallocate);
        profile_memcpy(size, iters, DEVICE_TO_HOST, false, reallocate);

        printf("\n");
        
        printf("Pinned transfers\n");
        profile_memcpy(size, iters, HOST_TO_DEVICE, true, reallocate);
        profile_memcpy(size, iters, DEVICE_TO_HOST, true, reallocate);

        return EXIT_SUCCESS;
}
	.file	"tmpxft_002b9d13_00000000-6_main.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._Z5usagePc.str1.1,"aMS",@progbits,1
.LC0:
	.string	"usage: %s memsize iters [-r]\n"
.LC1:
	.string	"    memsize : memory transferred in bytes (>0)\n"
.LC2:
	.string	"    iters   : number of iterations (>0)\n"
.LC3:
	.string	"    -r      : re-allocate memory for each iteration\n"
	.section	.text._Z5usagePc,"axG",@progbits,_Z5usagePc,comdat
	.weak	_Z5usagePc
	.type	_Z5usagePc, @function
_Z5usagePc:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdi, %rcx
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC0(%rip), %rdx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	stderr(%rip), %rdi
	leaq	.LC1(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movq	stderr(%rip), %rdi
	leaq	.LC2(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movq	stderr(%rip), %rdi
	leaq	.LC3(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	popq	%rcx
	.cfi_def_cfa_offset 8
	jmp	__fprintf_chk@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z5usagePc, .-_Z5usagePc
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC5:
	.string	"Error: memory copy\n"
.LC8:
	.string	"  Host to Device Time: %.6f s\n"
.LC9:
	.string	"  Host to Device Throughput: %.6f GB/s\n"
.LC10:
	.string	"  Device to Host Time: %.6f s\n"
.LC11:
	.string	"  Device to Host Throughput: %.6f GB/s\n"
	.text
	.globl	_Z14profile_memcpyiiibb
	.type	_Z14profile_memcpyiiibb, @function
_Z14profile_memcpyiiibb:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	%edi, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movl	%r8d, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movl	%ecx, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%esi, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%r8d, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%ecx, %ebx
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movl	%edx, 24(%rsp)
	xorl	%edx, %edx
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	movq	%rdx, 40(%rsp)
	movq	%rdx, 48(%rsp)
	testb	%r8b, %r8b
	jne	.L5
	movslq	%edi, %rax
	movq	%rax, 8(%rsp)
	testb	%cl, %cl
	je	.L6
	leaq	40(%rsp), %rdi
	movq	%rax, %rsi
	call	cudaMallocHost@PLT
	jmp	.L7
.L6:
	movslq	%edi, %rdi
	call	malloc@PLT
	movq	%rax, 40(%rsp)
.L7:
	leaq	48(%rsp), %rdi
	movslq	%r15d, %rsi
	call	cudaMalloc@PLT
.L5:
	leaq	56(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	64(%rsp), %rdi
	call	cudaEventCreate@PLT
	xorl	%eax, %eax
	movl	$0x00000000, 16(%rsp)
	movl	%eax, 20(%rsp)
	movslq	%r15d, %rax
	movq	%rax, 8(%rsp)
.L8:
	cmpl	%r12d, 20(%rsp)
	jge	.L38
	testb	%r14b, %r14b
	je	.L9
	testb	%r13b, %r13b
	je	.L10
	movq	8(%rsp), %rsi
	leaq	40(%rsp), %rdi
	call	cudaMallocHost@PLT
	jmp	.L11
.L10:
	movq	8(%rsp), %rdi
	call	malloc@PLT
	movq	%rax, 40(%rsp)
.L11:
	movq	8(%rsp), %rsi
	leaq	48(%rsp), %rdi
	call	cudaMalloc@PLT
.L9:
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	cmpl	$0, 24(%rsp)
	movq	40(%rsp), %rdi
	movq	48(%rsp), %rax
	jne	.L12
	movq	%rdi, %rsi
	movq	8(%rsp), %rdx
	movl	$1, %ecx
	movq	%rax, %rdi
	jmp	.L35
.L12:
	movq	8(%rsp), %rdx
	movl	$2, %ecx
	movq	%rax, %rsi
.L35:
	call	cudaMemcpy@PLT
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	movl	%eax, 28(%rsp)
	call	cudaEventRecord@PLT
	movq	64(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	64(%rsp), %rdx
	movq	56(%rsp), %rsi
	leaq	36(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movss	16(%rsp), %xmm2
	movl	28(%rsp), %ecx
	addss	36(%rsp), %xmm2
	movss	%xmm2, 16(%rsp)
	testl	%ecx, %ecx
	je	.L14
	movq	stderr(%rip), %rdi
	leaq	.LC5(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L14:
	testb	%r14b, %r14b
	je	.L15
	movq	40(%rsp), %rdi
	testb	%r13b, %r13b
	je	.L16
	call	cudaFreeHost@PLT
	jmp	.L17
.L16:
	call	free@PLT
.L17:
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
.L15:
	incl	20(%rsp)
	jmp	.L8
.L38:
	cvtsi2ssl	%r12d, %xmm1
	movss	16(%rsp), %xmm0
	cmpl	$0, 24(%rsp)
	divss	%xmm1, %xmm0
	cvtsi2ssl	%r15d, %xmm1
	divss	.LC6(%rip), %xmm0
	divss	%xmm0, %xmm1
	cvtss2sd	%xmm0, %xmm0
	divss	.LC7(%rip), %xmm1
	cvtss2sd	%xmm1, %xmm3
	movq	%xmm3, %r12
	jne	.L19
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	movq	%r12, %xmm0
	leaq	.LC9(%rip), %rsi
	jmp	.L36
.L19:
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	movq	%r12, %xmm0
	leaq	.LC11(%rip), %rsi
.L36:
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	movq	56(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	testb	%bpl, %bpl
	jne	.L4
	movq	40(%rsp), %rdi
	testb	%bl, %bl
	je	.L22
	call	cudaFreeHost@PLT
	jmp	.L23
.L22:
	call	free@PLT
.L23:
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
.L4:
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L24
	call	__stack_chk_fail@PLT
.L24:
	addq	$88, %rsp
	.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
.LFE2030:
	.size	_Z14profile_memcpyiiibb, .-_Z14profile_memcpyiiibb
	.section	.rodata.str1.1
.LC12:
	.string	"-r"
.LC14:
	.string	"Transfer size (MB): %f\n\n"
.LC15:
	.string	"Pageable transfers\n"
.LC16:
	.string	"\n"
.LC17:
	.string	"Pinned transfers\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	%edi, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	pushq	%rcx
	.cfi_def_cfa_offset 48
	cmpl	$3, %edi
	je	.L40
	cmpl	$4, %edi
	jne	.L41
	movq	24(%rsi), %rdi
	leaq	.LC12(%rip), %rsi
	call	strcmp@PLT
	testl	%eax, %eax
	je	.L40
.L41:
	movq	(%r12), %rdi
	call	_Z5usagePc
	movl	$1, %eax
	jmp	.L39
.L40:
	movq	8(%r12), %rdi
	call	atoi@PLT
	movq	16(%r12), %rdi
	movl	%eax, %ebx
	call	atoi@PLT
	movl	%eax, %ebp
	testl	%ebx, %ebx
	jle	.L41
	testl	%eax, %eax
	jle	.L41
	xorl	%edi, %edi
	xorl	%r12d, %r12d
	call	cudaFree@PLT
	cvtsi2ssl	%ebx, %xmm0
	movl	$2, %edi
	movb	$1, %al
	mulss	.LC13(%rip), %xmm0
	leaq	.LC14(%rip), %rsi
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC15(%rip), %rsi
	call	__printf_chk@PLT
	cmpl	$4, %r13d
	movl	%ebp, %esi
	movl	%ebx, %edi
	sete	%r12b
	xorl	%ecx, %ecx
	xorl	%edx, %edx
	movl	%r12d, %r8d
	call	_Z14profile_memcpyiiibb
	movl	%r12d, %r8d
	xorl	%ecx, %ecx
	movl	$1, %edx
	movl	%ebp, %esi
	movl	%ebx, %edi
	call	_Z14profile_memcpyiiibb
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%r12d, %r8d
	movl	$1, %ecx
	xorl	%edx, %edx
	movl	%ebp, %esi
	movl	%ebx, %edi
	call	_Z14profile_memcpyiiibb
	movl	%r12d, %r8d
	movl	$1, %ecx
	movl	%ebp, %esi
	movl	$1, %edx
	movl	%ebx, %edi
	call	_Z14profile_memcpyiiibb
	xorl	%eax, %eax
.L39:
	popq	%rdx
	.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
.LFE2031:
	.size	main, .-main
	.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
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC6:
	.long	1148846080
	.align 4
.LC7:
	.long	1315859240
	.align 4
.LC13:
	.long	897581056
	.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 <stdlib.h>

#define HOST_TO_DEVICE 0
#define DEVICE_TO_HOST 1

// Print the usage of the program
inline void usage(char *program)
{     
        fprintf(stderr, "usage: %s memsize iters [-r]\n", program);
        fprintf(stderr, "    memsize : memory transferred in bytes (>0)\n");
        fprintf(stderr, "    iters   : number of iterations (>0)\n");
        fprintf(stderr, "    -r      : re-allocate memory for each iteration\n");
}

// Allocate 'size' worth of bytes to host memory '*ptr'
inline void alloc_host_mem(void **ptr, int size, bool pinned)
{
        if (pinned) {
                hipHostMalloc(ptr, size, hipHostMallocDefault);
        } else {
                *ptr = malloc(size);
        }
}

// Free host memory pointed by 'ptr'
inline void free_host_mem(void *ptr, bool pinned)
{
        if (pinned) {
                hipHostFree(ptr);
        } else {
                free(ptr);
        }
}

// Profile memory copy performance between GPU and CPU
// For each iteration, we copy 'size' worth of bytes in a direction 
// size        :   memory copy size for each iteration
// iters       :   number of iterations
// direction   :   HOST_TO_DEVICE / DEVICE_TO_HOST
// pinned      :   whether enable pinned memory allocation at host
// reallocate  :   whether re-allocate memory for each time
// This function prints the average throughput and transfer time result    
void profile_memcpy(int size, int iters, int direction, bool pinned, bool reallocate)
{
        void *h = NULL, *d = NULL;      // host and device memory
        hipEvent_t start, stop;
        float time, avg_time, total_time, throughput;
        int i;
        hipError_t result;

        // Allocate host and device memory only one time
        if (!reallocate) {
                alloc_host_mem(&h, size, pinned);
                hipMalloc(&d, size);
        }

        hipEventCreate(&start);
        hipEventCreate(&stop);
        total_time = 0;

        for (i = 0; i < iters; i++) {
                // Re-allocate host and device memory for each time
                if (reallocate) {                        
                        alloc_host_mem(&h, size, pinned);
                        hipMalloc((void**)&d, size);               
                }

                hipEventRecord(start, 0);
                if (direction == HOST_TO_DEVICE) { 
                        result = hipMemcpy(d, h, size, hipMemcpyHostToDevice);
                } else {
                        result = hipMemcpy(h, d, size, hipMemcpyDeviceToHost);
                }
                hipEventRecord(stop, 0);
                hipEventSynchronize(stop);
                hipEventElapsedTime(&time, start, stop);
                total_time += time;
                
                if (result != hipSuccess) {
                        fprintf(stderr, "Error: memory copy\n");
                }

                if (reallocate) {
                        free_host_mem(h, pinned);
                        hipFree(d);
                }
        }

        // Calculate host to device information
        avg_time = total_time / iters / 1000;  // time in second
        throughput = (float)size / avg_time / 1000000000; // throughput in GB/s

        if (direction == HOST_TO_DEVICE) {
                printf("  Host to Device Time: %.6f s\n", avg_time);
                printf("  Host to Device Throughput: %.6f GB/s\n", throughput);
        } else {
                printf("  Device to Host Time: %.6f s\n", avg_time);
                printf("  Device to Host Throughput: %.6f GB/s\n", throughput);
        }

        hipEventDestroy(start);
        hipEventDestroy(stop);

        if (!reallocate) {
                free_host_mem(h, pinned);
                hipFree(d);
        }
}

int main(int argc, char **argv)
{
        int size, iters;
        bool reallocate = false;        // By default, we don't re-allocate memory for each test

        if (!(argc == 3 || (argc == 4 && strcmp(argv[3], "-r") == 0))) {
                usage(argv[0]);
                return EXIT_FAILURE;
        }

        size = atoi(argv[1]);
        iters = atoi(argv[2]);

        if (size <= 0 || iters <= 0) {
                usage(argv[0]);
                return EXIT_FAILURE;
        }

        if (argc == 4) {
                reallocate = true;
        }

        // warm up
        hipFree(0);

        // Profile memory copy
        printf("Transfer size (MB): %f\n\n", (float)size / (1024 * 1024));
        
        printf("Pageable transfers\n");
        profile_memcpy(size, iters, HOST_TO_DEVICE, false, reallocate);
        profile_memcpy(size, iters, DEVICE_TO_HOST, false, reallocate);

        printf("\n");
        
        printf("Pinned transfers\n");
        profile_memcpy(size, iters, HOST_TO_DEVICE, true, reallocate);
        profile_memcpy(size, iters, DEVICE_TO_HOST, true, reallocate);

        return EXIT_SUCCESS;
}
	.text
	.file	"main.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z14profile_memcpyiiibb
.LCPI0_0:
	.long	0x447a0000                      # float 1000
.LCPI0_1:
	.long	0x4e6e6b28                      # float 1.0E+9
	.text
	.globl	_Z14profile_memcpyiiibb
	.type	_Z14profile_memcpyiiibb,@function
_Z14profile_memcpyiiibb:                # @_Z14profile_memcpyiiibb
	.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	$56, %rsp
	.cfi_def_cfa_offset 112
	.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	%r8d, %ebp
	movl	%ecx, %ebx
	movl	%edx, %r14d
	movl	%esi, %r15d
	xorl	%eax, %eax
	movq	%rax, (%rsp)
	movq	%rax, 16(%rsp)
	movl	%edi, 48(%rsp)                  # 4-byte Spill
	movslq	%edi, %r13
	testl	%r8d, %r8d
	jne	.LBB0_5
# %bb.1:
	testb	%bl, %bl
	je	.LBB0_3
# %bb.2:
	movq	%rsp, %rdi
	movq	%r13, %rsi
	xorl	%edx, %edx
	callq	hipHostMalloc
	jmp	.LBB0_4
.LBB0_3:
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, (%rsp)
.LBB0_4:                                # %_Z14alloc_host_memPPvib.exit
	leaq	16(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
.LBB0_5:
	leaq	32(%rsp), %rdi
	callq	hipEventCreate
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	testl	%r15d, %r15d
	movl	%r15d, 44(%rsp)                 # 4-byte Spill
	xorps	%xmm0, %xmm0
	jle	.LBB0_6
# %bb.7:                                # %.lr.ph
	movss	%xmm0, 8(%rsp)                  # 4-byte Spill
.LBB0_8:                                # =>This Inner Loop Header: Depth=1
	testb	%bpl, %bpl
	je	.LBB0_13
# %bb.9:                                #   in Loop: Header=BB0_8 Depth=1
	testb	%bl, %bl
	je	.LBB0_11
# %bb.10:                               #   in Loop: Header=BB0_8 Depth=1
	movq	%rsp, %rdi
	movq	%r13, %rsi
	xorl	%edx, %edx
	callq	hipHostMalloc
	jmp	.LBB0_12
.LBB0_11:                               #   in Loop: Header=BB0_8 Depth=1
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, (%rsp)
.LBB0_12:                               # %_Z14alloc_host_memPPvib.exit31
                                        #   in Loop: Header=BB0_8 Depth=1
	leaq	16(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
.LBB0_13:                               #   in Loop: Header=BB0_8 Depth=1
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	testl	%r14d, %r14d
	je	.LBB0_14
# %bb.15:                               #   in Loop: Header=BB0_8 Depth=1
	movq	(%rsp), %rdi
	movq	16(%rsp), %rsi
	movq	%r13, %rdx
	movl	$2, %ecx
	jmp	.LBB0_16
.LBB0_14:                               #   in Loop: Header=BB0_8 Depth=1
	movq	16(%rsp), %rdi
	movq	(%rsp), %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
.LBB0_16:                               #   in Loop: Header=BB0_8 Depth=1
	callq	hipMemcpy
	movl	%eax, %r12d
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	24(%rsp), %rdi
	callq	hipEventSynchronize
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdx
	leaq	52(%rsp), %rdi
	callq	hipEventElapsedTime
	movss	8(%rsp), %xmm0                  # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	addss	52(%rsp), %xmm0
	movss	%xmm0, 8(%rsp)                  # 4-byte Spill
	testl	%r12d, %r12d
	jne	.LBB0_17
.LBB0_18:                               #   in Loop: Header=BB0_8 Depth=1
	testb	%bpl, %bpl
	je	.LBB0_23
# %bb.19:                               #   in Loop: Header=BB0_8 Depth=1
	movq	(%rsp), %rdi
	testb	%bl, %bl
	je	.LBB0_21
# %bb.20:                               #   in Loop: Header=BB0_8 Depth=1
	callq	hipHostFree
	jmp	.LBB0_22
.LBB0_21:                               #   in Loop: Header=BB0_8 Depth=1
	callq	free
.LBB0_22:                               # %_Z13free_host_memPvb.exit
                                        #   in Loop: Header=BB0_8 Depth=1
	movq	16(%rsp), %rdi
	callq	hipFree
.LBB0_23:                               #   in Loop: Header=BB0_8 Depth=1
	decl	%r15d
	jne	.LBB0_8
	jmp	.LBB0_24
.LBB0_17:                               #   in Loop: Header=BB0_8 Depth=1
	movq	stderr(%rip), %rcx
	movl	$.L.str, %edi
	movl	$19, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	jmp	.LBB0_18
.LBB0_6:
	movss	%xmm0, 8(%rsp)                  # 4-byte Spill
.LBB0_24:                               # %._crit_edge
	xorps	%xmm0, %xmm0
	cvtsi2ssl	44(%rsp), %xmm0         # 4-byte Folded Reload
	movss	8(%rsp), %xmm2                  # 4-byte Reload
                                        # xmm2 = mem[0],zero,zero,zero
	divss	%xmm0, %xmm2
	divss	.LCPI0_0(%rip), %xmm2
	cvtsi2ssl	48(%rsp), %xmm1         # 4-byte Folded Reload
	divss	%xmm2, %xmm1
	divss	.LCPI0_1(%rip), %xmm1
	testl	%r14d, %r14d
	xorps	%xmm0, %xmm0
	cvtss2sd	%xmm2, %xmm0
	cvtss2sd	%xmm1, %xmm1
	movsd	%xmm1, 8(%rsp)                  # 8-byte Spill
	movl	$.L.str.1, %eax
	movl	$.L.str.3, %edi
	cmoveq	%rax, %rdi
	movl	$.L.str.2, %eax
	movl	$.L.str.4, %r14d
	cmoveq	%rax, %r14
	movb	$1, %al
	callq	printf
	movq	%r14, %rdi
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	movq	32(%rsp), %rdi
	callq	hipEventDestroy
	movq	24(%rsp), %rdi
	callq	hipEventDestroy
	testb	%bpl, %bpl
	jne	.LBB0_29
# %bb.25:
	movq	(%rsp), %rdi
	testb	%bl, %bl
	je	.LBB0_27
# %bb.26:
	callq	hipHostFree
	jmp	.LBB0_28
.LBB0_27:
	callq	free
.LBB0_28:                               # %_Z13free_host_memPvb.exit32
	movq	16(%rsp), %rdi
	callq	hipFree
.LBB0_29:
	addq	$56, %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_end0:
	.size	_Z14profile_memcpyiiibb, .Lfunc_end0-_Z14profile_memcpyiiibb
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x35800000                      # float 9.53674316E-7
	.text
	.globl	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	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %r15
	movl	%edi, %r14d
	cmpl	$3, %edi
	je	.LBB1_4
# %bb.1:
	cmpl	$4, %r14d
	jne	.LBB1_3
# %bb.2:
	movq	24(%r15), %rdi
	movl	$.L.str.5, %esi
	callq	strcmp
	testl	%eax, %eax
	jne	.LBB1_3
.LBB1_4:
	movq	8(%r15), %rdi
	callq	atoi
	movl	%eax, %ebx
	movq	16(%r15), %rdi
	callq	atoi
	testl	%ebx, %ebx
	jle	.LBB1_3
# %bb.5:
	movl	%eax, %ebp
	testl	%eax, %eax
	jle	.LBB1_3
# %bb.6:
	xorl	%r15d, %r15d
	cmpl	$4, %r14d
	sete	%r15b
	xorl	%r14d, %r14d
	xorl	%edi, %edi
	callq	hipFree
	cvtsi2ss	%ebx, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	%ebx, %edi
	movl	%ebp, %esi
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	movl	%r15d, %r8d
	callq	_Z14profile_memcpyiiibb
	movl	%ebx, %edi
	movl	%ebp, %esi
	movl	$1, %edx
	xorl	%ecx, %ecx
	movl	%r15d, %r8d
	callq	_Z14profile_memcpyiiibb
	movl	$10, %edi
	callq	putchar@PLT
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	%ebx, %edi
	movl	%ebp, %esi
	xorl	%edx, %edx
	movl	$1, %ecx
	movl	%r15d, %r8d
	callq	_Z14profile_memcpyiiibb
	movl	%ebx, %edi
	movl	%ebp, %esi
	movl	$1, %edx
	movl	$1, %ecx
	movl	%r15d, %r8d
	callq	_Z14profile_memcpyiiibb
.LBB1_7:
	movl	%r14d, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.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
.LBB1_3:
	.cfi_def_cfa_offset 48
	movq	(%r15), %rdx
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rcx
	movl	$.L.str.11, %edi
	movl	$47, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movq	stderr(%rip), %rcx
	movl	$.L.str.12, %edi
	movl	$40, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movq	stderr(%rip), %rcx
	movl	$.L.str.13, %edi
	movl	$52, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$1, %r14d
	jmp	.LBB1_7
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Error: memory copy\n"
	.size	.L.str, 20

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"  Host to Device Time: %.6f s\n"
	.size	.L.str.1, 31

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"  Host to Device Throughput: %.6f GB/s\n"
	.size	.L.str.2, 40

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"  Device to Host Time: %.6f s\n"
	.size	.L.str.3, 31

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"  Device to Host Throughput: %.6f GB/s\n"
	.size	.L.str.4, 40

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"-r"
	.size	.L.str.5, 3

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Transfer size (MB): %f\n\n"
	.size	.L.str.6, 25

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"usage: %s memsize iters [-r]\n"
	.size	.L.str.10, 30

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"    memsize : memory transferred in bytes (>0)\n"
	.size	.L.str.11, 48

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"    iters   : number of iterations (>0)\n"
	.size	.L.str.12, 41

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"    -r      : re-allocate memory for each iteration\n"
	.size	.L.str.13, 53

	.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	"Pageable transfers"
	.size	.Lstr, 19

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Pinned transfers"
	.size	.Lstr.1, 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
