#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <cuda.h>

#define u32 unsigned int
#define BLOCK_SIZE 64

#define CREATE_RAND_ARR(arr, size, min, max) \
do {                                         \
    time_t t;                                \
    srand((unsigned)time(&t));               \
    for (u32 i = 0; i < size; i++)           \
        arr[i] = rand() % max + min;         \
} while (0)                                  \

template <typename T>
T cpuFindMax(T* arr, const u32 arr_size);

template <typename T>
T gpuFindMax(T* arr, const u32 arr_size);

__device__
float atomicFloatMax(float* address, const float val)
{
    if (*address > val) {
        return *address;
    }

    int* const addressInt = (int*)address;
    int old = *addressInt, assumed;

    do {
        assumed = old;
        if (__int_as_float(assumed) > val) {
            break;
        }
        old = atomicCAS(addressInt, assumed, __float_as_int(val));
    } while (old != assumed);

    return old;
}

__global__
void find_max_int_kernel(const int* __restrict__ arr, const u32 arr_size, int* maxval)
{
    __shared__ int shared_maxval;
    u32 id_x = blockIdx.x * blockDim.x + threadIdx.x;
    if (id_x == 0) {
        shared_maxval = arr[0];
    }
    __syncthreads();
    int register_maxval = shared_maxval;
    if (blockIdx.x == 0) {
        for (u32 i = threadIdx.x; i < arr_size; i += blockDim.x) {
            register_maxval = (register_maxval > arr[i])? register_maxval : arr[i];
        }
    }

    atomicMax(&shared_maxval, register_maxval);
    __syncthreads();

    if (id_x == 0) {
        *maxval = shared_maxval;
    }
}

__global__
void find_max_float_kernel(const float* __restrict__ arr, const u32 arr_size, float* maxval)
{
    __shared__ float shared_maxval;
    u32 id_x = blockIdx.x * blockDim.x + threadIdx.x;
    if (id_x == 0) {
        shared_maxval = arr[0];
    }
    __syncthreads();
    float register_maxval = shared_maxval;
    if (blockIdx.x == 0) {
        for (u32 i = threadIdx.x; i < arr_size; i += blockDim.x) {
            register_maxval = (register_maxval > arr[i])? register_maxval : arr[i];
        }
    }

    atomicFloatMax(&shared_maxval, register_maxval);
    __syncthreads();

    if (id_x == 0) {
        *maxval = shared_maxval;
    }
}

// __device__
// void warp_find_max_kernel(const int* __restrict__ arr, const u32 size, int* max)
// {
    
// }

// __host__
// int gpuWarpFindMax(int* h_arr, const u32 size)
// {
//     assert(size == 32);

//     int* d_arr;
//     cudaMalloc((void**)&d_arr, size * sizeof(int));
//     cudaMemcpy(d_arr, h_arr, size * sizeof(int), cudaMemcpyHostToDevice);

//     int* d_max;
//     cudaMalloc((void**)&d_max, 1 * sizeof(int));

//     warp_find_max_kernel(d_arr, d_max);
//     if (cudaSuccess != cudaGetLastError()) {
//         printf("warp_find_max_kernel fault!\n");
//     }

//     int* h_max = (int*)malloc(1 * sizeof(int));
//     cudaMemcpy(h_max, d_max, 1 * sizeof(int), cudaMemcpyDeviceToHost);
//     int max = *h_max;

//     cudaFree(d_arr);
//     cudaFree(d_max);
//     free(h_max);

//     return max;
// }

template <>
int gpuFindMax<int>(int* h_arr, const u32 arr_size)
{
    assert(arr_size > 0);

    int* d_arr;
    cudaMalloc((void**)&d_arr, arr_size * sizeof(int));
    cudaMemcpy(d_arr, h_arr, arr_size * sizeof(int), cudaMemcpyHostToDevice);

    int* d_maxval;
    cudaMalloc((void**)&d_maxval, 1 * sizeof(int));

    dim3 blocks = BLOCK_SIZE;
    dim3 grid = (arr_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
    find_max_int_kernel <<< grid , blocks >>> (d_arr, arr_size, d_maxval);
    if (cudaSuccess != cudaGetLastError()) {
        printf("find_max_int_kernel fault!\n");
    }

    int* h_maxval = (int*)malloc(1 * sizeof(int));
    cudaMemcpy(h_maxval, d_maxval, 1 * sizeof(int), cudaMemcpyDeviceToHost);
    int result = *h_maxval;

    cudaFree(d_arr);
    cudaFree(d_maxval);
    free(h_maxval);

    return result;
}

template <>
float gpuFindMax<float>(float* h_arr, const u32 arr_size)
{
    assert(arr_size > 0);

    float* d_arr;
    cudaMalloc((void**)&d_arr, arr_size * sizeof(float));
    cudaMemcpy(d_arr, h_arr, arr_size * sizeof(float), cudaMemcpyHostToDevice);

    float* d_maxval;
    cudaMalloc((void**)&d_maxval, 1 * sizeof(float));

    dim3 blocks = BLOCK_SIZE;
    dim3 grid = (arr_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
    find_max_float_kernel <<< grid , blocks >>> (d_arr, arr_size, d_maxval);
    if (cudaSuccess != cudaGetLastError()) {
        printf("find_max_float_kernel fault!\n");
    }

    float* h_maxval = (float*)malloc(1 * sizeof(float));
    cudaMemcpy(h_maxval, d_maxval, 1 * sizeof(float), cudaMemcpyDeviceToHost);
    float result = *h_maxval;

    cudaFree(d_arr);
    cudaFree(d_maxval);
    free(h_maxval);

    return result;
}

template <>
int cpuFindMax<int>(int* arr, const u32 arr_size)
{
    assert(arr_size > 0);
    int maxval = arr[0];
    for (u32 i = 0; i < arr_size; i++) {
        maxval = (arr[i] > maxval)? arr[i] : maxval;
    }

    return maxval;
}

template <>
float cpuFindMax<float>(float* arr, const u32 arr_size)
{
    assert(arr_size > 0);
    float maxval = arr[0];
    for (u32 i = 0; i < arr_size; i++) {
        maxval = (arr[i] > maxval)? arr[i] : maxval;
    }

    return maxval;
}

int main()
{
    const u32 arr_size = 1000;

    int* int_arr = (int*)malloc(arr_size * sizeof(int));
    CREATE_RAND_ARR(int_arr, arr_size, 0, 1000);

    float* float_arr = (float*)malloc(arr_size * sizeof(float));
    CREATE_RAND_ARR(float_arr, arr_size, 0, 1000);
    for (u32 i = 0; i < arr_size; i++) {
        float_arr[i] /= 10.0;
    }

    int cpu_int_maxval = cpuFindMax<int>(int_arr, arr_size);
    printf("CPU find max int element: %d\n", cpu_int_maxval);

    int gpu_int_maxval = gpuFindMax<int>(int_arr, arr_size);
    printf("GPU find max int element: %d\n", gpu_int_maxval);

    float cpu_float_maxval = cpuFindMax<float>(float_arr, arr_size);
    printf("CPU find max float element: %f\n", cpu_float_maxval);

    float gpu_float_maxval = gpuFindMax<float>(float_arr, arr_size);
    printf("GPU find max float element: %f\n", gpu_float_maxval);

    const u32 warp_size = 32;
    int* warp_arr = (int*)malloc(warp_size * sizeof(int));
    CREATE_RAND_ARR(warp_arr, warp_size, 0, 1000);

    // int gpu_warp_int_maxval = gpuWarpFindMax(warp_arr, warp_size);
    // printf("GPU warp find max int element: %d\n", gpu_warp_int_maxval);
    
    free(int_arr);
    free(float_arr);
    free(warp_arr);

    return 0;
}
	.file	"tmpxft_002352e3_00000000-6_main.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2035:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2035:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z14atomicFloatMaxPff
	.type	_Z14atomicFloatMaxPff, @function
_Z14atomicFloatMaxPff:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z14atomicFloatMaxPff, .-_Z14atomicFloatMaxPff
	.globl	_Z10cpuFindMaxIiET_PS0_j
	.type	_Z10cpuFindMaxIiET_PS0_j, @function
_Z10cpuFindMaxIiET_PS0_j:
.LFB2030:
	.cfi_startproc
	endbr64
	movl	(%rdi), %eax
	xorl	%edx, %edx
.L5:
	cmpl	%esi, %edx
	jnb	.L8
	movl	(%rdi,%rdx,4), %ecx
	cmpl	%ecx, %eax
	cmovl	%ecx, %eax
	incq	%rdx
	jmp	.L5
.L8:
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z10cpuFindMaxIiET_PS0_j, .-_Z10cpuFindMaxIiET_PS0_j
	.globl	_Z10cpuFindMaxIfET_PS0_j
	.type	_Z10cpuFindMaxIfET_PS0_j, @function
_Z10cpuFindMaxIfET_PS0_j:
.LFB2031:
	.cfi_startproc
	endbr64
	movss	(%rdi), %xmm0
	xorl	%eax, %eax
.L10:
	cmpl	%esi, %eax
	jnb	.L13
	movss	(%rdi,%rax,4), %xmm1
	incq	%rax
	maxss	%xmm0, %xmm1
	movaps	%xmm1, %xmm0
	jmp	.L10
.L13:
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z10cpuFindMaxIfET_PS0_j, .-_Z10cpuFindMaxIfET_PS0_j
	.globl	_Z43__device_stub__Z19find_max_int_kernelPKijPiPKijPi
	.type	_Z43__device_stub__Z19find_max_int_kernelPKijPiPKijPi, @function
_Z43__device_stub__Z19find_max_int_kernelPKijPiPKijPi:
.LFB2057:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movl	%esi, 12(%rsp)
	leaq	40(%rsp), %rcx
	leaq	60(%rsp), %rsi
	movq	%rdx, (%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rax, 96(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 104(%rsp)
	movq	%rsp, %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movl	$1, 56(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L14
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z19find_max_int_kernelPKijPi(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L14:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L16
	call	__stack_chk_fail@PLT
.L16:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2057:
	.size	_Z43__device_stub__Z19find_max_int_kernelPKijPiPKijPi, .-_Z43__device_stub__Z19find_max_int_kernelPKijPiPKijPi
	.globl	_Z19find_max_int_kernelPKijPi
	.type	_Z19find_max_int_kernelPKijPi, @function
_Z19find_max_int_kernelPKijPi:
.LFB2058:
	.cfi_startproc
	endbr64
	jmp	_Z43__device_stub__Z19find_max_int_kernelPKijPiPKijPi
	.cfi_endproc
.LFE2058:
	.size	_Z19find_max_int_kernelPKijPi, .-_Z19find_max_int_kernelPKijPi
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"find_max_int_kernel fault!\n"
	.text
	.globl	_Z10gpuFindMaxIiET_PS0_j
	.type	_Z10gpuFindMaxIiET_PS0_j, @function
_Z10gpuFindMaxIiET_PS0_j:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%rbp, %rbx
	salq	$2, %rbp
	movq	%rbp, %rsi
	subq	$48, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rdi
	call	cudaMalloc@PLT
	movq	(%rsp), %rdi
	movq	%rbp, %rdx
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	8(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
	leal	63(%rbx), %eax
	movl	$67108865, %edx
	xorl	%r9d, %r9d
	shrl	$6, %eax
	xorl	%r8d, %r8d
	salq	$6, %rdx
	movl	$1, %ecx
	movl	%eax, 28(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	28(%rsp), %rdi
	movl	36(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L20
	movq	8(%rsp), %rdx
	movq	(%rsp), %rdi
	movl	%ebx, %esi
	call	_Z43__device_stub__Z19find_max_int_kernelPKijPiPKijPi
.L20:
	call	cudaGetLastError@PLT
	testl	%eax, %eax
	je	.L21
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L21:
	movl	$4, %edi
	call	malloc@PLT
	movq	8(%rsp), %rsi
	movl	$2, %ecx
	movl	$4, %edx
	movq	%rax, %rdi
	movq	%rax, %rbx
	call	cudaMemcpy@PLT
	movq	(%rsp), %rdi
	movl	(%rbx), %ebp
	call	cudaFree@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	addq	$48, %rsp
	.cfi_def_cfa_offset 32
	movl	%ebp, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z10gpuFindMaxIiET_PS0_j, .-_Z10gpuFindMaxIiET_PS0_j
	.globl	_Z45__device_stub__Z21find_max_float_kernelPKfjPfPKfjPf
	.type	_Z45__device_stub__Z21find_max_float_kernelPKfjPfPKfjPf, @function
_Z45__device_stub__Z21find_max_float_kernelPKfjPfPKfjPf:
.LFB2059:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movl	%esi, 12(%rsp)
	leaq	40(%rsp), %rcx
	leaq	60(%rsp), %rsi
	movq	%rdx, (%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rax, 96(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 104(%rsp)
	movq	%rsp, %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movl	$1, 56(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L27
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z21find_max_float_kernelPKfjPf(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L27:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L29
	call	__stack_chk_fail@PLT
.L29:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2059:
	.size	_Z45__device_stub__Z21find_max_float_kernelPKfjPfPKfjPf, .-_Z45__device_stub__Z21find_max_float_kernelPKfjPfPKfjPf
	.globl	_Z21find_max_float_kernelPKfjPf
	.type	_Z21find_max_float_kernelPKfjPf, @function
_Z21find_max_float_kernelPKfjPf:
.LFB2060:
	.cfi_startproc
	endbr64
	jmp	_Z45__device_stub__Z21find_max_float_kernelPKfjPfPKfjPf
	.cfi_endproc
.LFE2060:
	.size	_Z21find_max_float_kernelPKfjPf, .-_Z21find_max_float_kernelPKfjPf
	.section	.rodata.str1.1
.LC1:
	.string	"find_max_float_kernel fault!\n"
	.text
	.globl	_Z10gpuFindMaxIfET_PS0_j
	.type	_Z10gpuFindMaxIfET_PS0_j, @function
_Z10gpuFindMaxIfET_PS0_j:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%rbp, %rbx
	salq	$2, %rbp
	movq	%rbp, %rsi
	subq	$64, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	leaq	16(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rdi
	movq	%rbp, %rdx
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	24(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
	leal	63(%rbx), %eax
	movl	$67108865, %edx
	xorl	%r9d, %r9d
	shrl	$6, %eax
	xorl	%r8d, %r8d
	salq	$6, %rdx
	movl	$1, %ecx
	movl	%eax, 44(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	44(%rsp), %rdi
	movl	52(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L33
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rdi
	movl	%ebx, %esi
	call	_Z45__device_stub__Z21find_max_float_kernelPKfjPfPKfjPf
.L33:
	call	cudaGetLastError@PLT
	testl	%eax, %eax
	je	.L34
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L34:
	movl	$4, %edi
	call	malloc@PLT
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movl	$4, %edx
	movq	%rax, %rbx
	movq	%rax, %rdi
	call	cudaMemcpy@PLT
	movss	(%rbx), %xmm0
	movq	16(%rsp), %rdi
	movss	%xmm0, 12(%rsp)
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movss	12(%rsp), %xmm0
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L35
	call	__stack_chk_fail@PLT
.L35:
	addq	$64, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z10gpuFindMaxIfET_PS0_j, .-_Z10gpuFindMaxIfET_PS0_j
	.section	.rodata.str1.1
.LC3:
	.string	"CPU find max int element: %d\n"
.LC4:
	.string	"GPU find max int element: %d\n"
.LC5:
	.string	"CPU find max float element: %f\n"
.LC6:
	.string	"GPU find max float element: %f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	$4000, %edi
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movl	$1000, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	xorl	%ebx, %ebx
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %r13
	call	malloc@PLT
	movq	%r13, %rdi
	movq	%rax, %rbp
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
.L41:
	call	rand@PLT
	cltd
	idivl	%r12d
	movl	%edx, 0(%rbp,%rbx,4)
	incq	%rbx
	cmpq	$1000, %rbx
	jne	.L41
	movl	$4000, %edi
	xorl	%r12d, %r12d
	movl	$1000, %r14d
	call	malloc@PLT
	movq	%r13, %rdi
	movq	%rax, %rbx
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
.L42:
	call	rand@PLT
	cltd
	idivl	%r14d
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, (%rbx,%r12,4)
	incq	%r12
	cmpq	$1000, %r12
	jne	.L42
	movss	.LC2(%rip), %xmm1
	xorl	%eax, %eax
.L43:
	movss	(%rbx,%rax,4), %xmm0
	divss	%xmm1, %xmm0
	movss	%xmm0, (%rbx,%rax,4)
	incq	%rax
	cmpq	$1000, %rax
	jne	.L43
	movl	$1000, %esi
	movq	%rbp, %rdi
	movl	$1000, %r14d
	call	_Z10cpuFindMaxIiET_PS0_j
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	movl	%eax, %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1000, %esi
	movq	%rbp, %rdi
	call	_Z10gpuFindMaxIiET_PS0_j
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	movl	%eax, %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1000, %esi
	movq	%rbx, %rdi
	call	_Z10cpuFindMaxIfET_PS0_j
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movl	$1000, %esi
	movq	%rbx, %rdi
	call	_Z10gpuFindMaxIfET_PS0_j
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movl	$128, %edi
	call	malloc@PLT
	movq	%r13, %rdi
	xorl	%r13d, %r13d
	movq	%rax, %r12
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
.L44:
	call	rand@PLT
	cltd
	idivl	%r14d
	movl	%edx, (%r12,%r13,4)
	incq	%r13
	cmpq	$32, %r13
	jne	.L44
	movq	%rbp, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L45
	call	__stack_chk_fail@PLT
.L45:
	addq	$16, %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
.LFE2032:
	.size	main, .-main
	.section	.rodata.str1.1
.LC7:
	.string	"_Z21find_max_float_kernelPKfjPf"
.LC8:
	.string	"_Z19find_max_int_kernelPKijPi"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2062:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC7(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z21find_max_float_kernelPKfjPf(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC8(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z19find_max_int_kernelPKijPi(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2062:
	.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
.LC2:
	.long	1092616192
	.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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <hip/hip_runtime.h>

#define u32 unsigned int
#define BLOCK_SIZE 64

#define CREATE_RAND_ARR(arr, size, min, max) \
do {                                         \
    time_t t;                                \
    srand((unsigned)time(&t));               \
    for (u32 i = 0; i < size; i++)           \
        arr[i] = rand() % max + min;         \
} while (0)                                  \

template <typename T>
T cpuFindMax(T* arr, const u32 arr_size);

template <typename T>
T gpuFindMax(T* arr, const u32 arr_size);

__device__
float atomicFloatMax(float* address, const float val)
{
    if (*address > val) {
        return *address;
    }

    int* const addressInt = (int*)address;
    int old = *addressInt, assumed;

    do {
        assumed = old;
        if (__int_as_float(assumed) > val) {
            break;
        }
        old = atomicCAS(addressInt, assumed, __float_as_int(val));
    } while (old != assumed);

    return old;
}

__global__
void find_max_int_kernel(const int* __restrict__ arr, const u32 arr_size, int* maxval)
{
    __shared__ int shared_maxval;
    u32 id_x = blockIdx.x * blockDim.x + threadIdx.x;
    if (id_x == 0) {
        shared_maxval = arr[0];
    }
    __syncthreads();
    int register_maxval = shared_maxval;
    if (blockIdx.x == 0) {
        for (u32 i = threadIdx.x; i < arr_size; i += blockDim.x) {
            register_maxval = (register_maxval > arr[i])? register_maxval : arr[i];
        }
    }

    atomicMax(&shared_maxval, register_maxval);
    __syncthreads();

    if (id_x == 0) {
        *maxval = shared_maxval;
    }
}

__global__
void find_max_float_kernel(const float* __restrict__ arr, const u32 arr_size, float* maxval)
{
    __shared__ float shared_maxval;
    u32 id_x = blockIdx.x * blockDim.x + threadIdx.x;
    if (id_x == 0) {
        shared_maxval = arr[0];
    }
    __syncthreads();
    float register_maxval = shared_maxval;
    if (blockIdx.x == 0) {
        for (u32 i = threadIdx.x; i < arr_size; i += blockDim.x) {
            register_maxval = (register_maxval > arr[i])? register_maxval : arr[i];
        }
    }

    atomicFloatMax(&shared_maxval, register_maxval);
    __syncthreads();

    if (id_x == 0) {
        *maxval = shared_maxval;
    }
}

// __device__
// void warp_find_max_kernel(const int* __restrict__ arr, const u32 size, int* max)
// {
    
// }

// __host__
// int gpuWarpFindMax(int* h_arr, const u32 size)
// {
//     assert(size == 32);

//     int* d_arr;
//     cudaMalloc((void**)&d_arr, size * sizeof(int));
//     cudaMemcpy(d_arr, h_arr, size * sizeof(int), cudaMemcpyHostToDevice);

//     int* d_max;
//     cudaMalloc((void**)&d_max, 1 * sizeof(int));

//     warp_find_max_kernel(d_arr, d_max);
//     if (cudaSuccess != cudaGetLastError()) {
//         printf("warp_find_max_kernel fault!\n");
//     }

//     int* h_max = (int*)malloc(1 * sizeof(int));
//     cudaMemcpy(h_max, d_max, 1 * sizeof(int), cudaMemcpyDeviceToHost);
//     int max = *h_max;

//     cudaFree(d_arr);
//     cudaFree(d_max);
//     free(h_max);

//     return max;
// }

template <>
int gpuFindMax<int>(int* h_arr, const u32 arr_size)
{
    assert(arr_size > 0);

    int* d_arr;
    hipMalloc((void**)&d_arr, arr_size * sizeof(int));
    hipMemcpy(d_arr, h_arr, arr_size * sizeof(int), hipMemcpyHostToDevice);

    int* d_maxval;
    hipMalloc((void**)&d_maxval, 1 * sizeof(int));

    dim3 blocks = BLOCK_SIZE;
    dim3 grid = (arr_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
    find_max_int_kernel <<< grid , blocks >>> (d_arr, arr_size, d_maxval);
    if (hipSuccess != hipGetLastError()) {
        printf("find_max_int_kernel fault!\n");
    }

    int* h_maxval = (int*)malloc(1 * sizeof(int));
    hipMemcpy(h_maxval, d_maxval, 1 * sizeof(int), hipMemcpyDeviceToHost);
    int result = *h_maxval;

    hipFree(d_arr);
    hipFree(d_maxval);
    free(h_maxval);

    return result;
}

template <>
float gpuFindMax<float>(float* h_arr, const u32 arr_size)
{
    assert(arr_size > 0);

    float* d_arr;
    hipMalloc((void**)&d_arr, arr_size * sizeof(float));
    hipMemcpy(d_arr, h_arr, arr_size * sizeof(float), hipMemcpyHostToDevice);

    float* d_maxval;
    hipMalloc((void**)&d_maxval, 1 * sizeof(float));

    dim3 blocks = BLOCK_SIZE;
    dim3 grid = (arr_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
    find_max_float_kernel <<< grid , blocks >>> (d_arr, arr_size, d_maxval);
    if (hipSuccess != hipGetLastError()) {
        printf("find_max_float_kernel fault!\n");
    }

    float* h_maxval = (float*)malloc(1 * sizeof(float));
    hipMemcpy(h_maxval, d_maxval, 1 * sizeof(float), hipMemcpyDeviceToHost);
    float result = *h_maxval;

    hipFree(d_arr);
    hipFree(d_maxval);
    free(h_maxval);

    return result;
}

template <>
int cpuFindMax<int>(int* arr, const u32 arr_size)
{
    assert(arr_size > 0);
    int maxval = arr[0];
    for (u32 i = 0; i < arr_size; i++) {
        maxval = (arr[i] > maxval)? arr[i] : maxval;
    }

    return maxval;
}

template <>
float cpuFindMax<float>(float* arr, const u32 arr_size)
{
    assert(arr_size > 0);
    float maxval = arr[0];
    for (u32 i = 0; i < arr_size; i++) {
        maxval = (arr[i] > maxval)? arr[i] : maxval;
    }

    return maxval;
}

int main()
{
    const u32 arr_size = 1000;

    int* int_arr = (int*)malloc(arr_size * sizeof(int));
    CREATE_RAND_ARR(int_arr, arr_size, 0, 1000);

    float* float_arr = (float*)malloc(arr_size * sizeof(float));
    CREATE_RAND_ARR(float_arr, arr_size, 0, 1000);
    for (u32 i = 0; i < arr_size; i++) {
        float_arr[i] /= 10.0;
    }

    int cpu_int_maxval = cpuFindMax<int>(int_arr, arr_size);
    printf("CPU find max int element: %d\n", cpu_int_maxval);

    int gpu_int_maxval = gpuFindMax<int>(int_arr, arr_size);
    printf("GPU find max int element: %d\n", gpu_int_maxval);

    float cpu_float_maxval = cpuFindMax<float>(float_arr, arr_size);
    printf("CPU find max float element: %f\n", cpu_float_maxval);

    float gpu_float_maxval = gpuFindMax<float>(float_arr, arr_size);
    printf("GPU find max float element: %f\n", gpu_float_maxval);

    const u32 warp_size = 32;
    int* warp_arr = (int*)malloc(warp_size * sizeof(int));
    CREATE_RAND_ARR(warp_arr, warp_size, 0, 1000);

    // int gpu_warp_int_maxval = gpuWarpFindMax(warp_arr, warp_size);
    // printf("GPU warp find max int element: %d\n", gpu_warp_int_maxval);
    
    free(int_arr);
    free(float_arr);
    free(warp_arr);

    return 0;
}
	.text
	.file	"main.hip"
	.globl	_Z34__device_stub__find_max_int_kernelPKijPi # -- Begin function _Z34__device_stub__find_max_int_kernelPKijPi
	.type	_Z34__device_stub__find_max_int_kernelPKijPi,@function
_Z34__device_stub__find_max_int_kernelPKijPi: # @_Z34__device_stub__find_max_int_kernelPKijPi
	.cfi_startproc
# %bb.0:
	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
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	12(%rsp), %rcx
	movl	%esi, (%rcx)
	leaq	32(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z19find_max_int_kernelPKijPi, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	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
	retq
.Lfunc_end0:
	.size	_Z34__device_stub__find_max_int_kernelPKijPi, .Lfunc_end0-_Z34__device_stub__find_max_int_kernelPKijPi
	.cfi_endproc
                                        # -- End function
	.globl	_Z36__device_stub__find_max_float_kernelPKfjPf # -- Begin function _Z36__device_stub__find_max_float_kernelPKfjPf
	.type	_Z36__device_stub__find_max_float_kernelPKfjPf,@function
_Z36__device_stub__find_max_float_kernelPKfjPf: # @_Z36__device_stub__find_max_float_kernelPKfjPf
	.cfi_startproc
# %bb.0:
	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
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	12(%rsp), %rcx
	movl	%esi, (%rcx)
	leaq	32(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z21find_max_float_kernelPKfjPf, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	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
	retq
.Lfunc_end1:
	.size	_Z36__device_stub__find_max_float_kernelPKfjPf, .Lfunc_end1-_Z36__device_stub__find_max_float_kernelPKfjPf
	.cfi_endproc
                                        # -- End function
	.globl	_Z10gpuFindMaxIiET_PS0_j        # -- Begin function _Z10gpuFindMaxIiET_PS0_j
	.type	_Z10gpuFindMaxIiET_PS0_j,@function
_Z10gpuFindMaxIiET_PS0_j:               # @_Z10gpuFindMaxIiET_PS0_j
	.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	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebx
	movq	%rdi, %r14
	movl	%esi, %r15d
	shlq	$2, %r15
	leaq	8(%rsp), %r12
	movq	%r12, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movq	(%r12), %rdi
	movq	%r14, %rsi
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	%rsp, %rdi
	movl	$4, %esi
	callq	hipMalloc
	leal	63(%rbx), %edi
	shrl	$6, %edi
	btsq	$32, %rdi
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$64, %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_2
# %bb.1:
	movq	8(%rsp), %rdi
	movq	(%rsp), %rdx
	movl	%ebx, %esi
	callq	_Z34__device_stub__find_max_int_kernelPKijPi
.LBB2_2:
	callq	hipGetLastError
	testl	%eax, %eax
	je	.LBB2_4
# %bb.3:
	movl	$.Lstr, %edi
	callq	puts@PLT
.LBB2_4:
	movl	$4, %edi
	callq	malloc
	movq	%rax, %rbx
	movq	(%rsp), %rsi
	movl	$4, %edx
	movq	%rax, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movl	(%rbx), %ebp
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	free
	movl	%ebp, %eax
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.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_end2:
	.size	_Z10gpuFindMaxIiET_PS0_j, .Lfunc_end2-_Z10gpuFindMaxIiET_PS0_j
	.cfi_endproc
                                        # -- End function
	.globl	_Z10gpuFindMaxIfET_PS0_j        # -- Begin function _Z10gpuFindMaxIfET_PS0_j
	.type	_Z10gpuFindMaxIfET_PS0_j,@function
_Z10gpuFindMaxIfET_PS0_j:               # @_Z10gpuFindMaxIfET_PS0_j
	.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	$24, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	%esi, %ebx
	movq	%rdi, %r14
	movl	%esi, %r15d
	shlq	$2, %r15
	leaq	16(%rsp), %r12
	movq	%r12, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movq	(%r12), %rdi
	movq	%r14, %rsi
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	%rsp, %rdi
	movl	$4, %esi
	callq	hipMalloc
	leal	63(%rbx), %edi
	shrl	$6, %edi
	btsq	$32, %rdi
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$64, %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_2
# %bb.1:
	movq	16(%rsp), %rdi
	movq	(%rsp), %rdx
	movl	%ebx, %esi
	callq	_Z36__device_stub__find_max_float_kernelPKfjPf
.LBB3_2:
	callq	hipGetLastError
	testl	%eax, %eax
	je	.LBB3_4
# %bb.3:
	movl	$.Lstr.1, %edi
	callq	puts@PLT
.LBB3_4:
	movl	$4, %edi
	callq	malloc
	movq	%rax, %rbx
	movq	(%rsp), %rsi
	movl	$4, %edx
	movq	%rax, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movss	(%rbx), %xmm0                   # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, 12(%rsp)                 # 4-byte Spill
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	free
	movss	12(%rsp), %xmm0                 # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	addq	$24, %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_end3:
	.size	_Z10gpuFindMaxIfET_PS0_j, .Lfunc_end3-_Z10gpuFindMaxIfET_PS0_j
	.cfi_endproc
                                        # -- End function
	.globl	_Z10cpuFindMaxIiET_PS0_j        # -- Begin function _Z10cpuFindMaxIiET_PS0_j
	.type	_Z10cpuFindMaxIiET_PS0_j,@function
_Z10cpuFindMaxIiET_PS0_j:               # @_Z10cpuFindMaxIiET_PS0_j
	.cfi_startproc
# %bb.0:
	movl	(%rdi), %eax
	testl	%esi, %esi
	je	.LBB4_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %ecx
	xorl	%edx, %edx
.LBB4_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rdi,%rdx,4), %esi
	cmpl	%eax, %esi
	cmovgl	%esi, %eax
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB4_2
.LBB4_3:                                # %._crit_edge
	retq
.Lfunc_end4:
	.size	_Z10cpuFindMaxIiET_PS0_j, .Lfunc_end4-_Z10cpuFindMaxIiET_PS0_j
	.cfi_endproc
                                        # -- End function
	.globl	_Z10cpuFindMaxIfET_PS0_j        # -- Begin function _Z10cpuFindMaxIfET_PS0_j
	.type	_Z10cpuFindMaxIfET_PS0_j,@function
_Z10cpuFindMaxIfET_PS0_j:               # @_Z10cpuFindMaxIfET_PS0_j
	.cfi_startproc
# %bb.0:
	movss	(%rdi), %xmm1                   # xmm1 = mem[0],zero,zero,zero
	testl	%esi, %esi
	je	.LBB5_1
# %bb.3:                                # %.lr.ph.preheader
	movl	%esi, %eax
	xorl	%ecx, %ecx
.LBB5_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movss	(%rdi,%rcx,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	maxss	%xmm1, %xmm0
	incq	%rcx
	movaps	%xmm0, %xmm1
	cmpq	%rcx, %rax
	jne	.LBB5_4
# %bb.2:                                # %._crit_edge
	retq
.LBB5_1:
	movaps	%xmm1, %xmm0
	retq
.Lfunc_end5:
	.size	_Z10cpuFindMaxIfET_PS0_j, .Lfunc_end5-_Z10cpuFindMaxIfET_PS0_j
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI6_0:
	.long	0x41200000                      # float 10
	.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
	movl	$4000, %edi                     # imm = 0xFA0
	callq	malloc
	movq	%rax, %rbx
	movq	%rsp, %rdi
	callq	time
	movl	%eax, %edi
	callq	srand
	xorl	%r14d, %r14d
.LBB6_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	movl	%eax, (%rbx,%r14,4)
	incq	%r14
	cmpq	$1000, %r14                     # imm = 0x3E8
	jne	.LBB6_1
# %bb.2:
	movl	$4000, %edi                     # imm = 0xFA0
	callq	malloc
	movq	%rax, %r14
	movq	%rsp, %rdi
	callq	time
	movl	%eax, %edi
	callq	srand
	xorl	%r15d, %r15d
.LBB6_3:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	%xmm0, (%r14,%r15,4)
	incq	%r15
	cmpq	$1000, %r15                     # imm = 0x3E8
	jne	.LBB6_3
# %bb.4:
	xorl	%eax, %eax
	movss	.LCPI6_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
.LBB6_5:                                # =>This Inner Loop Header: Depth=1
	movss	(%r14,%rax,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	divss	%xmm0, %xmm1
	movss	%xmm1, (%r14,%rax,4)
	incq	%rax
	cmpq	$1000, %rax                     # imm = 0x3E8
	jne	.LBB6_5
# %bb.6:
	movl	(%rbx), %esi
	xorl	%eax, %eax
.LBB6_7:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%rax,4), %ecx
	cmpl	%esi, %ecx
	cmovgl	%ecx, %esi
	incq	%rax
	cmpq	$1000, %rax                     # imm = 0x3E8
	jne	.LBB6_7
# %bb.8:                                # %_Z10cpuFindMaxIiET_PS0_j.exit
	xorl	%r15d, %r15d
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	movq	%rbx, %rdi
	movl	$1000, %esi                     # imm = 0x3E8
	callq	_Z10gpuFindMaxIiET_PS0_j
	movl	$.L.str.3, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
	movss	(%r14), %xmm0                   # xmm0 = mem[0],zero,zero,zero
.LBB6_9:                                # %.lr.ph.i29
                                        # =>This Inner Loop Header: Depth=1
	movaps	%xmm0, %xmm1
	movss	(%r14,%r15,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	maxss	%xmm1, %xmm0
	incq	%r15
	cmpq	$1000, %r15                     # imm = 0x3E8
	jne	.LBB6_9
# %bb.10:                               # %_Z10cpuFindMaxIfET_PS0_j.exit
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movq	%r14, %rdi
	movl	$1000, %esi                     # imm = 0x3E8
	callq	_Z10gpuFindMaxIfET_PS0_j
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	movq	%rsp, %rdi
	callq	time
	movl	%eax, %edi
	callq	srand
	movl	$32, %ebp
.LBB6_11:                               # =>This Inner Loop Header: Depth=1
	callq	rand
	decl	%ebp
	jne	.LBB6_11
# %bb.12:
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	xorl	%eax, %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
.Lfunc_end6:
	.size	main, .Lfunc_end6-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB7_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB7_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z19find_max_int_kernelPKijPi, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z21find_max_float_kernelPKfjPf, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end7:
	.size	__hip_module_ctor, .Lfunc_end7-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB8_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB8_2:
	retq
.Lfunc_end8:
	.size	__hip_module_dtor, .Lfunc_end8-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z19find_max_int_kernelPKijPi,@object # @_Z19find_max_int_kernelPKijPi
	.section	.rodata,"a",@progbits
	.globl	_Z19find_max_int_kernelPKijPi
	.p2align	3, 0x0
_Z19find_max_int_kernelPKijPi:
	.quad	_Z34__device_stub__find_max_int_kernelPKijPi
	.size	_Z19find_max_int_kernelPKijPi, 8

	.type	_Z21find_max_float_kernelPKfjPf,@object # @_Z21find_max_float_kernelPKfjPf
	.globl	_Z21find_max_float_kernelPKfjPf
	.p2align	3, 0x0
_Z21find_max_float_kernelPKfjPf:
	.quad	_Z36__device_stub__find_max_float_kernelPKfjPf
	.size	_Z21find_max_float_kernelPKfjPf, 8

	.type	.L.str.2,@object                # @.str.2
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.2:
	.asciz	"CPU find max int element: %d\n"
	.size	.L.str.2, 30

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"GPU find max int element: %d\n"
	.size	.L.str.3, 30

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"CPU find max float element: %f\n"
	.size	.L.str.4, 32

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"GPU find max float element: %f\n"
	.size	.L.str.5, 32

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z19find_max_int_kernelPKijPi"
	.size	.L__unnamed_1, 30

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z21find_max_float_kernelPKfjPf"
	.size	.L__unnamed_2, 32

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.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	"find_max_int_kernel fault!"
	.size	.Lstr, 27

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"find_max_float_kernel fault!"
	.size	.Lstr.1, 29

	.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 _Z34__device_stub__find_max_int_kernelPKijPi
	.addrsig_sym _Z36__device_stub__find_max_float_kernelPKfjPf
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z19find_max_int_kernelPKijPi
	.addrsig_sym _Z21find_max_float_kernelPKfjPf
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z19find_max_int_kernelPKijPi ; -- Begin function _Z19find_max_int_kernelPKijPi
	.globl	_Z19find_max_int_kernelPKijPi
	.p2align	8
	.type	_Z19find_max_int_kernelPKijPi,@function
_Z19find_max_int_kernelPKijPi:          ; @_Z19find_max_int_kernelPKijPi
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x24
	s_load_b64 s[4:5], s[0:1], 0x0
	v_sub_nc_u32_e32 v1, 0, v0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s3, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mul_i32 s2, s15, s3
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_cmp_eq_u32_e32 vcc_lo, s2, v1
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b32 s6, s[4:5], 0x0
	s_waitcnt lgkmcnt(0)
	v_dual_mov_b32 v1, 0 :: v_dual_mov_b32 v2, s6
	ds_store_b32 v1, v2
.LBB0_2:
	s_or_b32 exec_lo, exec_lo, s2
	s_load_b32 s6, s[0:1], 0x8
	v_mov_b32_e32 v1, 0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cmp_eq_u32 s15, 0
	ds_load_b32 v2, v1
	s_cselect_b32 s7, -1, 0
	v_cmp_gt_u32_e64 s2, s6, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, s7, s2
	s_and_saveexec_b32 s7, s2
	s_cbranch_execz .LBB0_6
; %bb.3:                                ; %.lr.ph.preheader
	s_mov_b32 s8, 0
.LBB0_4:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	v_lshlrev_b64 v[3:4], 2, v[0:1]
	v_add_nc_u32_e32 v0, s3, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v3, s2, s4, v3
	v_add_co_ci_u32_e64 v4, s2, s5, v4, s2
	s_delay_alu instid0(VALU_DEP_3)
	v_cmp_le_u32_e64 s2, s6, v0
	global_load_b32 v3, v[3:4], off
	s_or_b32 s8, s2, s8
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_max_i32_e32 v2, v2, v3
	s_and_not1_b32 exec_lo, exec_lo, s8
	s_cbranch_execnz .LBB0_4
; %bb.5:                                ; %Flow
	s_or_b32 exec_lo, exec_lo, s8
.LBB0_6:                                ; %Flow21
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 exec_lo, exec_lo, s7
	s_mov_b32 s2, exec_lo
	s_brev_b32 s3, 1
.LBB0_7:                                ; %ComputeLoop
                                        ; =>This Inner Loop Header: Depth=1
	s_ctz_i32_b32 s4, s2
	s_waitcnt lgkmcnt(0)
	v_readlane_b32 s5, v2, s4
	s_lshl_b32 s4, 1, s4
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_and_not1_b32 s2, s2, s4
	s_max_i32 s3, s3, s5
	s_cmp_lg_u32 s2, 0
	s_cbranch_scc1 .LBB0_7
; %bb.8:                                ; %ComputeEnd
	v_mbcnt_lo_u32_b32 v0, exec_lo, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_eq_u32_e64 s2, 0, v0
	s_and_saveexec_b32 s4, s2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_xor_b32 s2, exec_lo, s4
	s_cbranch_execz .LBB0_10
; %bb.9:
	v_dual_mov_b32 v0, 0 :: v_dual_mov_b32 v1, s3
	ds_max_i32 v0, v1
.LBB0_10:
	s_or_b32 exec_lo, exec_lo, s2
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_12
; %bb.11:
	v_mov_b32_e32 v0, 0
	s_load_b64 s[0:1], s[0:1], 0x10
	ds_load_b32 v1, v0
	s_waitcnt lgkmcnt(0)
	global_store_b32 v0, v1, s[0:1]
.LBB0_12:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z19find_max_int_kernelPKijPi
		.amdhsa_group_segment_fixed_size 4
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 5
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z19find_max_int_kernelPKijPi, .Lfunc_end0-_Z19find_max_int_kernelPKijPi
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 420
; NumSgprs: 18
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 4 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 5
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.protected	_Z21find_max_float_kernelPKfjPf ; -- Begin function _Z21find_max_float_kernelPKfjPf
	.globl	_Z21find_max_float_kernelPKfjPf
	.p2align	8
	.type	_Z21find_max_float_kernelPKfjPf,@function
_Z21find_max_float_kernelPKfjPf:        ; @_Z21find_max_float_kernelPKfjPf
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x24
	s_load_b64 s[4:5], s[0:1], 0x0
	v_sub_nc_u32_e32 v1, 0, v0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s6, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mul_i32 s2, s15, s6
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_cmp_eq_u32_e32 vcc_lo, s2, v1
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB1_2
; %bb.1:
	s_load_b32 s3, s[4:5], 0x0
	s_waitcnt lgkmcnt(0)
	v_dual_mov_b32 v1, 0 :: v_dual_mov_b32 v2, s3
	ds_store_b32 v1, v2
.LBB1_2:
	s_or_b32 exec_lo, exec_lo, s2
	v_mov_b32_e32 v1, 0
	s_load_b32 s7, s[0:1], 0x8
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_b32 v2, v1
	s_cmp_eq_u32 s15, 0
	s_cselect_b32 s3, -1, 0
	v_cmp_gt_u32_e64 s2, s7, v0
	s_waitcnt lgkmcnt(0)
	v_mov_b32_e32 v3, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, s3, s2
	s_and_saveexec_b32 s8, s2
	s_cbranch_execz .LBB1_6
; %bb.3:                                ; %.lr.ph.preheader
	v_mov_b32_e32 v3, v2
	s_mov_b32 s9, 0
.LBB1_4:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	v_lshlrev_b64 v[4:5], 2, v[0:1]
	v_add_nc_u32_e32 v0, s6, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v4, s2, s4, v4
	v_add_co_ci_u32_e64 v5, s2, s5, v5, s2
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_cmp_le_u32_e64 s2, s7, v0
	global_load_b32 v4, v[4:5], off
	s_or_b32 s9, s2, s9
	s_waitcnt vmcnt(0)
	v_cmp_gt_f32_e64 s3, v3, v4
	v_cndmask_b32_e64 v3, v4, v3, s3
	s_and_not1_b32 exec_lo, exec_lo, s9
	s_cbranch_execnz .LBB1_4
; %bb.5:                                ; %Flow23
	s_or_b32 exec_lo, exec_lo, s9
.LBB1_6:                                ; %Flow24
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 exec_lo, exec_lo, s8
	s_mov_b32 s3, exec_lo
	v_cmpx_ngt_f32_e32 v2, v3
	s_cbranch_execz .LBB1_11
; %bb.7:
	v_mov_b32_e32 v0, 0
	s_mov_b32 s4, 0
                                        ; implicit-def: $sgpr5
.LBB1_8:                                ; =>This Inner Loop Header: Depth=1
	s_or_b32 s5, s5, exec_lo
	s_mov_b32 s6, exec_lo
	v_cmpx_nlt_f32_e32 v3, v2
	s_cbranch_execz .LBB1_10
; %bb.9:                                ;   in Loop: Header=BB1_8 Depth=1
	ds_cmpstore_rtn_b32 v1, v0, v3, v2
	s_and_not1_b32 s5, s5, exec_lo
	s_waitcnt lgkmcnt(0)
	v_cmp_eq_u32_e64 s2, v1, v2
	v_mov_b32_e32 v2, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, s2, exec_lo
	s_or_b32 s5, s5, s2
.LBB1_10:                               ; %Flow
                                        ;   in Loop: Header=BB1_8 Depth=1
	s_or_b32 exec_lo, exec_lo, s6
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, exec_lo, s5
	s_or_b32 s4, s2, s4
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s4
	s_cbranch_execnz .LBB1_8
.LBB1_11:                               ; %Flow22
	s_or_b32 exec_lo, exec_lo, s3
	s_barrier
	buffer_gl0_inv
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB1_13
; %bb.12:
	v_mov_b32_e32 v0, 0
	s_load_b64 s[0:1], s[0:1], 0x10
	ds_load_b32 v1, v0
	s_waitcnt lgkmcnt(0)
	global_store_b32 v0, v1, s[0:1]
.LBB1_13:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z21find_max_float_kernelPKfjPf
		.amdhsa_group_segment_fixed_size 4
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 6
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end1:
	.size	_Z21find_max_float_kernelPKfjPf, .Lfunc_end1-_Z21find_max_float_kernelPKfjPf
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 444
; NumSgprs: 18
; NumVgprs: 6
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 4 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 6
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.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_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .actual_access:  read_only
        .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 4
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z19find_max_int_kernelPKijPi
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z19find_max_int_kernelPKijPi.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     5
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
  - .args:
      - .actual_access:  read_only
        .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 4
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z21find_max_float_kernelPKfjPf
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z21find_max_float_kernelPKfjPf.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     6
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
