
// Includes
#include <stdio.h>

// Type of the array in which we search for the maximum.
// If you use float, don't forget to type %f in the printf later on..
#define TYPE int

#define USE_NAIVE

// Variables
TYPE* h_A;
TYPE* d_A;

// Functions
void Cleanup(void);
void WorstCaseInit(TYPE*, int);

__device__ __host__ TYPE cumax(TYPE a, TYPE b)
{
	return a > b ? a : b;
}

// Schema des naiven Ansatz
// o o o o o o o o  n=1
// |/  |/  |/  |/
// o   o   o   o    n=2
// |  /    |  /
// | /     | /
// o       o        n=4
// |      /
// |    /
// |  /
// |/
// o				Ergebnis

__global__ void reduce_max_naive(TYPE* A, int n)
{
	int i = blockIdx.x * n;
	A[2*i] = cumax( A[2*i], A[2*i+n]);
}

// Host code
int main(int argc, char** argv)
{
    printf("Reduce\n");
	int N = 1<<15;
	int Nh = N / 2;
    size_t size = N * sizeof(TYPE);    

    // Allocate input vector h_A
    h_A = (TYPE*)malloc(size);
   	
    // Initialize input vector
    WorstCaseInit(h_A, N);
   	
    // Allocate vector in device memory
    cudaMalloc((void**)&d_A, size);

    // Copy vector from host memory to device memory
    cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice);
   
	// Start tracking of elapsed time.
	cudaEvent_t     start, stop;		
	cudaEventCreate( &start );
	cudaEventCreate( &stop );
	cudaEventRecord( start, 0 );

#ifdef USE_NAIVE	// Naive approach
	
	for (int n=1; n<N; n*=2)
		reduce_max_naive<<<Nh / n,1>>>(d_A, n);	

#else				// Better approach
	
	// TODO: Implement!

#endif

	// End tracking of elapsed time.
	cudaEventRecord( stop, 0 );
	cudaEventSynchronize( stop );
	float   elapsedTime;
	cudaEventElapsedTime( &elapsedTime, start, stop );
	printf( "Time: %f ms\n", elapsedTime );
	cudaEventDestroy( start );
	cudaEventDestroy( stop );

	// Find the truth... :)
	TYPE maximum = 0;
	for (int i = 0; i < N; ++i) 	
		maximum = cumax(h_A[i], maximum);
	
	// Copy result (first element only) from device memory to host memory
    cudaMemcpy(h_A, d_A, sizeof(TYPE), cudaMemcpyDeviceToHost);

	// Validate result from GPU.
	if (maximum == h_A[0])
		printf("PASSED: %i == %i", maximum, h_A[0]);
	else printf("FAILED: %i != %i", maximum, h_A[0]);
    
    Cleanup();
}

void Cleanup(void)
{
    // Free device memory
    if (d_A)
        cudaFree(d_A);
  
    // Free host memory
    if (h_A)
        free(h_A);  
        
    cudaThreadExit();
        
    printf("\nPress ENTER to exit...\n");
    fflush( stdout);
    fflush( stderr);
    getchar();    

    exit(0);
}

void WorstCaseInit(TYPE* data, int n)
{
	// Using a list sorted in ascending order is the worst case.
    for (int i = 0; i < n; ++i)
		data[i] = (TYPE)(i);
}

	.file	"tmpxft_002f9233_00000000-6_reduce.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2033:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2033:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z5cumaxii
	.type	_Z5cumaxii, @function
_Z5cumaxii:
.LFB2027:
	.cfi_startproc
	endbr64
	cmpl	%esi, %edi
	movl	%esi, %eax
	cmovge	%edi, %eax
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z5cumaxii, .-_Z5cumaxii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"\nPress ENTER to exit...\n"
	.text
	.globl	_Z7Cleanupv
	.type	_Z7Cleanupv, @function
_Z7Cleanupv:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	d_A(%rip), %rdi
	testq	%rdi, %rdi
	je	.L4
	call	cudaFree@PLT
.L4:
	movq	h_A(%rip), %rdi
	testq	%rdi, %rdi
	je	.L5
	call	free@PLT
.L5:
	call	cudaThreadExit@PLT
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	stdout(%rip), %rdi
	call	fflush@PLT
	movq	stderr(%rip), %rdi
	call	fflush@PLT
	call	getchar@PLT
	xorl	%edi, %edi
	call	exit@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z7Cleanupv, .-_Z7Cleanupv
	.globl	_Z13WorstCaseInitPii
	.type	_Z13WorstCaseInitPii, @function
_Z13WorstCaseInitPii:
.LFB2030:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L15:
	cmpl	%eax, %esi
	jle	.L17
	movl	%eax, (%rdi,%rax,4)
	incq	%rax
	jmp	.L15
.L17:
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z13WorstCaseInitPii, .-_Z13WorstCaseInitPii
	.globl	_Z37__device_stub__Z16reduce_max_naivePiiPii
	.type	_Z37__device_stub__Z16reduce_max_naivePiiPii, @function
_Z37__device_stub__Z16reduce_max_naivePiiPii:
.LFB2055:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movl	%esi, 4(%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L18
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z16reduce_max_naivePii(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L18:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L20
	call	__stack_chk_fail@PLT
.L20:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_Z37__device_stub__Z16reduce_max_naivePiiPii, .-_Z37__device_stub__Z16reduce_max_naivePiiPii
	.globl	_Z16reduce_max_naivePii
	.type	_Z16reduce_max_naivePii, @function
_Z16reduce_max_naivePii:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_Z37__device_stub__Z16reduce_max_naivePiiPii
	.cfi_endproc
.LFE2056:
	.size	_Z16reduce_max_naivePii, .-_Z16reduce_max_naivePii
	.section	.rodata.str1.1
.LC1:
	.string	"Reduce\n"
.LC2:
	.string	"Time: %f ms\n"
.LC3:
	.string	"PASSED: %i == %i"
.LC4:
	.string	"FAILED: %i != %i"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	movabsq	$4294967297, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	$15, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movl	$1, %ebx
	subq	$48, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$131072, %edi
	call	malloc@PLT
	movl	$32768, %esi
	movq	%rax, %rdi
	movq	%rax, h_A(%rip)
	call	_Z13WorstCaseInitPii
	movl	$131072, %esi
	leaq	d_A(%rip), %rdi
	call	cudaMalloc@PLT
	movl	$1, %ecx
	movl	$131072, %edx
	movq	h_A(%rip), %rsi
	movq	d_A(%rip), %rdi
	call	cudaMemcpy@PLT
	movq	%rsp, %rdi
	call	cudaEventCreate@PLT
	leaq	8(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
.L25:
	movl	$16384, %eax
	movq	%r12, 20(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	cltd
	movl	24(%rsp), %esi
	movl	$1, %ecx
	movq	%r12, 28(%rsp)
	idivl	%ebx
	movl	$1, 36(%rsp)
	movq	%r12, %rdx
	movl	%eax, 16(%rsp)
	movq	16(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	movq	d_A(%rip), %rdi
	movl	%ebx, %esi
	call	_Z37__device_stub__Z16reduce_max_naivePiiPii
.L24:
	addl	%ebx, %ebx
	decl	%ebp
	jne	.L25
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	xorl	%ebx, %ebx
	call	cudaEventRecord@PLT
	movq	8(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	8(%rsp), %rdx
	movq	(%rsp), %rsi
	leaq	28(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	28(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	8(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	h_A(%rip), %rdi
	xorl	%eax, %eax
.L26:
	movl	(%rdi,%rax,4), %edx
	cmpl	%edx, %ebx
	cmovl	%edx, %ebx
	incq	%rax
	cmpq	$32768, %rax
	jne	.L26
	movq	d_A(%rip), %rsi
	movl	$2, %ecx
	movl	$4, %edx
	call	cudaMemcpy@PLT
	movq	h_A(%rip), %rax
	movl	%ebx, %edx
	leaq	.LC4(%rip), %rsi
	movl	(%rax), %ecx
	cmpl	%ebx, %ecx
	jne	.L34
	movl	%ebx, %ecx
	leaq	.LC3(%rip), %rsi
.L34:
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L29
	call	__stack_chk_fail@PLT
.L29:
	call	_Z7Cleanupv
	.cfi_endproc
.LFE2028:
	.size	main, .-main
	.section	.rodata.str1.1
.LC5:
	.string	"_Z16reduce_max_naivePii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2058:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 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	.LC5(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z16reduce_max_naivePii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	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	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2058:
	.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
	.globl	d_A
	.bss
	.align 8
	.type	d_A, @object
	.size	d_A, 8
d_A:
	.zero	8
	.globl	h_A
	.align 8
	.type	h_A, @object
	.size	h_A, 8
h_A:
	.zero	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:


// Includes

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

// Type of the array in which we search for the maximum.
// If you use float, don't forget to type %f in the printf later on..
#define TYPE int

#define USE_NAIVE

// Variables
TYPE* h_A;
TYPE* d_A;

// Functions
void Cleanup(void);
void WorstCaseInit(TYPE*, int);

__device__ __host__ TYPE cumax(TYPE a, TYPE b)
{
	return a > b ? a : b;
}

// Schema des naiven Ansatz
// o o o o o o o o  n=1
// |/  |/  |/  |/
// o   o   o   o    n=2
// |  /    |  /
// | /     | /
// o       o        n=4
// |      /
// |    /
// |  /
// |/
// o				Ergebnis

__global__ void reduce_max_naive(TYPE* A, int n)
{
	int i = blockIdx.x * n;
	A[2*i] = cumax( A[2*i], A[2*i+n]);
}

// Host code
int main(int argc, char** argv)
{
    printf("Reduce\n");
	int N = 1<<15;
	int Nh = N / 2;
    size_t size = N * sizeof(TYPE);    

    // Allocate input vector h_A
    h_A = (TYPE*)malloc(size);
   	
    // Initialize input vector
    WorstCaseInit(h_A, N);
   	
    // Allocate vector in device memory
    hipMalloc((void**)&d_A, size);

    // Copy vector from host memory to device memory
    hipMemcpy(d_A, h_A, size, hipMemcpyHostToDevice);
   
	// Start tracking of elapsed time.
	hipEvent_t     start, stop;		
	hipEventCreate( &start );
	hipEventCreate( &stop );
	hipEventRecord( start, 0 );

#ifdef USE_NAIVE	// Naive approach
	
	for (int n=1; n<N; n*=2)
		reduce_max_naive<<<Nh / n,1>>>(d_A, n);	

#else				// Better approach
	
	// TODO: Implement!

#endif

	// End tracking of elapsed time.
	hipEventRecord( stop, 0 );
	hipEventSynchronize( stop );
	float   elapsedTime;
	hipEventElapsedTime( &elapsedTime, start, stop );
	printf( "Time: %f ms\n", elapsedTime );
	hipEventDestroy( start );
	hipEventDestroy( stop );

	// Find the truth... :)
	TYPE maximum = 0;
	for (int i = 0; i < N; ++i) 	
		maximum = cumax(h_A[i], maximum);
	
	// Copy result (first element only) from device memory to host memory
    hipMemcpy(h_A, d_A, sizeof(TYPE), hipMemcpyDeviceToHost);

	// Validate result from GPU.
	if (maximum == h_A[0])
		printf("PASSED: %i == %i", maximum, h_A[0]);
	else printf("FAILED: %i != %i", maximum, h_A[0]);
    
    Cleanup();
}

void Cleanup(void)
{
    // Free device memory
    if (d_A)
        hipFree(d_A);
  
    // Free host memory
    if (h_A)
        free(h_A);  
        
    hipDeviceReset();
        
    printf("\nPress ENTER to exit...\n");
    fflush( stdout);
    fflush( stderr);
    getchar();    

    exit(0);
}

void WorstCaseInit(TYPE* data, int n)
{
	// Using a list sorted in ascending order is the worst case.
    for (int i = 0; i < n; ++i)
		data[i] = (TYPE)(i);
}

	.text
	.file	"reduce.hip"
	.globl	_Z5cumaxii                      # -- Begin function _Z5cumaxii
	.type	_Z5cumaxii,@function
_Z5cumaxii:                             # @_Z5cumaxii
	.cfi_startproc
# %bb.0:
	movl	%esi, %eax
	cmpl	%esi, %edi
	cmovgl	%edi, %eax
	retq
.Lfunc_end0:
	.size	_Z5cumaxii, .Lfunc_end0-_Z5cumaxii
	.cfi_endproc
                                        # -- End function
	.globl	_Z31__device_stub__reduce_max_naivePii # -- Begin function _Z31__device_stub__reduce_max_naivePii
	.type	_Z31__device_stub__reduce_max_naivePii,@function
_Z31__device_stub__reduce_max_naivePii: # @_Z31__device_stub__reduce_max_naivePii
	.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	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	4(%rsp), %rcx
	movl	%esi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%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	$_Z16reduce_max_naivePii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	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	_Z31__device_stub__reduce_max_naivePii, .Lfunc_end1-_Z31__device_stub__reduce_max_naivePii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$131072, %edi                   # imm = 0x20000
	callq	malloc
	movq	%rax, h_A(%rip)
	xorl	%ecx, %ecx
.LBB2_1:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movl	%ecx, (%rax,%rcx,4)
	incq	%rcx
	cmpq	$32768, %rcx                    # imm = 0x8000
	jne	.LBB2_1
# %bb.2:                                # %_Z13WorstCaseInitPii.exit
	movabsq	$4294967296, %r15               # imm = 0x100000000
	movl	$d_A, %edi
	movl	$131072, %esi                   # imm = 0x20000
	callq	hipMalloc
	movq	d_A(%rip), %rdi
	movq	h_A(%rip), %rsi
	movl	$1, %ebx
	movl	$131072, %edx                   # imm = 0x20000
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	16(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r14), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	leaq	1(%r15), %r14
.LBB2_3:                                # =>This Inner Loop Header: Depth=1
	movw	$16384, %ax                     # imm = 0x4000
	xorl	%edx, %edx
	divw	%bx
	movzwl	%ax, %edi
	orq	%r15, %rdi
	movl	$1, %esi
	movq	%r14, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_5
# %bb.4:                                #   in Loop: Header=BB2_3 Depth=1
	movq	d_A(%rip), %rdi
	movl	%ebx, %esi
	callq	_Z31__device_stub__reduce_max_naivePii
.LBB2_5:                                #   in Loop: Header=BB2_3 Depth=1
	leal	(%rbx,%rbx), %eax
	cmpl	$16384, %ebx                    # imm = 0x4000
	movl	%eax, %ebx
	jb	.LBB2_3
# %bb.6:
	movq	8(%rsp), %rdi
	xorl	%r14d, %r14d
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	28(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	cvtss2sd	(%rbx), %xmm0
	movl	$.L.str.1, %edi
	movb	$1, %al
	callq	printf
	movq	16(%rsp), %rdi
	callq	hipEventDestroy
	movq	8(%rsp), %rdi
	callq	hipEventDestroy
	movq	h_A(%rip), %rdi
	xorl	%ebx, %ebx
.LBB2_7:                                # =>This Inner Loop Header: Depth=1
	movl	(%rdi,%r14,4), %eax
	cmpl	%ebx, %eax
	cmovgl	%eax, %ebx
	incq	%r14
	cmpq	$32768, %r14                    # imm = 0x8000
	jne	.LBB2_7
# %bb.8:
	movq	d_A(%rip), %rsi
	movl	$4, %edx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	h_A(%rip), %rax
	movl	(%rax), %edx
	cmpl	%edx, %ebx
	movl	$.L.str.2, %eax
	movl	$.L.str.3, %edi
	cmoveq	%rax, %rdi
	movl	%ebx, %esi
	xorl	%eax, %eax
	callq	printf
	callq	_Z7Cleanupv
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z13WorstCaseInitPii            # -- Begin function _Z13WorstCaseInitPii
	.type	_Z13WorstCaseInitPii,@function
_Z13WorstCaseInitPii:                   # @_Z13WorstCaseInitPii
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB3_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %eax
	xorl	%ecx, %ecx
.LBB3_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%ecx, (%rdi,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB3_2
.LBB3_3:                                # %._crit_edge
	retq
.Lfunc_end3:
	.size	_Z13WorstCaseInitPii, .Lfunc_end3-_Z13WorstCaseInitPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z7Cleanupv                     # -- Begin function _Z7Cleanupv
	.type	_Z7Cleanupv,@function
_Z7Cleanupv:                            # @_Z7Cleanupv
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	d_A(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB4_2
# %bb.1:
	callq	hipFree
.LBB4_2:
	movq	h_A(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB4_4
# %bb.3:
	callq	free
.LBB4_4:
	callq	hipDeviceReset
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movq	stdout(%rip), %rdi
	callq	fflush
	movq	stderr(%rip), %rdi
	callq	fflush
	callq	getchar
	xorl	%edi, %edi
	callq	exit
.Lfunc_end4:
	.size	_Z7Cleanupv, .Lfunc_end4-_Z7Cleanupv
	.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:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z16reduce_max_naivePii, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__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	.LBB6_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
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	h_A,@object                     # @h_A
	.bss
	.globl	h_A
	.p2align	3, 0x0
h_A:
	.quad	0
	.size	h_A, 8

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

	.type	_Z16reduce_max_naivePii,@object # @_Z16reduce_max_naivePii
	.section	.rodata,"a",@progbits
	.globl	_Z16reduce_max_naivePii
	.p2align	3, 0x0
_Z16reduce_max_naivePii:
	.quad	_Z31__device_stub__reduce_max_naivePii
	.size	_Z16reduce_max_naivePii, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"Time: %f ms\n"
	.size	.L.str.1, 13

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"PASSED: %i == %i"
	.size	.L.str.2, 17

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"FAILED: %i != %i"
	.size	.L.str.3, 17

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z16reduce_max_naivePii"
	.size	.L__unnamed_1, 24

	.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	"Reduce"
	.size	.Lstr, 7

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"\nPress ENTER to exit..."
	.size	.Lstr.1, 24

	.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 _Z31__device_stub__reduce_max_naivePii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym d_A
	.addrsig_sym _Z16reduce_max_naivePii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z16reduce_max_naivePii ; -- Begin function _Z16reduce_max_naivePii
	.globl	_Z16reduce_max_naivePii
	.p2align	8
	.type	_Z16reduce_max_naivePii,@function
_Z16reduce_max_naivePii:                ; @_Z16reduce_max_naivePii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x8
	s_load_b64 s[0:1], s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	s_mul_i32 s15, s15, s2
	s_ashr_i32 s3, s2, 31
	s_lshl_b32 s4, s15, 1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_ashr_i32 s5, s4, 31
	s_lshl_b64 s[4:5], s[4:5], 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	s_add_u32 s0, s0, s4
	s_addc_u32 s1, s1, s5
	s_lshl_b64 s[2:3], s[2:3], 2
	s_add_u32 s2, s0, s2
	s_addc_u32 s3, s1, s3
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x0
	s_load_b32 s2, s[2:3], 0x0
	s_waitcnt lgkmcnt(0)
	s_max_i32 s2, s4, s2
	s_delay_alu instid0(SALU_CYCLE_1)
	v_dual_mov_b32 v0, 0 :: v_dual_mov_b32 v1, s2
	global_store_b32 v0, v1, s[0:1]
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z16reduce_max_naivePii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 12
		.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 2
		.amdhsa_next_free_sgpr 16
		.amdhsa_reserve_vcc 0
		.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	_Z16reduce_max_naivePii, .Lfunc_end0-_Z16reduce_max_naivePii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 132
; NumSgprs: 16
; NumVgprs: 2
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 1
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 16
; NumVGPRsForWavesPerEU: 2
; 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:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 12
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z16reduce_max_naivePii
    .private_segment_fixed_size: 0
    .sgpr_count:     16
    .sgpr_spill_count: 0
    .symbol:         _Z16reduce_max_naivePii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     2
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
