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

// cache size constant for convenience/readability 
const size_t CACHESIZE = 1.5 * (1<<20); 

// function to handle CUDA API call error val returns
void check_error(cudaError_t cudaerr) {
    if (cudaerr != cudaSuccess) {
        printf("FAILED WITH ERROR: \"%s\".\n", cudaGetErrorString(cudaerr));
        exit(-1);
    }   
}

// hammer attempt test kernel
__global__  void hammer_attempt(int* data, int* indices, int* eviction, int iterations, int numvals, int stride) {
	uint tid = threadIdx.x + blockIdx.x * blockDim.x;
	uint nthreads = blockDim.x * gridDim.x;
	
	int sum;
	for (int j = 0; j < iterations; j++) {	
	
			
		if (tid < 8) {
			int index = indices[tid];
			int n = data[index];
			sum += (n << 1); 
		}
		
		
		
		/*
		if (tid == 0) {
			int n1 = data[1413];
			int n2 = data[56102];
			int n3 = data[101336];
			int n4 = data[169659];
			int n5 = data[199685];
			int n6 = data[272110];
			int n7 = data[309174];
			int n8 = data[368505];
			sum = sum + n1 - n2 + n3 - n4 + n5 - n6 + n7 - n8;
		}
		*/
		
		//__syncthreads();	
		for (int i = (tid * stride) + 393216; i < 393216*2.5; i += (nthreads * stride)) {
			int n = data[i];
			sum -= (n >> 1);
		//	__syncthreads();
		}

		/*
		for (int i = 393216; i < 393216*3; i+=8) {
			int n = data[i];
			sum -= (n >> 1); 
		}
		*/
		
		/*
		for (int i = tid; i < numvals; i += nthreads) {
			int n = data[eviction[i]];
			sum -= (n >> 1); 
		}
		*/
		
	}
}

int main(int argc, char** argv) {
	// command line params, seed rand
	int blocks = atoi(argv[1]);
    int threads = atoi(argv[2]);
    int stride = atoi(argv[3]);
	int iterations = atoi(argv[4]);
	srand(time(NULL));

	// host array of random vals
	int* data_host = (int*) malloc(2.5*CACHESIZE);
	for (int i = 0; i < (2.5*CACHESIZE)/4; i++) {
		data_host[i] = rand(); 
	}

	// copy to device
	int* data_device;
	check_error(cudaMalloc((void**)&data_device, 2.5*CACHESIZE));
	check_error(cudaMemcpy(data_device, data_host, 2.5*CACHESIZE, cudaMemcpyHostToDevice));

	// host array for addresses
	// pick addresses in first 1.5MB of data
	// - explanation of values -
	// 393216: number of integers containted in 1.5MB cache
	// 49512: number of integers in a 1/8 slice of cache
	// 24576: number of integers in a 1/16 slice of cache
	// address choosing process: pick address in first 1/16, skip next 1/16, pick address in next 1/16...
	int* indices_host = (int*)malloc(sizeof(int)*8); 	 
	for (int i = 0; i < 8; i++) {
		int sector_start = i * 49512; 
		indices_host[i] = (rand() % 24576) + sector_start;
		printf("%d\n", indices_host[i]);
	}

	// copy to device
	int* indices_device;
	check_error(cudaMalloc((void**)&indices_device, sizeof(int)*8));
	check_error(cudaMemcpy(indices_device, indices_host, sizeof(int)*8, cudaMemcpyHostToDevice));

	// host array of indices for eviction purposes
	int numvals = (393216*2) / stride;
	int* eviction_host = (int*)malloc(sizeof(int)*numvals);
	for (int i = 0; i < numvals; i++) {
		eviction_host[i] = 393216 + (i * stride);
	}

	// copy to device
	int* eviction_device;
	check_error(cudaMalloc((void**)&eviction_device, sizeof(int)*numvals));
	check_error(cudaMemcpy(eviction_device, eviction_host, sizeof(int)*numvals, cudaMemcpyHostToDevice));

	// launch kernel 
	hammer_attempt<<<blocks, threads>>>(data_device, indices_device, eviction_device, iterations, numvals, stride);
	check_error(cudaDeviceSynchronize()); 
}

	.file	"tmpxft_003679f2_00000000-6_indirect_hammer.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2044:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2044:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"FAILED WITH ERROR: \"%s\".\n"
	.text
	.globl	_Z11check_error9cudaError
	.type	_Z11check_error9cudaError, @function
_Z11check_error9cudaError:
.LFB2040:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L2
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	cudaGetErrorString@PLT
	movl	$2, %edi
	leaq	.LC0(%rip), %rsi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L2:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2040:
	.size	_Z11check_error9cudaError, .-_Z11check_error9cudaError
	.globl	_Z41__device_stub__Z14hammer_attemptPiS_S_iiiPiS_S_iii
	.type	_Z41__device_stub__Z14hammer_attemptPiS_S_iiiPiS_S_iii, @function
_Z41__device_stub__Z14hammer_attemptPiS_S_iiiPiS_S_iii:
.LFB2066:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	84(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movl	%ecx, 20(%rsp)
	leaq	64(%rsp), %rcx
	movl	%r8d, 16(%rsp)
	movl	%r9d, 12(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L8
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z14hammer_attemptPiS_S_iii(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 208
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L8:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2066:
	.size	_Z41__device_stub__Z14hammer_attemptPiS_S_iiiPiS_S_iii, .-_Z41__device_stub__Z14hammer_attemptPiS_S_iiiPiS_S_iii
	.globl	_Z14hammer_attemptPiS_S_iii
	.type	_Z14hammer_attemptPiS_S_iii, @function
_Z14hammer_attemptPiS_S_iii:
.LFB2067:
	.cfi_startproc
	endbr64
	jmp	_Z41__device_stub__Z14hammer_attemptPiS_S_iiiPiS_S_iii
	.cfi_endproc
.LFE2067:
	.size	_Z14hammer_attemptPiS_S_iii, .-_Z14hammer_attemptPiS_S_iii
	.section	.rodata.str1.1
.LC1:
	.string	"%d\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2041:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	8(%rsi), %rdi
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	call	atoi@PLT
	movq	16(%rbx), %rdi
	movl	%eax, %r13d
	call	atoi@PLT
	movq	24(%rbx), %rdi
	movl	%eax, %r14d
	call	atoi@PLT
	movq	32(%rbx), %rdi
	movl	%eax, %ebp
	call	atoi@PLT
	xorl	%edi, %edi
	movl	%eax, %r15d
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
	movl	$3932160, %edi
	call	malloc@PLT
	movq	%rax, %rbx
.L14:
	call	rand@PLT
	movl	%eax, (%rbx,%r12,4)
	incq	%r12
	cmpq	$983040, %r12
	jne	.L14
	movl	$3932160, %esi
	leaq	24(%rsp), %rdi
	xorl	%r12d, %r12d
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movq	24(%rsp), %rdi
	movq	%rbx, %rsi
	movl	$1, %ecx
	movl	$3932160, %edx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movl	$32, %edi
	call	malloc@PLT
	movq	%rax, %rbx
.L15:
	call	rand@PLT
	movl	$24576, %esi
	movl	$2, %edi
	cltd
	idivl	%esi
	imull	$49512, %r12d, %eax
	leaq	.LC1(%rip), %rsi
	addl	%eax, %edx
	xorl	%eax, %eax
	movl	%edx, (%rbx,%r12,4)
	incq	%r12
	call	__printf_chk@PLT
	cmpq	$8, %r12
	jne	.L15
	movl	$32, %esi
	leaq	32(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movq	32(%rsp), %rdi
	movq	%rbx, %rsi
	movl	$1, %ecx
	movl	$32, %edx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movl	$786432, %eax
	cltd
	idivl	%ebp
	movslq	%eax, %r12
	leaq	0(,%r12,4), %rax
	movq	%r12, %rbx
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	movl	$393216, %edx
	movq	%rax, %r12
	xorl	%eax, %eax
.L16:
	cmpl	%eax, %ebx
	jle	.L23
	movl	%edx, (%r12,%rax,4)
	incq	%rax
	addl	%ebp, %edx
	jmp	.L16
.L23:
	movq	8(%rsp), %rsi
	leaq	40(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movq	8(%rsp), %rdx
	movq	40(%rsp), %rdi
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movl	%r14d, 60(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	movl	%r13d, 48(%rsp)
	movq	%rax, 64(%rsp)
	movl	68(%rsp), %ecx
	movq	%rax, 52(%rsp)
	movq	60(%rsp), %rdx
	movq	48(%rsp), %rdi
	movl	56(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L18
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	movl	%ebp, %r9d
	movl	%ebx, %r8d
	movq	24(%rsp), %rdi
	movl	%r15d, %ecx
	call	_Z41__device_stub__Z14hammer_attemptPiS_S_iiiPiS_S_iii
.L18:
	call	cudaDeviceSynchronize@PLT
	movl	%eax, %edi
	call	_Z11check_error9cudaError
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L19
	call	__stack_chk_fail@PLT
.L19:
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	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
.LFE2041:
	.size	main, .-main
	.section	.rodata.str1.1
.LC2:
	.string	"_Z14hammer_attemptPiS_S_iii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2069:
	.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	.LC2(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z14hammer_attemptPiS_S_iii(%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
.LFE2069:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

// cache size constant for convenience/readability 
const size_t CACHESIZE = 1.5 * (1<<20); 

// function to handle CUDA API call error val returns
void check_error(hipError_t cudaerr) {
    if (cudaerr != hipSuccess) {
        printf("FAILED WITH ERROR: \"%s\".\n", hipGetErrorString(cudaerr));
        exit(-1);
    }   
}

// hammer attempt test kernel
__global__  void hammer_attempt(int* data, int* indices, int* eviction, int iterations, int numvals, int stride) {
	uint tid = threadIdx.x + blockIdx.x * blockDim.x;
	uint nthreads = blockDim.x * gridDim.x;
	
	int sum;
	for (int j = 0; j < iterations; j++) {	
	
			
		if (tid < 8) {
			int index = indices[tid];
			int n = data[index];
			sum += (n << 1); 
		}
		
		
		
		/*
		if (tid == 0) {
			int n1 = data[1413];
			int n2 = data[56102];
			int n3 = data[101336];
			int n4 = data[169659];
			int n5 = data[199685];
			int n6 = data[272110];
			int n7 = data[309174];
			int n8 = data[368505];
			sum = sum + n1 - n2 + n3 - n4 + n5 - n6 + n7 - n8;
		}
		*/
		
		//__syncthreads();	
		for (int i = (tid * stride) + 393216; i < 393216*2.5; i += (nthreads * stride)) {
			int n = data[i];
			sum -= (n >> 1);
		//	__syncthreads();
		}

		/*
		for (int i = 393216; i < 393216*3; i+=8) {
			int n = data[i];
			sum -= (n >> 1); 
		}
		*/
		
		/*
		for (int i = tid; i < numvals; i += nthreads) {
			int n = data[eviction[i]];
			sum -= (n >> 1); 
		}
		*/
		
	}
}

int main(int argc, char** argv) {
	// command line params, seed rand
	int blocks = atoi(argv[1]);
    int threads = atoi(argv[2]);
    int stride = atoi(argv[3]);
	int iterations = atoi(argv[4]);
	srand(time(NULL));

	// host array of random vals
	int* data_host = (int*) malloc(2.5*CACHESIZE);
	for (int i = 0; i < (2.5*CACHESIZE)/4; i++) {
		data_host[i] = rand(); 
	}

	// copy to device
	int* data_device;
	check_error(hipMalloc((void**)&data_device, 2.5*CACHESIZE));
	check_error(hipMemcpy(data_device, data_host, 2.5*CACHESIZE, hipMemcpyHostToDevice));

	// host array for addresses
	// pick addresses in first 1.5MB of data
	// - explanation of values -
	// 393216: number of integers containted in 1.5MB cache
	// 49512: number of integers in a 1/8 slice of cache
	// 24576: number of integers in a 1/16 slice of cache
	// address choosing process: pick address in first 1/16, skip next 1/16, pick address in next 1/16...
	int* indices_host = (int*)malloc(sizeof(int)*8); 	 
	for (int i = 0; i < 8; i++) {
		int sector_start = i * 49512; 
		indices_host[i] = (rand() % 24576) + sector_start;
		printf("%d\n", indices_host[i]);
	}

	// copy to device
	int* indices_device;
	check_error(hipMalloc((void**)&indices_device, sizeof(int)*8));
	check_error(hipMemcpy(indices_device, indices_host, sizeof(int)*8, hipMemcpyHostToDevice));

	// host array of indices for eviction purposes
	int numvals = (393216*2) / stride;
	int* eviction_host = (int*)malloc(sizeof(int)*numvals);
	for (int i = 0; i < numvals; i++) {
		eviction_host[i] = 393216 + (i * stride);
	}

	// copy to device
	int* eviction_device;
	check_error(hipMalloc((void**)&eviction_device, sizeof(int)*numvals));
	check_error(hipMemcpy(eviction_device, eviction_host, sizeof(int)*numvals, hipMemcpyHostToDevice));

	// launch kernel 
	hammer_attempt<<<blocks, threads>>>(data_device, indices_device, eviction_device, iterations, numvals, stride);
	check_error(hipDeviceSynchronize()); 
}

	.text
	.file	"indirect_hammer.hip"
	.globl	_Z11check_error10hipError_t     # -- Begin function _Z11check_error10hipError_t
	.type	_Z11check_error10hipError_t,@function
_Z11check_error10hipError_t:            # @_Z11check_error10hipError_t
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jne	.LBB0_2
# %bb.1:
	retq
.LBB0_2:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$-1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z11check_error10hipError_t, .Lfunc_end0-_Z11check_error10hipError_t
	.cfi_endproc
                                        # -- End function
	.globl	_Z29__device_stub__hammer_attemptPiS_S_iii # -- Begin function _Z29__device_stub__hammer_attemptPiS_S_iii
	.type	_Z29__device_stub__hammer_attemptPiS_S_iii,@function
_Z29__device_stub__hammer_attemptPiS_S_iii: # @_Z29__device_stub__hammer_attemptPiS_S_iii
	.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	$144, %rsp
	.cfi_def_cfa_offset 192
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	20(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	16(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	12(%rsp), %r8
	movl	%r9d, (%r8)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	32(%rsp), %r12
	leaq	24(%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	$_Z14hammer_attemptPiS_S_iii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$160, %rsp
	.cfi_adjust_cfa_offset -160
	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	_Z29__device_stub__hammer_attemptPiS_S_iii, .Lfunc_end1-_Z29__device_stub__hammer_attemptPiS_S_iii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function 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	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %r12
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, 12(%rsp)                  # 4-byte Spill
	movq	16(%r12), %rdi
	callq	atoi
	movl	%eax, %r15d
	movq	24(%r12), %rdi
	callq	atoi
	movl	%eax, %ebx
	movq	32(%r12), %rdi
	callq	atoi
	movl	%eax, 8(%rsp)                   # 4-byte Spill
	xorl	%r14d, %r14d
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	movl	$3932160, %edi                  # imm = 0x3C0000
	callq	malloc
	movq	%rax, %r12
.LBB2_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	movl	%eax, (%r12,%r14,4)
	incq	%r14
	cmpq	$983040, %r14                   # imm = 0xF0000
	jne	.LBB2_1
# %bb.2:
	leaq	32(%rsp), %r13
	movl	$3932160, %esi                  # imm = 0x3C0000
	movq	%r13, %rdi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movq	(%r13), %rdi
	movl	$3932160, %edx                  # imm = 0x3C0000
	movq	%r12, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movl	$32, %edi
	callq	malloc
	movq	%rax, %r12
	movq	%rax, %r14
	xorl	%r13d, %r13d
.LBB2_3:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	movslq	%eax, %rsi
	imulq	$715827883, %rsi, %rax          # imm = 0x2AAAAAAB
	movq	%rax, %rcx
	shrq	$63, %rcx
	shrq	$44, %rax
	addl	%ecx, %eax
	shll	$13, %eax
	leal	(%rax,%rax,2), %eax
	subl	%eax, %esi
	addl	%r13d, %esi
	movl	%esi, (%r14)
	movl	$.L.str.1, %edi
                                        # kill: def $esi killed $esi killed $rsi
	xorl	%eax, %eax
	callq	printf
	addq	$49512, %r13                    # imm = 0xC168
	addq	$4, %r14
	cmpq	$396096, %r13                   # imm = 0x60B40
	jne	.LBB2_3
# %bb.4:
	leaq	24(%rsp), %r13
	movl	$32, %esi
	movq	%r13, %rdi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movq	(%r13), %rdi
	movl	$32, %edx
	movq	%r12, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movl	$786432, %eax                   # imm = 0xC0000
	xorl	%edx, %edx
	idivl	%ebx
	movl	%eax, %r12d
	movslq	%eax, %r13
	shlq	$2, %r13
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, %rbp
	testl	%r12d, %r12d
	jle	.LBB2_7
# %bb.5:                                # %.lr.ph.preheader
	movl	%r12d, %eax
	movl	$393216, %ecx                   # imm = 0x60000
	xorl	%edx, %edx
.LBB2_6:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%ecx, (%rbp,%rdx,4)
	incq	%rdx
	addl	%ebx, %ecx
	cmpq	%rdx, %rax
	jne	.LBB2_6
.LBB2_7:                                # %._crit_edge
	leaq	16(%rsp), %r14
	movq	%r14, %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movq	(%r14), %rdi
	movq	%rbp, %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	movl	12(%rsp), %edi                  # 4-byte Reload
	btsq	$32, %rdi
	movl	%r15d, %edx
	btsq	$32, %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_9
# %bb.8:
	movq	32(%rsp), %rdi
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdx
	movl	8(%rsp), %ecx                   # 4-byte Reload
	movl	%r12d, %r8d
	movl	%ebx, %r9d
	callq	_Z29__device_stub__hammer_attemptPiS_S_iii
.LBB2_9:
	callq	hipDeviceSynchronize
	movl	%eax, %edi
	callq	_Z11check_error10hipError_t
	xorl	%eax, %eax
	addq	$40, %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_end2:
	.size	main, .Lfunc_end2-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:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB3_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB3_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z14hammer_attemptPiS_S_iii, %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_end3:
	.size	__hip_module_ctor, .Lfunc_end3-__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	.LBB4_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
.LBB4_2:
	retq
.Lfunc_end4:
	.size	__hip_module_dtor, .Lfunc_end4-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"FAILED WITH ERROR: \"%s\".\n"
	.size	.L.str, 26

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

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"%d\n"
	.size	.L.str.1, 4

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z14hammer_attemptPiS_S_iii"
	.size	.L__unnamed_1, 28

	.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

	.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 _Z29__device_stub__hammer_attemptPiS_S_iii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z14hammer_attemptPiS_S_iii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z14hammer_attemptPiS_S_iii ; -- Begin function _Z14hammer_attemptPiS_S_iii
	.globl	_Z14hammer_attemptPiS_S_iii
	.p2align	8
	.type	_Z14hammer_attemptPiS_S_iii,@function
_Z14hammer_attemptPiS_S_iii:            ; @_Z14hammer_attemptPiS_S_iii
; %bb.0:                                ; %._crit_edge31
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z14hammer_attemptPiS_S_iii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 36
		.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 1
		.amdhsa_next_free_sgpr 1
		.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	_Z14hammer_attemptPiS_S_iii, .Lfunc_end0-_Z14hammer_attemptPiS_S_iii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 4
; NumSgprs: 0
; NumVgprs: 0
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 0
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 1
; NumVGPRsForWavesPerEU: 1
; 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
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         28
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 36
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z14hammer_attemptPiS_S_iii
    .private_segment_fixed_size: 0
    .sgpr_count:     0
    .sgpr_spill_count: 0
    .symbol:         _Z14hammer_attemptPiS_S_iii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     0
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
