#include <iostream>
#include <cstdlib>
#include <cassert>
#include <zlib.h>
#include <png.h>

#define Z 2
#define Y 5
#define X 5
#define xBound X / 2
#define yBound Y / 2
#define SCALE 8

int read_png(const char* filename, unsigned char** image, unsigned* height, 
             unsigned* width, unsigned* channels) {

    unsigned char sig[8];
    FILE* infile;
    infile = fopen(filename, "rb");

    fread(sig, 1, 8, infile);
    if (!png_check_sig(sig, 8))
        return 1;   /* bad signature */

    png_structp png_ptr;
    png_infop info_ptr;

    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png_ptr)
        return 4;   /* out of memory */
  
    info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr) {
        png_destroy_read_struct(&png_ptr, NULL, NULL);
        return 4;   /* out of memory */
    }

    png_init_io(png_ptr, infile);
    png_set_sig_bytes(png_ptr, 8);
    png_read_info(png_ptr, info_ptr);
    int bit_depth, color_type;
    png_get_IHDR(png_ptr, info_ptr, width, height, &bit_depth, &color_type, NULL, NULL, NULL);

    png_uint_32  i, rowbytes;
    png_bytep  row_pointers[*height];
    png_read_update_info(png_ptr, info_ptr);
    rowbytes = png_get_rowbytes(png_ptr, info_ptr);
    *channels = (int) png_get_channels(png_ptr, info_ptr);

    if ((*image = (unsigned char *) malloc(rowbytes * *height)) == NULL) {
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
        return 3;
    }

    for (i = 0;  i < *height;  ++i)
        row_pointers[i] = *image + i * rowbytes;
    png_read_image(png_ptr, row_pointers);
    png_read_end(png_ptr, NULL);
    return 0;
}

void write_png(const char* filename, png_bytep image, const unsigned height, const unsigned width, 
               const unsigned channels) {
    FILE* fp = fopen(filename, "wb");
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    png_infop info_ptr = png_create_info_struct(png_ptr);
    png_init_io(png_ptr, fp);
    png_set_IHDR(png_ptr, info_ptr, width, height, 8,
                 PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
                 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
    png_set_filter(png_ptr, 0, PNG_NO_FILTERS);
    png_write_info(png_ptr, info_ptr);
    png_set_compression_level(png_ptr, 1);

    png_bytep row_ptr[height];
    for (int i = 0; i < height; ++ i) {
        row_ptr[i] = image + i * width * channels * sizeof(unsigned char);
    }
    png_write_image(png_ptr, row_ptr);
    png_write_end(png_ptr, NULL);
    png_destroy_write_struct(&png_ptr, &info_ptr);
    fclose(fp);
}

__constant__ char mask[Z][Y][X] = { { { -1, -4, -6, -4, -1 },
                                        { -2, -8, -12, -8, -2 },
                                        { 0, 0, 0, 0, 0 },
                                        { 2, 8, 12, 8, 2 },
                                        { 1, 4, 6, 4, 1 } },
                                      { { -1, -2, 0, 2, 1 },
                                        { -4, -8, 0, 8, 4 },
                                        { -6, -12, 0, 12, 6 },
                                        { -4, -8, 0, 8, 4 },
                                        { -1, -2, 0, 2, 1 } } };

inline __device__ int bound_check(int val, int lower, int upper) {
    if (val >= lower && val < upper)
        return 1;
    else
        return 0;
}

__global__ void sobel(unsigned char *s, unsigned char *t, unsigned height, unsigned width, unsigned channels) {

    // int tid = blockIdx.x * blockDim.x + threadIdx.x;
    float val[Z][3];
    __shared__ unsigned char RMat[5][262];
    __shared__ unsigned char GMat[5][262];
    __shared__ unsigned char BMat[5][262];
    // if (tid >= height) return;

    // int y = tid;
    for (int y = blockIdx.x; y < height; y += gridDim.x) {
        for (int x = threadIdx.x; x < width; x += blockDim.x) {
        // for (int x = 0; x < width; ++x) {
            /* Z axis of mask */
            /* Y and X axis of mask */
            for (int v = -yBound; v <= yBound; ++v) {
                if (bound_check(y + v, 0, height)) {
                    // FIXME: 目前這份 code 跑得過，只有在 threadNum 設成 64 的時候。
                    // 當我 threadNum 設成 256 時，當每個 blockIdx 進入第二次輪迴（blockIdx.x + gridDim.x）， x_row threadIdx.x 為 64 的地方都會出錯。
                    // 我強制設定為 threadNum 為 64，避免上述情況發生，但應該還是要 de 出來。
                    RMat[v+yBound][threadIdx.x + 2] = s[channels * (width * (y + v) + x) + 2];
                    GMat[v+yBound][threadIdx.x + 2] = s[channels * (width * (y + v) + x) + 1];
                    BMat[v+yBound][threadIdx.x + 2] = s[channels * (width * (y + v) + x) + 0];
                    if (threadIdx.x == 0) {
                        if (bound_check(x - 2, 0, width)) {
                            RMat[v+yBound][threadIdx.x] = s[channels * (width * (y + v) + x-2) + 2];
                            GMat[v+yBound][threadIdx.x] = s[channels * (width * (y + v) + x-2) + 1];
                            BMat[v+yBound][threadIdx.x] = s[channels * (width * (y + v) + x-2) + 0];
                        }
                        if (bound_check(x - 1, 0, width)) {
                            RMat[v+yBound][threadIdx.x + 1] = s[channels * (width * (y + v) + x-1) + 2];
                            GMat[v+yBound][threadIdx.x + 1] = s[channels * (width * (y + v) + x-1) + 1];
                            BMat[v+yBound][threadIdx.x + 1] = s[channels * (width * (y + v) + x-1) + 0];
                        }
                    } else if (threadIdx.x == blockDim.x - 1) {
                        if (bound_check(x + 1, 0, width)) {
                            RMat[v+yBound][threadIdx.x + 3] = s[channels * (width * (y + v) + x+1) + 2];
                            GMat[v+yBound][threadIdx.x + 3] = s[channels * (width * (y + v) + x+1) + 1];
                            BMat[v+yBound][threadIdx.x + 3] = s[channels * (width * (y + v) + x+1) + 0];
                        }
                        if (bound_check(x + 2, 0, width)) {
                            RMat[v+yBound][threadIdx.x + 4] = s[channels * (width * (y + v) + x+2) + 2];
                            GMat[v+yBound][threadIdx.x + 4] = s[channels * (width * (y + v) + x+2) + 1];
                            BMat[v+yBound][threadIdx.x + 4] = s[channels * (width * (y + v) + x+2) + 0];
                        }
                    }
                }
            }
            __syncthreads();

            for (int i = 0; i < Z; ++i) {

                val[i][2] = 0.;
                val[i][1] = 0.;
                val[i][0] = 0.;

                /* Y and X axis of mask */
                for (int v = -yBound; v <= yBound; ++v) {
                    for (int u = -xBound; u <= xBound; ++u) {
                        if (bound_check(x + u, 0, width) && bound_check(y + v, 0, height)) {
                            // const unsigned char R = s[channels * (width * (y + v) + (x + u)) + 2];
                            // const unsigned char G = s[channels * (width * (y + v) + (x + u)) + 1];
                            // const unsigned char B = s[channels * (width * (y + v) + (x + u)) + 0];
                            const unsigned char R = RMat[v+yBound][threadIdx.x + 2 + u];
                            const unsigned char G = GMat[v+yBound][threadIdx.x + 2 + u];
                            const unsigned char B = BMat[v+yBound][threadIdx.x + 2 + u];
                            val[i][2] += R * mask[i][u + xBound][v + yBound];
                            val[i][1] += G * mask[i][u + xBound][v + yBound];
                            val[i][0] += B * mask[i][u + xBound][v + yBound];
                        }
                    }
                }
            }
            float totalR = 0.;
            float totalG = 0.;
            float totalB = 0.;
            for (int i = 0; i < Z; ++i) {
                totalR += val[i][2] * val[i][2];
                totalG += val[i][1] * val[i][1];
                totalB += val[i][0] * val[i][0];
            }
            totalR = sqrt(totalR) / SCALE;
            totalG = sqrt(totalG) / SCALE;
            totalB = sqrt(totalB) / SCALE;
            const unsigned char cR = (totalR > 255.) ? 255 : totalR;
            const unsigned char cG = (totalG > 255.) ? 255 : totalG;
            const unsigned char cB = (totalB > 255.) ? 255 : totalB;
            t[channels * (width * y + x) + 2] = cR;
            t[channels * (width * y + x) + 1] = cG;
            t[channels * (width * y + x) + 0] = cB;
            __syncthreads();
        }
    }
}

int main(int argc, char **argv) {
    assert(argc == 3);
    unsigned height, width, channels;
    unsigned char *src = NULL, *dst;
    unsigned char *dsrc, *ddst;

    /* read the image to src, and get height, width, channels */
    if (read_png(argv[1], &src, &height, &width, &channels)) {
        std::cerr << "Error in read png" << std::endl;
        return -1;
    }

    dst = (unsigned char *)malloc(height * width * channels * sizeof(unsigned char));
    cudaHostRegister(src, height * width * channels * sizeof(unsigned char), cudaHostRegisterDefault);

    // cudaMalloc(...) for device src and device dst
    cudaMalloc(&dsrc, height * width * channels * sizeof(unsigned char));
    cudaMalloc(&ddst, height * width * channels * sizeof(unsigned char));

    // cudaMemcpy(...) copy source image to device (mask matrix if necessary)
    cudaMemcpy(dsrc, src, height * width * channels * sizeof(unsigned char), cudaMemcpyHostToDevice);

    // decide to use how many blocks and threads
    const int num_threads = 64;
    // const int num_blocks = height / num_threads + 1;
    const int num_blocks = 2048;

    // launch cuda kernel
    sobel <<<num_blocks, num_threads>>> (dsrc, ddst, height, width, channels);

    // cudaMemcpy(...) copy result image to host
    cudaMemcpy(dst, ddst, height * width * channels * sizeof(unsigned char), cudaMemcpyDeviceToHost);

    write_png(argv[2], dst, height, width, channels);
    free(src);
    free(dst);
    cudaFree(dsrc);
    cudaFree(ddst);
    return 0;
}


	.file	"tmpxft_002300bf_00000000-6_lab5.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3654:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3654:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"rb"
.LC1:
	.string	"1.6.43"
	.text
	.globl	_Z8read_pngPKcPPhPjS3_S3_
	.type	_Z8read_pngPKcPPhPjS3_S3_, @function
_Z8read_pngPKcPPhPjS3_S3_:
.LFB3648:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%r15
	.cfi_offset 15, -24
	movq	%r8, %r15
	pushq	%r14
	.cfi_offset 14, -32
	movq	%rsi, %r14
	leaq	.LC0(%rip), %rsi
	pushq	%r13
	pushq	%r12
	.cfi_offset 13, -40
	.cfi_offset 12, -48
	movq	%rdx, %r12
	pushq	%rbx
	.cfi_offset 3, -56
	leaq	-64(%rbp), %rbx
	subq	$72, %rsp
	movq	%rcx, -104(%rbp)
	movq	%fs:40, %rax
	movq	%rax, -56(%rbp)
	xorl	%eax, %eax
	call	fopen@PLT
	movq	%rbx, %rdi
	movl	$8, %ecx
	movl	$1, %edx
	movq	%rax, %r8
	movl	$8, %esi
	movq	%rax, %r13
	call	__fread_chk@PLT
	movq	%rbx, %rdi
	movl	$8, %edx
	xorl	%esi, %esi
	call	png_sig_cmp@PLT
	movl	$1, %ebx
	testl	%eax, %eax
	jne	.L2
	leaq	.LC1(%rip), %rdi
	xorl	%ecx, %ecx
	xorl	%edx, %edx
	xorl	%esi, %esi
	movl	%eax, %ebx
	call	png_create_read_struct@PLT
	movq	%rax, -80(%rbp)
	movq	%rax, %rdi
	testq	%rax, %rax
	je	.L4
	call	png_create_info_struct@PLT
	movq	%rax, -72(%rbp)
	testq	%rax, %rax
	jne	.L5
	leaq	-80(%rbp), %rdi
	xorl	%edx, %edx
	xorl	%esi, %esi
	call	png_destroy_read_struct@PLT
.L4:
	movl	$4, %ebx
	jmp	.L2
.L5:
	movq	-80(%rbp), %rdi
	movq	%r13, %rsi
	call	png_init_io@PLT
	movq	-80(%rbp), %rdi
	movl	$8, %esi
	call	png_set_sig_bytes@PLT
	movq	-72(%rbp), %rsi
	movq	-80(%rbp), %rdi
	call	png_read_info@PLT
	movq	%r12, %rcx
	leaq	-84(%rbp), %r9
	leaq	-88(%rbp), %r8
	pushq	%rax
	movq	-104(%rbp), %rdx
	movq	-72(%rbp), %rsi
	movq	-80(%rbp), %rdi
	pushq	$0
	pushq	$0
	pushq	$0
	call	png_get_IHDR@PLT
	movl	(%r12), %eax
	addq	$32, %rsp
	movq	%rsp, %rdx
	leaq	15(,%rax,8), %rax
	shrq	$4, %rax
	salq	$4, %rax
	movq	%rax, %rcx
	andq	$-4096, %rcx
	subq	%rcx, %rdx
.L6:
	cmpq	%rdx, %rsp
	je	.L7
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L6
.L7:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L8
	orq	$0, -8(%rsp,%rax)
.L8:
	movq	-72(%rbp), %rsi
	movq	-80(%rbp), %rdi
	movq	%rsp, -104(%rbp)
	call	png_read_update_info@PLT
	movq	-72(%rbp), %rsi
	movq	-80(%rbp), %rdi
	call	png_get_rowbytes@PLT
	movq	-72(%rbp), %rsi
	movq	-80(%rbp), %rdi
	movq	%rax, -112(%rbp)
	movl	%eax, %r13d
	call	png_get_channels@PLT
	movq	-112(%rbp), %rdx
	movzbl	%al, %eax
	movl	%eax, (%r15)
	movl	(%r12), %r12d
	movl	%edx, %edi
	imull	%r12d, %edi
	call	malloc@PLT
	xorl	%esi, %esi
	xorl	%edx, %edx
	movq	%rax, (%r14)
	testq	%rax, %rax
	jne	.L9
	leaq	-72(%rbp), %rsi
	leaq	-80(%rbp), %rdi
	movl	$3, %ebx
	call	png_destroy_read_struct@PLT
	jmp	.L2
.L9:
	cmpl	%r12d, %edx
	jnb	.L23
	movl	%esi, %edi
	movq	-104(%rbp), %rcx
	addl	%r13d, %esi
	addq	%rax, %rdi
	movq	%rdi, (%rcx,%rdx,8)
	incq	%rdx
	jmp	.L9
.L23:
	movq	-104(%rbp), %rsi
	movq	-80(%rbp), %rdi
	call	png_read_image@PLT
	movq	-80(%rbp), %rdi
	xorl	%esi, %esi
	call	png_read_end@PLT
.L2:
	movq	-56(%rbp), %rax
	subq	%fs:40, %rax
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	leaq	-40(%rbp), %rsp
	movl	%ebx, %eax
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE3648:
	.size	_Z8read_pngPKcPPhPjS3_S3_, .-_Z8read_pngPKcPPhPjS3_S3_
	.section	.rodata.str1.1
.LC2:
	.string	"wb"
	.text
	.globl	_Z9write_pngPKcPhjjj
	.type	_Z9write_pngPKcPhjjj, @function
_Z9write_pngPKcPhjjj:
.LFB3649:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%r15
	.cfi_offset 15, -24
	movl	%r8d, %r15d
	pushq	%r14
	.cfi_offset 14, -32
	movq	%rsi, %r14
	leaq	.LC2(%rip), %rsi
	pushq	%r13
	pushq	%r12
	.cfi_offset 13, -40
	.cfi_offset 12, -48
	movl	%edx, %r12d
	pushq	%rbx
	.cfi_offset 3, -56
	movl	%ecx, %ebx
	subq	$40, %rsp
	movq	%fs:40, %rax
	movq	%rax, -56(%rbp)
	xorl	%eax, %eax
	call	fopen@PLT
	xorl	%ecx, %ecx
	xorl	%edx, %edx
	xorl	%esi, %esi
	leaq	.LC1(%rip), %rdi
	movq	%rax, %r13
	call	png_create_write_struct@PLT
	movq	%rax, %rdi
	movq	%rax, -72(%rbp)
	call	png_create_info_struct@PLT
	movq	-72(%rbp), %rdi
	movq	%r13, %rsi
	movq	%rax, -64(%rbp)
	call	png_init_io@PLT
	movl	%r12d, %ecx
	movl	$2, %r9d
	movl	%ebx, %edx
	pushq	%rax
	movq	-64(%rbp), %rsi
	movl	$8, %r8d
	pushq	$0
	movq	-72(%rbp), %rdi
	pushq	$0
	pushq	$0
	call	png_set_IHDR@PLT
	movq	-72(%rbp), %rdi
	addq	$32, %rsp
	xorl	%edx, %edx
	xorl	%esi, %esi
	call	png_set_filter@PLT
	movq	-64(%rbp), %rsi
	movq	-72(%rbp), %rdi
	call	png_write_info@PLT
	movq	-72(%rbp), %rdi
	movl	$1, %esi
	call	png_set_compression_level@PLT
	movl	%r12d, %eax
	movq	%rsp, %rdx
	leaq	15(,%rax,8), %rax
	shrq	$4, %rax
	salq	$4, %rax
	movq	%rax, %rcx
	andq	$-4096, %rcx
	subq	%rcx, %rdx
.L25:
	cmpq	%rdx, %rsp
	je	.L26
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L25
.L26:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L27
	orq	$0, -8(%rsp,%rax)
.L27:
	imull	%r15d, %ebx
	movq	%rsp, %rsi
	xorl	%edx, %edx
	xorl	%eax, %eax
.L28:
	cmpl	%r12d, %eax
	jnb	.L35
	movl	%edx, %ecx
	addl	%ebx, %edx
	addq	%r14, %rcx
	movq	%rcx, (%rsi,%rax,8)
	incq	%rax
	jmp	.L28
.L35:
	movq	-72(%rbp), %rdi
	call	png_write_image@PLT
	movq	-72(%rbp), %rdi
	xorl	%esi, %esi
	call	png_write_end@PLT
	leaq	-72(%rbp), %rdi
	leaq	-64(%rbp), %rsi
	call	png_destroy_write_struct@PLT
	movq	%r13, %rdi
	call	fclose@PLT
	movq	-56(%rbp), %rax
	subq	%fs:40, %rax
	je	.L30
	call	__stack_chk_fail@PLT
.L30:
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE3649:
	.size	_Z9write_pngPKcPhjjj, .-_Z9write_pngPKcPhjjj
	.globl	_Z29__device_stub__Z5sobelPhS_jjjPhS_jjj
	.type	_Z29__device_stub__Z5sobelPhS_jjjPhS_jjj, @function
_Z29__device_stub__Z5sobelPhS_jjjPhS_jjj:
.LFB3676:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	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)
	movq	%fs:40, %rax
	movq	%rax, 136(%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)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L36
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z5sobelPhS_jjj(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	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 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L36:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L38
	call	__stack_chk_fail@PLT
.L38:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3676:
	.size	_Z29__device_stub__Z5sobelPhS_jjjPhS_jjj, .-_Z29__device_stub__Z5sobelPhS_jjjPhS_jjj
	.globl	_Z5sobelPhS_jjj
	.type	_Z5sobelPhS_jjj, @function
_Z5sobelPhS_jjj:
.LFB3677:
	.cfi_startproc
	endbr64
	jmp	_Z29__device_stub__Z5sobelPhS_jjjPhS_jjj
	.cfi_endproc
.LFE3677:
	.size	_Z5sobelPhS_jjj, .-_Z5sobelPhS_jjj
	.section	.rodata.str1.1
.LC3:
	.string	"Error in read png"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3651:
	.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
	movq	%rsi, %r13
	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	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	8(%r13), %rdi
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	leaq	16(%rsp), %rcx
	xorl	%eax, %eax
	leaq	12(%rsp), %rdx
	leaq	24(%rsp), %rsi
	leaq	20(%rsp), %r8
	movq	%rax, 24(%rsp)
	call	_Z8read_pngPKcPPhPjS3_S3_
	testl	%eax, %eax
	je	.L42
	leaq	.LC3(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	orl	$-1, %r12d
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	jmp	.L41
.L42:
	movl	12(%rsp), %ebp
	imull	16(%rsp), %ebp
	movl	%eax, %r12d
	movl	20(%rsp), %ebx
	imull	%ebx, %ebp
	movq	%rbp, %rdi
	call	malloc@PLT
	movq	24(%rsp), %r15
	xorl	%edx, %edx
	movq	%rbp, %rsi
	movq	%rax, %r14
	movq	%r15, %rdi
	call	cudaHostRegister@PLT
	movl	12(%rsp), %esi
	leaq	32(%rsp), %rdi
	imull	%ebx, %esi
	imull	16(%rsp), %esi
	call	cudaMalloc@PLT
	movl	12(%rsp), %esi
	leaq	40(%rsp), %rdi
	imull	%ebx, %esi
	imull	16(%rsp), %esi
	call	cudaMalloc@PLT
	movl	12(%rsp), %edx
	movq	32(%rsp), %rdi
	movq	%r15, %rsi
	movl	$1, %ecx
	imull	%ebx, %edx
	imull	16(%rsp), %edx
	call	cudaMemcpy@PLT
	movl	$67108865, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$2097153, %edi
	salq	$6, %rdx
	movl	$1, %ecx
	movl	$1, %esi
	salq	$11, %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L44
	movl	16(%rsp), %ecx
	movl	12(%rsp), %edx
	movl	%ebx, %r8d
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	call	_Z29__device_stub__Z5sobelPhS_jjjPhS_jjj
.L44:
	movl	12(%rsp), %edx
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	movq	%r14, %rdi
	imull	%ebx, %edx
	imull	16(%rsp), %edx
	call	cudaMemcpy@PLT
	movl	16(%rsp), %ecx
	movl	12(%rsp), %edx
	movl	%ebx, %r8d
	movq	16(%r13), %rdi
	movq	%r14, %rsi
	call	_Z9write_pngPKcPhjjj
	movq	%r15, %rdi
	call	free@PLT
	movq	%r14, %rdi
	call	free@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
.L41:
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L45
	call	__stack_chk_fail@PLT
.L45:
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	movl	%r12d, %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
.LFE3651:
	.size	main, .-main
	.section	.rodata.str1.1
.LC4:
	.string	"_Z5sobelPhS_jjj"
.LC5:
	.string	"mask"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3679:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC4(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z5sobelPhS_jjj(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC5(%rip), %rdx
	movl	$50, %r9d
	leaq	_ZL4mask(%rip), %rsi
	pushq	$1
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	call	__cudaRegisterVar@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3679:
	.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
	.local	_ZL4mask
	.comm	_ZL4mask,50,32
	.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 <iostream>
#include <cstdlib>
#include <cassert>
#include <zlib.h>
#include <png.h>

#define Z 2
#define Y 5
#define X 5
#define xBound X / 2
#define yBound Y / 2
#define SCALE 8

int read_png(const char* filename, unsigned char** image, unsigned* height, 
             unsigned* width, unsigned* channels) {

    unsigned char sig[8];
    FILE* infile;
    infile = fopen(filename, "rb");

    fread(sig, 1, 8, infile);
    if (!png_check_sig(sig, 8))
        return 1;   /* bad signature */

    png_structp png_ptr;
    png_infop info_ptr;

    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png_ptr)
        return 4;   /* out of memory */
  
    info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr) {
        png_destroy_read_struct(&png_ptr, NULL, NULL);
        return 4;   /* out of memory */
    }

    png_init_io(png_ptr, infile);
    png_set_sig_bytes(png_ptr, 8);
    png_read_info(png_ptr, info_ptr);
    int bit_depth, color_type;
    png_get_IHDR(png_ptr, info_ptr, width, height, &bit_depth, &color_type, NULL, NULL, NULL);

    png_uint_32  i, rowbytes;
    png_bytep  row_pointers[*height];
    png_read_update_info(png_ptr, info_ptr);
    rowbytes = png_get_rowbytes(png_ptr, info_ptr);
    *channels = (int) png_get_channels(png_ptr, info_ptr);

    if ((*image = (unsigned char *) malloc(rowbytes * *height)) == NULL) {
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
        return 3;
    }

    for (i = 0;  i < *height;  ++i)
        row_pointers[i] = *image + i * rowbytes;
    png_read_image(png_ptr, row_pointers);
    png_read_end(png_ptr, NULL);
    return 0;
}

void write_png(const char* filename, png_bytep image, const unsigned height, const unsigned width, 
               const unsigned channels) {
    FILE* fp = fopen(filename, "wb");
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    png_infop info_ptr = png_create_info_struct(png_ptr);
    png_init_io(png_ptr, fp);
    png_set_IHDR(png_ptr, info_ptr, width, height, 8,
                 PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
                 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
    png_set_filter(png_ptr, 0, PNG_NO_FILTERS);
    png_write_info(png_ptr, info_ptr);
    png_set_compression_level(png_ptr, 1);

    png_bytep row_ptr[height];
    for (int i = 0; i < height; ++ i) {
        row_ptr[i] = image + i * width * channels * sizeof(unsigned char);
    }
    png_write_image(png_ptr, row_ptr);
    png_write_end(png_ptr, NULL);
    png_destroy_write_struct(&png_ptr, &info_ptr);
    fclose(fp);
}

__constant__ char mask[Z][Y][X] = { { { -1, -4, -6, -4, -1 },
                                        { -2, -8, -12, -8, -2 },
                                        { 0, 0, 0, 0, 0 },
                                        { 2, 8, 12, 8, 2 },
                                        { 1, 4, 6, 4, 1 } },
                                      { { -1, -2, 0, 2, 1 },
                                        { -4, -8, 0, 8, 4 },
                                        { -6, -12, 0, 12, 6 },
                                        { -4, -8, 0, 8, 4 },
                                        { -1, -2, 0, 2, 1 } } };

inline __device__ int bound_check(int val, int lower, int upper) {
    if (val >= lower && val < upper)
        return 1;
    else
        return 0;
}

__global__ void sobel(unsigned char *s, unsigned char *t, unsigned height, unsigned width, unsigned channels) {

    // int tid = blockIdx.x * blockDim.x + threadIdx.x;
    float val[Z][3];
    __shared__ unsigned char RMat[5][262];
    __shared__ unsigned char GMat[5][262];
    __shared__ unsigned char BMat[5][262];
    // if (tid >= height) return;

    // int y = tid;
    for (int y = blockIdx.x; y < height; y += gridDim.x) {
        for (int x = threadIdx.x; x < width; x += blockDim.x) {
        // for (int x = 0; x < width; ++x) {
            /* Z axis of mask */
            /* Y and X axis of mask */
            for (int v = -yBound; v <= yBound; ++v) {
                if (bound_check(y + v, 0, height)) {
                    // FIXME: 目前這份 code 跑得過，只有在 threadNum 設成 64 的時候。
                    // 當我 threadNum 設成 256 時，當每個 blockIdx 進入第二次輪迴（blockIdx.x + gridDim.x）， x_row threadIdx.x 為 64 的地方都會出錯。
                    // 我強制設定為 threadNum 為 64，避免上述情況發生，但應該還是要 de 出來。
                    RMat[v+yBound][threadIdx.x + 2] = s[channels * (width * (y + v) + x) + 2];
                    GMat[v+yBound][threadIdx.x + 2] = s[channels * (width * (y + v) + x) + 1];
                    BMat[v+yBound][threadIdx.x + 2] = s[channels * (width * (y + v) + x) + 0];
                    if (threadIdx.x == 0) {
                        if (bound_check(x - 2, 0, width)) {
                            RMat[v+yBound][threadIdx.x] = s[channels * (width * (y + v) + x-2) + 2];
                            GMat[v+yBound][threadIdx.x] = s[channels * (width * (y + v) + x-2) + 1];
                            BMat[v+yBound][threadIdx.x] = s[channels * (width * (y + v) + x-2) + 0];
                        }
                        if (bound_check(x - 1, 0, width)) {
                            RMat[v+yBound][threadIdx.x + 1] = s[channels * (width * (y + v) + x-1) + 2];
                            GMat[v+yBound][threadIdx.x + 1] = s[channels * (width * (y + v) + x-1) + 1];
                            BMat[v+yBound][threadIdx.x + 1] = s[channels * (width * (y + v) + x-1) + 0];
                        }
                    } else if (threadIdx.x == blockDim.x - 1) {
                        if (bound_check(x + 1, 0, width)) {
                            RMat[v+yBound][threadIdx.x + 3] = s[channels * (width * (y + v) + x+1) + 2];
                            GMat[v+yBound][threadIdx.x + 3] = s[channels * (width * (y + v) + x+1) + 1];
                            BMat[v+yBound][threadIdx.x + 3] = s[channels * (width * (y + v) + x+1) + 0];
                        }
                        if (bound_check(x + 2, 0, width)) {
                            RMat[v+yBound][threadIdx.x + 4] = s[channels * (width * (y + v) + x+2) + 2];
                            GMat[v+yBound][threadIdx.x + 4] = s[channels * (width * (y + v) + x+2) + 1];
                            BMat[v+yBound][threadIdx.x + 4] = s[channels * (width * (y + v) + x+2) + 0];
                        }
                    }
                }
            }
            __syncthreads();

            for (int i = 0; i < Z; ++i) {

                val[i][2] = 0.;
                val[i][1] = 0.;
                val[i][0] = 0.;

                /* Y and X axis of mask */
                for (int v = -yBound; v <= yBound; ++v) {
                    for (int u = -xBound; u <= xBound; ++u) {
                        if (bound_check(x + u, 0, width) && bound_check(y + v, 0, height)) {
                            // const unsigned char R = s[channels * (width * (y + v) + (x + u)) + 2];
                            // const unsigned char G = s[channels * (width * (y + v) + (x + u)) + 1];
                            // const unsigned char B = s[channels * (width * (y + v) + (x + u)) + 0];
                            const unsigned char R = RMat[v+yBound][threadIdx.x + 2 + u];
                            const unsigned char G = GMat[v+yBound][threadIdx.x + 2 + u];
                            const unsigned char B = BMat[v+yBound][threadIdx.x + 2 + u];
                            val[i][2] += R * mask[i][u + xBound][v + yBound];
                            val[i][1] += G * mask[i][u + xBound][v + yBound];
                            val[i][0] += B * mask[i][u + xBound][v + yBound];
                        }
                    }
                }
            }
            float totalR = 0.;
            float totalG = 0.;
            float totalB = 0.;
            for (int i = 0; i < Z; ++i) {
                totalR += val[i][2] * val[i][2];
                totalG += val[i][1] * val[i][1];
                totalB += val[i][0] * val[i][0];
            }
            totalR = sqrt(totalR) / SCALE;
            totalG = sqrt(totalG) / SCALE;
            totalB = sqrt(totalB) / SCALE;
            const unsigned char cR = (totalR > 255.) ? 255 : totalR;
            const unsigned char cG = (totalG > 255.) ? 255 : totalG;
            const unsigned char cB = (totalB > 255.) ? 255 : totalB;
            t[channels * (width * y + x) + 2] = cR;
            t[channels * (width * y + x) + 1] = cG;
            t[channels * (width * y + x) + 0] = cB;
            __syncthreads();
        }
    }
}

int main(int argc, char **argv) {
    assert(argc == 3);
    unsigned height, width, channels;
    unsigned char *src = NULL, *dst;
    unsigned char *dsrc, *ddst;

    /* read the image to src, and get height, width, channels */
    if (read_png(argv[1], &src, &height, &width, &channels)) {
        std::cerr << "Error in read png" << std::endl;
        return -1;
    }

    dst = (unsigned char *)malloc(height * width * channels * sizeof(unsigned char));
    hipHostRegister(src, height * width * channels * sizeof(unsigned char), hipHostRegisterDefault);

    // cudaMalloc(...) for device src and device dst
    hipMalloc(&dsrc, height * width * channels * sizeof(unsigned char));
    hipMalloc(&ddst, height * width * channels * sizeof(unsigned char));

    // cudaMemcpy(...) copy source image to device (mask matrix if necessary)
    hipMemcpy(dsrc, src, height * width * channels * sizeof(unsigned char), hipMemcpyHostToDevice);

    // decide to use how many blocks and threads
    const int num_threads = 64;
    // const int num_blocks = height / num_threads + 1;
    const int num_blocks = 2048;

    // launch cuda kernel
    sobel <<<num_blocks, num_threads>>> (dsrc, ddst, height, width, channels);

    // cudaMemcpy(...) copy result image to host
    hipMemcpy(dst, ddst, height * width * channels * sizeof(unsigned char), hipMemcpyDeviceToHost);

    write_png(argv[2], dst, height, width, channels);
    free(src);
    free(dst);
    hipFree(dsrc);
    hipFree(ddst);
    return 0;
}


	.text
	.file	"lab5.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z8read_pngPKcPPhPjS3_S3_       # -- Begin function _Z8read_pngPKcPPhPjS3_S3_
	.type	_Z8read_pngPKcPPhPjS3_S3_,@function
_Z8read_pngPKcPPhPjS3_S3_:              # @_Z8read_pngPKcPPhPjS3_S3_
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$56, %rsp
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	movq	%r8, -72(%rbp)                  # 8-byte Spill
	movq	%rcx, %r13
	movq	%rdx, %r12
	movq	%rsi, %r15
	movl	$.L.str, %esi
	callq	fopen
	movq	%rax, %rbx
	leaq	-96(%rbp), %r14
	movl	$1, %esi
	movl	$8, %edx
	movq	%r14, %rdi
	movq	%rax, %rcx
	callq	fread
	movl	$8, %edx
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	png_sig_cmp
	movl	$1, %r14d
	testl	%eax, %eax
	je	.LBB0_1
.LBB0_10:
	movl	%r14d, %eax
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa %rsp, 8
	retq
.LBB0_1:
	.cfi_def_cfa %rbp, 16
	movq	%r15, -64(%rbp)                 # 8-byte Spill
	movq	%r13, -56(%rbp)                 # 8-byte Spill
	movl	$.L.str.1, %edi
	xorl	%esi, %esi
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	callq	png_create_read_struct
	movq	%rax, -48(%rbp)
	movl	$4, %r14d
	testq	%rax, %rax
	je	.LBB0_10
# %bb.2:
	movq	%rax, %r15
	movq	%rax, %rdi
	callq	png_create_info_struct
	movq	%rax, -80(%rbp)
	testq	%rax, %rax
	je	.LBB0_3
# %bb.4:
	movq	%rax, %r13
	movq	%r15, %rdi
	movq	%rbx, %rsi
	callq	png_init_io
	movq	-48(%rbp), %rdi
	movl	$8, %esi
	callq	png_set_sig_bytes
	movq	-48(%rbp), %rdi
	movq	%r13, %rsi
	callq	png_read_info
	movq	-48(%rbp), %rdi
	subq	$32, %rsp
	xorps	%xmm0, %xmm0
	movups	%xmm0, (%rsp)
	movq	$0, 16(%rsp)
	leaq	-88(%rbp), %r8
	leaq	-84(%rbp), %r9
	movq	%r13, %rsi
	movq	-56(%rbp), %rdx                 # 8-byte Reload
	movq	%r12, %r14
	movq	%r12, %rcx
	callq	png_get_IHDR
	addq	$32, %rsp
	movq	%rsp, %r15
	movl	(%r12), %eax
	movq	%rsp, %r12
	leaq	15(,%rax,8), %rax
	andq	$-16, %rax
	subq	%rax, %r12
	movq	%r12, %rsp
	movq	-48(%rbp), %rdi
	movq	%r13, %rsi
	callq	png_read_update_info
	movq	-48(%rbp), %rdi
	movq	%r13, %rsi
	callq	png_get_rowbytes
	movq	%rax, %rbx
	movq	-48(%rbp), %rdi
	movq	%r13, %rsi
	callq	png_get_channels
	movzbl	%al, %eax
	movq	-72(%rbp), %rcx                 # 8-byte Reload
	movl	%eax, (%rcx)
	movl	(%r14), %r14d
	movl	%r14d, %edi
	imull	%ebx, %edi
	callq	malloc
	movq	-64(%rbp), %rcx                 # 8-byte Reload
	movq	%rax, (%rcx)
	testq	%rax, %rax
	je	.LBB0_11
# %bb.5:                                # %.preheader
	testq	%r14, %r14
	je	.LBB0_8
# %bb.6:                                # %.lr.ph.preheader
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.LBB0_7:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%ecx, %esi
	addq	%rax, %rsi
	movq	%rsi, (%r12,%rdx,8)
	incq	%rdx
	addq	%rbx, %rcx
	cmpq	%rdx, %r14
	jne	.LBB0_7
.LBB0_8:                                # %._crit_edge
	movq	-48(%rbp), %rdi
	movq	%r12, %rsi
	callq	png_read_image
	movq	-48(%rbp), %rdi
	xorl	%r14d, %r14d
	xorl	%esi, %esi
	callq	png_read_end
	jmp	.LBB0_9
.LBB0_3:
	leaq	-48(%rbp), %rdi
	xorl	%esi, %esi
	xorl	%edx, %edx
	callq	png_destroy_read_struct
	jmp	.LBB0_10
.LBB0_11:
	leaq	-48(%rbp), %rdi
	leaq	-80(%rbp), %rsi
	xorl	%edx, %edx
	callq	png_destroy_read_struct
	movl	$3, %r14d
.LBB0_9:
	movq	%r15, %rsp
	jmp	.LBB0_10
.Lfunc_end0:
	.size	_Z8read_pngPKcPPhPjS3_S3_, .Lfunc_end0-_Z8read_pngPKcPPhPjS3_S3_
	.cfi_endproc
                                        # -- End function
	.globl	_Z9write_pngPKcPhjjj            # -- Begin function _Z9write_pngPKcPhjjj
	.type	_Z9write_pngPKcPhjjj,@function
_Z9write_pngPKcPhjjj:                   # @_Z9write_pngPKcPhjjj
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$40, %rsp
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	movl	%r8d, %r14d
	movl	%ecx, -44(%rbp)                 # 4-byte Spill
	movl	%edx, -48(%rbp)                 # 4-byte Spill
	movq	%rsi, %r15
	movl	$.L.str.2, %esi
	callq	fopen
	movq	%rax, -56(%rbp)                 # 8-byte Spill
	xorl	%ebx, %ebx
	movl	$.L.str.1, %edi
	xorl	%esi, %esi
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	callq	png_create_write_struct
	movq	%rax, %r12
	movq	%rax, -72(%rbp)
	movq	%rax, %rdi
	callq	png_create_info_struct
	movq	%rax, %r13
	movq	%rax, -64(%rbp)
	movq	%r12, %rdi
	movq	-56(%rbp), %rsi                 # 8-byte Reload
	callq	png_init_io
	subq	$8, %rsp
	movq	%r12, %rdi
	movq	%r13, %rsi
	movl	-44(%rbp), %edx                 # 4-byte Reload
	movl	-48(%rbp), %ecx                 # 4-byte Reload
	movl	$8, %r8d
	movl	$2, %r9d
	pushq	%rbx
	pushq	%rbx
	pushq	%rbx
	callq	png_set_IHDR
	addq	$32, %rsp
	movq	%r12, %rdi
	xorl	%esi, %esi
	xorl	%edx, %edx
	callq	png_set_filter
	movq	%r12, %rdi
	movq	%r13, %rsi
	callq	png_write_info
	movq	%r12, %rdi
	movl	$1, %esi
	callq	png_set_compression_level
	movq	%rsp, %r13
	movl	-48(%rbp), %edx                 # 4-byte Reload
	movl	%edx, %eax
	movq	%rsp, %rsi
	leaq	15(,%rax,8), %rcx
	andq	$-16, %rcx
	subq	%rcx, %rsi
	movq	%rsi, %rsp
	testl	%edx, %edx
	je	.LBB1_3
# %bb.1:                                # %.lr.ph
	imull	-44(%rbp), %r14d                # 4-byte Folded Reload
	xorl	%ecx, %ecx
.LBB1_2:                                # =>This Inner Loop Header: Depth=1
	movl	%ebx, %edx
	addq	%r15, %rdx
	movq	%rdx, (%rsi,%rcx,8)
	incq	%rcx
	addl	%r14d, %ebx
	cmpq	%rcx, %rax
	jne	.LBB1_2
.LBB1_3:                                # %._crit_edge
	movq	%r12, %rdi
	callq	png_write_image
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	png_write_end
	leaq	-72(%rbp), %rdi
	leaq	-64(%rbp), %rsi
	callq	png_destroy_write_struct
	movq	-56(%rbp), %rdi                 # 8-byte Reload
	callq	fclose
	movq	%r13, %rsp
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa %rsp, 8
	retq
.Lfunc_end1:
	.size	_Z9write_pngPKcPhjjj, .Lfunc_end1-_Z9write_pngPKcPhjjj
	.cfi_endproc
                                        # -- End function
	.globl	_Z20__device_stub__sobelPhS_jjj # -- Begin function _Z20__device_stub__sobelPhS_jjj
	.type	_Z20__device_stub__sobelPhS_jjj,@function
_Z20__device_stub__sobelPhS_jjj:        # @_Z20__device_stub__sobelPhS_jjj
	.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	$128, %rsp
	.cfi_def_cfa_offset 176
	.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)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%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	$_Z5sobelPhS_jjj, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	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_end2:
	.size	_Z20__device_stub__sobelPhS_jjj, .Lfunc_end2-_Z20__device_stub__sobelPhS_jjj
	.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, %rbx
	leaq	32(%rsp), %rsi
	movq	$0, (%rsi)
	movq	8(%rbx), %rdi
	leaq	4(%rsp), %rdx
	movq	%rsp, %rcx
	leaq	20(%rsp), %r8
	callq	_Z8read_pngPKcPPhPjS3_S3_
	testl	%eax, %eax
	je	.LBB3_2
# %bb.1:
	movl	$_ZSt4cerr, %ebx
	movl	$_ZSt4cerr, %edi
	movl	$.L.str.3, %esi
	movl	$17, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cerr(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cerr, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$-1, %r14d
	jmp	.LBB3_5
.LBB3_2:
	movl	(%rsp), %r13d
	imull	4(%rsp), %r13d
	movl	20(%rsp), %ebp
	imull	%ebp, %r13d
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, %r15
	movq	32(%rsp), %r12
	xorl	%r14d, %r14d
	movq	%r12, %rdi
	movq	%r13, %rsi
	xorl	%edx, %edx
	callq	hipHostRegister
	movl	4(%rsp), %esi
	imull	%ebp, %esi
	imull	(%rsp), %esi
	leaq	24(%rsp), %r13
	movq	%r13, %rdi
	callq	hipMalloc
	movl	4(%rsp), %esi
	imull	%ebp, %esi
	imull	(%rsp), %esi
	leaq	8(%rsp), %rdi
	callq	hipMalloc
	movq	(%r13), %rdi
	movl	4(%rsp), %edx
	imull	%ebp, %edx
	imull	(%rsp), %edx
	movq	%r12, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movabsq	$4294967360, %rdx               # imm = 0x100000040
	leaq	1984(%rdx), %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_4
# %bb.3:
	movq	24(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	4(%rsp), %edx
	movl	(%rsp), %ecx
	movl	%ebp, %r8d
	callq	_Z20__device_stub__sobelPhS_jjj
.LBB3_4:
	movq	8(%rsp), %rsi
	movl	4(%rsp), %edx
	imull	%ebp, %edx
	imull	(%rsp), %edx
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	16(%rbx), %rdi
	movl	4(%rsp), %edx
	movl	(%rsp), %ecx
	movq	%r15, %rsi
	movl	%ebp, %r8d
	callq	_Z9write_pngPKcPhjjj
	movq	%r12, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
.LBB3_5:
	movl	%r14d, %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_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z5sobelPhS_jjj, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$0, 8(%rsp)
	movl	$1, (%rsp)
	movl	$mask, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movl	$50, %r9d
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	callq	__hipRegisterVar
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__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	.LBB5_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
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"rb"
	.size	.L.str, 3

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"1.6.43"
	.size	.L.str.1, 7

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"wb"
	.size	.L.str.2, 3

	.type	mask,@object                    # @mask
	.local	mask
	.comm	mask,50,16
	.type	_Z5sobelPhS_jjj,@object         # @_Z5sobelPhS_jjj
	.section	.rodata,"a",@progbits
	.globl	_Z5sobelPhS_jjj
	.p2align	3, 0x0
_Z5sobelPhS_jjj:
	.quad	_Z20__device_stub__sobelPhS_jjj
	.size	_Z5sobelPhS_jjj, 8

	.type	.L.str.3,@object                # @.str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.3:
	.asciz	"Error in read png"
	.size	.L.str.3, 18

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z5sobelPhS_jjj"
	.size	.L__unnamed_1, 16

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"mask"
	.size	.L__unnamed_2, 5

	.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 _Z20__device_stub__sobelPhS_jjj
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym mask
	.addrsig_sym _Z5sobelPhS_jjj
	.addrsig_sym _ZSt4cerr
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z5sobelPhS_jjj         ; -- Begin function _Z5sobelPhS_jjj
	.globl	_Z5sobelPhS_jjj
	.p2align	8
	.type	_Z5sobelPhS_jjj,@function
_Z5sobelPhS_jjj:                        ; @_Z5sobelPhS_jjj
; %bb.0:
	s_load_b128 s[16:19], s[2:3], 0x10
	s_waitcnt lgkmcnt(0)
	s_cmp_ge_u32 s15, s16
	s_cbranch_scc1 .LBB0_30
; %bb.1:                                ; %.lr.ph
	s_load_b64 s[0:1], s[0:1], 0x4
	v_and_b32_e32 v1, 0x3ff, v0
	s_clause 0x1
	s_load_b128 s[20:23], s[2:3], 0x0
	s_load_b32 s12, s[2:3], 0x20
	v_bfe_u32 v4, v0, 10, 10
	v_bfe_u32 v0, v0, 20, 10
	v_dual_mov_b32 v9, 0 :: v_dual_add_nc_u32 v6, 4, v1
	s_mov_b32 s14, 0
	s_mul_i32 s24, s18, s17
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s0, s0, 16
	s_add_u32 s10, s2, 32
	s_mul_i32 s0, s0, s1
	s_addc_u32 s11, s3, 0
	s_add_i32 s2, s15, -2
	v_mul_lo_u32 v5, s0, v1
	v_mad_u64_u32 v[2:3], null, s17, s2, v[1:2]
	v_mul_u32_u24_e32 v3, s1, v4
	v_cmp_gt_u32_e64 s0, s17, v1
	v_cmp_ne_u32_e64 s1, 0, v1
	s_mul_i32 s13, s12, s17
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_3) | instid1(VALU_DEP_3)
	v_add3_u32 v5, v5, v3, v0
	v_mul_lo_u32 v0, s18, v2
	v_add_nc_u32_e32 v7, -2, v2
	s_mul_i32 s19, s13, s18
	v_mad_u64_u32 v[3:4], null, v5, 24, 0xf60
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_mul_lo_u32 v7, s18, v7
	v_add_nc_u32_e32 v8, s18, v0
.LBB0_2:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_4 Depth 2
                                        ;       Child Loop BB0_5 Depth 3
                                        ;       Child Loop BB0_21 Depth 3
                                        ;         Child Loop BB0_22 Depth 4
                                        ;           Child Loop BB0_23 Depth 5
	s_and_saveexec_b32 s25, s0
	s_cbranch_execz .LBB0_29
; %bb.3:                                ; %.preheader237.lr.ph
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_load_b32 s2, s[10:11], 0xc
	v_dual_mov_b32 v10, v2 :: v_dual_mov_b32 v11, v1
	s_mul_i32 s27, s15, s17
	s_mov_b32 s29, 0
	s_mov_b32 s30, s14
	s_waitcnt lgkmcnt(0)
	s_and_b32 s26, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_i32 s2, s26, -1
	s_mul_i32 s28, s18, s26
	v_cmp_eq_u32_e64 s2, s2, v1
.LBB0_4:                                ; %.preheader237
                                        ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Loop Header: Depth=2
                                        ;       Child Loop BB0_5 Depth 3
                                        ;       Child Loop BB0_21 Depth 3
                                        ;         Child Loop BB0_22 Depth 4
                                        ;           Child Loop BB0_23 Depth 5
	v_add_nc_u32_e32 v4, 1, v11
	v_add_nc_u32_e32 v5, 2, v11
	v_add_nc_u32_e32 v12, -2, v11
	v_cmp_lt_i32_e32 vcc_lo, -2, v11
	v_cmp_lt_i32_e64 s4, -3, v11
	v_cmp_gt_i32_e64 s3, s17, v4
	v_mov_b32_e32 v4, v10
	v_cmp_gt_i32_e64 s5, s17, v5
	v_cmp_lt_i32_e64 s6, 1, v11
	v_cmp_gt_i32_e64 s7, s17, v12
	v_cmp_lt_i32_e64 s8, 0, v11
	v_cmp_ge_i32_e64 s9, s17, v11
	s_mov_b32 s31, -2
	s_and_b32 s3, vcc_lo, s3
	s_and_b32 s4, s4, s5
	s_and_b32 s5, s6, s7
	s_and_b32 s6, s8, s9
	s_mov_b32 s7, s30
	s_mov_b32 s8, 0
.LBB0_5:                                ;   Parent Loop BB0_2 Depth=1
                                        ;     Parent Loop BB0_4 Depth=2
                                        ; =>    This Inner Loop Header: Depth=3
	s_add_i32 s9, s15, s31
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(SALU_CYCLE_1)
	s_cmp_lt_i32 s9, 0
	s_cselect_b32 s33, -1, 0
	s_cmp_ge_i32 s9, s16
	s_cselect_b32 s9, -1, 0
	s_or_b32 s9, s33, s9
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_b32 vcc_lo, exec_lo, s9
	s_cbranch_vccnz .LBB0_19
; %bb.6:                                ;   in Loop: Header=BB0_5 Depth=3
	v_add_nc_u32_e32 v5, s7, v0
	s_mov_b32 s9, 0
                                        ; implicit-def: $sgpr34
	s_delay_alu instid0(VALU_DEP_1)
	v_add_nc_u32_e32 v12, 2, v5
	v_add_nc_u32_e32 v13, 1, v5
	s_clause 0x2
	global_load_u8 v14, v12, s[20:21]
	global_load_u8 v13, v13, s[20:21]
	global_load_u8 v5, v5, s[20:21]
	v_add_nc_u32_e32 v12, s8, v1
	s_waitcnt vmcnt(2)
	ds_store_b8 v12, v14 offset:2626
	s_waitcnt vmcnt(1)
	ds_store_b8 v12, v13 offset:1314
	s_waitcnt vmcnt(0)
	ds_store_b8 v12, v5 offset:2
                                        ; implicit-def: $vgpr5
	s_and_saveexec_b32 s33, s1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_xor_b32 s33, exec_lo, s33
	s_cbranch_execz .LBB0_12
; %bb.7:                                ;   in Loop: Header=BB0_5 Depth=3
	s_mov_b32 s35, 0
                                        ; implicit-def: $sgpr34
	s_and_saveexec_b32 s9, s2
	s_cbranch_execz .LBB0_11
; %bb.8:                                ;   in Loop: Header=BB0_5 Depth=3
	s_and_saveexec_b32 s34, s3
	s_cbranch_execz .LBB0_10
; %bb.9:                                ;   in Loop: Header=BB0_5 Depth=3
	v_add_nc_u32_e32 v5, s7, v8
	s_delay_alu instid0(VALU_DEP_1)
	v_add_nc_u32_e32 v13, 2, v5
	v_add_nc_u32_e32 v14, 1, v5
	s_clause 0x2
	global_load_u8 v13, v13, s[20:21]
	global_load_u8 v14, v14, s[20:21]
	global_load_u8 v5, v5, s[20:21]
	s_waitcnt vmcnt(2)
	ds_store_b8 v12, v13 offset:2627
	s_waitcnt vmcnt(1)
	ds_store_b8 v12, v14 offset:1315
	s_waitcnt vmcnt(0)
	ds_store_b8 v12, v5 offset:3
.LBB0_10:                               ;   in Loop: Header=BB0_5 Depth=3
	s_or_b32 exec_lo, exec_lo, s34
	s_mov_b32 s34, 2
	s_and_b32 s35, s4, exec_lo
.LBB0_11:                               ; %Flow370
                                        ;   in Loop: Header=BB0_5 Depth=3
	s_or_b32 exec_lo, exec_lo, s9
	v_mov_b32_e32 v5, v6
	s_and_b32 s9, s35, exec_lo
.LBB0_12:                               ; %Flow
                                        ;   in Loop: Header=BB0_5 Depth=3
	s_or_saveexec_b32 s33, s33
	v_mov_b32_e32 v12, s34
	s_xor_b32 exec_lo, exec_lo, s33
	s_cbranch_execz .LBB0_16
; %bb.13:                               ;   in Loop: Header=BB0_5 Depth=3
	s_and_saveexec_b32 s34, s5
	s_cbranch_execz .LBB0_15
; %bb.14:                               ;   in Loop: Header=BB0_5 Depth=3
	v_dual_mov_b32 v14, s8 :: v_dual_add_nc_u32 v5, s7, v7
	s_delay_alu instid0(VALU_DEP_1)
	v_add_nc_u32_e32 v12, 2, v5
	v_add_nc_u32_e32 v13, 1, v5
	s_clause 0x2
	global_load_u8 v12, v12, s[20:21]
	global_load_u8 v13, v13, s[20:21]
	global_load_u8 v5, v5, s[20:21]
	s_waitcnt vmcnt(2)
	ds_store_b8 v14, v12 offset:2624
	s_waitcnt vmcnt(1)
	ds_store_b8 v14, v13 offset:1312
	s_waitcnt vmcnt(0)
	ds_store_b8 v14, v5
.LBB0_15:                               ;   in Loop: Header=BB0_5 Depth=3
	s_or_b32 exec_lo, exec_lo, s34
	v_dual_mov_b32 v5, 1 :: v_dual_mov_b32 v12, -1
	s_and_not1_b32 s9, s9, exec_lo
	s_and_b32 s34, s6, exec_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 s9, s9, s34
.LBB0_16:                               ; %Flow371
                                        ;   in Loop: Header=BB0_5 Depth=3
	s_or_b32 exec_lo, exec_lo, s33
	s_and_saveexec_b32 s33, s9
	s_cbranch_execz .LBB0_18
; %bb.17:                               ; %.sink.split
                                        ;   in Loop: Header=BB0_5 Depth=3
	v_add_nc_u32_e32 v12, v4, v12
	v_add_nc_u32_e32 v5, s8, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v12, v12, s18
	v_add_nc_u32_e32 v13, 2, v12
	v_add_nc_u32_e32 v14, 1, v12
	s_clause 0x2
	global_load_u8 v13, v13, s[20:21]
	global_load_u8 v14, v14, s[20:21]
	global_load_u8 v12, v12, s[20:21]
	s_waitcnt vmcnt(2)
	ds_store_b8 v5, v13 offset:2624
	s_waitcnt vmcnt(1)
	ds_store_b8 v5, v14 offset:1312
	s_waitcnt vmcnt(0)
	ds_store_b8 v5, v12
.LBB0_18:                               ; %Flow372
                                        ;   in Loop: Header=BB0_5 Depth=3
	s_or_b32 exec_lo, exec_lo, s33
.LBB0_19:                               ;   in Loop: Header=BB0_5 Depth=3
	v_add_nc_u32_e32 v4, s17, v4
	s_add_i32 s31, s31, 1
	s_addk_i32 s8, 0x106
	s_add_i32 s7, s7, s24
	s_cmp_eq_u32 s31, 3
	s_cbranch_scc0 .LBB0_5
; %bb.20:                               ;   in Loop: Header=BB0_4 Depth=2
	s_mov_b32 s3, 0
	s_mov_b32 s4, -1
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
.LBB0_21:                               ;   Parent Loop BB0_2 Depth=1
                                        ;     Parent Loop BB0_4 Depth=2
                                        ; =>    This Loop Header: Depth=3
                                        ;         Child Loop BB0_22 Depth 4
                                        ;           Child Loop BB0_23 Depth 5
	v_mad_u64_u32 v[4:5], null, s3, 12, v[3:4]
	s_xor_b32 s8, s4, -1
	s_mul_i32 s6, s3, 25
	v_dual_mov_b32 v5, 0 :: v_dual_mov_b32 v12, v1
	v_dual_mov_b32 v13, 0 :: v_dual_mov_b32 v14, 0
	s_getpc_b64 s[4:5]
	s_add_u32 s4, s4, mask@rel32@lo+4
	s_addc_u32 s5, s5, mask@rel32@hi+12
	s_mul_hi_u32 s3, s3, 25
	s_add_u32 s4, s6, s4
	s_addc_u32 s5, s3, s5
	s_mov_b32 s9, -2
	ds_store_b32 v4, v9 offset:8
	ds_store_2addr_b32 v4, v9, v9 offset1:1
.LBB0_22:                               ; %.preheader
                                        ;   Parent Loop BB0_2 Depth=1
                                        ;     Parent Loop BB0_4 Depth=2
                                        ;       Parent Loop BB0_21 Depth=3
                                        ; =>      This Loop Header: Depth=4
                                        ;           Child Loop BB0_23 Depth 5
	s_add_i32 s3, s9, s15
	v_mov_b32_e32 v15, -5
	s_cmp_lt_i32 s3, 0
	s_cselect_b32 s6, -1, 0
	s_cmp_ge_i32 s3, s16
	s_cselect_b32 s3, -1, 0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 s31, s6, s3
	s_mov_b64 s[6:7], s[4:5]
.LBB0_23:                               ;   Parent Loop BB0_2 Depth=1
                                        ;     Parent Loop BB0_4 Depth=2
                                        ;       Parent Loop BB0_21 Depth=3
                                        ;         Parent Loop BB0_22 Depth=4
                                        ; =>        This Inner Loop Header: Depth=5
	v_add3_u32 v16, v11, v15, 3
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_cmp_gt_i32_e32 vcc_lo, 0, v16
	v_cmp_le_i32_e64 s3, s17, v16
	s_or_b32 s3, vcc_lo, s3
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s3, s3, s31
	s_xor_b32 s33, s3, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s3, s33
	s_cbranch_execz .LBB0_25
; %bb.24:                               ;   in Loop: Header=BB0_23 Depth=5
	global_load_i8 v16, v9, s[6:7]
	v_add_nc_u32_e32 v17, v12, v15
	ds_load_u8 v18, v17 offset:2629
	ds_load_u8 v19, v17 offset:1317
	ds_load_u8 v17, v17 offset:5
	s_waitcnt vmcnt(0) lgkmcnt(2)
	v_mul_i32_i24_e32 v18, v16, v18
	s_waitcnt lgkmcnt(1)
	v_mul_i32_i24_e32 v19, v16, v19
	s_waitcnt lgkmcnt(0)
	v_mul_i32_i24_e32 v16, v16, v17
	v_cvt_f32_i32_e32 v17, v18
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cvt_f32_i32_e32 v18, v19
	v_cvt_f32_i32_e32 v16, v16
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_dual_add_f32 v14, v14, v17 :: v_dual_add_f32 v13, v13, v18
	v_add_f32_e32 v5, v5, v16
	ds_store_b32 v4, v14 offset:8
	ds_store_2addr_b32 v4, v5, v13 offset1:1
.LBB0_25:                               ;   in Loop: Header=BB0_23 Depth=5
	s_or_b32 exec_lo, exec_lo, s3
	v_add_co_u32 v15, s3, v15, 1
	s_add_u32 s6, s6, 5
	s_addc_u32 s7, s7, 0
	s_and_b32 vcc_lo, exec_lo, s3
	s_cbranch_vccz .LBB0_23
; %bb.26:                               ;   in Loop: Header=BB0_22 Depth=4
	s_add_i32 s9, s9, 1
	v_add_nc_u32_e32 v12, 0x106, v12
	s_add_u32 s4, s4, 1
	s_addc_u32 s5, s5, 0
	s_cmp_eq_u32 s9, 3
	s_cbranch_scc0 .LBB0_22
; %bb.27:                               ;   in Loop: Header=BB0_21 Depth=3
	s_mov_b32 s3, 1
	s_and_not1_b32 vcc_lo, exec_lo, s8
	s_mov_b32 s4, 0
	s_cbranch_vccnz .LBB0_21
; %bb.28:                               ; %.preheader236.preheader
                                        ;   in Loop: Header=BB0_4 Depth=2
	ds_load_b64 v[4:5], v3 offset:16
	ds_load_b128 v[12:15], v3
	s_add_i32 s30, s30, s28
	s_waitcnt lgkmcnt(1)
	v_dual_mul_f32 v5, v5, v5 :: v_dual_add_nc_u32 v10, s26, v10
	s_waitcnt lgkmcnt(0)
	v_dual_mul_f32 v4, v4, v4 :: v_dual_mul_f32 v15, v15, v15
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_dual_fmac_f32 v4, v13, v13 :: v_dual_fmac_f32 v15, v12, v12
	v_fmac_f32_e32 v5, v14, v14
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_dual_mul_f32 v13, 0x4f800000, v4 :: v_dual_mul_f32 v14, 0x4f800000, v15
	v_mul_f32_e32 v12, 0x4f800000, v5
	v_cmp_gt_f32_e32 vcc_lo, 0xf800000, v5
	v_cmp_gt_f32_e64 s3, 0xf800000, v4
	v_cmp_gt_f32_e64 s4, 0xf800000, v15
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cndmask_b32_e32 v5, v5, v12, vcc_lo
	v_cndmask_b32_e64 v4, v4, v13, s3
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cndmask_b32_e64 v12, v15, v14, s4
	v_sqrt_f32_e32 v13, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sqrt_f32_e32 v14, v4
	v_sqrt_f32_e32 v15, v12
	s_delay_alu instid0(TRANS32_DEP_3)
	v_add_nc_u32_e32 v16, -1, v13
	s_waitcnt_depctr 0xfff
	v_add_nc_u32_e32 v18, -1, v14
	v_add_nc_u32_e32 v17, 1, v13
	v_add_nc_u32_e32 v20, -1, v15
	v_add_nc_u32_e32 v19, 1, v14
	v_fma_f32 v22, -v16, v13, v5
	v_fma_f32 v24, -v18, v14, v4
	v_fma_f32 v23, -v17, v13, v5
	v_fma_f32 v26, -v20, v15, v12
	v_add_nc_u32_e32 v21, 1, v15
	v_cmp_ge_f32_e64 s5, 0, v22
	v_fma_f32 v25, -v19, v14, v4
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_fma_f32 v27, -v21, v15, v12
	v_cndmask_b32_e64 v13, v13, v16, s5
	v_cmp_ge_f32_e64 s5, 0, v24
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_cndmask_b32_e64 v14, v14, v18, s5
	v_cmp_ge_f32_e64 s5, 0, v26
	v_cndmask_b32_e64 v15, v15, v20, s5
	v_cmp_lt_f32_e64 s5, 0, v23
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cndmask_b32_e64 v13, v13, v17, s5
	v_cmp_lt_f32_e64 s5, 0, v25
	v_mul_f32_e32 v16, 0x37800000, v13
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cndmask_b32_e64 v14, v14, v19, s5
	v_cmp_lt_f32_e64 s5, 0, v27
	v_dual_cndmask_b32 v13, v13, v16 :: v_dual_mul_f32 v18, 0x37800000, v14
	v_cmp_class_f32_e64 vcc_lo, v5, 0x260
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cndmask_b32_e64 v14, v14, v18, s3
	v_cndmask_b32_e32 v5, v13, v5, vcc_lo
	v_cmp_class_f32_e64 vcc_lo, v4, 0x260
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_cndmask_b32_e32 v4, v14, v4, vcc_lo
	v_cndmask_b32_e64 v15, v15, v21, s5
	v_cmp_class_f32_e64 vcc_lo, v12, 0x260
	v_mul_f32_e32 v4, 0x3e000000, v4
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v19, 0x37800000, v15
	v_cndmask_b32_e64 v15, v15, v19, s4
	v_add_nc_u32_e32 v17, s27, v11
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_dual_cndmask_b32 v12, v15, v12 :: v_dual_add_nc_u32 v11, s26, v11
	v_mul_lo_u32 v13, v17, s18
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cmp_le_u32_e32 vcc_lo, s17, v11
	v_dual_mul_f32 v12, 0x3e000000, v12 :: v_dual_mul_f32 v5, 0x3e000000, v5
	s_or_b32 s29, vcc_lo, s29
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v15, 1, v13
	v_add_nc_u32_e32 v14, 2, v13
	v_cmp_nlt_f32_e64 s3, 0x437f0000, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cndmask_b32_e64 v5, 0x437f0000, v5, s3
	v_cmp_nlt_f32_e64 s3, 0x437f0000, v4
	v_cvt_i32_f32_e32 v5, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cndmask_b32_e64 v4, 0x437f0000, v4, s3
	v_cmp_nlt_f32_e64 s3, 0x437f0000, v12
	v_cvt_i32_f32_e32 v4, v4
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e64 v12, 0x437f0000, v12, s3
	v_cvt_i32_f32_e32 v12, v12
	s_clause 0x2
	global_store_b8 v15, v4, s[22:23]
	global_store_b8 v14, v5, s[22:23]
	global_store_b8 v13, v12, s[22:23]
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_and_not1_b32 exec_lo, exec_lo, s29
	s_cbranch_execnz .LBB0_4
.LBB0_29:                               ; %Flow375
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s25
	v_add_nc_u32_e32 v2, s13, v2
	s_add_i32 s15, s15, s12
	s_add_i32 s14, s14, s19
	s_cmp_ge_u32 s15, s16
	s_cbranch_scc0 .LBB0_2
.LBB0_30:                               ; %._crit_edge249
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z5sobelPhS_jjj
		.amdhsa_group_segment_fixed_size 28512
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 1
		.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 2
		.amdhsa_next_free_vgpr 28
		.amdhsa_next_free_sgpr 36
		.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	_Z5sobelPhS_jjj, .Lfunc_end0-_Z5sobelPhS_jjj
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1988
; NumSgprs: 38
; NumVgprs: 28
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 28512 bytes/workgroup (compile time only)
; SGPRBlocks: 4
; VGPRBlocks: 3
; NumSGPRsForWavesPerEU: 38
; NumVGPRsForWavesPerEU: 28
; 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: 2
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.protected	mask                    ; @mask
	.type	mask,@object
	.data
	.globl	mask
	.p2align	4, 0x0
mask:
	.ascii	"\377\374\372\374\377"
	.ascii	"\376\370\364\370\376"
	.zero	5
	.ascii	"\002\b\f\b\002"
	.ascii	"\001\004\006\004\001"
	.ascii	"\377\376\000\002\001"
	.ascii	"\374\370\000\b\004"
	.ascii	"\372\364\000\f\006"
	.ascii	"\374\370\000\b\004"
	.ascii	"\377\376\000\002\001"
	.size	mask, 50

	.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 mask
	.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:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 28512
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z5sobelPhS_jjj
    .private_segment_fixed_size: 0
    .sgpr_count:     38
    .sgpr_spill_count: 0
    .symbol:         _Z5sobelPhS_jjj.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     28
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
