/**
*   Programação Concorrente - SSC0143 - 2 Semestre de 2015
*   Prof. Dr. Júlio Cezar Estrella
*   Trabalho 3 - Smoothing de imagem utilizando CUDA
*  
*   Alunos:
*       Thiago Ledur Lima       - 8084214
*       Rodrigo Neves Bernardi  - 8066395   
**/


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include "Smoothing_CUDA.h"
#include <sys/time.h>

#define FILTERDIM 5
#define FILTERSIZE 25
#define TILEWIDTH 16

typedef unsigned char uchar;

typedef struct IMGstructure {
    char type[3];
    int width;
    int height;
    int maxVal;
    uchar *pixel;
    uchar *r;
    uchar *g;
    uchar *b;
} Image;

// verifica erros
void cudaCheck(cudaError_t error) {
    if(error != cudaSuccess) {
        fprintf(stderr,"ERROR: %s\n", cudaGetErrorString(error));
        exit(1);
    }
}

// aloca memória necessária para uma Image, recebendo suas informações
void allocData(Image *img, char* type, int width, int height, int maxVal) {
    strcpy(img->type, type);
    img->width = width;
    img->height = height;
    img->maxVal = maxVal;

    if(img->type[0] == 'P' && img->type[1] == '2') {
        img->pixel = (uchar *)malloc(width * height * sizeof(uchar));
        img->r = NULL;
        img->g = NULL;
        img->b = NULL;
    }

    else if(img->type[0] == 'P' && img->type[1] == '3') {
        img->pixel = NULL;
        img->r = (uchar *)malloc(width * height * sizeof(uchar));
        img->g = (uchar *)malloc(width * height * sizeof(uchar));
        img->b = (uchar *)malloc(width * height * sizeof(uchar));
    }
}

// libera memória usada por uma Image
void freeData(Image *img) {
    
    if (img->type[0] == 'P' && img->type[1] == '2') {
        free(img->pixel);
    }

    else if(img->type[0] == 'P' && img->type[1] == '3') {
        free(img->r);
        free(img->g);
        free(img->b);    
    }
}

// cria uma estrutura Image, incluindo a leitura do arquivo e a chamada da função para alocar a imagem na memória
void readImage(Image *imgIn, Image *imgOut, char *in) {
    FILE *input;
    int i;
    char type[3];
    int maxVal, width, height;

    input = fopen(in, "r");

    if (input == NULL)
        printf("Can't read image file.\n");
    
    else {
        // le tipo
        rewind(input);
        fscanf(input, "%s", type);

        printf("%s\n", type);

        // compara tipo para ver se é pgm (escala de cinza)
        if(type[0] == 'P' && type[1] == '2') {
            // pula fim da linha
            while (getc(input) != '\n');

            // pula comentario da linha
            while (getc(input) == '#') {
                while (getc(input) != '\n');
            }

            // volta um caracter
            fseek(input, -1, SEEK_CUR);

            // le dimensões da imagem e a escala das cores
            fscanf(input, "%d", &width);
            fscanf(input, "%d", &height);
            fscanf(input, "%d", &maxVal);

            // aloca as matrizes width x height das imagens de entrada e saída na memória
            allocData(imgIn, type, width, height, maxVal);
            allocData(imgOut, type, width, height, maxVal);
            
            // le dados do arquivo
            for(i = 0; i < width * height ; i++) {
                // fscanf(input, "%hhu %hhu %hhu", &(imgIn->data->r[i]), &(imgIn->data->g[i]), &(imgIn->data->b[i]));
                fscanf(input, "%hhu\n", &(imgIn->pixel[i]));        
            }
        }

        // se nao, imagem é ppm (colorida)
        else if(type[0] == 'P' && type[1] == '3') {
            // pula fim da linha
            while (getc(input) != '\n');

            // pula comentario da linha
            while (getc(input) == '#') {
                while (getc(input) != '\n');
            }

            // volta um caracter
            fseek(input, -1, SEEK_CUR);

            // le dimensões da imagem e a escala das cores
            fscanf(input, "%d", &width);
            fscanf(input, "%d", &height);
            fscanf(input, "%d", &maxVal);

            // aloca as matrizes width x height das imagens de entrada e saída na memória
            allocData(imgIn, type, width, height, maxVal);
            allocData(imgOut, type, width, height, maxVal);
            
            // le dados do arquivo
            for(i = 0; i < width * height ; i++) {
                // fscanf(input, "%hhu %hhu %hhu", &(imgIn->data->r[i]), &(imgIn->data->g[i]), &(imgIn->data->b[i]));
                fscanf(input, "%hhu %hhu %hhu\n", &(imgIn->r[i]), &(imgIn->g[i]), &(imgIn->b[i]));        
            }
        }
    }

    fclose(input);
}

// cria um arquivo com o resultado guardado na matriz de imagem de saída
void saveImage(Image *img, char *out) {
    FILE *output;
    int i;

    // escreve header contendo tipo, comentário, dimensões e escala
    output = fopen(out, "w");
    fprintf(output, "%s\n", img->type);
    fprintf(output, "#imagem com smooth\n");
    fprintf(output, "%d %d\n", img->width, img->height);
    fprintf(output, "%d\n", img->maxVal);

    // escreve dados da imagem

    if(img->type[0] == 'P' && img->type[1] == '2') {
        for (i = 0; i < img->height * img->width; i++) {
            //fprintf(output, "%d %d %d\t", img->data[i][j].r, img->data[i][j].g, img->data[i][j].b);
            // fprintf(output, "%hhu %hhu %hhu ", img->data->r[i], img->data->g[i], img->data->b[i]);
            
            if((i >= 2*img->width) && (i < ((img->height*img->width) - 2*img->width)))
                fprintf(output, "%hhu\n", img->pixel[i]);
        }
    }

    else if(img->type[0] == 'P' && img->type[1] == '3') {
        for (i = 0; i < img->height * img->width; i++) {
            //fprintf(output, "%d %d %d\t", img->data[i][j].r, img->data[i][j].g, img->data[i][j].b);
            // fprintf(output, "%hhu %hhu %hhu ", img->data->r[i], img->data->g[i], img->data->b[i]);
        
            if((i >= 2*img->width) && (i < ((img->height*img->width) - 2*img->width)))
                fprintf(output, "%hhu %hhu %hhu\n", img->r[i], img->g[i], img->b[i]);
        }
    }

    fclose(output);
}

// função de kernel para filtro smooth
__global__ void filter(uchar *in, uchar *out, int H, int W) {
    int i, j, k;

    int x = blockDim.x * blockIdx.x + threadIdx.x;
    int y = blockDim.y * blockIdx.y + threadIdx.y;

    int idx = y * W + x;
    float Pvalue = 0;

    k = FILTERDIM / 2;

    // restringe as bordas da imagem
    if (x >= W-k|| y >= H-k|| x <= k-1 || y <= k-1) return;

    // aplica convolucao
    for(i = 0; i <= FILTERDIM - 1; i++) {
        for(j = 0; j <= FILTERDIM - 1; j++) {
           Pvalue += in[(idx - k + i) + W * (j - k)];
        }
    }
    out[idx] = (uchar)(Pvalue / FILTERSIZE);
    //printf("\nFILTER FINISHED IN : %d %d\n", x, y);
}


int main(int argc, char const *argv[]) {
    Image imgIn, imgOut;

    uchar *inR, *inG, *inB, *inPixel, *outR, *outG, *outB, *outPixel;
    int size;
    char in[20], out[20], tempo[20];
    double result;

    // Tempo total
    struct timeval startTimeCudaTotal, endTimeCudaTotal;
    gettimeofday(&startTimeCudaTotal, NULL);

    //strcpy(in, "image.ppm");
    //strcpy(in, "image.pgm");
    //strcpy(out, "out_image.ppm");
    //strcpy(out, "out_image.pgm");
    strcpy(in, argv[1]);
    strcpy(out, argv[2]);
    strcpy(tempo, "time.txt");

    // le a imagem de entrada
    readImage(&imgIn, &imgOut, in);

    size = imgIn.width * imgIn.height;

    // execucao do programa para imagens em escala de cinza (PGM)
    if(imgIn.type[0] == 'P' && imgIn.type[1] == '2') {
        // Tempo com a alocacao de memoria
        struct timeval startTimeCudaMem, endTimeCudaMem;
        gettimeofday(&startTimeCudaMem, NULL);
    
        cudaCheck(cudaMalloc((void**)&inPixel, size * sizeof(uchar)));
        cudaCheck(cudaMalloc((void**)&outPixel, size * sizeof(uchar)));
    
        cudaCheck(cudaMemcpy(inPixel, imgIn.pixel, size * sizeof(uchar), cudaMemcpyHostToDevice));
    
        // define grid e bloco
        dim3 gridDim(imgIn.width / TILEWIDTH + 1, imgIn.height / TILEWIDTH + 1);
        dim3 blockDim(TILEWIDTH, TILEWIDTH);
    
        // tempo do smoothing
        struct timeval startTimeCuda, endTimeCuda;
        gettimeofday(&startTimeCuda, NULL);
            
        // aplica o filter como funcao global usando grid e bloco
        filter<<<gridDim, blockDim>>>(inPixel, outPixel, imgIn.height, imgIn.width);
    
        gettimeofday(&endTimeCuda, NULL);
    
        cudaCheck(cudaMemcpy(imgOut.pixel, outPixel, size * sizeof(uchar), cudaMemcpyDeviceToHost));
    
        gettimeofday(&endTimeCudaMem, NULL);

        // Salva imagem filtrada
        saveImage(&imgOut, out);

        // Libera memoria
        cudaFree(inPixel);
        cudaFree(outPixel);

        freeData(&imgIn);
        freeData(&imgOut);

        gettimeofday(&endTimeCudaTotal, NULL);

        // Calcula o tempo
        result = endTimeCuda.tv_sec - startTimeCuda.tv_sec + (endTimeCuda.tv_usec - startTimeCuda.tv_usec) / 1000000.0;
    }

    // Execucao do programa para imagens coloridas (PPM)
    else if(imgIn.type[0] == 'P' && imgIn.type[1] == '3') {
        // Tempo com a alocacao de memoria
        struct timeval startTimeCudaMem, endTimeCudaMem;
        gettimeofday(&startTimeCudaMem, NULL);
    
        cudaCheck(cudaMalloc((void**)&inR, size * sizeof(uchar)));
        cudaCheck(cudaMalloc((void**)&inG, size * sizeof(uchar)));
        cudaCheck(cudaMalloc((void**)&inB, size * sizeof(uchar)));
        cudaCheck(cudaMalloc((void**)&outR, size * sizeof(uchar)));
        cudaCheck(cudaMalloc((void**)&outG, size * sizeof(uchar)));
        cudaCheck(cudaMalloc((void**)&outB, size * sizeof(uchar)));
    
        cudaCheck(cudaMemcpy(inR, imgIn.r, size * sizeof(uchar), cudaMemcpyHostToDevice));
        cudaCheck(cudaMemcpy(inG, imgIn.g, size * sizeof(uchar), cudaMemcpyHostToDevice));
        cudaCheck(cudaMemcpy(inB, imgIn.b, size * sizeof(uchar), cudaMemcpyHostToDevice));
    
        // define grid e bloco
        dim3 gridDim(imgIn.width / TILEWIDTH + 1, imgIn.height / TILEWIDTH + 1);
        dim3 blockDim(TILEWIDTH, TILEWIDTH);
    
        // tempo do smoothing
        struct timeval startTimeCuda, endTimeCuda;
        gettimeofday(&startTimeCuda, NULL);
            
        // aplica o filter como funcao global usando grid e bloco
        filter<<<gridDim, blockDim>>>(inR, outR, imgIn.height, imgIn.width);
        filter<<<gridDim, blockDim>>>(inG, outG, imgIn.height, imgIn.width);
        filter<<<gridDim, blockDim>>>(inB, outB, imgIn.height, imgIn.width);
    
        gettimeofday(&endTimeCuda, NULL);
    
        cudaCheck(cudaMemcpy(imgOut.r, outR, size * sizeof(uchar), cudaMemcpyDeviceToHost));
        cudaCheck(cudaMemcpy(imgOut.g, outG, size * sizeof(uchar), cudaMemcpyDeviceToHost));
        cudaCheck(cudaMemcpy(imgOut.b, outB, size * sizeof(uchar), cudaMemcpyDeviceToHost));
    
        gettimeofday(&endTimeCudaMem, NULL);

        // Salva imagem filtrada
        saveImage(&imgOut, out);

        // Libera memoria
        cudaFree(inR);
        cudaFree(inB);
        cudaFree(inG);
        cudaFree(outR);
        cudaFree(outG);
        cudaFree(outB);

        freeData(&imgIn);
        freeData(&imgOut);

        gettimeofday(&endTimeCudaTotal, NULL);

        // Calcula o tempo
        result = endTimeCuda.tv_sec - startTimeCuda.tv_sec + (endTimeCuda.tv_usec - startTimeCuda.tv_usec) / 1000000.0;
    }
    
    //double resultMem = endTimeCudaMem.tv_sec - startTimeCudaMem.tv_sec + (endTimeCudaMem.tv_usec - startTimeCudaMem.tv_usec) / 1000000.0;
    double resultTotal = endTimeCudaTotal.tv_sec - startTimeCudaTotal.tv_sec + (endTimeCudaTotal.tv_usec - startTimeCudaTotal.tv_usec) / 1000000.0;

    printf("GPU: %lfs\n", result);
    printf("GPU + memoria + in/out - TOTAL : %lfs\n", resultTotal);

    return 0;
}
	.file	"tmpxft_002ac9e8_00000000-6_Trab3CUD.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2035:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2035:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"ERROR: %s\n"
	.text
	.globl	_Z9cudaCheck9cudaError
	.type	_Z9cudaCheck9cudaError, @function
_Z9cudaCheck9cudaError:
.LFB2027:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L2
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC0(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L2:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9cudaCheck9cudaError, .-_Z9cudaCheck9cudaError
	.globl	_Z9allocDataP12IMGstructurePciii
	.type	_Z9allocDataP12IMGstructurePciii, @function
_Z9allocDataP12IMGstructurePciii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	%r8d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movl	%ecx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%edx, %ebp
	movl	$3, %edx
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rdi, %rbx
	pushq	%r9
	.cfi_def_cfa_offset 48
	call	__strcpy_chk@PLT
	movl	(%rbx), %eax
	movl	%ebp, 4(%rbx)
	movl	%r12d, 8(%rbx)
	movl	%r13d, 12(%rbx)
	cmpw	$12880, %ax
	jne	.L9
	imull	%r12d, %ebp
	movslq	%ebp, %rdi
	call	malloc@PLT
	xorl	%ecx, %ecx
	movq	%rax, 16(%rbx)
	movq	%rcx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%rcx, 40(%rbx)
	jmp	.L8
.L9:
	cmpw	$13136, %ax
	jne	.L8
	imull	%r12d, %ebp
	xorl	%edx, %edx
	movq	%rdx, 16(%rbx)
	movslq	%ebp, %rbp
	movq	%rbp, %rdi
	call	malloc@PLT
	movq	%rbp, %rdi
	movq	%rax, 24(%rbx)
	call	malloc@PLT
	movq	%rbp, %rdi
	movq	%rax, 32(%rbx)
	call	malloc@PLT
	movq	%rax, 40(%rbx)
.L8:
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z9allocDataP12IMGstructurePciii, .-_Z9allocDataP12IMGstructurePciii
	.globl	_Z8freeDataP12IMGstructure
	.type	_Z8freeDataP12IMGstructure, @function
_Z8freeDataP12IMGstructure:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	(%rdi), %eax
	movq	%rdi, %rbx
	cmpw	$12880, %ax
	jne	.L13
	movq	16(%rdi), %rdi
	jmp	.L16
.L13:
	cmpw	$13136, %ax
	jne	.L12
	movq	24(%rdi), %rdi
	call	free@PLT
	movq	32(%rbx), %rdi
	call	free@PLT
	movq	40(%rbx), %rdi
.L16:
	popq	%rbx
	.cfi_remember_state
	.cfi_def_cfa_offset 8
	jmp	free@PLT
.L12:
	.cfi_restore_state
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z8freeDataP12IMGstructure, .-_Z8freeDataP12IMGstructure
	.section	.rodata.str1.1
.LC1:
	.string	"r"
.LC2:
	.string	"Can't read image file.\n"
.LC3:
	.string	"%s"
.LC4:
	.string	"%s\n"
.LC5:
	.string	"%d"
.LC6:
	.string	"%hhu\n"
.LC7:
	.string	"%hhu %hhu %hhu\n"
	.text
	.globl	_Z9readImageP12IMGstructureS0_Pc
	.type	_Z9readImageP12IMGstructureS0_Pc, @function
_Z9readImageP12IMGstructureS0_Pc:
.LFB2030:
	.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
	leaq	.LC1(%rip), %rsi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rdi, %rbp
	movq	%rdx, %rdi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	jne	.L18
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L19
.L18:
	movq	%rax, %rdi
	leaq	37(%rsp), %r12
	call	rewind@PLT
	movq	%r12, %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	leaq	.LC3(%rip), %rsi
	call	__isoc23_fscanf@PLT
	xorl	%eax, %eax
	movq	%r12, %rdx
	movl	$2, %edi
	leaq	.LC4(%rip), %rsi
	call	__printf_chk@PLT
	cmpb	$80, 37(%rsp)
	jne	.L19
	movb	38(%rsp), %al
	cmpb	$50, %al
	jne	.L21
.L22:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$10, %eax
	jne	.L22
.L23:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$35, %eax
	jne	.L43
.L24:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$10, %eax
	jne	.L24
	jmp	.L23
.L43:
	leaq	.LC5(%rip), %r14
	movl	$1, %edx
	orq	$-1, %rsi
	movq	%rbx, %rdi
	call	fseek@PLT
	movq	%r14, %rsi
	leaq	28(%rsp), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movq	%r14, %rsi
	leaq	32(%rsp), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movq	%r14, %rsi
	leaq	24(%rsp), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movl	32(%rsp), %r15d
	movq	%r12, %rsi
	movq	%rbp, %rdi
	movl	28(%rsp), %r14d
	movl	24(%rsp), %r8d
	movl	%r15d, %ecx
	movl	%r14d, %edx
	movl	%r8d, 12(%rsp)
	call	_Z9allocDataP12IMGstructurePciii
	movl	12(%rsp), %r8d
	movq	%r12, %rsi
	movq	%r13, %rdi
	movl	%r15d, %ecx
	movl	%r14d, %edx
	leaq	.LC6(%rip), %r13
	xorl	%r12d, %r12d
	call	_Z9allocDataP12IMGstructurePciii
.L25:
	movl	28(%rsp), %eax
	imull	32(%rsp), %eax
	cmpl	%r12d, %eax
	jle	.L19
	movq	16(%rbp), %rdx
	movq	%r13, %rsi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	addq	%r12, %rdx
	incq	%r12
	call	__isoc23_fscanf@PLT
	jmp	.L25
.L21:
	cmpb	$51, %al
	jne	.L19
.L28:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$10, %eax
	jne	.L28
.L29:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$35, %eax
	jne	.L44
.L30:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$10, %eax
	jne	.L30
	jmp	.L29
.L44:
	leaq	.LC5(%rip), %r14
	movl	$1, %edx
	orq	$-1, %rsi
	movq	%rbx, %rdi
	call	fseek@PLT
	movq	%r14, %rsi
	leaq	28(%rsp), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movq	%r14, %rsi
	leaq	32(%rsp), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movq	%r14, %rsi
	leaq	24(%rsp), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movl	32(%rsp), %r15d
	movq	%r12, %rsi
	movq	%rbp, %rdi
	movl	28(%rsp), %r14d
	movl	24(%rsp), %r8d
	movl	%r15d, %ecx
	movl	%r14d, %edx
	movl	%r8d, 12(%rsp)
	call	_Z9allocDataP12IMGstructurePciii
	movl	12(%rsp), %r8d
	movq	%r12, %rsi
	movq	%r13, %rdi
	movl	%r15d, %ecx
	movl	%r14d, %edx
	leaq	.LC7(%rip), %r13
	xorl	%r12d, %r12d
	call	_Z9allocDataP12IMGstructurePciii
.L31:
	movl	28(%rsp), %eax
	imull	32(%rsp), %eax
	cmpl	%r12d, %eax
	jle	.L19
	movq	32(%rbp), %rcx
	movq	24(%rbp), %rdx
	movq	%r13, %rsi
	movq	%rbx, %rdi
	movq	40(%rbp), %r8
	xorl	%eax, %eax
	addq	%r12, %rcx
	addq	%r12, %rdx
	addq	%r12, %r8
	incq	%r12
	call	__isoc23_fscanf@PLT
	jmp	.L31
.L19:
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L33
	call	__stack_chk_fail@PLT
.L33:
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	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
.LFE2030:
	.size	_Z9readImageP12IMGstructureS0_Pc, .-_Z9readImageP12IMGstructureS0_Pc
	.section	.rodata.str1.1
.LC8:
	.string	"w"
.LC9:
	.string	"#imagem com smooth\n"
.LC10:
	.string	"%d %d\n"
.LC11:
	.string	"%d\n"
	.text
	.globl	_Z9saveImageP12IMGstructurePc
	.type	_Z9saveImageP12IMGstructurePc, @function
_Z9saveImageP12IMGstructurePc:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rdi, %rbx
	movq	%rsi, %rdi
	leaq	.LC8(%rip), %rsi
	pushq	%rdx
	.cfi_def_cfa_offset 48
	call	fopen@PLT
	movq	%rbx, %rcx
	movl	$2, %esi
	leaq	.LC4(%rip), %rdx
	movq	%rax, %rbp
	movq	%rax, %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$2, %esi
	movq	%rbp, %rdi
	xorl	%eax, %eax
	leaq	.LC9(%rip), %rdx
	call	__fprintf_chk@PLT
	movl	4(%rbx), %ecx
	movl	8(%rbx), %r8d
	movq	%rbp, %rdi
	leaq	.LC10(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	12(%rbx), %ecx
	movl	$2, %esi
	movq	%rbp, %rdi
	leaq	.LC11(%rip), %rdx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	(%rbx), %eax
	cmpw	$12880, %ax
	jne	.L56
	xorl	%r12d, %r12d
	leaq	.LC6(%rip), %r13
.L46:
	movl	4(%rbx), %eax
	movl	8(%rbx), %edx
	movl	%r12d, %ecx
	imull	%eax, %edx
	cmpl	%r12d, %edx
	jle	.L51
	addl	%eax, %eax
	cmpl	%r12d, %eax
	jg	.L48
	subl	%eax, %edx
	cmpl	%ecx, %edx
	jle	.L48
	movq	16(%rbx), %rax
	movq	%r13, %rdx
	movl	$2, %esi
	movq	%rbp, %rdi
	movzbl	(%rax,%r12), %ecx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L48:
	incq	%r12
	jmp	.L46
.L53:
	addl	%eax, %eax
	cmpl	%r12d, %eax
	jle	.L58
.L52:
	incq	%r12
.L50:
	movl	4(%rbx), %eax
	movl	8(%rbx), %edx
	movl	%r12d, %ecx
	imull	%eax, %edx
	cmpl	%r12d, %edx
	jg	.L53
.L51:
	popq	%rax
	.cfi_remember_state
	.cfi_def_cfa_offset 40
	movq	%rbp, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	jmp	fclose@PLT
.L56:
	.cfi_restore_state
	cmpw	$13136, %ax
	jne	.L51
	xorl	%r12d, %r12d
	leaq	.LC7(%rip), %r13
	jmp	.L50
.L58:
	subl	%eax, %edx
	cmpl	%ecx, %edx
	jle	.L52
	movq	40(%rbx), %rdx
	movq	32(%rbx), %rax
	movl	$2, %esi
	movq	%rbp, %rdi
	movq	24(%rbx), %rcx
	movzbl	(%rdx,%r12), %r9d
	movzbl	(%rax,%r12), %r8d
	movq	%r13, %rdx
	xorl	%eax, %eax
	movzbl	(%rcx,%r12), %ecx
	call	__fprintf_chk@PLT
	jmp	.L52
	.cfi_endproc
.LFE2031:
	.size	_Z9saveImageP12IMGstructurePc, .-_Z9saveImageP12IMGstructurePc
	.globl	_Z29__device_stub__Z6filterPhS_iiPhS_ii
	.type	_Z29__device_stub__Z6filterPhS_iiPhS_ii, @function
_Z29__device_stub__Z6filterPhS_iiPhS_ii:
.LFB2057:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L59
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z6filterPhS_ii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L59:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L61
	call	__stack_chk_fail@PLT
.L61:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2057:
	.size	_Z29__device_stub__Z6filterPhS_iiPhS_ii, .-_Z29__device_stub__Z6filterPhS_iiPhS_ii
	.globl	_Z6filterPhS_ii
	.type	_Z6filterPhS_ii, @function
_Z6filterPhS_ii:
.LFB2058:
	.cfi_startproc
	endbr64
	jmp	_Z29__device_stub__Z6filterPhS_iiPhS_ii
	.cfi_endproc
.LFE2058:
	.size	_Z6filterPhS_ii, .-_Z6filterPhS_ii
	.section	.rodata.str1.1
.LC13:
	.string	"GPU: %lfs\n"
.LC14:
	.string	"GPU + memoria + in/out - TOTAL : %lfs\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2032:
	.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
	movq	%rsi, %rbx
	xorl	%esi, %esi
	subq	$376, %rsp
	.cfi_def_cfa_offset 432
	movsd	%xmm0, 8(%rsp)
	leaq	104(%rsp), %rdi
	leaq	300(%rsp), %rbp
	movq	%fs:40, %rax
	movq	%rax, 360(%rsp)
	xorl	%eax, %eax
	leaq	320(%rsp), %r15
	leaq	248(%rsp), %r13
	call	gettimeofday@PLT
	movq	8(%rbx), %rsi
	movq	%rbp, %rdi
	movl	$20, %edx
	leaq	200(%rsp), %r14
	call	__strcpy_chk@PLT
	movq	16(%rbx), %rsi
	movl	$20, %edx
	movq	%r15, %rdi
	call	__strcpy_chk@PLT
	movq	%rbp, %rdx
	movq	%r13, %rsi
	movq	%r14, %rdi
	movabsq	$8392585648223906164, %rax
	movb	$0, 348(%rsp)
	movq	%rax, 340(%rsp)
	call	_Z9readImageP12IMGstructureS0_Pc
	movl	204(%rsp), %r12d
	movl	208(%rsp), %ebp
	movl	200(%rsp), %eax
	movsd	8(%rsp), %xmm0
	movl	%r12d, %ebx
	imull	%ebp, %ebx
	cmpw	$12880, %ax
	jne	.L65
	xorl	%esi, %esi
	movslq	%ebx, %rbx
	leaq	136(%rsp), %rdi
	call	gettimeofday@PLT
	movq	%rbx, %rsi
	leaq	40(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	%rbx, %rsi
	leaq	72(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	216(%rsp), %rsi
	movq	40(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movl	%r12d, %eax
	movl	$16, %ecx
	xorl	%esi, %esi
	cltd
	leaq	168(%rsp), %rdi
	movl	$1, 88(%rsp)
	idivl	%ecx
	movl	$1, 100(%rsp)
	incl	%eax
	movl	%eax, 80(%rsp)
	movl	%ebp, %eax
	cltd
	idivl	%ecx
	incl	%eax
	movl	%eax, 84(%rsp)
	movabsq	$68719476752, %rax
	movq	%rax, 92(%rsp)
	call	gettimeofday@PLT
	movl	100(%rsp), %ecx
	movl	88(%rsp), %esi
	xorl	%r9d, %r9d
	movq	92(%rsp), %rdx
	movq	80(%rsp), %rdi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L66
	movq	72(%rsp), %rsi
	movq	40(%rsp), %rdi
	movl	%r12d, %ecx
	movl	%ebp, %edx
	call	_Z29__device_stub__Z6filterPhS_iiPhS_ii
.L66:
	leaq	184(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	72(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdx
	movq	264(%rsp), %rdi
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	leaq	152(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	%r15, %rsi
	movq	%r13, %rdi
	call	_Z9saveImageP12IMGstructurePc
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rdi
	jmp	.L73
.L65:
	cmpw	$13136, %ax
	jne	.L67
	movslq	%ebx, %rbx
	xorl	%esi, %esi
	leaq	136(%rsp), %rdi
	call	gettimeofday@PLT
	movq	%rbx, %rsi
	leaq	16(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	%rbx, %rsi
	leaq	24(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	%rbx, %rsi
	leaq	32(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	%rbx, %rsi
	leaq	48(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	%rbx, %rsi
	leaq	56(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	%rbx, %rsi
	leaq	64(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	224(%rsp), %rsi
	movq	16(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	232(%rsp), %rsi
	movq	24(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	240(%rsp), %rsi
	movq	32(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movl	%r12d, %eax
	movl	$16, %ecx
	xorl	%esi, %esi
	cltd
	leaq	168(%rsp), %rdi
	movl	$1, 88(%rsp)
	idivl	%ecx
	movl	$1, 100(%rsp)
	incl	%eax
	movl	%eax, 80(%rsp)
	movl	%ebp, %eax
	cltd
	idivl	%ecx
	incl	%eax
	movl	%eax, 84(%rsp)
	movabsq	$68719476752, %rax
	movq	%rax, 92(%rsp)
	call	gettimeofday@PLT
	movl	100(%rsp), %ecx
	movl	88(%rsp), %esi
	xorl	%r9d, %r9d
	movq	92(%rsp), %rdx
	movq	80(%rsp), %rdi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L68
	movq	48(%rsp), %rsi
	movq	16(%rsp), %rdi
	movl	%r12d, %ecx
	movl	%ebp, %edx
	call	_Z29__device_stub__Z6filterPhS_iiPhS_ii
.L68:
	movl	100(%rsp), %ecx
	movq	92(%rsp), %rdx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	80(%rsp), %rdi
	movl	88(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L69
	movq	56(%rsp), %rsi
	movq	24(%rsp), %rdi
	movl	%r12d, %ecx
	movl	%ebp, %edx
	call	_Z29__device_stub__Z6filterPhS_iiPhS_ii
.L69:
	movl	100(%rsp), %ecx
	movq	92(%rsp), %rdx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	80(%rsp), %rdi
	movl	88(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L70
	movq	64(%rsp), %rsi
	movq	32(%rsp), %rdi
	movl	%r12d, %ecx
	movl	%ebp, %edx
	call	_Z29__device_stub__Z6filterPhS_iiPhS_ii
.L70:
	leaq	184(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	48(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdx
	movq	272(%rsp), %rdi
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	56(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdx
	movq	280(%rsp), %rdi
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	movq	64(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdx
	movq	288(%rsp), %rdi
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	leaq	152(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	%r15, %rsi
	movq	%r13, %rdi
	call	_Z9saveImageP12IMGstructurePc
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	64(%rsp), %rdi
.L73:
	call	cudaFree@PLT
	movq	%r14, %rdi
	call	_Z8freeDataP12IMGstructure
	movq	%r13, %rdi
	call	_Z8freeDataP12IMGstructure
	leaq	120(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	192(%rsp), %rax
	subq	176(%rsp), %rax
	cvtsi2sdq	%rax, %xmm0
	movq	184(%rsp), %rax
	subq	168(%rsp), %rax
	divsd	.LC12(%rip), %xmm0
	cvtsi2sdq	%rax, %xmm1
	addsd	%xmm1, %xmm0
.L67:
	movq	128(%rsp), %rax
	subq	112(%rsp), %rax
	movl	$2, %edi
	leaq	.LC13(%rip), %rsi
	cvtsi2sdq	%rax, %xmm1
	movq	120(%rsp), %rax
	subq	104(%rsp), %rax
	divsd	.LC12(%rip), %xmm1
	cvtsi2sdq	%rax, %xmm2
	movb	$1, %al
	addsd	%xmm2, %xmm1
	movq	%xmm1, %rbx
	call	__printf_chk@PLT
	movq	%rbx, %xmm0
	leaq	.LC14(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	360(%rsp), %rax
	subq	%fs:40, %rax
	je	.L71
	call	__stack_chk_fail@PLT
.L71:
	addq	$376, %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
.LFE2032:
	.size	main, .-main
	.section	.rodata.str1.1
.LC15:
	.string	"_Z6filterPhS_ii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2060:
	.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	.LC15(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z6filterPhS_ii(%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
.LFE2060:
	.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.cst8,"aM",@progbits,8
	.align 8
.LC12:
	.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:

/**
*   Programação Concorrente - SSC0143 - 2 Semestre de 2015
*   Prof. Dr. Júlio Cezar Estrella
*   Trabalho 3 - Smoothing de imagem utilizando CUDA
*  
*   Alunos:
*       Thiago Ledur Lima       - 8084214
*       Rodrigo Neves Bernardi  - 8066395   
**/



#include <hip/hip_runtime.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include "Smoothing_CUDA.h"
#include <sys/time.h>

#define FILTERDIM 5
#define FILTERSIZE 25
#define TILEWIDTH 16

typedef unsigned char uchar;

typedef struct IMGstructure {
    char type[3];
    int width;
    int height;
    int maxVal;
    uchar *pixel;
    uchar *r;
    uchar *g;
    uchar *b;
} Image;

// verifica erros
void cudaCheck(hipError_t error) {
    if(error != hipSuccess) {
        fprintf(stderr,"ERROR: %s\n", hipGetErrorString(error));
        exit(1);
    }
}

// aloca memória necessária para uma Image, recebendo suas informações
void allocData(Image *img, char* type, int width, int height, int maxVal) {
    strcpy(img->type, type);
    img->width = width;
    img->height = height;
    img->maxVal = maxVal;

    if(img->type[0] == 'P' && img->type[1] == '2') {
        img->pixel = (uchar *)malloc(width * height * sizeof(uchar));
        img->r = NULL;
        img->g = NULL;
        img->b = NULL;
    }

    else if(img->type[0] == 'P' && img->type[1] == '3') {
        img->pixel = NULL;
        img->r = (uchar *)malloc(width * height * sizeof(uchar));
        img->g = (uchar *)malloc(width * height * sizeof(uchar));
        img->b = (uchar *)malloc(width * height * sizeof(uchar));
    }
}

// libera memória usada por uma Image
void freeData(Image *img) {
    
    if (img->type[0] == 'P' && img->type[1] == '2') {
        free(img->pixel);
    }

    else if(img->type[0] == 'P' && img->type[1] == '3') {
        free(img->r);
        free(img->g);
        free(img->b);    
    }
}

// cria uma estrutura Image, incluindo a leitura do arquivo e a chamada da função para alocar a imagem na memória
void readImage(Image *imgIn, Image *imgOut, char *in) {
    FILE *input;
    int i;
    char type[3];
    int maxVal, width, height;

    input = fopen(in, "r");

    if (input == NULL)
        printf("Can't read image file.\n");
    
    else {
        // le tipo
        rewind(input);
        fscanf(input, "%s", type);

        printf("%s\n", type);

        // compara tipo para ver se é pgm (escala de cinza)
        if(type[0] == 'P' && type[1] == '2') {
            // pula fim da linha
            while (getc(input) != '\n');

            // pula comentario da linha
            while (getc(input) == '#') {
                while (getc(input) != '\n');
            }

            // volta um caracter
            fseek(input, -1, SEEK_CUR);

            // le dimensões da imagem e a escala das cores
            fscanf(input, "%d", &width);
            fscanf(input, "%d", &height);
            fscanf(input, "%d", &maxVal);

            // aloca as matrizes width x height das imagens de entrada e saída na memória
            allocData(imgIn, type, width, height, maxVal);
            allocData(imgOut, type, width, height, maxVal);
            
            // le dados do arquivo
            for(i = 0; i < width * height ; i++) {
                // fscanf(input, "%hhu %hhu %hhu", &(imgIn->data->r[i]), &(imgIn->data->g[i]), &(imgIn->data->b[i]));
                fscanf(input, "%hhu\n", &(imgIn->pixel[i]));        
            }
        }

        // se nao, imagem é ppm (colorida)
        else if(type[0] == 'P' && type[1] == '3') {
            // pula fim da linha
            while (getc(input) != '\n');

            // pula comentario da linha
            while (getc(input) == '#') {
                while (getc(input) != '\n');
            }

            // volta um caracter
            fseek(input, -1, SEEK_CUR);

            // le dimensões da imagem e a escala das cores
            fscanf(input, "%d", &width);
            fscanf(input, "%d", &height);
            fscanf(input, "%d", &maxVal);

            // aloca as matrizes width x height das imagens de entrada e saída na memória
            allocData(imgIn, type, width, height, maxVal);
            allocData(imgOut, type, width, height, maxVal);
            
            // le dados do arquivo
            for(i = 0; i < width * height ; i++) {
                // fscanf(input, "%hhu %hhu %hhu", &(imgIn->data->r[i]), &(imgIn->data->g[i]), &(imgIn->data->b[i]));
                fscanf(input, "%hhu %hhu %hhu\n", &(imgIn->r[i]), &(imgIn->g[i]), &(imgIn->b[i]));        
            }
        }
    }

    fclose(input);
}

// cria um arquivo com o resultado guardado na matriz de imagem de saída
void saveImage(Image *img, char *out) {
    FILE *output;
    int i;

    // escreve header contendo tipo, comentário, dimensões e escala
    output = fopen(out, "w");
    fprintf(output, "%s\n", img->type);
    fprintf(output, "#imagem com smooth\n");
    fprintf(output, "%d %d\n", img->width, img->height);
    fprintf(output, "%d\n", img->maxVal);

    // escreve dados da imagem

    if(img->type[0] == 'P' && img->type[1] == '2') {
        for (i = 0; i < img->height * img->width; i++) {
            //fprintf(output, "%d %d %d\t", img->data[i][j].r, img->data[i][j].g, img->data[i][j].b);
            // fprintf(output, "%hhu %hhu %hhu ", img->data->r[i], img->data->g[i], img->data->b[i]);
            
            if((i >= 2*img->width) && (i < ((img->height*img->width) - 2*img->width)))
                fprintf(output, "%hhu\n", img->pixel[i]);
        }
    }

    else if(img->type[0] == 'P' && img->type[1] == '3') {
        for (i = 0; i < img->height * img->width; i++) {
            //fprintf(output, "%d %d %d\t", img->data[i][j].r, img->data[i][j].g, img->data[i][j].b);
            // fprintf(output, "%hhu %hhu %hhu ", img->data->r[i], img->data->g[i], img->data->b[i]);
        
            if((i >= 2*img->width) && (i < ((img->height*img->width) - 2*img->width)))
                fprintf(output, "%hhu %hhu %hhu\n", img->r[i], img->g[i], img->b[i]);
        }
    }

    fclose(output);
}

// função de kernel para filtro smooth
__global__ void filter(uchar *in, uchar *out, int H, int W) {
    int i, j, k;

    int x = blockDim.x * blockIdx.x + threadIdx.x;
    int y = blockDim.y * blockIdx.y + threadIdx.y;

    int idx = y * W + x;
    float Pvalue = 0;

    k = FILTERDIM / 2;

    // restringe as bordas da imagem
    if (x >= W-k|| y >= H-k|| x <= k-1 || y <= k-1) return;

    // aplica convolucao
    for(i = 0; i <= FILTERDIM - 1; i++) {
        for(j = 0; j <= FILTERDIM - 1; j++) {
           Pvalue += in[(idx - k + i) + W * (j - k)];
        }
    }
    out[idx] = (uchar)(Pvalue / FILTERSIZE);
    //printf("\nFILTER FINISHED IN : %d %d\n", x, y);
}


int main(int argc, char const *argv[]) {
    Image imgIn, imgOut;

    uchar *inR, *inG, *inB, *inPixel, *outR, *outG, *outB, *outPixel;
    int size;
    char in[20], out[20], tempo[20];
    double result;

    // Tempo total
    struct timeval startTimeCudaTotal, endTimeCudaTotal;
    gettimeofday(&startTimeCudaTotal, NULL);

    //strcpy(in, "image.ppm");
    //strcpy(in, "image.pgm");
    //strcpy(out, "out_image.ppm");
    //strcpy(out, "out_image.pgm");
    strcpy(in, argv[1]);
    strcpy(out, argv[2]);
    strcpy(tempo, "time.txt");

    // le a imagem de entrada
    readImage(&imgIn, &imgOut, in);

    size = imgIn.width * imgIn.height;

    // execucao do programa para imagens em escala de cinza (PGM)
    if(imgIn.type[0] == 'P' && imgIn.type[1] == '2') {
        // Tempo com a alocacao de memoria
        struct timeval startTimeCudaMem, endTimeCudaMem;
        gettimeofday(&startTimeCudaMem, NULL);
    
        cudaCheck(hipMalloc((void**)&inPixel, size * sizeof(uchar)));
        cudaCheck(hipMalloc((void**)&outPixel, size * sizeof(uchar)));
    
        cudaCheck(hipMemcpy(inPixel, imgIn.pixel, size * sizeof(uchar), hipMemcpyHostToDevice));
    
        // define grid e bloco
        dim3 gridDim(imgIn.width / TILEWIDTH + 1, imgIn.height / TILEWIDTH + 1);
        dim3 blockDim(TILEWIDTH, TILEWIDTH);
    
        // tempo do smoothing
        struct timeval startTimeCuda, endTimeCuda;
        gettimeofday(&startTimeCuda, NULL);
            
        // aplica o filter como funcao global usando grid e bloco
        filter<<<gridDim, blockDim>>>(inPixel, outPixel, imgIn.height, imgIn.width);
    
        gettimeofday(&endTimeCuda, NULL);
    
        cudaCheck(hipMemcpy(imgOut.pixel, outPixel, size * sizeof(uchar), hipMemcpyDeviceToHost));
    
        gettimeofday(&endTimeCudaMem, NULL);

        // Salva imagem filtrada
        saveImage(&imgOut, out);

        // Libera memoria
        hipFree(inPixel);
        hipFree(outPixel);

        freeData(&imgIn);
        freeData(&imgOut);

        gettimeofday(&endTimeCudaTotal, NULL);

        // Calcula o tempo
        result = endTimeCuda.tv_sec - startTimeCuda.tv_sec + (endTimeCuda.tv_usec - startTimeCuda.tv_usec) / 1000000.0;
    }

    // Execucao do programa para imagens coloridas (PPM)
    else if(imgIn.type[0] == 'P' && imgIn.type[1] == '3') {
        // Tempo com a alocacao de memoria
        struct timeval startTimeCudaMem, endTimeCudaMem;
        gettimeofday(&startTimeCudaMem, NULL);
    
        cudaCheck(hipMalloc((void**)&inR, size * sizeof(uchar)));
        cudaCheck(hipMalloc((void**)&inG, size * sizeof(uchar)));
        cudaCheck(hipMalloc((void**)&inB, size * sizeof(uchar)));
        cudaCheck(hipMalloc((void**)&outR, size * sizeof(uchar)));
        cudaCheck(hipMalloc((void**)&outG, size * sizeof(uchar)));
        cudaCheck(hipMalloc((void**)&outB, size * sizeof(uchar)));
    
        cudaCheck(hipMemcpy(inR, imgIn.r, size * sizeof(uchar), hipMemcpyHostToDevice));
        cudaCheck(hipMemcpy(inG, imgIn.g, size * sizeof(uchar), hipMemcpyHostToDevice));
        cudaCheck(hipMemcpy(inB, imgIn.b, size * sizeof(uchar), hipMemcpyHostToDevice));
    
        // define grid e bloco
        dim3 gridDim(imgIn.width / TILEWIDTH + 1, imgIn.height / TILEWIDTH + 1);
        dim3 blockDim(TILEWIDTH, TILEWIDTH);
    
        // tempo do smoothing
        struct timeval startTimeCuda, endTimeCuda;
        gettimeofday(&startTimeCuda, NULL);
            
        // aplica o filter como funcao global usando grid e bloco
        filter<<<gridDim, blockDim>>>(inR, outR, imgIn.height, imgIn.width);
        filter<<<gridDim, blockDim>>>(inG, outG, imgIn.height, imgIn.width);
        filter<<<gridDim, blockDim>>>(inB, outB, imgIn.height, imgIn.width);
    
        gettimeofday(&endTimeCuda, NULL);
    
        cudaCheck(hipMemcpy(imgOut.r, outR, size * sizeof(uchar), hipMemcpyDeviceToHost));
        cudaCheck(hipMemcpy(imgOut.g, outG, size * sizeof(uchar), hipMemcpyDeviceToHost));
        cudaCheck(hipMemcpy(imgOut.b, outB, size * sizeof(uchar), hipMemcpyDeviceToHost));
    
        gettimeofday(&endTimeCudaMem, NULL);

        // Salva imagem filtrada
        saveImage(&imgOut, out);

        // Libera memoria
        hipFree(inR);
        hipFree(inB);
        hipFree(inG);
        hipFree(outR);
        hipFree(outG);
        hipFree(outB);

        freeData(&imgIn);
        freeData(&imgOut);

        gettimeofday(&endTimeCudaTotal, NULL);

        // Calcula o tempo
        result = endTimeCuda.tv_sec - startTimeCuda.tv_sec + (endTimeCuda.tv_usec - startTimeCuda.tv_usec) / 1000000.0;
    }
    
    //double resultMem = endTimeCudaMem.tv_sec - startTimeCudaMem.tv_sec + (endTimeCudaMem.tv_usec - startTimeCudaMem.tv_usec) / 1000000.0;
    double resultTotal = endTimeCudaTotal.tv_sec - startTimeCudaTotal.tv_sec + (endTimeCudaTotal.tv_usec - startTimeCudaTotal.tv_usec) / 1000000.0;

    printf("GPU: %lfs\n", result);
    printf("GPU + memoria + in/out - TOTAL : %lfs\n", resultTotal);

    return 0;
}
	.text
	.file	"Trab3CUD.hip"
	.globl	_Z9cudaCheck10hipError_t        # -- Begin function _Z9cudaCheck10hipError_t
	.type	_Z9cudaCheck10hipError_t,@function
_Z9cudaCheck10hipError_t:               # @_Z9cudaCheck10hipError_t
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jne	.LBB0_2
# %bb.1:
	retq
.LBB0_2:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	stderr(%rip), %rbx
	callq	hipGetErrorString
	movl	$.L.str, %esi
	movq	%rbx, %rdi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z9cudaCheck10hipError_t, .Lfunc_end0-_Z9cudaCheck10hipError_t
	.cfi_endproc
                                        # -- End function
	.globl	_Z9allocDataP12IMGstructurePciii # -- Begin function _Z9allocDataP12IMGstructurePciii
	.type	_Z9allocDataP12IMGstructurePciii,@function
_Z9allocDataP12IMGstructurePciii:       # @_Z9allocDataP12IMGstructurePciii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%r8d, %r15d
	movl	%ecx, %ebp
	movl	%edx, %r14d
	movq	%rdi, %rbx
	callq	strcpy
	movl	%r14d, 4(%rbx)
	movl	%ebp, 8(%rbx)
	movl	%r15d, 12(%rbx)
	cmpb	$80, (%rbx)
	jne	.LBB1_5
# %bb.1:
	movzbl	1(%rbx), %eax
	cmpl	$51, %eax
	je	.LBB1_4
# %bb.2:
	cmpl	$50, %eax
	jne	.LBB1_5
# %bb.3:
	imull	%r14d, %ebp
	movslq	%ebp, %rdi
	callq	malloc
	movq	%rax, 16(%rbx)
	xorps	%xmm0, %xmm0
	movups	%xmm0, 24(%rbx)
	movq	$0, 40(%rbx)
	jmp	.LBB1_5
.LBB1_4:
	movq	$0, 16(%rbx)
	imull	%r14d, %ebp
	movslq	%ebp, %r14
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, 24(%rbx)
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, 32(%rbx)
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, 40(%rbx)
.LBB1_5:
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z9allocDataP12IMGstructurePciii, .Lfunc_end1-_Z9allocDataP12IMGstructurePciii
	.cfi_endproc
                                        # -- End function
	.globl	_Z8freeDataP12IMGstructure      # -- Begin function _Z8freeDataP12IMGstructure
	.type	_Z8freeDataP12IMGstructure,@function
_Z8freeDataP12IMGstructure:             # @_Z8freeDataP12IMGstructure
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	cmpb	$80, (%rdi)
	jne	.LBB2_6
# %bb.1:
	movq	%rdi, %rbx
	movzbl	1(%rdi), %eax
	cmpl	$51, %eax
	je	.LBB2_4
# %bb.2:
	cmpl	$50, %eax
	jne	.LBB2_6
# %bb.3:
	addq	$16, %rbx
	jmp	.LBB2_5
.LBB2_6:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB2_4:
	.cfi_def_cfa_offset 16
	movq	24(%rbx), %rdi
	callq	free
	movq	32(%rbx), %rdi
	callq	free
	addq	$40, %rbx
.LBB2_5:                                # %.sink.split
	movq	(%rbx), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	free                            # TAILCALL
.Lfunc_end2:
	.size	_Z8freeDataP12IMGstructure, .Lfunc_end2-_Z8freeDataP12IMGstructure
	.cfi_endproc
                                        # -- End function
	.globl	_Z9readImageP12IMGstructureS0_Pc # -- Begin function _Z9readImageP12IMGstructureS0_Pc
	.type	_Z9readImageP12IMGstructureS0_Pc,@function
_Z9readImageP12IMGstructureS0_Pc:       # @_Z9readImageP12IMGstructureS0_Pc
	.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, %r15
	movq	%rdi, %r14
	movl	$.L.str.1, %esi
	movq	%rdx, %rdi
	callq	fopen
	movq	%rax, %rbx
	testq	%rax, %rax
	je	.LBB3_1
# %bb.2:
	movq	%rbx, %rdi
	callq	rewind
	leaq	21(%rsp), %r12
	movl	$.L.str.3, %esi
	movq	%rbx, %rdi
	movq	%r12, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movq	%r12, %rdi
	callq	puts@PLT
	movb	(%r12), %cl
	movb	1(%r12), %al
	cmpb	$80, %cl
	jne	.LBB3_11
# %bb.3:
	cmpb	$50, %al
	jne	.LBB3_11
.LBB3_4:                                # %.preheader49
                                        # =>This Inner Loop Header: Depth=1
	movq	%rbx, %rdi
	callq	getc
	cmpl	$10, %eax
	jne	.LBB3_4
# %bb.5:                                # %.preheader48
	movq	%r15, 24(%rsp)                  # 8-byte Spill
.LBB3_6:                                # %.preheader48
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_7 Depth 2
	movq	%rbx, %rdi
	callq	getc
	cmpl	$35, %eax
	jne	.LBB3_8
.LBB3_7:                                # %.preheader
                                        #   Parent Loop BB3_6 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%rbx, %rdi
	callq	getc
	cmpl	$10, %eax
	jne	.LBB3_7
	jmp	.LBB3_6
.LBB3_1:
	movl	$.Lstr, %edi
	callq	puts@PLT
	jmp	.LBB3_20
.LBB3_11:
	cmpb	$80, %cl
	jne	.LBB3_20
# %bb.12:
	cmpb	$51, %al
	jne	.LBB3_20
.LBB3_13:                               # %.preheader53
                                        # =>This Inner Loop Header: Depth=1
	movq	%rbx, %rdi
	callq	getc
	cmpl	$10, %eax
	jne	.LBB3_13
# %bb.14:                               # %.preheader52
	movq	%r15, 24(%rsp)                  # 8-byte Spill
.LBB3_15:                               # %.preheader52
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_16 Depth 2
	movq	%rbx, %rdi
	callq	getc
	cmpl	$35, %eax
	jne	.LBB3_17
.LBB3_16:                               # %.preheader51
                                        #   Parent Loop BB3_15 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%rbx, %rdi
	callq	getc
	cmpl	$10, %eax
	jne	.LBB3_16
	jmp	.LBB3_15
.LBB3_8:                                # %._crit_edge56
	movq	%rbx, %rdi
	movq	$-1, %rsi
	movl	$1, %edx
	callq	fseek
	leaq	16(%rsp), %r12
	movl	$.L.str.5, %esi
	movq	%rbx, %rdi
	movq	%r12, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	leaq	12(%rsp), %r13
	movl	$.L.str.5, %esi
	movq	%rbx, %rdi
	movq	%r13, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	leaq	36(%rsp), %rbp
	movl	$.L.str.5, %esi
	movq	%rbx, %rdi
	movq	%rbp, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	(%r12), %r12d
	movl	(%r13), %r13d
	movl	(%rbp), %ebp
	leaq	21(%rsp), %r15
	movq	%r14, %rdi
	movq	%r15, %rsi
	movl	%r12d, %edx
	movl	%r13d, %ecx
	movl	%ebp, %r8d
	callq	_Z9allocDataP12IMGstructurePciii
	movq	24(%rsp), %rdi                  # 8-byte Reload
	movq	%r15, %rsi
	movl	%r12d, %edx
	movl	%r13d, %ecx
	movl	%ebp, %r8d
	callq	_Z9allocDataP12IMGstructurePciii
	imull	%r12d, %r13d
	testl	%r13d, %r13d
	jle	.LBB3_20
# %bb.9:                                # %.lr.ph59
	xorl	%r15d, %r15d
.LBB3_10:                               # =>This Inner Loop Header: Depth=1
	movq	16(%r14), %rdx
	addq	%r15, %rdx
	movl	$.L.str.6, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r15
	movslq	16(%rsp), %rax
	movslq	12(%rsp), %rcx
	imulq	%rax, %rcx
	cmpq	%rcx, %r15
	jl	.LBB3_10
	jmp	.LBB3_20
.LBB3_17:                               # %._crit_edge
	movq	%rbx, %rdi
	movq	$-1, %rsi
	movl	$1, %edx
	callq	fseek
	leaq	16(%rsp), %r15
	movl	$.L.str.5, %esi
	movq	%rbx, %rdi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	leaq	12(%rsp), %r12
	movl	$.L.str.5, %esi
	movq	%rbx, %rdi
	movq	%r12, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	leaq	36(%rsp), %r13
	movl	$.L.str.5, %esi
	movq	%rbx, %rdi
	movq	%r13, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	(%r15), %ebp
	movl	(%r12), %r15d
	movl	(%r13), %r12d
	leaq	21(%rsp), %r13
	movq	%r14, %rdi
	movq	%r13, %rsi
	movl	%ebp, %edx
	movl	%r15d, %ecx
	movl	%r12d, %r8d
	callq	_Z9allocDataP12IMGstructurePciii
	movq	24(%rsp), %rdi                  # 8-byte Reload
	movq	%r13, %rsi
	movl	%ebp, %edx
	movl	%r15d, %ecx
	movl	%r12d, %r8d
	callq	_Z9allocDataP12IMGstructurePciii
	imull	%ebp, %r15d
	testl	%r15d, %r15d
	jle	.LBB3_20
# %bb.18:                               # %.lr.ph
	xorl	%r15d, %r15d
.LBB3_19:                               # =>This Inner Loop Header: Depth=1
	movq	24(%r14), %rdx
	addq	%r15, %rdx
	movq	32(%r14), %rcx
	addq	%r15, %rcx
	movq	40(%r14), %r8
	addq	%r15, %r8
	movl	$.L.str.7, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r15
	movslq	16(%rsp), %rax
	movslq	12(%rsp), %rcx
	imulq	%rax, %rcx
	cmpq	%rcx, %r15
	jl	.LBB3_19
.LBB3_20:                               # %.loopexit
	movq	%rbx, %rdi
	callq	fclose
	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	_Z9readImageP12IMGstructureS0_Pc, .Lfunc_end3-_Z9readImageP12IMGstructureS0_Pc
	.cfi_endproc
                                        # -- End function
	.globl	_Z9saveImageP12IMGstructurePc   # -- Begin function _Z9saveImageP12IMGstructurePc
	.type	_Z9saveImageP12IMGstructurePc,@function
_Z9saveImageP12IMGstructurePc:          # @_Z9saveImageP12IMGstructurePc
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rax
	movq	%rdi, %rbx
	movl	$.L.str.8, %esi
	movq	%rax, %rdi
	callq	fopen
	movq	%rax, %r14
	movl	$.L.str.4, %esi
	movq	%rax, %rdi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$.L.str.9, %edi
	movl	$19, %esi
	movl	$1, %edx
	movq	%r14, %rcx
	callq	fwrite@PLT
	movl	4(%rbx), %edx
	movl	8(%rbx), %ecx
	movl	$.L.str.10, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movl	12(%rbx), %edx
	movl	$.L.str.11, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	cmpb	$80, (%rbx)
	jne	.LBB4_15
# %bb.1:
	movzbl	1(%rbx), %eax
	cmpl	$51, %eax
	je	.LBB4_9
# %bb.2:
	cmpl	$50, %eax
	jne	.LBB4_15
# %bb.3:                                # %.preheader
	movl	4(%rbx), %eax
	movl	8(%rbx), %ecx
	movl	%eax, %edx
	imull	%ecx, %edx
	testl	%edx, %edx
	jle	.LBB4_15
# %bb.4:                                # %.lr.ph55
	xorl	%r15d, %r15d
.LBB4_5:                                # =>This Inner Loop Header: Depth=1
	leal	(%rax,%rax), %esi
	movslq	%esi, %rdi
	cmpq	%rdi, %r15
	jl	.LBB4_8
# %bb.6:                                #   in Loop: Header=BB4_5 Depth=1
	subl	%esi, %edx
	movslq	%edx, %rdx
	cmpq	%rdx, %r15
	jge	.LBB4_8
# %bb.7:                                #   in Loop: Header=BB4_5 Depth=1
	movq	16(%rbx), %rax
	movzbl	(%rax,%r15), %edx
	movl	$.L.str.6, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movl	4(%rbx), %eax
	movl	8(%rbx), %ecx
.LBB4_8:                                #   in Loop: Header=BB4_5 Depth=1
	incq	%r15
	movl	%eax, %edx
	imull	%ecx, %edx
	movslq	%edx, %rsi
	cmpq	%rsi, %r15
	jl	.LBB4_5
	jmp	.LBB4_15
.LBB4_9:                                # %.preheader51
	movl	4(%rbx), %eax
	movl	8(%rbx), %ecx
	movl	%eax, %edx
	imull	%ecx, %edx
	testl	%edx, %edx
	jle	.LBB4_15
# %bb.10:                               # %.lr.ph
	xorl	%r15d, %r15d
.LBB4_11:                               # =>This Inner Loop Header: Depth=1
	leal	(%rax,%rax), %esi
	movslq	%esi, %rdi
	cmpq	%rdi, %r15
	jl	.LBB4_14
# %bb.12:                               #   in Loop: Header=BB4_11 Depth=1
	subl	%esi, %edx
	movslq	%edx, %rdx
	cmpq	%rdx, %r15
	jge	.LBB4_14
# %bb.13:                               #   in Loop: Header=BB4_11 Depth=1
	movq	24(%rbx), %rax
	movq	32(%rbx), %rcx
	movzbl	(%rax,%r15), %edx
	movzbl	(%rcx,%r15), %ecx
	movq	40(%rbx), %rax
	movzbl	(%rax,%r15), %r8d
	movl	$.L.str.7, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movl	4(%rbx), %eax
	movl	8(%rbx), %ecx
.LBB4_14:                               #   in Loop: Header=BB4_11 Depth=1
	incq	%r15
	movl	%eax, %edx
	imull	%ecx, %edx
	movslq	%edx, %rsi
	cmpq	%rsi, %r15
	jl	.LBB4_11
.LBB4_15:                               # %.loopexit
	movq	%r14, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.Lfunc_end4:
	.size	_Z9saveImageP12IMGstructurePc, .Lfunc_end4-_Z9saveImageP12IMGstructurePc
	.cfi_endproc
                                        # -- End function
	.globl	_Z21__device_stub__filterPhS_ii # -- Begin function _Z21__device_stub__filterPhS_ii
	.type	_Z21__device_stub__filterPhS_ii,@function
_Z21__device_stub__filterPhS_ii:        # @_Z21__device_stub__filterPhS_ii
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%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	$_Z6filterPhS_ii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end5:
	.size	_Z21__device_stub__filterPhS_ii, .Lfunc_end5-_Z21__device_stub__filterPhS_ii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI6_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$320, %rsp                      # imm = 0x140
	.cfi_def_cfa_offset 368
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %r14
	movabsq	$68719476752, %rbx              # imm = 0x1000000010
	leaq	144(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	8(%r14), %rsi
	leaq	288(%rsp), %r15
	movq	%r15, %rdi
	callq	strcpy
	movq	16(%r14), %rsi
	leaq	160(%rsp), %rdi
	callq	strcpy
	leaq	48(%rsp), %r12
	leaq	240(%rsp), %rsi
	movq	%r12, %rdi
	movq	%r15, %rdx
	callq	_Z9readImageP12IMGstructureS0_Pc
	movslq	4(%r12), %rax
	movslq	8(%r12), %r14
	imulq	%rax, %r14
	cmpb	$80, (%r12)
	jne	.LBB6_5
# %bb.1:
	cmpb	$50, 49(%rsp)
	jne	.LBB6_5
# %bb.2:
	leaq	224(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leaq	16(%rsp), %r15
	movq	%r15, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	leaq	8(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	movq	(%r15), %rdi
	movq	64(%rsp), %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	movl	52(%rsp), %eax
	movl	56(%rsp), %ecx
	leal	15(%rax), %edx
	testl	%eax, %eax
	cmovnsl	%eax, %edx
	sarl	$4, %edx
	incl	%edx
	leal	15(%rcx), %r15d
	testl	%ecx, %ecx
	cmovnsl	%ecx, %r15d
	sarl	$4, %r15d
	incl	%r15d
	shlq	$32, %r15
	orq	%rdx, %r15
	leaq	96(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	%r15, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB6_4
# %bb.3:
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	52(%rsp), %ecx
	movl	56(%rsp), %edx
	callq	_Z21__device_stub__filterPhS_ii
.LBB6_4:
	leaq	192(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leaq	240(%rsp), %r15
	movq	16(%r15), %rdi
	movq	8(%rsp), %rsi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	leaq	208(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leaq	160(%rsp), %rsi
	movq	%r15, %rdi
	callq	_Z9saveImageP12IMGstructurePc
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	jmp	.LBB6_12
.LBB6_5:
	leaq	224(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movslq	%r14d, %r14
	leaq	16(%rsp), %r13
	movq	%r13, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	leaq	8(%rsp), %r12
	movq	%r12, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	leaq	112(%rsp), %r15
	movq	%r15, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	leaq	40(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	leaq	32(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	leaq	24(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	movq	(%r13), %rdi
	movq	72(%rsp), %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	movq	(%r12), %rdi
	movq	80(%rsp), %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	movq	(%r15), %rdi
	movq	88(%rsp), %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	movl	52(%rsp), %eax
	movl	56(%rsp), %ecx
	leal	15(%rax), %edx
	testl	%eax, %eax
	cmovnsl	%eax, %edx
	sarl	$4, %edx
	incl	%edx
	leal	15(%rcx), %r15d
	testl	%ecx, %ecx
	cmovnsl	%ecx, %r15d
	sarl	$4, %r15d
	incl	%r15d
	shlq	$32, %r15
	orq	%rdx, %r15
	leaq	96(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	%r15, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB6_7
# %bb.6:
	movq	16(%rsp), %rdi
	movq	40(%rsp), %rsi
	movl	52(%rsp), %ecx
	movl	56(%rsp), %edx
	callq	_Z21__device_stub__filterPhS_ii
.LBB6_7:
	movq	%r15, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB6_9
# %bb.8:
	movq	8(%rsp), %rdi
	movq	32(%rsp), %rsi
	movl	52(%rsp), %ecx
	movl	56(%rsp), %edx
	callq	_Z21__device_stub__filterPhS_ii
.LBB6_9:
	movq	%r15, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB6_11
# %bb.10:
	movq	112(%rsp), %rdi
	movq	24(%rsp), %rsi
	movl	52(%rsp), %ecx
	movl	56(%rsp), %edx
	callq	_Z21__device_stub__filterPhS_ii
.LBB6_11:
	leaq	192(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leaq	240(%rsp), %r15
	movq	24(%r15), %rdi
	movq	40(%rsp), %rsi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	movq	32(%r15), %rdi
	movq	32(%rsp), %rsi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	movq	40(%r15), %rdi
	movq	24(%rsp), %rsi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	leaq	208(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leaq	160(%rsp), %rsi
	movq	%r15, %rdi
	callq	_Z9saveImageP12IMGstructurePc
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	112(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
.LBB6_12:
	callq	hipFree
	leaq	48(%rsp), %rdi
	callq	_Z8freeDataP12IMGstructure
	movq	%r15, %rdi
	callq	_Z8freeDataP12IMGstructure
	leaq	128(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%rbx), %rax
	movq	8(%rbx), %rcx
	subq	96(%rsp), %rax
	cvtsi2sd	%rax, %xmm1
	subq	104(%rsp), %rcx
	cvtsi2sd	%rcx, %xmm0
	divsd	.LCPI6_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	movq	128(%rsp), %rax
	movq	136(%rsp), %rcx
	subq	144(%rsp), %rax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	subq	152(%rsp), %rcx
	cvtsi2sd	%rcx, %xmm2
	divsd	.LCPI6_0(%rip), %xmm2
	addsd	%xmm1, %xmm2
	movsd	%xmm2, 120(%rsp)                # 8-byte Spill
	movl	$.L.str.13, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.14, %edi
	movsd	120(%rsp), %xmm0                # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	xorl	%eax, %eax
	addq	$320, %rsp                      # imm = 0x140
	.cfi_def_cfa_offset 48
	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_end6:
	.size	main, .Lfunc_end6-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB7_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB7_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z6filterPhS_ii, %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_end7:
	.size	__hip_module_ctor, .Lfunc_end7-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB8_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB8_2:
	retq
.Lfunc_end8:
	.size	__hip_module_dtor, .Lfunc_end8-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"ERROR: %s\n"
	.size	.L.str, 11

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"r"
	.size	.L.str.1, 2

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"%s"
	.size	.L.str.3, 3

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"%s\n"
	.size	.L.str.4, 4

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%d"
	.size	.L.str.5, 3

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"%hhu\n"
	.size	.L.str.6, 6

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"%hhu %hhu %hhu\n"
	.size	.L.str.7, 16

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"w"
	.size	.L.str.8, 2

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"#imagem com smooth\n"
	.size	.L.str.9, 20

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"%d %d\n"
	.size	.L.str.10, 7

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"%d\n"
	.size	.L.str.11, 4

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

	.type	.L.str.13,@object               # @.str.13
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.13:
	.asciz	"GPU: %lfs\n"
	.size	.L.str.13, 11

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"GPU + memoria + in/out - TOTAL : %lfs\n"
	.size	.L.str.14, 39

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

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

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Can't read image file."
	.size	.Lstr, 23

	.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 _Z21__device_stub__filterPhS_ii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z6filterPhS_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z6filterPhS_ii         ; -- Begin function _Z6filterPhS_ii
	.globl	_Z6filterPhS_ii
	.p2align	8
	.type	_Z6filterPhS_ii,@function
_Z6filterPhS_ii:                        ; @_Z6filterPhS_ii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x24
	s_load_b64 s[4:5], s[0:1], 0x10
	v_and_b32_e32 v1, 0x3ff, v0
	v_bfe_u32 v0, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s3, s2, 16
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(VALU_DEP_2)
	v_mad_u64_u32 v[2:3], null, s14, s2, v[1:2]
	v_mad_u64_u32 v[3:4], null, s15, s3, v[0:1]
	s_add_i32 s2, s5, -2
	s_add_i32 s3, s4, -2
	v_cmp_gt_i32_e32 vcc_lo, s2, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_cmp_gt_i32_e64 s2, s3, v3
	v_cmp_lt_i32_e64 s3, 1, v2
	v_cmp_lt_i32_e64 s4, 1, v3
	s_and_b32 s2, vcc_lo, s2
	s_delay_alu instid0(VALU_DEP_2) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, s3, s2
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, s4, s2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s3, s2
	s_cbranch_execz .LBB0_6
; %bb.1:                                ; %.preheader38
	s_load_b128 s[0:3], s[0:1], 0x0
	v_mad_u64_u32 v[0:1], null, v3, s5, v[2:3]
	v_mov_b32_e32 v2, 0
	s_mov_b32 s4, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_4) | instid1(SALU_CYCLE_1)
	v_ashrrev_i32_e32 v1, 31, v0
	s_waitcnt lgkmcnt(0)
	s_add_u32 s0, s0, -2
	s_addc_u32 s1, s1, -1
	s_lshl_b32 s6, s5, 1
	s_sub_i32 s6, 0, s6
.LBB0_2:                                ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_3 Depth 2
	s_ashr_i32 s7, s4, 31
	v_add_co_u32 v3, vcc_lo, v0, s4
	v_add_co_ci_u32_e32 v4, vcc_lo, s7, v1, vcc_lo
	s_mov_b32 s7, s6
	s_mov_b32 s8, 5
.LBB0_3:                                ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_ashr_i32 s9, s7, 31
	s_add_u32 s10, s0, s7
	s_addc_u32 s9, s1, s9
	v_add_co_u32 v5, vcc_lo, s10, v3
	v_add_co_ci_u32_e32 v6, vcc_lo, s9, v4, vcc_lo
	s_add_i32 s8, s8, -1
	s_add_i32 s7, s7, s5
	s_cmp_lg_u32 s8, 0
	global_load_u8 v5, v[5:6], off
	s_waitcnt vmcnt(0)
	v_cvt_f32_ubyte0_e32 v5, v5
	s_delay_alu instid0(VALU_DEP_1)
	v_add_f32_e32 v2, v2, v5
	s_cbranch_scc1 .LBB0_3
; %bb.4:                                ;   in Loop: Header=BB0_2 Depth=1
	s_add_i32 s4, s4, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lg_u32 s4, 5
	s_cbranch_scc1 .LBB0_2
; %bb.5:
	v_div_scale_f32 v3, null, 0x41c80000, 0x41c80000, v2
	v_div_scale_f32 v6, vcc_lo, v2, 0x41c80000, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v4, v3
	s_waitcnt_depctr 0xfff
	v_fma_f32 v5, -v3, v4, 1.0
	v_fmac_f32_e32 v4, v5, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v5, v6, v4
	v_fma_f32 v7, -v3, v5, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v5, v7, v4
	v_fma_f32 v3, -v3, v5, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_div_fmas_f32 v3, v3, v4, v5
	v_add_co_u32 v0, vcc_lo, s2, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	v_div_fixup_f32 v2, v3, 0x41c80000, v2
	s_delay_alu instid0(VALU_DEP_1)
	v_cvt_i32_f32_e32 v2, v2
	global_store_b8 v[0:1], v2, off
.LBB0_6:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z6filterPhS_ii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.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 8
		.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	_Z6filterPhS_ii, .Lfunc_end0-_Z6filterPhS_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 444
; NumSgprs: 18
; NumVgprs: 8
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 8
; 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:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z6filterPhS_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z6filterPhS_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     8
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
