#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>       /* round() */

typedef struct bmpFileHeader
{
  /* 2 bytes de identificación */
  unsigned int size;        /* Tamaño del archivo */
  unsigned short resv1;       /* Reservado */
  unsigned short resv2;       /* Reservado */
  unsigned int offset;      /* Offset hasta hasta los datos de imagen */
} bmpFileHeader;

typedef struct bmpInfoHeader
{
  unsigned int headersize;  /* Tamaño de la cabecera */
  unsigned int width;       /* Ancho */
  unsigned int height;      /* Alto */
  unsigned short planes;      /* Planos de color (Siempre 1) */
  unsigned short bpp;         /* bits por pixel */
  unsigned int compress;    /* compresión */
  unsigned int imgsize;     /* tamaño de los datos de imagen */
  unsigned int bpmx;        /* Resolución X en bits por metro */
  unsigned int bpmy;        /* Resolución Y en bits por metro */
  unsigned int colors;      /* colors used en la paleta */
  unsigned int imxtcolors;  /* Colores importantes. 0 si son todos */
} bmpInfoHeader;

unsigned char *LoadBMP(char *filename, bmpInfoHeader *bInfoHeader);
void DisplayInfo(bmpInfoHeader *info);
void TextDisplay(bmpInfoHeader *info, unsigned char *img);

void SaveBMP(char *filename, bmpInfoHeader *info, unsigned char *imgdata);

__global__
void escalaGrises(unsigned char *img, unsigned char *grayimg, int numRows, int numCols)
{
  int Col = threadIdx.x + blockIdx.x*blockDim.x;
  int Row = threadIdx.y + blockIdx.y*blockDim.y;

  if(Col < numCols && Row < numRows)
  {
    int grayOffset = Col + Row*numCols;
    int rgbOffset = grayOffset*3;
    unsigned char r = img[rgbOffset + 2];
    unsigned char g = img[rgbOffset + 1];
    unsigned char b = img[rgbOffset    ];
    grayimg[grayOffset] = 0.21f*r + 0.71f*g + 0.07*b;
  }
}

int main()
{
  bmpInfoHeader info;
  unsigned char *img, *im2;
  int blockThreadSize, numberOfBlocks;
  int numRows, numCols;

  img = LoadBMP("magia_sonrisa.bmp", &info);
  DisplayInfo(&info);
  TextDisplay(&info, img);
  numRows = info.height;
  numCols = info.width;

  blockThreadSize = 32;
  numberOfBlocks = 1 + ((numRows - 1) / blockThreadSize);

  const int isize = (numRows*numCols)*sizeof(char);
  cudaMalloc((void**)&im2, isize);

  const dim3 blockSize(blockThreadSize, blockThreadSize, 1);
  const dim3 gridSize(numberOfBlocks , ceil(numCols/blockThreadSize), 1);
  escalaGrises<<<gridSize, blockSize>>>(img, im2, numRows, numCols);

  cudaMemcpy(img, im2, isize, cudaMemcpyDeviceToHost);
  cudaFree(im2);

  SaveBMP("resultado.bmp", &info, img);

  free(img);

  return 0;
}

void TextDisplay(bmpInfoHeader *info, unsigned char *img)
{
  int x, y;
  /* Reducimos la resolución vertical y horizontal para que la imagen entre en pantalla */
  static const int reduccionX=6, reduccionY=4;
  /* Si la componente supera el umbral, el color se marcará como 1. */
  static const int umbral=90;
  /* Asignamos caracteres a los colores en pantalla */
  static unsigned char colores[9]=" bgfrRGB";
  int r,g,b;

  /* Dibujamos la imagen */
  for (y=info->height; y>0; y-=reduccionY)
  {
    for (x=0; x<info->width; x+=reduccionX)
    {
      b=(img[3*(x+y*info->width)]>umbral);
      g=(img[3*(x+y*info->width)+1]>umbral);
      r=(img[3*(x+y*info->width)+2]>umbral);

      printf("%c", colores[b+g*2+r*4]);
    }
    printf("\n");
  }
}

unsigned char *LoadBMP(char *filename, bmpInfoHeader *bInfoHeader)
{

  FILE *f;
  bmpFileHeader header;     /* cabecera */
  unsigned char *imgdata;   /* datos de imagen */
  unsigned short type;            /* 2 bytes identificativos */

  f=fopen (filename, "r");
  if (!f)
    return NULL;            /* Si no podemos leer, no hay imagen*/

  /* Leemos los dos primeros bytes */
  fread(&type, sizeof(unsigned short), 1, f);
  if (type !=0x4D42)        /* Comprobamos el formato */
  {
    fclose(f);
    return NULL;
  }

  /* Leemos la cabecera de fichero completa */
  fread(&header, sizeof(bmpFileHeader), 1, f);

  /* Leemos la cabecera de información completa */
  fread(bInfoHeader, sizeof(bmpInfoHeader), 1, f);

  /* Reservamos memoria para la imagen, ¿cuánta? 
     Tanto como indique imgsize */
  imgdata=(unsigned char*)malloc(bInfoHeader->imgsize);

  /* Nos situamos en el sitio donde empiezan los datos de imagen,
   nos lo indica el offset de la cabecera de fichero*/
  fseek(f, header.offset, SEEK_SET);

  /* Leemos los datos de imagen, tantos bytes como imgsize */
  fread(imgdata, bInfoHeader->imgsize,1, f);

  /* Cerramos */
  fclose(f);

  /* Devolvemos la imagen */
  return imgdata;
}

void DisplayInfo(bmpInfoHeader *info)
{
  printf("Tamaño de la cabecera: %u\n", info->headersize);
  printf("Anchura: %d\n", info->width);
  printf("Altura: %d\n", info->height);
  printf("Planos (1): %d\n", info->planes);
  printf("Bits por pixel: %d\n", info->bpp);
  printf("Compresión: %d\n", info->compress);
  printf("Tamaño de datos de imagen: %u\n", info->imgsize);
  printf("Resolucón horizontal: %u\n", info->bpmx);
  printf("Resolucón vertical: %u\n", info->bpmy);
  printf("Colores en paleta: %d\n", info->colors);
  printf("Colores importantes: %d\n", info->imxtcolors);
}

void SaveBMP(char *filename, bmpInfoHeader *info, unsigned char *imgdata)
{
  bmpFileHeader header;
  FILE *f;
  unsigned short type;
  
  f=fopen(filename, "w+");
  header.size = info->imgsize + sizeof(bmpFileHeader) + sizeof(bmpInfoHeader);
  /* header.resv1=0; */
  /* header.resv2=1; */
  
  /* El offset será el tamaño de las dos cabeceras + 2 (información de fichero)*/
  header.offset = sizeof(bmpFileHeader) + sizeof(bmpInfoHeader) + 2;
  
  /* Escribimos la identificación del archivo */
  type = 0x4D42;
  fwrite(&type, sizeof(type),1,f);
  
  /* Escribimos la cabecera de fichero */
  fwrite(&header, sizeof(bmpFileHeader),1,f);
  
  /* Escribimos la información básica de la imagen */
  fwrite(info, sizeof(bmpInfoHeader),1,f);
  
  /* Escribimos la imagen */
  fwrite(imgdata, info->imgsize, 1, f);
  
  fclose(f);
}
	.file	"tmpxft_0020bf42_00000000-6_imagen.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%c"
.LC1:
	.string	"\n"
	.text
	.globl	_Z11TextDisplayP13bmpInfoHeaderPh
	.type	_Z11TextDisplayP13bmpInfoHeaderPh, @function
_Z11TextDisplayP13bmpInfoHeaderPh:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	leaq	_ZZ11TextDisplayP13bmpInfoHeaderPhE7colores(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movl	8(%rdi), %r13d
	movq	%rdi, %rbx
.L3:
	testl	%r13d, %r13d
	jle	.L9
	xorl	%r14d, %r14d
.L5:
	movl	4(%rbx), %eax
	cmpl	%eax, %r14d
	jnb	.L10
	imull	%r13d, %eax
	xorl	%ecx, %ecx
	movl	$2, %edi
	leaq	.LC0(%rip), %rsi
	addl	%r14d, %eax
	leal	(%rax,%rax,2), %eax
	leal	1(%rax), %edx
	cmpb	$90, (%r12,%rdx)
	movl	%eax, %edx
	seta	%cl
	cmpb	$90, (%r12,%rdx)
	seta	%dl
	addl	$2, %eax
	cmpb	$90, (%r12,%rax)
	movzbl	%dl, %edx
	seta	%al
	leal	(%rdx,%rcx,2), %edx
	addl	$6, %r14d
	movzbl	%al, %eax
	leal	(%rdx,%rax,4), %eax
	cltq
	movzbl	0(%rbp,%rax), %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L5
.L10:
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	subl	$4, %r13d
	call	__printf_chk@PLT
	jmp	.L3
.L9:
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z11TextDisplayP13bmpInfoHeaderPh, .-_Z11TextDisplayP13bmpInfoHeaderPh
	.section	.rodata.str1.1
.LC2:
	.string	"r"
	.text
	.globl	_Z7LoadBMPPcP13bmpInfoHeader
	.type	_Z7LoadBMPPcP13bmpInfoHeader, @function
_Z7LoadBMPPcP13bmpInfoHeader:
.LFB2029:
	.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
	movq	%rsi, %r12
	leaq	.LC2(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	testq	%rax, %rax
	je	.L12
	leaq	10(%rsp), %rdi
	movq	%rax, %r8
	movl	$1, %ecx
	movq	%rax, %rbx
	movl	$2, %edx
	movl	$2, %esi
	call	__fread_chk@PLT
	cmpw	$19778, 10(%rsp)
	je	.L13
	movq	%rbx, %rdi
	call	fclose@PLT
.L12:
	xorl	%ebp, %ebp
	jmp	.L11
.L13:
	leaq	12(%rsp), %rdi
	movq	%rbx, %r8
	movl	$1, %ecx
	movl	$12, %edx
	movl	$12, %esi
	call	__fread_chk@PLT
	movq	%rbx, %r8
	movl	$1, %ecx
	movq	%r12, %rdi
	movl	$40, %edx
	orq	$-1, %rsi
	call	__fread_chk@PLT
	movl	20(%r12), %r13d
	movq	%r13, %rdi
	call	malloc@PLT
	movl	20(%rsp), %esi
	xorl	%edx, %edx
	movq	%rbx, %rdi
	movq	%rax, %rbp
	call	fseek@PLT
	movl	20(%r12), %edx
	movq	%rbp, %rdi
	movq	%rbx, %r8
	movl	$1, %ecx
	movq	%r13, %rsi
	call	__fread_chk@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
.L11:
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	movq	%rbp, %rax
	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
.LFE2029:
	.size	_Z7LoadBMPPcP13bmpInfoHeader, .-_Z7LoadBMPPcP13bmpInfoHeader
	.section	.rodata.str1.1
.LC3:
	.string	"Tama\303\261o de la cabecera: %u\n"
.LC4:
	.string	"Anchura: %d\n"
.LC5:
	.string	"Altura: %d\n"
.LC6:
	.string	"Planos (1): %d\n"
.LC7:
	.string	"Bits por pixel: %d\n"
.LC8:
	.string	"Compresi\303\263n: %d\n"
.LC9:
	.string	"Tama\303\261o de datos de imagen: %u\n"
.LC10:
	.string	"Resoluc\303\263n horizontal: %u\n"
.LC11:
	.string	"Resoluc\303\263n vertical: %u\n"
.LC12:
	.string	"Colores en paleta: %d\n"
.LC13:
	.string	"Colores importantes: %d\n"
	.text
	.globl	_Z11DisplayInfoP13bmpInfoHeader
	.type	_Z11DisplayInfoP13bmpInfoHeader, @function
_Z11DisplayInfoP13bmpInfoHeader:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	(%rdi), %edx
	movq	%rdi, %rbx
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	4(%rbx), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC4(%rip), %rsi
	call	__printf_chk@PLT
	movl	8(%rbx), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC5(%rip), %rsi
	call	__printf_chk@PLT
	movzwl	12(%rbx), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC6(%rip), %rsi
	call	__printf_chk@PLT
	movzwl	14(%rbx), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC7(%rip), %rsi
	call	__printf_chk@PLT
	movl	16(%rbx), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC8(%rip), %rsi
	call	__printf_chk@PLT
	movl	20(%rbx), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC9(%rip), %rsi
	call	__printf_chk@PLT
	movl	24(%rbx), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC10(%rip), %rsi
	call	__printf_chk@PLT
	movl	28(%rbx), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC11(%rip), %rsi
	call	__printf_chk@PLT
	movl	32(%rbx), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC12(%rip), %rsi
	call	__printf_chk@PLT
	movl	36(%rbx), %edx
	leaq	.LC13(%rip), %rsi
	popq	%rbx
	.cfi_def_cfa_offset 8
	movl	$2, %edi
	xorl	%eax, %eax
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2030:
	.size	_Z11DisplayInfoP13bmpInfoHeader, .-_Z11DisplayInfoP13bmpInfoHeader
	.section	.rodata.str1.1
.LC14:
	.string	"w+"
	.text
	.globl	_Z7SaveBMPPcP13bmpInfoHeaderPh
	.type	_Z7SaveBMPPcP13bmpInfoHeaderPh, @function
_Z7SaveBMPPcP13bmpInfoHeaderPh:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movq	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rsi, %rbp
	leaq	.LC14(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	leaq	10(%rsp), %rdi
	movl	$1, %edx
	movl	$2, %esi
	movq	%rax, %rbx
	movl	20(%rbp), %eax
	movl	$54, 20(%rsp)
	movq	%rbx, %rcx
	movw	$19778, 10(%rsp)
	addl	$52, %eax
	movl	%eax, 12(%rsp)
	call	fwrite@PLT
	leaq	12(%rsp), %rdi
	movq	%rbx, %rcx
	movl	$1, %edx
	movl	$12, %esi
	call	fwrite@PLT
	movq	%rbx, %rcx
	movl	$1, %edx
	movq	%rbp, %rdi
	movl	$40, %esi
	call	fwrite@PLT
	movl	20(%rbp), %esi
	movq	%r12, %rdi
	movq	%rbx, %rcx
	movl	$1, %edx
	call	fwrite@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z7SaveBMPPcP13bmpInfoHeaderPh, .-_Z7SaveBMPPcP13bmpInfoHeaderPh
	.globl	_Z36__device_stub__Z12escalaGrisesPhS_iiPhS_ii
	.type	_Z36__device_stub__Z12escalaGrisesPhS_iiPhS_ii, @function
_Z36__device_stub__Z12escalaGrisesPhS_iiPhS_ii:
.LFB2056:
	.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	.L25
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z12escalaGrisesPhS_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
.L25:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z36__device_stub__Z12escalaGrisesPhS_iiPhS_ii, .-_Z36__device_stub__Z12escalaGrisesPhS_iiPhS_ii
	.globl	_Z12escalaGrisesPhS_ii
	.type	_Z12escalaGrisesPhS_ii, @function
_Z12escalaGrisesPhS_ii:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z36__device_stub__Z12escalaGrisesPhS_iiPhS_ii
	.cfi_endproc
.LFE2057:
	.size	_Z12escalaGrisesPhS_ii, .-_Z12escalaGrisesPhS_ii
	.section	.rodata.str1.1
.LC15:
	.string	"magia_sonrisa.bmp"
.LC16:
	.string	"resultado.bmp"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	leaq	.LC15(%rip), %rdi
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$80, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	leaq	32(%rsp), %rbp
	movq	%rbp, %rsi
	call	_Z7LoadBMPPcP13bmpInfoHeader
	movq	%rbp, %rdi
	movq	%rax, %rbx
	call	_Z11DisplayInfoP13bmpInfoHeader
	movq	%rbx, %rsi
	movq	%rbp, %rdi
	call	_Z11TextDisplayP13bmpInfoHeaderPh
	movl	40(%rsp), %r14d
	movl	36(%rsp), %r13d
	movq	%rsp, %rdi
	movl	%r14d, %r12d
	imull	%r13d, %r12d
	movslq	%r12d, %r12
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	leal	-1(%r14), %eax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$32, %ecx
	cltd
	movl	$1, %esi
	idivl	%ecx
	incl	%eax
	movl	%eax, 20(%rsp)
	movl	%r13d, %eax
	cltd
	idivl	%ecx
	movabsq	$137438953504, %rdx
	movl	$1, %ecx
	movl	%eax, 24(%rsp)
	movq	20(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L31
	movq	(%rsp), %rsi
	movl	%r13d, %ecx
	movl	%r14d, %edx
	movq	%rbx, %rdi
	call	_Z36__device_stub__Z12escalaGrisesPhS_iiPhS_ii
.L31:
	movq	(%rsp), %rsi
	movl	$2, %ecx
	movq	%r12, %rdx
	movq	%rbx, %rdi
	call	cudaMemcpy@PLT
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	leaq	.LC16(%rip), %rdi
	movq	%rbx, %rdx
	movq	%rbp, %rsi
	call	_Z7SaveBMPPcP13bmpInfoHeaderPh
	movq	%rbx, %rdi
	call	free@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L32
	call	__stack_chk_fail@PLT
.L32:
	addq	$80, %rsp
	.cfi_def_cfa_offset 48
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC17:
	.string	"_Z12escalaGrisesPhS_ii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.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	.LC17(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z12escalaGrisesPhS_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
.LFE2059:
	.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
	.align 8
	.type	_ZZ11TextDisplayP13bmpInfoHeaderPhE7colores, @object
	.size	_ZZ11TextDisplayP13bmpInfoHeaderPhE7colores, 9
_ZZ11TextDisplayP13bmpInfoHeaderPhE7colores:
	.string	" bgfrRGB"
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>       /* round() */

typedef struct bmpFileHeader
{
  /* 2 bytes de identificación */
  unsigned int size;        /* Tamaño del archivo */
  unsigned short resv1;       /* Reservado */
  unsigned short resv2;       /* Reservado */
  unsigned int offset;      /* Offset hasta hasta los datos de imagen */
} bmpFileHeader;

typedef struct bmpInfoHeader
{
  unsigned int headersize;  /* Tamaño de la cabecera */
  unsigned int width;       /* Ancho */
  unsigned int height;      /* Alto */
  unsigned short planes;      /* Planos de color (Siempre 1) */
  unsigned short bpp;         /* bits por pixel */
  unsigned int compress;    /* compresión */
  unsigned int imgsize;     /* tamaño de los datos de imagen */
  unsigned int bpmx;        /* Resolución X en bits por metro */
  unsigned int bpmy;        /* Resolución Y en bits por metro */
  unsigned int colors;      /* colors used en la paleta */
  unsigned int imxtcolors;  /* Colores importantes. 0 si son todos */
} bmpInfoHeader;

unsigned char *LoadBMP(char *filename, bmpInfoHeader *bInfoHeader);
void DisplayInfo(bmpInfoHeader *info);
void TextDisplay(bmpInfoHeader *info, unsigned char *img);

void SaveBMP(char *filename, bmpInfoHeader *info, unsigned char *imgdata);

__global__
void escalaGrises(unsigned char *img, unsigned char *grayimg, int numRows, int numCols)
{
  int Col = threadIdx.x + blockIdx.x*blockDim.x;
  int Row = threadIdx.y + blockIdx.y*blockDim.y;

  if(Col < numCols && Row < numRows)
  {
    int grayOffset = Col + Row*numCols;
    int rgbOffset = grayOffset*3;
    unsigned char r = img[rgbOffset + 2];
    unsigned char g = img[rgbOffset + 1];
    unsigned char b = img[rgbOffset    ];
    grayimg[grayOffset] = 0.21f*r + 0.71f*g + 0.07*b;
  }
}

int main()
{
  bmpInfoHeader info;
  unsigned char *img, *im2;
  int blockThreadSize, numberOfBlocks;
  int numRows, numCols;

  img = LoadBMP("magia_sonrisa.bmp", &info);
  DisplayInfo(&info);
  TextDisplay(&info, img);
  numRows = info.height;
  numCols = info.width;

  blockThreadSize = 32;
  numberOfBlocks = 1 + ((numRows - 1) / blockThreadSize);

  const int isize = (numRows*numCols)*sizeof(char);
  hipMalloc((void**)&im2, isize);

  const dim3 blockSize(blockThreadSize, blockThreadSize, 1);
  const dim3 gridSize(numberOfBlocks , ceil(numCols/blockThreadSize), 1);
  escalaGrises<<<gridSize, blockSize>>>(img, im2, numRows, numCols);

  hipMemcpy(img, im2, isize, hipMemcpyDeviceToHost);
  hipFree(im2);

  SaveBMP("resultado.bmp", &info, img);

  free(img);

  return 0;
}

void TextDisplay(bmpInfoHeader *info, unsigned char *img)
{
  int x, y;
  /* Reducimos la resolución vertical y horizontal para que la imagen entre en pantalla */
  static const int reduccionX=6, reduccionY=4;
  /* Si la componente supera el umbral, el color se marcará como 1. */
  static const int umbral=90;
  /* Asignamos caracteres a los colores en pantalla */
  static unsigned char colores[9]=" bgfrRGB";
  int r,g,b;

  /* Dibujamos la imagen */
  for (y=info->height; y>0; y-=reduccionY)
  {
    for (x=0; x<info->width; x+=reduccionX)
    {
      b=(img[3*(x+y*info->width)]>umbral);
      g=(img[3*(x+y*info->width)+1]>umbral);
      r=(img[3*(x+y*info->width)+2]>umbral);

      printf("%c", colores[b+g*2+r*4]);
    }
    printf("\n");
  }
}

unsigned char *LoadBMP(char *filename, bmpInfoHeader *bInfoHeader)
{

  FILE *f;
  bmpFileHeader header;     /* cabecera */
  unsigned char *imgdata;   /* datos de imagen */
  unsigned short type;            /* 2 bytes identificativos */

  f=fopen (filename, "r");
  if (!f)
    return NULL;            /* Si no podemos leer, no hay imagen*/

  /* Leemos los dos primeros bytes */
  fread(&type, sizeof(unsigned short), 1, f);
  if (type !=0x4D42)        /* Comprobamos el formato */
  {
    fclose(f);
    return NULL;
  }

  /* Leemos la cabecera de fichero completa */
  fread(&header, sizeof(bmpFileHeader), 1, f);

  /* Leemos la cabecera de información completa */
  fread(bInfoHeader, sizeof(bmpInfoHeader), 1, f);

  /* Reservamos memoria para la imagen, ¿cuánta? 
     Tanto como indique imgsize */
  imgdata=(unsigned char*)malloc(bInfoHeader->imgsize);

  /* Nos situamos en el sitio donde empiezan los datos de imagen,
   nos lo indica el offset de la cabecera de fichero*/
  fseek(f, header.offset, SEEK_SET);

  /* Leemos los datos de imagen, tantos bytes como imgsize */
  fread(imgdata, bInfoHeader->imgsize,1, f);

  /* Cerramos */
  fclose(f);

  /* Devolvemos la imagen */
  return imgdata;
}

void DisplayInfo(bmpInfoHeader *info)
{
  printf("Tamaño de la cabecera: %u\n", info->headersize);
  printf("Anchura: %d\n", info->width);
  printf("Altura: %d\n", info->height);
  printf("Planos (1): %d\n", info->planes);
  printf("Bits por pixel: %d\n", info->bpp);
  printf("Compresión: %d\n", info->compress);
  printf("Tamaño de datos de imagen: %u\n", info->imgsize);
  printf("Resolucón horizontal: %u\n", info->bpmx);
  printf("Resolucón vertical: %u\n", info->bpmy);
  printf("Colores en paleta: %d\n", info->colors);
  printf("Colores importantes: %d\n", info->imxtcolors);
}

void SaveBMP(char *filename, bmpInfoHeader *info, unsigned char *imgdata)
{
  bmpFileHeader header;
  FILE *f;
  unsigned short type;
  
  f=fopen(filename, "w+");
  header.size = info->imgsize + sizeof(bmpFileHeader) + sizeof(bmpInfoHeader);
  /* header.resv1=0; */
  /* header.resv2=1; */
  
  /* El offset será el tamaño de las dos cabeceras + 2 (información de fichero)*/
  header.offset = sizeof(bmpFileHeader) + sizeof(bmpInfoHeader) + 2;
  
  /* Escribimos la identificación del archivo */
  type = 0x4D42;
  fwrite(&type, sizeof(type),1,f);
  
  /* Escribimos la cabecera de fichero */
  fwrite(&header, sizeof(bmpFileHeader),1,f);
  
  /* Escribimos la información básica de la imagen */
  fwrite(info, sizeof(bmpInfoHeader),1,f);
  
  /* Escribimos la imagen */
  fwrite(imgdata, info->imgsize, 1, f);
  
  fclose(f);
}
	.text
	.file	"imagen.hip"
	.globl	_Z27__device_stub__escalaGrisesPhS_ii # -- Begin function _Z27__device_stub__escalaGrisesPhS_ii
	.type	_Z27__device_stub__escalaGrisesPhS_ii,@function
_Z27__device_stub__escalaGrisesPhS_ii:  # @_Z27__device_stub__escalaGrisesPhS_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	$_Z12escalaGrisesPhS_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_end0:
	.size	_Z27__device_stub__escalaGrisesPhS_ii, .Lfunc_end0-_Z27__device_stub__escalaGrisesPhS_ii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$48, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	8(%rsp), %r15
	movl	$.L.str, %edi
	movq	%r15, %rsi
	callq	_Z7LoadBMPPcP13bmpInfoHeader
	movq	%rax, %rbx
	movq	%r15, %rdi
	callq	_Z11DisplayInfoP13bmpInfoHeader
	movq	%r15, %rdi
	movq	%rbx, %rsi
	callq	_Z11TextDisplayP13bmpInfoHeaderPh
	movl	4(%r15), %r14d
	movl	8(%r15), %r15d
	leal	-1(%r15), %eax
	leal	30(%r15), %r13d
	testl	%eax, %eax
	cmovnsl	%eax, %r13d
	sarl	$5, %r13d
	incl	%r13d
	movl	%r14d, %eax
	imull	%r15d, %eax
	movslq	%eax, %r12
	movq	%rsp, %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	leal	31(%r14), %edi
	testl	%r14d, %r14d
	cmovnsl	%r14d, %edi
	sarl	$5, %edi
	shlq	$32, %rdi
	orq	%r13, %rdi
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_2
# %bb.1:
	movq	(%rsp), %rsi
	movq	%rbx, %rdi
	movl	%r15d, %edx
	movl	%r14d, %ecx
	callq	_Z27__device_stub__escalaGrisesPhS_ii
.LBB1_2:
	movq	(%rsp), %rsi
	movq	%rbx, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	(%rsp), %rdi
	callq	hipFree
	leaq	8(%rsp), %rsi
	movl	$.L.str.1, %edi
	movq	%rbx, %rdx
	callq	_Z7SaveBMPPcP13bmpInfoHeaderPh
	movq	%rbx, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$48, %rsp
	.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_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z7LoadBMPPcP13bmpInfoHeader    # -- Begin function _Z7LoadBMPPcP13bmpInfoHeader
	.type	_Z7LoadBMPPcP13bmpInfoHeader,@function
_Z7LoadBMPPcP13bmpInfoHeader:           # @_Z7LoadBMPPcP13bmpInfoHeader
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %r14
	movl	$.L.str.4, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB2_1
# %bb.2:
	movq	%rax, %rbx
	leaq	10(%rsp), %r15
	movl	$2, %esi
	movl	$1, %edx
	movq	%r15, %rdi
	movq	%rax, %rcx
	callq	fread
	movzwl	(%r15), %eax
	cmpl	$19778, %eax                    # imm = 0x4D42
	jne	.LBB2_3
# %bb.4:
	leaq	12(%rsp), %r12
	movl	$12, %esi
	movl	$1, %edx
	movq	%r12, %rdi
	movq	%rbx, %rcx
	callq	fread
	movl	$40, %esi
	movl	$1, %edx
	movq	%r14, %rdi
	movq	%rbx, %rcx
	callq	fread
	movl	20(%r14), %edi
	callq	malloc
	movq	%rax, %r15
	movl	8(%r12), %esi
	movq	%rbx, %rdi
	xorl	%edx, %edx
	callq	fseek
	movl	20(%r14), %esi
	movl	$1, %edx
	movq	%r15, %rdi
	movq	%rbx, %rcx
	callq	fread
	jmp	.LBB2_5
.LBB2_1:
	xorl	%r15d, %r15d
	jmp	.LBB2_6
.LBB2_3:
	xorl	%r15d, %r15d
.LBB2_5:                                # %.sink.split
	movq	%rbx, %rdi
	callq	fclose
.LBB2_6:
	movq	%r15, %rax
	addq	$24, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z7LoadBMPPcP13bmpInfoHeader, .Lfunc_end2-_Z7LoadBMPPcP13bmpInfoHeader
	.cfi_endproc
                                        # -- End function
	.globl	_Z11DisplayInfoP13bmpInfoHeader # -- Begin function _Z11DisplayInfoP13bmpInfoHeader
	.type	_Z11DisplayInfoP13bmpInfoHeader,@function
_Z11DisplayInfoP13bmpInfoHeader:        # @_Z11DisplayInfoP13bmpInfoHeader
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movl	(%rdi), %esi
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	movl	4(%rbx), %esi
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	movl	8(%rbx), %esi
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
	movzwl	12(%rbx), %esi
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
	movzwl	14(%rbx), %esi
	movl	$.L.str.9, %edi
	xorl	%eax, %eax
	callq	printf
	movl	16(%rbx), %esi
	movl	$.L.str.10, %edi
	xorl	%eax, %eax
	callq	printf
	movl	20(%rbx), %esi
	movl	$.L.str.11, %edi
	xorl	%eax, %eax
	callq	printf
	movl	24(%rbx), %esi
	movl	$.L.str.12, %edi
	xorl	%eax, %eax
	callq	printf
	movl	28(%rbx), %esi
	movl	$.L.str.13, %edi
	xorl	%eax, %eax
	callq	printf
	movl	32(%rbx), %esi
	movl	$.L.str.14, %edi
	xorl	%eax, %eax
	callq	printf
	movl	36(%rbx), %esi
	movl	$.L.str.15, %edi
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	printf                          # TAILCALL
.Lfunc_end3:
	.size	_Z11DisplayInfoP13bmpInfoHeader, .Lfunc_end3-_Z11DisplayInfoP13bmpInfoHeader
	.cfi_endproc
                                        # -- End function
	.globl	_Z11TextDisplayP13bmpInfoHeaderPh # -- Begin function _Z11TextDisplayP13bmpInfoHeaderPh
	.type	_Z11TextDisplayP13bmpInfoHeaderPh,@function
_Z11TextDisplayP13bmpInfoHeaderPh:      # @_Z11TextDisplayP13bmpInfoHeaderPh
	.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	8(%rdi), %r15d
	testl	%r15d, %r15d
	jle	.LBB4_6
# %bb.1:                                # %.preheader.lr.ph
	movq	%rsi, %rbx
	movq	%rdi, %r14
.LBB4_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_4 Depth 2
	movl	4(%r14), %eax
	testl	%eax, %eax
	je	.LBB4_5
# %bb.3:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB4_2 Depth=1
	xorl	%ebp, %ebp
.LBB4_4:                                # %.lr.ph
                                        #   Parent Loop BB4_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	imull	%r15d, %eax
	addl	%ebp, %eax
	leal	(%rax,%rax,2), %ecx
	xorl	%edx, %edx
	cmpb	$91, (%rbx,%rcx)
	setae	%dl
	leal	(%rax,%rax,2), %ecx
	incl	%ecx
	xorl	%esi, %esi
	cmpb	$91, (%rbx,%rcx)
	setae	%sil
	leal	(%rax,%rax,2), %eax
	addl	$2, %eax
	xorl	%ecx, %ecx
	cmpb	$91, (%rbx,%rax)
	setae	%cl
	leaq	(%rdx,%rsi,2), %rax
	movzbl	_ZZ11TextDisplayP13bmpInfoHeaderPhE7colores(%rax,%rcx,4), %edi
	callq	putchar@PLT
	addl	$6, %ebp
	movl	4(%r14), %eax
	cmpl	%eax, %ebp
	jb	.LBB4_4
.LBB4_5:                                # %._crit_edge
                                        #   in Loop: Header=BB4_2 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	leal	-4(%r15), %eax
	cmpl	$4, %r15d
	movl	%eax, %r15d
	jg	.LBB4_2
.LBB4_6:                                # %._crit_edge25
	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_end4:
	.size	_Z11TextDisplayP13bmpInfoHeaderPh, .Lfunc_end4-_Z11TextDisplayP13bmpInfoHeaderPh
	.cfi_endproc
                                        # -- End function
	.globl	_Z7SaveBMPPcP13bmpInfoHeaderPh  # -- Begin function _Z7SaveBMPPcP13bmpInfoHeaderPh
	.type	_Z7SaveBMPPcP13bmpInfoHeaderPh,@function
_Z7SaveBMPPcP13bmpInfoHeaderPh:         # @_Z7SaveBMPPcP13bmpInfoHeaderPh
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdx, %rbx
	movq	%rsi, %r15
	movl	$.L.str.16, %esi
	callq	fopen
	movq	%rax, %r14
	movl	20(%r15), %eax
	addl	$52, %eax
	leaq	12(%rsp), %r12
	movl	%eax, (%r12)
	movl	$54, 8(%r12)
	leaq	10(%rsp), %rdi
	movw	$19778, (%rdi)                  # imm = 0x4D42
	movl	$2, %esi
	movl	$1, %edx
	movq	%r14, %rcx
	callq	fwrite
	movl	$12, %esi
	movl	$1, %edx
	movq	%r12, %rdi
	movq	%r14, %rcx
	callq	fwrite
	movl	$40, %esi
	movl	$1, %edx
	movq	%r15, %rdi
	movq	%r14, %rcx
	callq	fwrite
	movl	20(%r15), %esi
	movl	$1, %edx
	movq	%rbx, %rdi
	movq	%r14, %rcx
	callq	fwrite
	movq	%r14, %rdi
	addq	$24, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.Lfunc_end5:
	.size	_Z7SaveBMPPcP13bmpInfoHeaderPh, .Lfunc_end5-_Z7SaveBMPPcP13bmpInfoHeaderPh
	.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	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z12escalaGrisesPhS_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_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__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	.LBB7_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
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z12escalaGrisesPhS_ii,@object  # @_Z12escalaGrisesPhS_ii
	.section	.rodata,"a",@progbits
	.globl	_Z12escalaGrisesPhS_ii
	.p2align	3, 0x0
_Z12escalaGrisesPhS_ii:
	.quad	_Z27__device_stub__escalaGrisesPhS_ii
	.size	_Z12escalaGrisesPhS_ii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"magia_sonrisa.bmp"
	.size	.L.str, 18

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"resultado.bmp"
	.size	.L.str.1, 14

	.type	_ZZ11TextDisplayP13bmpInfoHeaderPhE7colores,@object # @_ZZ11TextDisplayP13bmpInfoHeaderPhE7colores
_ZZ11TextDisplayP13bmpInfoHeaderPhE7colores:
	.asciz	" bgfrRGB"
	.size	_ZZ11TextDisplayP13bmpInfoHeaderPhE7colores, 9

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Tama\303\261o de la cabecera: %u\n"
	.size	.L.str.5, 28

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

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

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Planos (1): %d\n"
	.size	.L.str.8, 16

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Bits por pixel: %d\n"
	.size	.L.str.9, 20

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Compresi\303\263n: %d\n"
	.size	.L.str.10, 17

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"Tama\303\261o de datos de imagen: %u\n"
	.size	.L.str.11, 32

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Resoluc\303\263n horizontal: %u\n"
	.size	.L.str.12, 27

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Resoluc\303\263n vertical: %u\n"
	.size	.L.str.13, 25

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"Colores en paleta: %d\n"
	.size	.L.str.14, 23

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"Colores importantes: %d\n"
	.size	.L.str.15, 25

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"w+"
	.size	.L.str.16, 3

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z12escalaGrisesPhS_ii"
	.size	.L__unnamed_1, 23

	.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 _Z27__device_stub__escalaGrisesPhS_ii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z12escalaGrisesPhS_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z12escalaGrisesPhS_ii  ; -- Begin function _Z12escalaGrisesPhS_ii
	.globl	_Z12escalaGrisesPhS_ii
	.p2align	8
	.type	_Z12escalaGrisesPhS_ii,@function
_Z12escalaGrisesPhS_ii:                 ; @_Z12escalaGrisesPhS_ii
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x24
	s_load_b64 s[2:3], s[0:1], 0x10
	v_and_b32_e32 v2, 0x3ff, v0
	v_bfe_u32 v3, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s5, s4, 16
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[0:1], null, s14, s4, v[2:3]
	v_mad_u64_u32 v[1:2], null, s15, s5, v[3:4]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_gt_i32_e32 vcc_lo, s3, v0
	v_cmp_gt_i32_e64 s2, s2, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, vcc_lo, s2
	s_and_saveexec_b32 s4, s2
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b128 s[4:7], s[0:1], 0x0
	v_mad_u64_u32 v[2:3], null, v1, s3, v[0:1]
	s_mov_b32 s0, 0x1eb851ec
	s_mov_b32 s1, 0x3fb1eb85
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshl_add_u32 v0, v2, 1, v2
	v_ashrrev_i32_e32 v1, 31, v0
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s4, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s5, v1, vcc_lo
	s_clause 0x1
	global_load_u16 v3, v[0:1], off
	global_load_u8 v0, v[0:1], off offset:2
	s_waitcnt vmcnt(1)
	v_cvt_f32_ubyte1_e32 v1, v3
	s_waitcnt vmcnt(0)
	v_cvt_f32_ubyte0_e32 v0, v0
	v_and_b32_e32 v3, 0xff, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v1, 0x3f35c28f, v1
	v_and_b32_e32 v3, 0xffff, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fmamk_f32 v0, v0, 0x3e570a3d, v1
	v_cvt_f64_u32_e32 v[3:4], v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_f64_f32_e32 v[0:1], v0
	v_fma_f64 v[0:1], v[3:4], s[0:1], v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_cvt_i32_f64_e32 v3, v[0:1]
	v_ashrrev_i32_e32 v1, 31, v2
	v_add_co_u32 v0, vcc_lo, s6, v2
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v1, vcc_lo
	global_store_b8 v[0:1], v3, off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z12escalaGrisesPhS_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 5
		.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	_Z12escalaGrisesPhS_ii, .Lfunc_end0-_Z12escalaGrisesPhS_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 312
; NumSgprs: 18
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 5
; 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:           _Z12escalaGrisesPhS_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z12escalaGrisesPhS_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     5
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
