#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

__device__ void _3Dstencil_(float *d_e,float *d_r,int X,int Y,int Z,int k, int x, int y,int z)
{     
    int h_r_i = x + ( y * (X) ) + ( z* (X*Y) );
    int h_e_i = h_r_i;
    d_r[h_r_i] = d_e[h_e_i];
    for(int lk =0;lk<(k/2);lk++)
    {
        h_e_i = (x+lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];

        h_e_i = (x-lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];

        h_e_i = (x) + ( (y+lk) * (X) ) + ( (z) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];

        h_e_i = (x) + ( (y-lk) * (X) ) + ( (z) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];


        h_e_i = (x) + ( (y) * (X) ) + ( (z+lk) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];

        h_e_i = (x) + ( (y) * (X) ) + ( (z-lk) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];
    }  
}
__global__ void _3Dstencil_SharedMemory_Cube(float *d_e,float *d_r,int X,int Y,int Z,int k,int t)
{
    int x,y,globalidx,localIdx;
    x = threadIdx.x + (blockIdx.x*blockDim.x);
    y = threadIdx.y + (blockIdx.y*blockDim.y);
    int k2=k/2;

    //conta para tamanho do cubo em cada dimensão DIM+t*k2

    extern __shared__ float cube[];
    for(int z=0;z<Z;z++)
    {
        globalidx = x + y*X + z*(X*Y);
        localIdx = threadIdx.x + threadIdx.y*blockDim.x + z*(blockDim.x*blockDim.y);
        cube[localIdx] = d_e[globalidx];
    }
    for(int z=k2;z<Z-k2;z++)
    {
        _3Dstencil_(d_e,d_r,X,Y,Z,k,x+k2,y+k2,z);
    }
}

/*
*argumentos
*1 - n_elementos
*2 - threads por bloco
*3 - n_blocos
*4 - print
*/
int main(int argc, char* argv[]) {

    float *h_e,*h_r,*h_r_test;
    float *d_e, *d_r;
    int size,tam,times,sharedSize;
    clock_t Ticks[2];

    

    

    times = 1;
    int X=8;
    int Y=8;
    int BX=8;
    int BY=8;
    int Z=4;
    int k=2;
    int GX=1;
    int GY=1;

    if(argc > 1)
    {
        X = atoi(argv[1]);
        BX=X;
    }
      
    if(argc > 2)
    {
        Y = atoi(argv[2]);
        BY = Y;
    }
      
    if(argc > 3)
      Z = atoi(argv[3]);
    if(argc > 4)
      k = atoi(argv[4]);

    if(X>32)
    {
        GX = ceil((float)X/(float)32);
        BX = 32;
    }
    if(Y>32)
    {
        GY = ceil((float)Y/(float)32);
        BY = 32;
    }
    
    
    dim3 block_dim(BX,BY,1);
    dim3 grid_dim(GX,GY,1);

    sharedSize = ((block_dim.x+k)*(block_dim.y+k)*(Z+k))*sizeof(float);
    size = (X+k) * (Y+k) * (Z+k) * sizeof(float);
    tam = X * Y * Z;

    X=X+k;
    Y=Y+k;
    Z=Z+k;


    h_e = (float*) malloc(size);
    h_r = (float*) malloc(size);
    h_r_test = (float*) malloc(size);
    cudaMalloc(&d_e, size);
    cudaMalloc(&d_r, size);


    for (int i = 0; i < size/sizeof(float); i++) {
        h_e[i] = (float)(rand()%9000)/100.0;
        h_r[i] = 0;
    }

    /* Copy vectors from host memory to device memory */
    cudaMemcpy(d_e, h_e, size, cudaMemcpyHostToDevice);
    cudaMemcpy(d_r, h_r, size, cudaMemcpyHostToDevice);
    int k2 = k/2;
    for(int t =0; t<times; t++)
    {

        for(int z=k2;  z<Z-k2;  z++)
        {
        
            for(int y=k2;  y<Y-k2;  y++)
            {
                for(int x=k2;  x<X-k2;  x++)
                {
                    
                    
                    int h_r_i = x + ( y * (X) ) + ( z* (X*Y) );
                        
                    int h_e_i = h_r_i;
                    h_r_test[h_r_i] = h_e[h_e_i];
                    for(int lk =0;lk<(k/2);lk++)
                        {
                            

                            
                            if(x+lk >= X)
                                h_e_i = (x-lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
                            else
                                h_e_i = (x+lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];

                            if(x-lk < 0)
                                h_e_i = (x+lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
                            else
                                h_e_i = (x-lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];


                            if(y+lk >= Y)
                                h_e_i = (x) + ( (y-lk) * (X) ) + ( (z) * (X*Y) );
                            else
                                h_e_i = (x) + ( (y+lk) * (X) ) + ( (z) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];

                            if(y-lk < 0)
                                h_e_i = (x) + ( (y+lk) * (X) ) + ( (z) * (X*Y) );
                            else
                                h_e_i = (x) + ( (y-lk) * (X) ) + ( (z) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];


                            if(z+lk >= Z)
                                h_e_i = (x) + ( (y) * (X) ) + ( (z-lk) * (X*Y) );
                            else
                                h_e_i = (x) + ( (y) * (X) ) + ( (z+lk) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];

                            if(z-lk < 0)
                                h_e_i = (x) + ( (y) * (X) ) + ( (z+lk) * (X*Y) );
                            else
                                h_e_i = (x) + ( (y) * (X) ) + ( (z-lk) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];

                        }  
                }
            }
            
        }

        for (int i = 0; i < tam; i++) 
        {
            h_e[i] = h_r_test[i];
        }

    }


    cudaEvent_t start, stop;
    cudaEventCreate (&start);
    cudaEventCreate (&stop);
    cudaEventRecord (start, 0); 

    /******************
    *** Kernel Call ***
    *******************/
    //_3Dstencil_global<<<blks,th_p_blk>>>(d_e,d_r,X,Y,Z);
    _3Dstencil_SharedMemory_Cube<<<grid_dim,block_dim,sharedSize>>>(d_e,d_r,X,Y,Z,k,times);

    cudaError_t err = cudaSuccess;
    err = cudaGetLastError();
    if (err != cudaSuccess)
    {
        fprintf(stderr, "Failed to launch _3Dstencil_global kernel (error code %s)!\n", cudaGetErrorString(err));
    }
    /******************
    *** Kernel Call ***
    *******************/



    cudaDeviceSynchronize();
    cudaEventRecord (stop, 0);
    cudaEventSynchronize (stop);
    float elapsedTime;
    cudaEventElapsedTime (&elapsedTime, start, stop);
    cudaEventDestroy(start);
    cudaEventDestroy(stop);

    
    Ticks[1] = clock();
    double Tempo = (Ticks[1] - Ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
    printf("X %d || Y %d \nBX %d || BY %d\nZ %d \n",X,Y,BX,BY,Z);
    printf ("[%d,%.5f,%lf],\n", tam,elapsedTime,Tempo/1000.0);
 
    cudaMemcpy(h_r, d_r, size, cudaMemcpyDeviceToHost);

    bool certo=true;
    //printf("threads/blk %d -- blocks %d\n",th_p_blk,blks);
    for (int i = 0; i < tam; i++){
        //printf("%d - %d\n",h_z_res[i],h_z[i]);
        if(h_r_test[i] != h_r[i])
          certo=false;
    }
    if(!certo)
    printf("\n*****\n certo = %s\n*****\n", certo ? "true" : "false");

    cudaFree(d_e);
    cudaFree(d_r);
    std::free(h_e);
    std::free(h_r);
    std::free(h_r_test);

    return 0;
} /* main */


	.file	"tmpxft_0035c8ce_00000000-6_3DstencilGPUGlobalMemoryCube.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
	.globl	_Z11_3Dstencil_PfS_iiiiiii
	.type	_Z11_3Dstencil_PfS_iiiiiii, @function
_Z11_3Dstencil_PfS_iiiiiii:
.LFB2040:
	.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
.LFE2040:
	.size	_Z11_3Dstencil_PfS_iiiiiii, .-_Z11_3Dstencil_PfS_iiiiiii
	.globl	_Z55__device_stub__Z28_3Dstencil_SharedMemory_CubePfS_iiiiiPfS_iiiii
	.type	_Z55__device_stub__Z28_3Dstencil_SharedMemory_CubePfS_iiiiiPfS_iiiii, @function
_Z55__device_stub__Z28_3Dstencil_SharedMemory_CubePfS_iiiiiPfS_iiiii:
.LFB2066:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	40(%rsp), %rcx
	movl	%r8d, 4(%rsp)
	movl	%r9d, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 128(%rsp)
	movq	%rsp, %rax
	movq	%rax, 136(%rsp)
	leaq	176(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L4
	pushq	40(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 192
	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 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L4:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L6
	call	__stack_chk_fail@PLT
.L6:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2066:
	.size	_Z55__device_stub__Z28_3Dstencil_SharedMemory_CubePfS_iiiiiPfS_iiiii, .-_Z55__device_stub__Z28_3Dstencil_SharedMemory_CubePfS_iiiiiPfS_iiiii
	.globl	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii
	.type	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii, @function
_Z28_3Dstencil_SharedMemory_CubePfS_iiiii:
.LFB2067:
	.cfi_startproc
	endbr64
	jmp	_Z55__device_stub__Z28_3Dstencil_SharedMemory_CubePfS_iiiiiPfS_iiiii
	.cfi_endproc
.LFE2067:
	.size	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii, .-_Z28_3Dstencil_SharedMemory_CubePfS_iiiii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"Failed to launch _3Dstencil_global kernel (error code %s)!\n"
.LC6:
	.string	"X %d || Y %d \nBX %d || BY %d\nZ %d \n"
.LC7:
	.string	"[%d,%.5f,%lf],\n"
.LC8:
	.string	"false"
.LC9:
	.string	"\n*****\n certo = %s\n*****\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
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$200, %rsp
	.cfi_def_cfa_offset 256
	movq	%fs:40, %rax
	movq	%rax, 184(%rsp)
	xorl	%eax, %eax
	cmpl	$1, %edi
	jle	.L47
	movl	%edi, %ebx
	movq	8(%rsi), %rdi
	movq	%rsi, %r12
	call	atoi@PLT
	movl	%eax, %ebp
	cmpl	$2, %ebx
	je	.L12
	movq	16(%r12), %rdi
	call	atoi@PLT
	movl	%eax, %r15d
	cmpl	$3, %ebx
	je	.L48
	movq	24(%r12), %rdi
	call	atoi@PLT
	movl	%eax, %r14d
	cmpl	$4, %ebx
	je	.L49
	movq	32(%r12), %rdi
	call	atoi@PLT
	movl	%eax, %ebx
	jmp	.L13
.L48:
	movl	$4, %r14d
.L49:
	movl	$2, %ebx
.L13:
	cmpl	$32, %ebp
	jg	.L45
	jmp	.L60
.L12:
	cmpl	$32, %eax
	jle	.L61
	movl	$8, %r15d
	movl	$4, %r14d
.L45:
	cvtsi2ssl	%ebp, %xmm0
	mulss	.LC0(%rip), %xmm0
	call	ceilf@PLT
	movl	$32, 12(%rsp)
	cvttss2sil	%xmm0, %r12d
	jmp	.L11
.L47:
	movl	$8, %r15d
	movl	$8, %ebp
	movl	$2, %ebx
	movl	$8, 12(%rsp)
	movl	$4, %r14d
	movl	$1, %r12d
	jmp	.L51
.L60:
	movl	%ebp, 12(%rsp)
	movl	$1, %r12d
.L11:
	cmpl	$32, %r15d
	jle	.L51
	cvtsi2ssl	%r15d, %xmm0
	mulss	.LC0(%rip), %xmm0
	call	ceilf@PLT
	movl	$32, 24(%rsp)
	cvttss2sil	%xmm0, %eax
	jmp	.L14
.L51:
	movl	%r15d, 24(%rsp)
	movl	$1, %eax
.L14:
	movl	%eax, 176(%rsp)
	leal	(%r14,%rbx), %eax
	movl	24(%rsp), %esi
	movl	%eax, 8(%rsp)
	movl	12(%rsp), %eax
	leal	(%rsi,%rbx), %ecx
	movl	%r12d, 172(%rsp)
	leal	(%rbx,%rbp), %r12d
	movl	$1, 168(%rsp)
	addl	%ebx, %eax
	movl	$1, 180(%rsp)
	imull	%ecx, %eax
	movl	8(%rsp), %ecx
	imull	%ecx, %eax
	sall	$2, %eax
	movl	%eax, 84(%rsp)
	leal	(%rbx,%r15), %eax
	movl	%eax, %r13d
	movl	%eax, 28(%rsp)
	imull	%ebp, %r15d
	movl	%ecx, %eax
	imull	%r12d, %r13d
	imull	%r14d, %r15d
	imull	%r13d, %eax
	movl	%r15d, 40(%rsp)
	xorl	%r15d, %r15d
	sall	$2, %eax
	cltq
	movq	%rax, %rdi
	movq	%rax, (%rsp)
	call	malloc@PLT
	movq	(%rsp), %rdi
	movq	%rax, %rbp
	call	malloc@PLT
	movq	(%rsp), %rdi
	movq	%rax, 16(%rsp)
	call	malloc@PLT
	movq	(%rsp), %rsi
	leaq	128(%rsp), %rdi
	movq	%rax, %r14
	call	cudaMalloc@PLT
	movq	(%rsp), %rsi
	leaq	136(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	(%rsp), %rax
	shrq	$2, %rax
	movq	%rax, 32(%rsp)
.L15:
	cmpq	%r15, 32(%rsp)
	je	.L62
	call	rand@PLT
	movl	$9000, %esi
	cltd
	idivl	%esi
	movq	16(%rsp), %rax
	movl	$0x00000000, (%rax,%r15,4)
	cvtsi2ssl	%edx, %xmm0
	divss	.LC1(%rip), %xmm0
	movss	%xmm0, 0(%rbp,%r15,4)
	incq	%r15
	jmp	.L15
.L62:
	movq	(%rsp), %rdx
	movq	128(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	call	cudaMemcpy@PLT
	movq	(%rsp), %rdx
	movq	16(%rsp), %rsi
	movl	$1, %ecx
	movq	136(%rsp), %rdi
	call	cudaMemcpy@PLT
	movl	%ebx, %eax
	movl	$2, %ecx
	cltd
	idivl	%ecx
	leal	1(%r12,%r13), %edx
	movl	%eax, %ecx
	imull	%eax, %edx
	movl	%eax, %r8d
	movl	%eax, %r10d
	imull	%r12d, %ecx
	imull	%r13d, %r8d
	movl	%edx, 76(%rsp)
	movl	%ecx, 88(%rsp)
	movl	8(%rsp), %ecx
	subl	%eax, %ecx
	movl	%ecx, 92(%rsp)
	movl	28(%rsp), %ecx
	subl	%eax, %ecx
	movl	%ecx, 104(%rsp)
	movl	%r12d, %ecx
	subl	%eax, %ecx
	movl	%ecx, 108(%rsp)
.L17:
	cmpl	%r10d, 92(%rsp)
	jle	.L52
	movl	76(%rsp), %ecx
	movl	88(%rsp), %r9d
	movl	%eax, %edi
	movl	%ecx, 32(%rsp)
	movslq	%r8d, %rcx
	movq	%rcx, 96(%rsp)
.L36:
	cmpl	%edi, 104(%rsp)
	jle	.L33
	leal	(%r9,%rax), %edx
	movslq	%eax, %rcx
	leal	(%rdx,%r8), %esi
	movl	%esi, 52(%rsp)
	movq	96(%rsp), %rsi
	leaq	(%rsi,%rcx), %rdx
	movslq	%r9d, %rsi
	addq	%rsi, %rdx
	leaq	0(,%rdx,4), %rsi
	movl	32(%rsp), %edx
	movl	%edx, 44(%rsp)
	movl	%edx, 48(%rsp)
.L34:
	movl	%ecx, 80(%rsp)
	cmpl	%ecx, 108(%rsp)
	jle	.L63
	movl	44(%rsp), %edx
	movss	0(%rbp,%rsi), %xmm0
	movl	%edx, 56(%rsp)
	movl	%edx, 60(%rsp)
	movl	48(%rsp), %edx
	movl	%edx, 64(%rsp)
	movl	%edx, 68(%rsp)
	movl	52(%rsp), %edx
	movl	%edx, 72(%rsp)
	movl	%edx, %r11d
	xorl	%edx, %edx
.L18:
	cmpl	%eax, %edx
	jge	.L64
	movl	80(%rsp), %r15d
	addl	%edx, %r15d
	cmpl	%r15d, %r12d
	movl	56(%rsp), %r15d
	cmovg	60(%rsp), %r15d
	cmpl	%edx, 80(%rsp)
	movslq	%r15d, %r15
	addss	0(%rbp,%r15,4), %xmm0
	movl	60(%rsp), %r15d
	cmovns	56(%rsp), %r15d
	movslq	%r15d, %r15
	addss	0(%rbp,%r15,4), %xmm0
	leal	(%rdi,%rdx), %r15d
	cmpl	%r15d, 28(%rsp)
	movl	64(%rsp), %r15d
	cmovg	68(%rsp), %r15d
	cmpl	%edx, %edi
	movslq	%r15d, %r15
	addss	0(%rbp,%r15,4), %xmm0
	movl	68(%rsp), %r15d
	cmovns	64(%rsp), %r15d
	movslq	%r15d, %r15
	addss	0(%rbp,%r15,4), %xmm0
	leal	(%r10,%rdx), %r15d
	cmpl	%r15d, 8(%rsp)
	movl	72(%rsp), %r15d
	cmovg	%r11d, %r15d
	cmpl	%edx, %r10d
	movslq	%r15d, %r15
	addss	0(%rbp,%r15,4), %xmm0
	movl	72(%rsp), %r15d
	cmovs	%r11d, %r15d
	subl	%r13d, 72(%rsp)
	incl	%edx
	addl	%r13d, %r11d
	addl	%r12d, 68(%rsp)
	movslq	%r15d, %r15
	subl	%r12d, 64(%rsp)
	incl	60(%rsp)
	addss	0(%rbp,%r15,4), %xmm0
	decl	56(%rsp)
	jmp	.L18
.L64:
	movss	%xmm0, (%r14,%rsi)
	incq	%rcx
	addq	$4, %rsi
	incl	52(%rsp)
	incl	48(%rsp)
	incl	44(%rsp)
	jmp	.L34
.L63:
	addl	%r12d, 32(%rsp)
	incl	%edi
	addl	%r12d, %r9d
	jmp	.L36
.L33:
	addl	%r13d, 76(%rsp)
	incl	%r10d
	addl	%r13d, %r8d
	jmp	.L17
.L52:
	xorl	%eax, %eax
.L35:
	cmpl	%eax, 40(%rsp)
	jle	.L65
	movss	(%r14,%rax,4), %xmm0
	movss	%xmm0, 0(%rbp,%rax,4)
	incq	%rax
	jmp	.L35
.L65:
	leaq	144(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	152(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	144(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	12(%rsp), %eax
	xorl	%r9d, %r9d
	movl	168(%rsp), %ecx
	movslq	84(%rsp), %r8
	movq	172(%rsp), %rdi
	movl	%eax, 160(%rsp)
	movl	24(%rsp), %eax
	movl	180(%rsp), %esi
	movl	%eax, 164(%rsp)
	movq	160(%rsp), %rdx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L38
	pushq	%rsi
	.cfi_def_cfa_offset 264
	movl	%ebx, %r9d
	movl	%r12d, %edx
	pushq	$1
	.cfi_def_cfa_offset 272
	movl	24(%rsp), %r8d
	movl	44(%rsp), %ecx
	movq	152(%rsp), %rsi
	movq	144(%rsp), %rdi
	call	_Z55__device_stub__Z28_3Dstencil_SharedMemory_CubePfS_iiiiiPfS_iiiii
	popq	%rdi
	.cfi_def_cfa_offset 264
	popq	%r8
	.cfi_def_cfa_offset 256
.L38:
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L39
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC3(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L39:
	call	cudaDeviceSynchronize@PLT
	movq	152(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	152(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	152(%rsp), %rdx
	leaq	124(%rsp), %rdi
	movq	144(%rsp), %rsi
	call	cudaEventElapsedTime@PLT
	movq	144(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	152(%rsp), %rdi
	call	cudaEventDestroy@PLT
	call	clock@PLT
	movl	%r12d, %edx
	movl	$2, %edi
	leaq	.LC6(%rip), %rsi
	cvtsi2sdq	%rax, %xmm1
	mulsd	.LC4(%rip), %xmm1
	divsd	.LC5(%rip), %xmm1
	movsd	%xmm1, 32(%rsp)
	pushq	%rax
	.cfi_def_cfa_offset 264
	movl	16(%rsp), %eax
	pushq	%rax
	.cfi_def_cfa_offset 272
	movl	40(%rsp), %r9d
	xorl	%eax, %eax
	movl	28(%rsp), %r8d
	movl	44(%rsp), %ecx
	call	__printf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 264
	popq	%rcx
	.cfi_def_cfa_offset 256
	leaq	.LC7(%rip), %rsi
	movl	40(%rsp), %edx
	movl	$2, %edi
	movb	$2, %al
	movsd	32(%rsp), %xmm1
	cvtss2sd	124(%rsp), %xmm0
	divsd	.LC4(%rip), %xmm1
	call	__printf_chk@PLT
	movq	(%rsp), %rdx
	movq	16(%rsp), %rdi
	movl	$2, %ecx
	movq	136(%rsp), %rsi
	call	cudaMemcpy@PLT
	xorl	%eax, %eax
	movb	$1, %dl
.L40:
	cmpl	%eax, 40(%rsp)
	jle	.L66
	movq	16(%rsp), %rbx
	movss	(%r14,%rax,4), %xmm0
	ucomiss	(%rbx,%rax,4), %xmm0
	jp	.L53
	je	.L41
.L53:
	xorl	%edx, %edx
.L41:
	incq	%rax
	jmp	.L40
.L66:
	testb	%dl, %dl
	jne	.L44
	leaq	.LC8(%rip), %rdx
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L44:
	movq	128(%rsp), %rdi
	call	cudaFree@PLT
	movq	136(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	free@PLT
	movq	%r14, %rdi
	call	free@PLT
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L46
	call	__stack_chk_fail@PLT
.L61:
	movl	%eax, 12(%rsp)
	movl	$8, %r15d
	movl	$1, %eax
	movl	$1, %r12d
	movl	$8, 24(%rsp)
	movl	$4, %r14d
	jmp	.L14
.L46:
	addq	$200, %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
.LC10:
	.string	"_Z28_3Dstencil_SharedMemory_CubePfS_iiiii"
	.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	.LC10(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii(%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
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	1023410176
	.align 4
.LC1:
	.long	1120403456
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC4:
	.long	0
	.long	1083129856
	.align 8
.LC5:
	.long	0
	.long	1093567616
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

__device__ void _3Dstencil_(float *d_e,float *d_r,int X,int Y,int Z,int k, int x, int y,int z)
{     
    int h_r_i = x + ( y * (X) ) + ( z* (X*Y) );
    int h_e_i = h_r_i;
    d_r[h_r_i] = d_e[h_e_i];
    for(int lk =0;lk<(k/2);lk++)
    {
        h_e_i = (x+lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];

        h_e_i = (x-lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];

        h_e_i = (x) + ( (y+lk) * (X) ) + ( (z) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];

        h_e_i = (x) + ( (y-lk) * (X) ) + ( (z) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];


        h_e_i = (x) + ( (y) * (X) ) + ( (z+lk) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];

        h_e_i = (x) + ( (y) * (X) ) + ( (z-lk) * (X*Y) );
        d_r[h_r_i] += d_e[h_e_i];
    }  
}
__global__ void _3Dstencil_SharedMemory_Cube(float *d_e,float *d_r,int X,int Y,int Z,int k,int t)
{
    int x,y,globalidx,localIdx;
    x = threadIdx.x + (blockIdx.x*blockDim.x);
    y = threadIdx.y + (blockIdx.y*blockDim.y);
    int k2=k/2;

    //conta para tamanho do cubo em cada dimensão DIM+t*k2

    extern __shared__ float cube[];
    for(int z=0;z<Z;z++)
    {
        globalidx = x + y*X + z*(X*Y);
        localIdx = threadIdx.x + threadIdx.y*blockDim.x + z*(blockDim.x*blockDim.y);
        cube[localIdx] = d_e[globalidx];
    }
    for(int z=k2;z<Z-k2;z++)
    {
        _3Dstencil_(d_e,d_r,X,Y,Z,k,x+k2,y+k2,z);
    }
}

/*
*argumentos
*1 - n_elementos
*2 - threads por bloco
*3 - n_blocos
*4 - print
*/
int main(int argc, char* argv[]) {

    float *h_e,*h_r,*h_r_test;
    float *d_e, *d_r;
    int size,tam,times,sharedSize;
    clock_t Ticks[2];

    

    

    times = 1;
    int X=8;
    int Y=8;
    int BX=8;
    int BY=8;
    int Z=4;
    int k=2;
    int GX=1;
    int GY=1;

    if(argc > 1)
    {
        X = atoi(argv[1]);
        BX=X;
    }
      
    if(argc > 2)
    {
        Y = atoi(argv[2]);
        BY = Y;
    }
      
    if(argc > 3)
      Z = atoi(argv[3]);
    if(argc > 4)
      k = atoi(argv[4]);

    if(X>32)
    {
        GX = ceil((float)X/(float)32);
        BX = 32;
    }
    if(Y>32)
    {
        GY = ceil((float)Y/(float)32);
        BY = 32;
    }
    
    
    dim3 block_dim(BX,BY,1);
    dim3 grid_dim(GX,GY,1);

    sharedSize = ((block_dim.x+k)*(block_dim.y+k)*(Z+k))*sizeof(float);
    size = (X+k) * (Y+k) * (Z+k) * sizeof(float);
    tam = X * Y * Z;

    X=X+k;
    Y=Y+k;
    Z=Z+k;


    h_e = (float*) malloc(size);
    h_r = (float*) malloc(size);
    h_r_test = (float*) malloc(size);
    hipMalloc(&d_e, size);
    hipMalloc(&d_r, size);


    for (int i = 0; i < size/sizeof(float); i++) {
        h_e[i] = (float)(rand()%9000)/100.0;
        h_r[i] = 0;
    }

    /* Copy vectors from host memory to device memory */
    hipMemcpy(d_e, h_e, size, hipMemcpyHostToDevice);
    hipMemcpy(d_r, h_r, size, hipMemcpyHostToDevice);
    int k2 = k/2;
    for(int t =0; t<times; t++)
    {

        for(int z=k2;  z<Z-k2;  z++)
        {
        
            for(int y=k2;  y<Y-k2;  y++)
            {
                for(int x=k2;  x<X-k2;  x++)
                {
                    
                    
                    int h_r_i = x + ( y * (X) ) + ( z* (X*Y) );
                        
                    int h_e_i = h_r_i;
                    h_r_test[h_r_i] = h_e[h_e_i];
                    for(int lk =0;lk<(k/2);lk++)
                        {
                            

                            
                            if(x+lk >= X)
                                h_e_i = (x-lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
                            else
                                h_e_i = (x+lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];

                            if(x-lk < 0)
                                h_e_i = (x+lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
                            else
                                h_e_i = (x-lk) + ( (y) * (X) ) + ( (z) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];


                            if(y+lk >= Y)
                                h_e_i = (x) + ( (y-lk) * (X) ) + ( (z) * (X*Y) );
                            else
                                h_e_i = (x) + ( (y+lk) * (X) ) + ( (z) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];

                            if(y-lk < 0)
                                h_e_i = (x) + ( (y+lk) * (X) ) + ( (z) * (X*Y) );
                            else
                                h_e_i = (x) + ( (y-lk) * (X) ) + ( (z) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];


                            if(z+lk >= Z)
                                h_e_i = (x) + ( (y) * (X) ) + ( (z-lk) * (X*Y) );
                            else
                                h_e_i = (x) + ( (y) * (X) ) + ( (z+lk) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];

                            if(z-lk < 0)
                                h_e_i = (x) + ( (y) * (X) ) + ( (z+lk) * (X*Y) );
                            else
                                h_e_i = (x) + ( (y) * (X) ) + ( (z-lk) * (X*Y) );
                            h_r_test[h_r_i] += h_e[h_e_i];

                        }  
                }
            }
            
        }

        for (int i = 0; i < tam; i++) 
        {
            h_e[i] = h_r_test[i];
        }

    }


    hipEvent_t start, stop;
    hipEventCreate (&start);
    hipEventCreate (&stop);
    hipEventRecord (start, 0); 

    /******************
    *** Kernel Call ***
    *******************/
    //_3Dstencil_global<<<blks,th_p_blk>>>(d_e,d_r,X,Y,Z);
    _3Dstencil_SharedMemory_Cube<<<grid_dim,block_dim,sharedSize>>>(d_e,d_r,X,Y,Z,k,times);

    hipError_t err = hipSuccess;
    err = hipGetLastError();
    if (err != hipSuccess)
    {
        fprintf(stderr, "Failed to launch _3Dstencil_global kernel (error code %s)!\n", hipGetErrorString(err));
    }
    /******************
    *** Kernel Call ***
    *******************/



    hipDeviceSynchronize();
    hipEventRecord (stop, 0);
    hipEventSynchronize (stop);
    float elapsedTime;
    hipEventElapsedTime (&elapsedTime, start, stop);
    hipEventDestroy(start);
    hipEventDestroy(stop);

    
    Ticks[1] = clock();
    double Tempo = (Ticks[1] - Ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
    printf("X %d || Y %d \nBX %d || BY %d\nZ %d \n",X,Y,BX,BY,Z);
    printf ("[%d,%.5f,%lf],\n", tam,elapsedTime,Tempo/1000.0);
 
    hipMemcpy(h_r, d_r, size, hipMemcpyDeviceToHost);

    bool certo=true;
    //printf("threads/blk %d -- blocks %d\n",th_p_blk,blks);
    for (int i = 0; i < tam; i++){
        //printf("%d - %d\n",h_z_res[i],h_z[i]);
        if(h_r_test[i] != h_r[i])
          certo=false;
    }
    if(!certo)
    printf("\n*****\n certo = %s\n*****\n", certo ? "true" : "false");

    hipFree(d_e);
    hipFree(d_r);
    std::free(h_e);
    std::free(h_r);
    std::free(h_r_test);

    return 0;
} /* main */


	.text
	.file	"3DstencilGPUGlobalMemoryCube.hip"
	.globl	_Z43__device_stub___3Dstencil_SharedMemory_CubePfS_iiiii # -- Begin function _Z43__device_stub___3Dstencil_SharedMemory_CubePfS_iiiii
	.type	_Z43__device_stub___3Dstencil_SharedMemory_CubePfS_iiiii,@function
_Z43__device_stub___3Dstencil_SharedMemory_CubePfS_iiiii: # @_Z43__device_stub___3Dstencil_SharedMemory_CubePfS_iiiii
	.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	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	4(%rsp), %rcx
	movl	%r8d, (%rcx)
	movq	%rsp, %r8
	movl	%r9d, (%r8)
	leaq	80(%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	192(%rsp), %rax
	movq	%rax, 48(%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	$_Z28_3Dstencil_SharedMemory_CubePfS_iiiii, %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_end0:
	.size	_Z43__device_stub___3Dstencil_SharedMemory_CubePfS_iiiii, .Lfunc_end0-_Z43__device_stub___3Dstencil_SharedMemory_CubePfS_iiiii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x3d000000                      # float 0.03125
.LCPI1_1:
	.long	0x42c80000                      # float 100
	.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	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$280, %rsp                      # imm = 0x118
	.cfi_def_cfa_offset 336
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movabsq	$4294967296, %rax               # imm = 0x100000000
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	cmpl	$2, %edi
	jl	.LBB1_1
# %bb.2:
	movq	%rsi, %rbx
	movl	%edi, %ebp
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %r14d
	movl	$2, %r13d
	movl	$8, %r12d
	cmpl	$2, %ebp
	je	.LBB1_3
# %bb.4:
	movq	16(%rbx), %rdi
	callq	atoi
	movl	%eax, %r12d
	cmpl	$4, %ebp
	jb	.LBB1_3
# %bb.5:
	movq	24(%rbx), %rdi
	callq	atoi
	movl	%eax, %r15d
	cmpl	$4, %ebp
	je	.LBB1_7
# %bb.6:
	movq	32(%rbx), %rdi
	callq	atoi
	movl	%eax, %r13d
.LBB1_7:                                # %.thread308
	movq	%r15, %rbp
	jmp	.LBB1_8
.LBB1_1:
	movl	$2, %r13d
	movl	$4, %ebp
	movl	$1, %ebx
	movl	$8, %r15d
	movl	$8, %r14d
	movl	$8, %r12d
	movl	$8, %eax
	jmp	.LBB1_13
.LBB1_3:
	movl	$4, %ebp
.LBB1_8:                                # %.thread308
	movl	$1, %ebx
	movl	%r14d, %r15d
	cmpl	$33, %r14d
	jl	.LBB1_10
# %bb.9:
	cvtsi2ss	%r14d, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %ebx
	movl	$32, %r15d
.LBB1_10:
	cmpl	$33, %r12d
	jl	.LBB1_11
# %bb.12:
	xorps	%xmm0, %xmm0
	cvtsi2ss	%r12d, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %eax
	shlq	$32, %rax
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	movl	$32, %eax
	jmp	.LBB1_13
.LBB1_11:
	movl	%r12d, %eax
.LBB1_13:                               # %.thread323
	movl	%r15d, %ecx
	movq	%rcx, 184(%rsp)                 # 8-byte Spill
	movl	%ebx, %ecx
	movq	%rcx, 176(%rsp)                 # 8-byte Spill
	movq	%r15, 200(%rsp)                 # 8-byte Spill
	leal	(%r13,%r15), %ecx
	movq	%rax, 152(%rsp)                 # 8-byte Spill
	addl	%r13d, %eax
	movl	%eax, 32(%rsp)                  # 4-byte Spill
	leal	(%r13,%rbp), %eax
	imull	%eax, %ecx
	movl	%ecx, 20(%rsp)                  # 4-byte Spill
	movq	%r14, 48(%rsp)                  # 8-byte Spill
	addl	%r13d, %r14d
	leal	(%r12,%r13), %ecx
	movl	%r14d, %r15d
	movl	%ecx, 24(%rsp)                  # 4-byte Spill
	imull	%ecx, %r15d
	movl	%eax, 120(%rsp)                 # 4-byte Spill
	imull	%r15d, %eax
	shll	$2, %eax
	imull	%r12d, %ebp
	movq	%rbp, 112(%rsp)                 # 8-byte Spill
	movslq	%eax, %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r12
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, 104(%rsp)                 # 8-byte Spill
	leaq	128(%rsp), %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	leaq	72(%rsp), %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	movq	%rbx, 88(%rsp)                  # 8-byte Spill
	testl	%ebx, %ebx
	je	.LBB1_16
# %bb.14:                               # %.lr.ph.preheader
	movq	88(%rsp), %rbx                  # 8-byte Reload
	shrq	$2, %rbx
	cmpq	$1, %rbx
	adcq	$0, %rbx
	xorl	%ebp, %ebp
.LBB1_15:                               # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$1954687339, %rax, %rcx         # imm = 0x7482296B
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$44, %rcx
	addl	%edx, %ecx
	imull	$9000, %ecx, %ecx               # imm = 0x2328
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	divss	.LCPI1_1(%rip), %xmm0
	movq	40(%rsp), %rax                  # 8-byte Reload
	movss	%xmm0, (%rax,%rbp,4)
	movl	$0, (%r12,%rbp,4)
	incq	%rbp
	cmpq	%rbp, %rbx
	jne	.LBB1_15
.LBB1_16:                               # %.critedge
	movq	152(%rsp), %rax                 # 8-byte Reload
	shlq	$32, %rax
	movq	%rax, 96(%rsp)                  # 8-byte Spill
	movl	20(%rsp), %eax                  # 4-byte Reload
	imull	32(%rsp), %eax                  # 4-byte Folded Reload
	movl	%eax, 20(%rsp)                  # 4-byte Spill
	movq	48(%rsp), %rax                  # 8-byte Reload
	imull	112(%rsp), %eax                 # 4-byte Folded Reload
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	movq	128(%rsp), %rdi
	movq	40(%rsp), %rsi                  # 8-byte Reload
	movq	88(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	72(%rsp), %rdi
	movq	%r12, 192(%rsp)                 # 8-byte Spill
	movq	%r12, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%r13d, %ecx
	shrl	$31, %ecx
	addl	%r13d, %ecx
	sarl	%ecx
	movl	120(%rsp), %ebx                 # 4-byte Reload
	movl	%ebx, %eax
	subl	%ecx, %eax
	movl	%ecx, 28(%rsp)                  # 4-byte Spill
	cmpl	%eax, %ecx
	movq	104(%rsp), %rsi                 # 8-byte Reload
	movl	24(%rsp), %ebp                  # 4-byte Reload
	movq	40(%rsp), %r12                  # 8-byte Reload
	jge	.LBB1_29
# %bb.17:                               # %.preheader339.lr.ph
	movl	%ebp, %edx
	movl	28(%rsp), %ecx                  # 4-byte Reload
	subl	%ecx, %edx
	movl	%r14d, %edi
	subl	%ecx, %edi
	movslq	%ecx, %r8
	movslq	%r14d, %r9
	movq	%r9, 224(%rsp)                  # 8-byte Spill
	movslq	%r15d, %r9
	movq	%r9, 208(%rsp)                  # 8-byte Spill
	cltq
	movq	%rax, 216(%rsp)                 # 8-byte Spill
	movl	%edx, 140(%rsp)                 # 4-byte Spill
	movslq	%edx, %rax
	movq	%rax, 232(%rsp)                 # 8-byte Spill
	movl	%edi, 144(%rsp)                 # 4-byte Spill
	movslq	%edi, %rdi
	negl	%ecx
	movl	%ecx, 148(%rsp)                 # 4-byte Spill
	movq	%r8, 160(%rsp)                  # 8-byte Spill
	movq	%r8, 56(%rsp)                   # 8-byte Spill
	movq	%r13, 248(%rsp)                 # 8-byte Spill
	movq	%rdi, 240(%rsp)                 # 8-byte Spill
.LBB1_18:                               # %.preheader339
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_20 Depth 2
                                        #       Child Loop BB1_22 Depth 3
                                        #         Child Loop BB1_24 Depth 4
	movl	140(%rsp), %eax                 # 4-byte Reload
	cmpl	%eax, 28(%rsp)                  # 4-byte Folded Reload
	jge	.LBB1_28
# %bb.19:                               # %.preheader338.lr.ph
                                        #   in Loop: Header=BB1_18 Depth=1
	movq	56(%rsp), %r8                   # 8-byte Reload
	imulq	208(%rsp), %r8                  # 8-byte Folded Reload
	movq	160(%rsp), %rax                 # 8-byte Reload
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movq	%r8, 256(%rsp)                  # 8-byte Spill
.LBB1_20:                               # %.preheader338
                                        #   Parent Loop BB1_18 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_22 Depth 3
                                        #         Child Loop BB1_24 Depth 4
	movl	144(%rsp), %eax                 # 4-byte Reload
	cmpl	%eax, 28(%rsp)                  # 4-byte Folded Reload
	jge	.LBB1_27
# %bb.21:                               # %.lr.ph344
                                        #   in Loop: Header=BB1_20 Depth=2
	movq	32(%rsp), %rax                  # 8-byte Reload
	imulq	224(%rsp), %rax                 # 8-byte Folded Reload
	leal	(%rax,%r8), %edx
	movq	160(%rsp), %r9                  # 8-byte Reload
	movq	%rax, 264(%rsp)                 # 8-byte Spill
.LBB1_22:                               #   Parent Loop BB1_18 Depth=1
                                        #     Parent Loop BB1_20 Depth=2
                                        # =>    This Loop Header: Depth=3
                                        #         Child Loop BB1_24 Depth 4
	leaq	(%r9,%rax), %rcx
	leaq	(%rcx,%r8), %r10
	movss	(%r12,%r10,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%rsi,%r10,4)
	cmpl	$2, %r13d
	jl	.LBB1_26
# %bb.23:                               # %.lr.ph342
                                        #   in Loop: Header=BB1_22 Depth=3
	movq	%r10, 112(%rsp)                 # 8-byte Spill
	leal	(%r9,%r8), %eax
	leaq	(%r12,%rcx,4), %rbp
	movl	%r9d, %r11d
	movq	%r9, 120(%rsp)                  # 8-byte Spill
	movl	%r9d, %r13d
	movq	32(%rsp), %rcx                  # 8-byte Reload
	movl	%ecx, %edi
                                        # kill: def $ecx killed $ecx killed $rcx
	movq	56(%rsp), %r8                   # 8-byte Reload
	movl	%r8d, %esi
                                        # kill: def $r8d killed $r8d killed $r8
	movl	%ebx, %r10d
	movl	148(%rsp), %ebx                 # 4-byte Reload
.LBB1_24:                               #   Parent Loop BB1_18 Depth=1
                                        #     Parent Loop BB1_20 Depth=2
                                        #       Parent Loop BB1_22 Depth=3
                                        # =>      This Inner Loop Header: Depth=4
	cmpl	%r14d, %r11d
	movl	%r13d, %r9d
	cmovll	%r11d, %r9d
	addl	%edx, %r9d
	movslq	%r9d, %r9
	addss	(%r12,%r9,4), %xmm0
	testl	%r13d, %r13d
	movl	%r13d, %r9d
	cmovsl	%r11d, %r9d
	addl	%edx, %r9d
	movslq	%r9d, %r9
	addss	(%r12,%r9,4), %xmm0
	cmpl	24(%rsp), %edi                  # 4-byte Folded Reload
	movl	%ecx, %r9d
	cmovll	%edi, %r9d
	imull	%r14d, %r9d
	addl	%eax, %r9d
	movslq	%r9d, %r9
	addss	(%r12,%r9,4), %xmm0
	testl	%ecx, %ecx
	movl	%ecx, %r9d
	cmovsl	%edi, %r9d
	imull	%r14d, %r9d
	addl	%eax, %r9d
	movslq	%r9d, %r9
	addss	(%r12,%r9,4), %xmm0
	cmpl	%r10d, %esi
	movl	%r8d, %r9d
	cmovll	%esi, %r9d
	imull	%r15d, %r9d
	movslq	%r9d, %r9
	addss	(%rbp,%r9,4), %xmm0
	testl	%r8d, %r8d
	movl	%r8d, %r9d
	cmovsl	%esi, %r9d
	imull	%r15d, %r9d
	movslq	%r9d, %r9
	addss	(%rbp,%r9,4), %xmm0
	decl	%r8d
	incl	%esi
	decl	%ecx
	incl	%edi
	decl	%r13d
	incl	%r11d
	incl	%ebx
	jne	.LBB1_24
# %bb.25:                               # %._crit_edge
                                        #   in Loop: Header=BB1_22 Depth=3
	movq	104(%rsp), %rsi                 # 8-byte Reload
	movq	112(%rsp), %rax                 # 8-byte Reload
	movss	%xmm0, (%rsi,%rax,4)
	movq	248(%rsp), %r13                 # 8-byte Reload
	movl	%r10d, %ebx
	movl	24(%rsp), %ebp                  # 4-byte Reload
	movq	240(%rsp), %rdi                 # 8-byte Reload
	movq	256(%rsp), %r8                  # 8-byte Reload
	movq	264(%rsp), %rax                 # 8-byte Reload
	movq	120(%rsp), %r9                  # 8-byte Reload
.LBB1_26:                               #   in Loop: Header=BB1_22 Depth=3
	incq	%r9
	cmpq	%rdi, %r9
	jne	.LBB1_22
.LBB1_27:                               # %._crit_edge345
                                        #   in Loop: Header=BB1_20 Depth=2
	movq	32(%rsp), %rax                  # 8-byte Reload
	incq	%rax
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	cmpq	232(%rsp), %rax                 # 8-byte Folded Reload
	jne	.LBB1_20
.LBB1_28:                               # %._crit_edge347
                                        #   in Loop: Header=BB1_18 Depth=1
	movq	56(%rsp), %rax                  # 8-byte Reload
	incq	%rax
	movq	%rax, 56(%rsp)                  # 8-byte Spill
	cmpq	216(%rsp), %rax                 # 8-byte Folded Reload
	jne	.LBB1_18
.LBB1_29:                               # %.preheader
	movq	96(%rsp), %rax                  # 8-byte Reload
	addq	184(%rsp), %rax                 # 8-byte Folded Reload
	movq	%rax, 96(%rsp)                  # 8-byte Spill
	movq	176(%rsp), %rax                 # 8-byte Reload
	orq	%rax, 80(%rsp)                  # 8-byte Folded Spill
	shll	$2, 20(%rsp)                    # 4-byte Folded Spill
	movq	48(%rsp), %rax                  # 8-byte Reload
	movl	%eax, %r15d
	testl	%eax, %eax
	jle	.LBB1_31
# %bb.30:                               # %.lr.ph350.preheader
	leaq	(,%r15,4), %rdx
	movq	40(%rsp), %rdi                  # 8-byte Reload
	callq	memcpy@PLT
.LBB1_31:                               # %._crit_edge351
	leaq	168(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventCreate
	leaq	64(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r12), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movslq	20(%rsp), %r8                   # 4-byte Folded Reload
	movq	80(%rsp), %rdi                  # 8-byte Reload
	movl	$1, %esi
	movq	96(%rsp), %rdx                  # 8-byte Reload
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_33
# %bb.32:
	movq	128(%rsp), %rdi
	movq	72(%rsp), %rsi
	movl	$1, (%rsp)
	movl	%r14d, %edx
	movl	%ebp, %ecx
	movl	%ebx, %r8d
	movl	%r13d, %r9d
	callq	_Z43__device_stub___3Dstencil_SharedMemory_CubePfS_iiiii
.LBB1_33:
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB1_34
.LBB1_35:
	callq	hipDeviceSynchronize
	movq	64(%rsp), %rdi
	xorl	%ebp, %ebp
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	64(%rsp), %rdi
	callq	hipEventSynchronize
	movq	168(%rsp), %rsi
	movq	64(%rsp), %rdx
	leaq	276(%rsp), %r13
	movq	%r13, %rdi
	callq	hipEventElapsedTime
	movq	168(%rsp), %rdi
	callq	hipEventDestroy
	movq	64(%rsp), %rdi
	callq	hipEventDestroy
	callq	clock
	movl	$.L.str.1, %edi
	movl	%r14d, %esi
	movl	24(%rsp), %edx                  # 4-byte Reload
	movq	200(%rsp), %rcx                 # 8-byte Reload
                                        # kill: def $ecx killed $ecx killed $rcx
	movq	152(%rsp), %r8                  # 8-byte Reload
                                        # kill: def $r8d killed $r8d killed $r8
	movl	%ebx, %r9d
	xorl	%eax, %eax
	callq	printf
	cvtss2sd	(%r13), %xmm0
	movl	$.L.str.2, %edi
	xorps	%xmm1, %xmm1
	movq	48(%rsp), %rbx                  # 8-byte Reload
	movl	%ebx, %esi
	movb	$2, %al
	callq	printf
	movq	72(%rsp), %rsi
	movq	192(%rsp), %r14                 # 8-byte Reload
	movq	%r14, %rdi
	movq	88(%rsp), %rdx                  # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%ebx, %ebx
	movq	104(%rsp), %rbx                 # 8-byte Reload
	jle	.LBB1_40
# %bb.36:                               # %.lr.ph355.preheader
	movb	$1, %al
	xorl	%ecx, %ecx
.LBB1_37:                               # %.lr.ph355
                                        # =>This Inner Loop Header: Depth=1
	movss	(%rbx,%rcx,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	ucomiss	(%r14,%rcx,4), %xmm0
	movzbl	%al, %eax
	cmovnel	%ebp, %eax
	cmovpl	%ebp, %eax
	incq	%rcx
	cmpq	%rcx, %r15
	jne	.LBB1_37
# %bb.38:                               # %._crit_edge356
	testb	$1, %al
	jne	.LBB1_40
# %bb.39:
	movl	$.L.str.3, %edi
	movl	$.L.str.5, %esi
	xorl	%eax, %eax
	callq	printf
.LBB1_40:                               # %._crit_edge356.thread
	movq	128(%rsp), %rdi
	callq	hipFree
	movq	72(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%rbx, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$280, %rsp                      # imm = 0x118
	.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
.LBB1_34:
	.cfi_def_cfa_offset 336
	movq	stderr(%rip), %r13
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %esi
	movq	%r13, %rdi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB1_35
.Lfunc_end1:
	.size	main, .Lfunc_end1-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	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z28_3Dstencil_SharedMemory_CubePfS_iiiii, %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_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii,@object # @_Z28_3Dstencil_SharedMemory_CubePfS_iiiii
	.section	.rodata,"a",@progbits
	.globl	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii
	.p2align	3, 0x0
_Z28_3Dstencil_SharedMemory_CubePfS_iiiii:
	.quad	_Z43__device_stub___3Dstencil_SharedMemory_CubePfS_iiiii
	.size	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Failed to launch _3Dstencil_global kernel (error code %s)!\n"
	.size	.L.str, 60

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"X %d || Y %d \nBX %d || BY %d\nZ %d \n"
	.size	.L.str.1, 36

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"[%d,%.5f,%lf],\n"
	.size	.L.str.2, 16

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"\n*****\n certo = %s\n*****\n"
	.size	.L.str.3, 26

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"false"
	.size	.L.str.5, 6

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z28_3Dstencil_SharedMemory_CubePfS_iiiii"
	.size	.L__unnamed_1, 42

	.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 _Z43__device_stub___3Dstencil_SharedMemory_CubePfS_iiiii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z28_3Dstencil_SharedMemory_CubePfS_iiiii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii ; -- Begin function _Z28_3Dstencil_SharedMemory_CubePfS_iiiii
	.globl	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii
	.p2align	8
	.type	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii,@function
_Z28_3Dstencil_SharedMemory_CubePfS_iiiii: ; @_Z28_3Dstencil_SharedMemory_CubePfS_iiiii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x34
	s_load_b256 s[4:11], s[0:1], 0x0
	v_bfe_u32 v3, v0, 10, 10
	v_and_b32_e32 v2, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s0, s2, 16
	s_and_b32 s2, s2, 0xffff
	s_mul_i32 s15, s15, s0
	s_mul_i32 s14, s14, s2
	v_add_nc_u32_e32 v4, s15, v3
	s_cmp_lt_i32 s10, 1
	s_mul_i32 s1, s9, s8
	s_cbranch_scc1 .LBB0_3
; %bb.1:                                ; %.lr.ph.preheader
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v0, s8, v4
	v_mad_u32_u24 v1, v3, s2, v2
	s_mul_i32 s0, s0, s2
	s_mov_b32 s2, s10
	s_lshl_b32 s0, s0, 2
	v_lshl_add_u32 v5, v1, 2, 0
	s_delay_alu instid0(VALU_DEP_3)
	v_add3_u32 v0, v2, v0, s14
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_ashrrev_i32_e32 v1, 31, v0
	s_add_i32 s2, s2, -1
	s_cmp_eq_u32 s2, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[6:7], 2, v[0:1]
	v_add_nc_u32_e32 v0, s1, v0
	v_add_co_u32 v6, vcc_lo, s4, v6
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v7, vcc_lo, s5, v7, vcc_lo
	global_load_b32 v1, v[6:7], off
	s_waitcnt vmcnt(0)
	ds_store_b32 v5, v1
	v_add_nc_u32_e32 v5, s0, v5
	s_cbranch_scc0 .LBB0_2
.LBB0_3:                                ; %.preheader
	s_lshr_b32 s0, s11, 31
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s0, s11, s0
	s_ashr_i32 s2, s0, 1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_sub_i32 s3, s10, s2
	s_cmp_ge_i32 s2, s3
	s_cbranch_scc1 .LBB0_8
; %bb.4:                                ; %.lr.ph41
	v_add_nc_u32_e32 v0, s2, v4
	s_cmp_gt_i32 s11, 1
	v_add_nc_u32_e32 v4, s2, v2
	s_cselect_b32 s0, -1, 0
	s_add_i32 s9, s9, 1
	v_mul_lo_u32 v5, v0, s8
	s_mul_i32 s9, s2, s9
	v_cndmask_b32_e64 v6, 0, 1, s0
	v_add3_u32 v3, s15, s9, v3
	s_add_i32 s9, s1, 1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	s_mul_i32 s0, s2, s9
	s_mov_b32 s9, s2
	v_mad_u64_u32 v[0:1], null, s8, v3, v[4:5]
	v_add3_u32 v1, v4, s14, v5
	v_add3_u32 v4, v2, v5, s0
	v_cmp_ne_u32_e64 s0, 1, v6
.LBB0_5:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_6 Depth 2
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mad_u64_u32 v[2:3], null, s9, s1, v[1:2]
	v_mov_b32_e32 v7, v4
	v_mov_b32_e32 v8, v4
	v_mov_b32_e32 v9, v0
	v_mov_b32_e32 v10, v0
	s_mov_b32 s10, s2
	v_ashrrev_i32_e32 v3, 31, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[2:3]
	v_add_co_u32 v5, vcc_lo, s4, v2
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v6, vcc_lo, s5, v3, vcc_lo
	v_add_co_u32 v2, vcc_lo, s6, v2
	v_add_co_ci_u32_e32 v3, vcc_lo, s7, v3, vcc_lo
	global_load_b32 v11, v[5:6], off
	v_mov_b32_e32 v5, v4
	v_mov_b32_e32 v6, v4
	s_and_b32 vcc_lo, exec_lo, s0
	s_waitcnt vmcnt(0)
	global_store_b32 v[2:3], v11, off
	s_cbranch_vccnz .LBB0_7
.LBB0_6:                                ;   Parent Loop BB0_5 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	v_add_nc_u32_e32 v12, s14, v5
	v_add_nc_u32_e32 v5, 1, v5
	s_add_i32 s10, s10, -1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	s_cmp_lg_u32 s10, 0
	v_ashrrev_i32_e32 v13, 31, v12
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[12:13], 2, v[12:13]
	v_add_co_u32 v12, vcc_lo, s4, v12
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v13, vcc_lo, s5, v13, vcc_lo
	global_load_b32 v14, v[12:13], off
	s_waitcnt vmcnt(0)
	v_dual_add_f32 v14, v11, v14 :: v_dual_add_nc_u32 v11, s14, v7
	v_add_nc_u32_e32 v12, s14, v6
	v_add_nc_u32_e32 v7, s8, v7
	global_store_b32 v[2:3], v14, off
	v_ashrrev_i32_e32 v13, 31, v12
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[12:13], 2, v[12:13]
	v_add_co_u32 v12, vcc_lo, s4, v12
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_add_co_ci_u32_e32 v13, vcc_lo, s5, v13, vcc_lo
	global_load_b32 v13, v[12:13], off
	v_ashrrev_i32_e32 v12, 31, v11
	v_lshlrev_b64 v[11:12], 2, v[11:12]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v11, vcc_lo, s4, v11
	v_add_co_ci_u32_e32 v12, vcc_lo, s5, v12, vcc_lo
	s_waitcnt vmcnt(0)
	v_add_f32_e32 v13, v14, v13
	global_store_b32 v[2:3], v13, off
	global_load_b32 v14, v[11:12], off
	v_add_nc_u32_e32 v11, s14, v8
	v_subrev_nc_u32_e32 v8, s8, v8
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v12, 31, v11
	v_lshlrev_b64 v[11:12], 2, v[11:12]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v11, vcc_lo, s4, v11
	v_add_co_ci_u32_e32 v12, vcc_lo, s5, v12, vcc_lo
	s_waitcnt vmcnt(0)
	v_add_f32_e32 v13, v13, v14
	global_store_b32 v[2:3], v13, off
	global_load_b32 v14, v[11:12], off
	v_add_nc_u32_e32 v11, s14, v9
	v_add_nc_u32_e32 v9, s1, v9
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v12, 31, v11
	v_lshlrev_b64 v[11:12], 2, v[11:12]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v11, vcc_lo, s4, v11
	v_add_co_ci_u32_e32 v12, vcc_lo, s5, v12, vcc_lo
	s_waitcnt vmcnt(0)
	v_add_f32_e32 v13, v13, v14
	global_store_b32 v[2:3], v13, off
	global_load_b32 v14, v[11:12], off
	v_add_nc_u32_e32 v11, s14, v10
	v_subrev_nc_u32_e32 v10, s1, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v12, 31, v11
	v_lshlrev_b64 v[11:12], 2, v[11:12]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v11, vcc_lo, s4, v11
	v_add_co_ci_u32_e32 v12, vcc_lo, s5, v12, vcc_lo
	s_waitcnt vmcnt(0)
	v_add_f32_e32 v13, v13, v14
	global_store_b32 v[2:3], v13, off
	global_load_b32 v11, v[11:12], off
	s_waitcnt vmcnt(0)
	v_dual_add_f32 v11, v13, v11 :: v_dual_add_nc_u32 v6, -1, v6
	global_store_b32 v[2:3], v11, off
	s_cbranch_scc1 .LBB0_6
.LBB0_7:                                ; %_Z11_3Dstencil_PfS_iiiiiii.exit
                                        ;   in Loop: Header=BB0_5 Depth=1
	v_add_nc_u32_e32 v0, s1, v0
	v_add_nc_u32_e32 v4, s1, v4
	s_add_i32 s9, s9, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_ge_i32 s9, s3
	s_cbranch_scc0 .LBB0_5
.LBB0_8:                                ; %._crit_edge
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z28_3Dstencil_SharedMemory_CubePfS_iiiii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 296
		.amdhsa_user_sgpr_count 14
		.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 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 15
		.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	_Z28_3Dstencil_SharedMemory_CubePfS_iiiii, .Lfunc_end0-_Z28_3Dstencil_SharedMemory_CubePfS_iiiii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 876
; NumSgprs: 18
; NumVgprs: 15
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 15
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.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
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         28
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     by_value
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         44
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         52
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         54
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         56
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         58
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         60
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         62
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         104
        .size:           2
        .value_kind:     hidden_grid_dims
      - .offset:         160
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 296
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z28_3Dstencil_SharedMemory_CubePfS_iiiii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z28_3Dstencil_SharedMemory_CubePfS_iiiii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     15
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
