#include <stdio.h>
#include <cuda.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

typedef struct
{
    unsigned char red, green, blue;
} PPMPixel;

typedef struct
{
    unsigned char gray;
} PGMPixel;

typedef struct
{
    int x, y;
    PPMPixel *data;
} PPMImage;

typedef struct
{
    int x, y;
    PGMPixel *data;
} PGMImage;

#define CREATOR "V"
#define RGB_COMPONENT_COLOR 255

static PPMImage *readPPM(const char *filename)
{

    char buff[16];
    PPMImage *img;
    FILE *fp;
    int c, rgb_comp_color;
    //open PPM file for reading
    fp = fopen(filename, "rb");
    if (!fp)
    {
        fprintf(stderr, "Unable to open file '%s'\n", filename);
        exit(1);
    }

    //read image format
    if (!fgets(buff, sizeof(buff), fp))
    {
        perror(filename);
        exit(1);
    }

    //check the image format
    if (buff[0] != 'P' || buff[1] != '6')
    {
        fprintf(stderr, "Invalid image format (must be 'P6')\n");
        exit(1);
    }

    //alloc memory form image
    img = (PPMImage *)malloc(sizeof(PPMImage));
    if (!img)
    {
        fprintf(stderr, "Unable to allocate memory\n");
        exit(1);
    }

    //check for comments
    c = getc(fp);
    while (c == '#')
    {
        while (getc(fp) != '\n')
            ;
        c = getc(fp);
    }

    ungetc(c, fp);
    //read image size information
    if (fscanf(fp, "%d %d", &img->x, &img->y) != 2)
    {
        fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
        exit(1);
    }
    //read rgb component
    if (fscanf(fp, "%d", &rgb_comp_color) != 1)
    {
        fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
        exit(1);
    }

    //check rgb component depth
    if (rgb_comp_color != RGB_COMPONENT_COLOR)
    {
        fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
        exit(1);
    }

    while (fgetc(fp) != '\n')
        ;
    //memory allocation for pixel data
    img->data = (PPMPixel *)malloc(img->x * img->y * sizeof(PPMPixel));

    if (!img)
    {
        fprintf(stderr, "Unable to allocate memory\n");
        exit(1);
    }

    //read pixel data from file
    if (fread(img->data, 3 * img->x, img->y, fp) != img->y)
    {
        fprintf(stderr, "Error loading image '%s'\n", filename);
        exit(1);
    }
    fclose(fp);
    return img;
}
void writePGM(const char *filename, PGMImage *gry)
{
    FILE *fp;
    //open file for output
    fp = fopen(filename, "wb");
    if (!fp)
    {
        fprintf(stderr, "Unable to open file '%s'\n", filename);
        exit(1);
    }

    //write the header file
    //image format
    fprintf(fp, "P5\n");

    //comments
    fprintf(fp, "# Created by %s\n", CREATOR);
    //image size
    fprintf(fp, "%d %d\n", gry->x, gry->y);
    // rgb component depth
    fprintf(fp, "%d\n", RGB_COMPONENT_COLOR);

    // pixel data
    fwrite(gry->data, gry->x, gry->y, fp);
    fclose(fp);
}

static PGMImage *changeColorPPM(PPMImage *img, double *ttt)
{
    PGMImage *gry;
    int i, grayValue;
    double graymapval;
    if (img)
    {
        gry = (PGMImage *)malloc(sizeof(PGMImage));
        gry->x = img->x;
        gry->y = img->y;
        gry->data = (PGMPixel *)malloc(gry->x * gry->y * sizeof(PGMPixel));
        clock_t start = clock();

        for (i = 0; i < img->x * img->y; i++)
        {
            graymapval = 0.3 * (img->data[i].red) + 0.59 * (img->data[i].green) + 0.11 * (img->data[i].blue);
            grayValue = (unsigned char)(((unsigned int)graymapval) % (RGB_COMPONENT_COLOR + 1));
            gry->data[i].gray = grayValue;
        }
        clock_t end = clock();
        *ttt = (((double)(end - start)) / CLOCKS_PER_SEC) * 1000;
    }
    return gry;
}

__global__ void toGRAY(unsigned char *R, unsigned char *G, unsigned char *B, unsigned char *GRAY, long long n)
{
    int tid = blockIdx.x * blockDim.x + threadIdx.x;
    if (tid < n)
    {
        GRAY[tid] = (unsigned char)(((unsigned int)(0.3 * (R[tid]) + 0.59 * (G[tid]) + 0.11 * (B[tid]))) % (256));
    }
}

int main()
{
    char nameppm[] = "JX0.ppm";
    char namepgm[] = "JX0.pgm";
    double serialTime[10];
    printf("Serial Computation Time: \n");
    printf("I\tTime\n");
    for (int i = 0; i <= 9; i++)
    {
        char str[10];
        sprintf(str, "%d", i);
        nameppm[1] = i + '0';
        namepgm[1] = i + '0';
        PPMImage *image;
        PGMImage *grayImage;
        image = readPPM(nameppm);
        double ttt;
        grayImage = changeColorPPM(image, &ttt);
        printf("%d\t%f\n", i, ttt);
        serialTime[i] = ttt;
        writePGM(namepgm, grayImage);
        free(image->data);
        free(image);
        free(grayImage->data);
        free(grayImage);
    }
    long long len;
    long long lenb, mx, mn;
    mx = pow(2, 10);
    mn = pow(2, 0);
    lenb = mn;
    double getTime[11][12];
    getTime[0][0] = 0;
    int ii = 0;
    for (; lenb <= mx; lenb *= 2)
    {

        for (int i = 0; i <= 9; i++)
        {
            unsigned char *R, *G, *B, *GRAY, *dR, *dG, *dB, *dGRAY;
            char str[10];
            sprintf(str, "%d", i);
            nameppm[1] = i + '0';
            namepgm[1] = i + '0';
            PPMImage *img;
            PGMImage *gry;

            img = readPPM(nameppm);
            len = (img->x * img->y);
            gry = (PGMImage *)malloc(sizeof(PGMImage));
            gry->x = img->x;
            gry->y = img->y;
            gry->data = (PGMPixel *)malloc(gry->x * gry->y * sizeof(PGMPixel));

            R = (unsigned char *)malloc(len * sizeof(unsigned char));
            G = (unsigned char *)malloc(len * sizeof(unsigned char));
            B = (unsigned char *)malloc(len * sizeof(unsigned char));
            GRAY = (unsigned char *)malloc(len * sizeof(unsigned char));
            cudaMalloc((void **)&dR, len * sizeof(unsigned char));
            cudaMalloc((void **)&dG, len * sizeof(unsigned char));
            cudaMalloc((void **)&dB, len * sizeof(unsigned char));
            cudaMalloc((void **)&dGRAY, len * sizeof(unsigned char));
            for (int j = 0; j < len; j++)
            {
                R[j] = img->data[j].red;
                G[j] = img->data[j].green;
                B[j] = img->data[j].blue;
            }
            cudaMemcpy(dR, R, len * sizeof(unsigned char), cudaMemcpyHostToDevice);
            cudaMemcpy(dG, G, len * sizeof(unsigned char), cudaMemcpyHostToDevice);
            cudaMemcpy(dB, B, len * sizeof(unsigned char), cudaMemcpyHostToDevice);
            int blockSize = lenb;
            int numBlocks = (len + blockSize - 1) / blockSize;
            cudaEvent_t start, stop;
            cudaEventCreate(&start);
            cudaEventCreate(&stop);
            cudaEventRecord(start);

            toGRAY<<<numBlocks, blockSize>>>(dR, dG, dB, dGRAY, len);

            cudaEventRecord(stop);
            cudaEventSynchronize(stop);
            float tot_time = 0;
            cudaEventElapsedTime(&tot_time, start, stop);
            getTime[i + 1][0] = i;
            getTime[0][ii + 1] = lenb;
            getTime[i + 1][ii + 1] = tot_time; //FOR PARALLEL TIME GRAPH
            //getTime[i+1][ii+1] = (serialTime[i]/tot_time);  //FOR SPEEDUP GRAPH

            cudaMemcpy(GRAY, dGRAY, len * sizeof(unsigned char), cudaMemcpyDeviceToHost);
            for (int j = 0; j < len; j++)
            {
                gry->data[j].gray = GRAY[j];
            }
            writePGM(namepgm, gry);
            free(R);
            free(G);
            free(B);
            free(GRAY);
            cudaFree(dR);
            cudaFree(dG);
            cudaFree(dB);
            cudaFree(dGRAY);
            free(img->data);
            free(img);
            free(gry->data);
            free(gry);
        }
        ii++;
    }
    printf("Parallel Computation Time: \n");
    for (int i = 0; i < 11; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            if (j == 0)
            {
                printf("%.0f", getTime[i][j]);
            }
            else if (i == 0)
            {
                printf(",%.0f", getTime[i][j]);
            }
            else
                printf(",%f", getTime[i][j]);
        }
        printf("\n");
    }
    printf("Speed Up: \n");
    for (int i = 0; i < 11; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            if (j == 0)
            {
                printf("%.0f", getTime[i][j]);
            }
            else if (i == 0)
            {
                printf(",%.0f", getTime[i][j]);
            }
            else
                printf(",%f", (serialTime[i - 1] / (double)getTime[i][j]));
        }
        printf("\n");
    }
    return 0;
}

	.file	"tmpxft_0033b913_00000000-6_PPM2PGM_Parallel.cudafe1.cpp"
	.text
#APP

	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"rb"
.LC1:
	.string	"Unable to open file '%s'\n"
.LC2:
	.string	"Invalid image format (must be 'P6')\n"
.LC3:
	.string	"Unable to allocate memory\n"
.LC4:
	.string	"%d %d"
.LC5:
	.string	"Invalid image size (error loading '%s')\n"
.LC6:
	.string	"%d"
.LC7:
	.string	"Invalid rgb component (error loading '%s')\n"
.LC8:
	.string	"'%s' does not have 8-bits components\n"
.LC9:
	.string	"Error loading image '%s'\n"
#NO_APP
	.text
	.type	_ZL7readPPMPKc, @function
_ZL7readPPMPKc:
.LFB2027:
	.cfi_startproc
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC0(%rip), %rsi
	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
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movq	%r12, %rcx
	leaq	.LC1(%rip), %rdx
	testq	%rax, %rax
	je	.L22
	leaq	8(%rsp), %rdi
	movq	%rax, %rcx
	movl	$16, %edx
	movq	%rax, %rbx
	movl	$16, %esi
	call	__fgets_chk@PLT
	testq	%rax, %rax
	jne	.L3
	movq	%r12, %rdi
	call	perror@PLT
.L21:
	movl	$1, %edi
	call	exit@PLT
.L3:
	cmpb	$80, 8(%rsp)
	jne	.L4
	cmpb	$54, 9(%rsp)
	je	.L5
.L4:
	leaq	.LC2(%rip), %rdx
	jmp	.L23
.L5:
	movl	$16, %edi
	call	malloc@PLT
	movq	%rax, %rbp
	testq	%rax, %rax
	jne	.L20
	leaq	.LC3(%rip), %rdx
.L23:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L21
.L8:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$10, %eax
	jne	.L8
.L20:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$35, %eax
	je	.L8
	movq	%rbx, %rsi
	movl	%eax, %edi
	call	ungetc@PLT
	leaq	4(%rbp), %rcx
	movq	%rbp, %rdx
	movq	%rbx, %rdi
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movq	%r12, %rcx
	leaq	.LC5(%rip), %rdx
	cmpl	$2, %eax
	jne	.L22
	leaq	4(%rsp), %rdx
	xorl	%eax, %eax
	leaq	.LC6(%rip), %rsi
	movq	%rbx, %rdi
	call	__isoc23_fscanf@PLT
	movq	%r12, %rcx
	leaq	.LC7(%rip), %rdx
	decl	%eax
	jne	.L22
	cmpl	$255, 4(%rsp)
	leaq	.LC8(%rip), %rdx
	jne	.L22
.L11:
	movq	%rbx, %rdi
	call	fgetc@PLT
	cmpl	$10, %eax
	jne	.L11
	movl	0(%rbp), %r14d
	movl	4(%rbp), %r15d
	movl	%r14d, %r13d
	imull	%r15d, %r13d
	movslq	%r13d, %r13
	leaq	0(%r13,%r13,2), %r13
	movq	%r13, %rdi
	call	malloc@PLT
	leal	(%r14,%r14,2), %edx
	movslq	%r15d, %rcx
	movq	%rbx, %r8
	movq	%rax, 8(%rbp)
	movslq	%edx, %rdx
	movq	%rax, %rdi
	movq	%r13, %rsi
	call	__fread_chk@PLT
	movslq	4(%rbp), %rdx
	cmpq	%rax, %rdx
	je	.L12
	movq	%r12, %rcx
	leaq	.LC9(%rip), %rdx
.L22:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L21
.L12:
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	movq	%rbp, %rax
	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
.LFE2027:
	.size	_ZL7readPPMPKc, .-_ZL7readPPMPKc
	.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
.LC10:
	.string	"wb"
.LC11:
	.string	"P5\n"
.LC12:
	.string	"V"
.LC13:
	.string	"# Created by %s\n"
.LC14:
	.string	"%d %d\n"
.LC15:
	.string	"%d\n"
	.text
	.globl	_Z8writePGMPKcP8PGMImage
	.type	_Z8writePGMPKcP8PGMImage, @function
_Z8writePGMPKcP8PGMImage:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rsi, %rbp
	leaq	.LC10(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	call	fopen@PLT
	testq	%rax, %rax
	jne	.L27
	movq	stderr(%rip), %rdi
	movq	%r12, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC1(%rip), %rdx
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L27:
	movq	%rax, %rbx
	movq	%rax, %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC11(%rip), %rdx
	call	__fprintf_chk@PLT
	movq	%rbx, %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC12(%rip), %rcx
	leaq	.LC13(%rip), %rdx
	call	__fprintf_chk@PLT
	movl	4(%rbp), %r8d
	movl	0(%rbp), %ecx
	movq	%rbx, %rdi
	leaq	.LC14(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	%rbx, %rdi
	movl	$255, %ecx
	xorl	%eax, %eax
	leaq	.LC15(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movq	8(%rbp), %rdi
	movslq	4(%rbp), %rdx
	movq	%rbx, %rcx
	movslq	0(%rbp), %rsi
	call	fwrite@PLT
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	fclose@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z8writePGMPKcP8PGMImage, .-_Z8writePGMPKcP8PGMImage
	.globl	_Z32__device_stub__Z6toGRAYPhS_S_S_xPhS_S_S_x
	.type	_Z32__device_stub__Z6toGRAYPhS_S_S_xPhS_S_S_x, @function
_Z32__device_stub__Z6toGRAYPhS_S_S_xPhS_S_S_x:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L29
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z6toGRAYPhS_S_S_x(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 192
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L29:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L31
	call	__stack_chk_fail@PLT
.L31:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z32__device_stub__Z6toGRAYPhS_S_S_xPhS_S_S_x, .-_Z32__device_stub__Z6toGRAYPhS_S_S_xPhS_S_S_x
	.globl	_Z6toGRAYPhS_S_S_x
	.type	_Z6toGRAYPhS_S_S_x, @function
_Z6toGRAYPhS_S_S_x:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z32__device_stub__Z6toGRAYPhS_S_S_xPhS_S_S_x
	.cfi_endproc
.LFE2057:
	.size	_Z6toGRAYPhS_S_S_x, .-_Z6toGRAYPhS_S_S_x
	.section	.rodata.str1.1
.LC16:
	.string	"Serial Computation Time: \n"
.LC17:
	.string	"I\tTime\n"
.LC23:
	.string	"%d\t%f\n"
.LC26:
	.string	"Parallel Computation Time: \n"
.LC27:
	.string	"%.0f"
.LC28:
	.string	",%.0f"
.LC29:
	.string	",%f"
.LC30:
	.string	"\n"
.LC31:
	.string	"Speed Up: \n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	leaq	.LC6(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$1336, %rsp
	.cfi_def_cfa_offset 1392
	movq	%fs:40, %rax
	movq	%rax, 1320(%rsp)
	movabsq	$30804399575029834, %rax
	movq	%rax, 1294(%rsp)
	movabsq	$30794503970379850, %rax
	movq	%rax, 1302(%rsp)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L38:
	leaq	1310(%rsp), %rdi
	movl	%r12d, %r8d
	movq	%r14, %rcx
	xorl	%eax, %eax
	movl	$10, %edx
	movl	$2, %esi
	movl	%r12d, (%rsp)
	call	__sprintf_chk@PLT
	leal	48(%r12), %eax
	leaq	1294(%rsp), %rdi
	movb	%al, 1295(%rsp)
	movb	%al, 1303(%rsp)
	call	_ZL7readPPMPKc
	movq	%rax, %rbx
	testq	%rax, %rax
	je	.L35
	movl	$16, %edi
	call	malloc@PLT
	movl	(%rbx), %edi
	movq	%rax, %rbp
	movl	%edi, (%rax)
	movl	4(%rbx), %eax
	imull	%eax, %edi
	movl	%eax, 4(%rbp)
	movslq	%edi, %rdi
	call	malloc@PLT
	movq	%rax, 8(%rbp)
	movq	%rax, %r15
	call	clock@PLT
	movl	(%rbx), %esi
	imull	4(%rbx), %esi
	movsd	.LC18(%rip), %xmm2
	movsd	.LC19(%rip), %xmm3
	movq	%rax, %r13
	xorl	%eax, %eax
	movsd	.LC20(%rip), %xmm4
.L36:
	cmpl	%eax, %esi
	jle	.L70
	leaq	(%rax,%rax,2), %rdx
	addq	8(%rbx), %rdx
	movzbl	(%rdx), %edi
	cvtsi2sdl	%edi, %xmm0
	movzbl	1(%rdx), %edi
	movzbl	2(%rdx), %edx
	cvtsi2sdl	%edi, %xmm1
	mulsd	%xmm2, %xmm0
	mulsd	%xmm3, %xmm1
	addsd	%xmm1, %xmm0
	cvtsi2sdl	%edx, %xmm1
	mulsd	%xmm4, %xmm1
	addsd	%xmm1, %xmm0
	cvttsd2siq	%xmm0, %rdx
	movb	%dl, (%r15,%rax)
	incq	%rax
	jmp	.L36
.L70:
	call	clock@PLT
	subq	%r13, %rax
	cvtsi2sdq	%rax, %xmm0
	divsd	.LC21(%rip), %xmm0
	mulsd	.LC22(%rip), %xmm0
	movq	%xmm0, %r15
.L35:
	movl	(%rsp), %edx
	movq	%r15, %xmm0
	movl	$2, %edi
	movb	$1, %al
	leaq	.LC23(%rip), %rsi
	call	__printf_chk@PLT
	movq	%rbp, %rsi
	leaq	1302(%rsp), %rdi
	movq	%r15, 152(%rsp,%r12,8)
	call	_Z8writePGMPKcP8PGMImage
	movq	8(%rbx), %rdi
	incq	%r12
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	8(%rbp), %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	cmpq	$10, %r12
	jne	.L38
	movq	$0x000000000, 232(%rsp)
	xorl	%r14d, %r14d
	movl	$1, %r12d
.L39:
	leaq	232(%rsp), %rbp
	movslq	%r12d, %rax
	xorl	%r13d, %r13d
	movq	%rbp, 64(%rsp)
	movq	%rax, 72(%rsp)
.L45:
	movl	%r13d, %r8d
	movl	$2, %esi
	movl	$10, %edx
	xorl	%eax, %eax
	leaq	.LC6(%rip), %rcx
	leaq	1310(%rsp), %rdi
	call	__sprintf_chk@PLT
	leal	48(%r13), %eax
	leaq	1294(%rsp), %rdi
	movb	%al, 1295(%rsp)
	movb	%al, 1303(%rsp)
	call	_ZL7readPPMPKc
	movl	$16, %edi
	movl	(%rax), %edx
	movl	4(%rax), %r15d
	movq	%rax, %rbx
	movl	%edx, %eax
	movl	%edx, 24(%rsp)
	imull	%r15d, %eax
	movl	%eax, 20(%rsp)
	cltq
	movq	%rax, (%rsp)
	call	malloc@PLT
	movl	24(%rsp), %edx
	movq	(%rsp), %rdi
	movl	%r15d, 4(%rax)
	movl	%edx, (%rax)
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	movq	(%rsp), %rdi
	movq	%rax, 24(%rsp)
	movq	8(%rsp), %rax
	movq	24(%rsp), %rcx
	movq	%rcx, 8(%rax)
	call	malloc@PLT
	movq	(%rsp), %rdi
	movq	%rax, 32(%rsp)
	call	malloc@PLT
	movq	(%rsp), %rdi
	movq	%rax, 40(%rsp)
	call	malloc@PLT
	movq	(%rsp), %rdi
	movq	%rax, 48(%rsp)
	call	malloc@PLT
	movq	(%rsp), %rsi
	leaq	80(%rsp), %rdi
	movq	%rax, 56(%rsp)
	call	cudaMalloc@PLT
	movq	(%rsp), %rsi
	leaq	88(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	(%rsp), %rsi
	leaq	96(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	(%rsp), %rsi
	leaq	104(%rsp), %rdi
	call	cudaMalloc@PLT
	xorl	%eax, %eax
.L40:
	cmpl	%eax, 20(%rsp)
	jle	.L71
	movq	8(%rbx), %rcx
	leaq	(%rax,%rax,2), %rdx
	movq	32(%rsp), %rsi
	movb	(%rcx,%rdx), %cl
	movb	%cl, (%rsi,%rax)
	movq	8(%rbx), %rcx
	movq	40(%rsp), %rsi
	movb	1(%rcx,%rdx), %cl
	movb	%cl, (%rsi,%rax)
	movq	8(%rbx), %rcx
	movb	2(%rcx,%rdx), %dl
	movq	48(%rsp), %rcx
	movb	%dl, (%rcx,%rax)
	incq	%rax
	jmp	.L40
.L71:
	movq	(%rsp), %rdx
	movq	32(%rsp), %rsi
	movl	$1, %ecx
	movq	80(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	(%rsp), %rdx
	movq	40(%rsp), %rsi
	movl	$1, %ecx
	movq	88(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	48(%rsp), %rsi
	movq	(%rsp), %rdx
	movl	$1, %ecx
	movq	96(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	(%rsp), %rcx
	movq	72(%rsp), %rax
	leaq	112(%rsp), %rdi
	leaq	-1(%rax,%rcx), %rax
	cqto
	idivq	72(%rsp)
	movq	%rax, %r15
	call	cudaEventCreate@PLT
	leaq	120(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	112(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	%r12d, 140(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	movl	%r15d, 128(%rsp)
	movq	%rax, 144(%rsp)
	movl	148(%rsp), %ecx
	movq	%rax, 132(%rsp)
	movq	140(%rsp), %rdx
	movq	128(%rsp), %rdi
	movl	136(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L42
	movq	(%rsp), %r8
	movq	104(%rsp), %rcx
	movq	96(%rsp), %rdx
	movq	88(%rsp), %rsi
	movq	80(%rsp), %rdi
	call	_Z32__device_stub__Z6toGRAYPhS_S_S_xPhS_S_S_x
.L42:
	movq	120(%rsp), %rdi
	xorl	%esi, %esi
	leal	1(%r13), %r15d
	call	cudaEventRecord@PLT
	movq	120(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	120(%rsp), %rdx
	movq	112(%rsp), %rsi
	leaq	140(%rsp), %rdi
	movl	$0x00000000, 140(%rsp)
	call	cudaEventElapsedTime@PLT
	cvtsi2sdl	%r13d, %xmm0
	movq	64(%rsp), %rax
	movq	(%rsp), %rdx
	movq	104(%rsp), %rsi
	movq	56(%rsp), %rdi
	movl	$2, %ecx
	leaq	232(%rsp), %r13
	movsd	%xmm0, 96(%rax)
	cvtsi2sdq	%r12, %xmm0
	movsd	%xmm0, 240(%rsp,%r14,8)
	cvtss2sd	140(%rsp), %xmm0
	movsd	%xmm0, 104(%rax,%r14,8)
	call	cudaMemcpy@PLT
	xorl	%eax, %eax
.L43:
	cmpl	%eax, 20(%rsp)
	jle	.L72
	movq	56(%rsp), %rcx
	movb	(%rcx,%rax), %dl
	movq	24(%rsp), %rcx
	movb	%dl, (%rcx,%rax)
	incq	%rax
	jmp	.L43
.L72:
	movq	8(%rsp), %rsi
	leaq	1302(%rsp), %rdi
	call	_Z8writePGMPKcP8PGMImage
	movq	32(%rsp), %rdi
	call	free@PLT
	movq	40(%rsp), %rdi
	call	free@PLT
	movq	48(%rsp), %rdi
	call	free@PLT
	movq	56(%rsp), %rdi
	call	free@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	movq	96(%rsp), %rdi
	call	cudaFree@PLT
	movq	104(%rsp), %rdi
	call	cudaFree@PLT
	movq	8(%rbx), %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	24(%rsp), %rdi
	call	free@PLT
	movq	8(%rsp), %rdi
	call	free@PLT
	addq	$96, 64(%rsp)
	cmpl	$10, %r15d
	je	.L73
	movl	%r15d, %r13d
	jmp	.L45
.L73:
	incq	%r14
	addq	%r12, %r12
	cmpq	$11, %r14
	jne	.L39
	leaq	.LC26(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%ebx, %ebx
	call	__printf_chk@PLT
	movq	%rbp, %r12
	leaq	.LC29(%rip), %r15
.L47:
	xorl	%r14d, %r14d
.L53:
	testq	%r14, %r14
	jne	.L48
	movsd	(%r12), %xmm0
	leaq	.LC27(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movl	$1, %r14d
	call	__printf_chk@PLT
.L48:
	testl	%ebx, %ebx
	jne	.L50
	movsd	0(%r13,%r14,8), %xmm0
	leaq	.LC28(%rip), %rsi
	jmp	.L68
.L50:
	movsd	(%r12,%r14,8), %xmm0
	movq	%r15, %rsi
.L68:
	movl	$2, %edi
	movb	$1, %al
	incq	%r14
	call	__printf_chk@PLT
	cmpq	$12, %r14
	jne	.L53
	leaq	.LC30(%rip), %r14
	movl	$2, %edi
	xorl	%eax, %eax
	incl	%ebx
	movq	%r14, %rsi
	addq	$96, %r12
	call	__printf_chk@PLT
	cmpl	$11, %ebx
	jne	.L47
	leaq	.LC31(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%r12d, %r12d
	call	__printf_chk@PLT
	leaq	.LC29(%rip), %r15
.L54:
	xorl	%ebx, %ebx
.L60:
	testq	%rbx, %rbx
	jne	.L55
	movsd	0(%rbp), %xmm0
	leaq	.LC27(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movl	$1, %ebx
	call	__printf_chk@PLT
.L55:
	testq	%r12, %r12
	jne	.L57
	movsd	0(%r13,%rbx,8), %xmm0
	leaq	.LC28(%rip), %rsi
	jmp	.L69
.L57:
	movsd	144(%rsp,%r12,8), %xmm0
	movq	%r15, %rsi
	divsd	0(%rbp,%rbx,8), %xmm0
.L69:
	movl	$2, %edi
	movb	$1, %al
	incq	%rbx
	call	__printf_chk@PLT
	cmpq	$12, %rbx
	jne	.L60
	movq	%r14, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%r12
	call	__printf_chk@PLT
	addq	$96, %rbp
	cmpq	$11, %r12
	jne	.L54
	movq	1320(%rsp), %rax
	subq	%fs:40, %rax
	je	.L61
	call	__stack_chk_fail@PLT
.L61:
	addq	$1336, %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
.LFE2030:
	.size	main, .-main
	.section	.rodata.str1.1
.LC32:
	.string	"_Z6toGRAYPhS_S_S_x"
	.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	.LC32(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z6toGRAYPhS_S_S_x(%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.cst8,"aM",@progbits,8
	.align 8
.LC18:
	.long	858993459
	.long	1070805811
	.align 8
.LC19:
	.long	-1374389535
	.long	1071833415
	.align 8
.LC20:
	.long	-1030792151
	.long	1069295861
	.align 8
.LC21:
	.long	0
	.long	1093567616
	.align 8
.LC22:
	.long	0
	.long	1083129856
	.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 <stdio.h>
#include <hip/hip_runtime.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

typedef struct
{
    unsigned char red, green, blue;
} PPMPixel;

typedef struct
{
    unsigned char gray;
} PGMPixel;

typedef struct
{
    int x, y;
    PPMPixel *data;
} PPMImage;

typedef struct
{
    int x, y;
    PGMPixel *data;
} PGMImage;

#define CREATOR "V"
#define RGB_COMPONENT_COLOR 255

static PPMImage *readPPM(const char *filename)
{

    char buff[16];
    PPMImage *img;
    FILE *fp;
    int c, rgb_comp_color;
    //open PPM file for reading
    fp = fopen(filename, "rb");
    if (!fp)
    {
        fprintf(stderr, "Unable to open file '%s'\n", filename);
        exit(1);
    }

    //read image format
    if (!fgets(buff, sizeof(buff), fp))
    {
        perror(filename);
        exit(1);
    }

    //check the image format
    if (buff[0] != 'P' || buff[1] != '6')
    {
        fprintf(stderr, "Invalid image format (must be 'P6')\n");
        exit(1);
    }

    //alloc memory form image
    img = (PPMImage *)malloc(sizeof(PPMImage));
    if (!img)
    {
        fprintf(stderr, "Unable to allocate memory\n");
        exit(1);
    }

    //check for comments
    c = getc(fp);
    while (c == '#')
    {
        while (getc(fp) != '\n')
            ;
        c = getc(fp);
    }

    ungetc(c, fp);
    //read image size information
    if (fscanf(fp, "%d %d", &img->x, &img->y) != 2)
    {
        fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
        exit(1);
    }
    //read rgb component
    if (fscanf(fp, "%d", &rgb_comp_color) != 1)
    {
        fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
        exit(1);
    }

    //check rgb component depth
    if (rgb_comp_color != RGB_COMPONENT_COLOR)
    {
        fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
        exit(1);
    }

    while (fgetc(fp) != '\n')
        ;
    //memory allocation for pixel data
    img->data = (PPMPixel *)malloc(img->x * img->y * sizeof(PPMPixel));

    if (!img)
    {
        fprintf(stderr, "Unable to allocate memory\n");
        exit(1);
    }

    //read pixel data from file
    if (fread(img->data, 3 * img->x, img->y, fp) != img->y)
    {
        fprintf(stderr, "Error loading image '%s'\n", filename);
        exit(1);
    }
    fclose(fp);
    return img;
}
void writePGM(const char *filename, PGMImage *gry)
{
    FILE *fp;
    //open file for output
    fp = fopen(filename, "wb");
    if (!fp)
    {
        fprintf(stderr, "Unable to open file '%s'\n", filename);
        exit(1);
    }

    //write the header file
    //image format
    fprintf(fp, "P5\n");

    //comments
    fprintf(fp, "# Created by %s\n", CREATOR);
    //image size
    fprintf(fp, "%d %d\n", gry->x, gry->y);
    // rgb component depth
    fprintf(fp, "%d\n", RGB_COMPONENT_COLOR);

    // pixel data
    fwrite(gry->data, gry->x, gry->y, fp);
    fclose(fp);
}

static PGMImage *changeColorPPM(PPMImage *img, double *ttt)
{
    PGMImage *gry;
    int i, grayValue;
    double graymapval;
    if (img)
    {
        gry = (PGMImage *)malloc(sizeof(PGMImage));
        gry->x = img->x;
        gry->y = img->y;
        gry->data = (PGMPixel *)malloc(gry->x * gry->y * sizeof(PGMPixel));
        clock_t start = clock();

        for (i = 0; i < img->x * img->y; i++)
        {
            graymapval = 0.3 * (img->data[i].red) + 0.59 * (img->data[i].green) + 0.11 * (img->data[i].blue);
            grayValue = (unsigned char)(((unsigned int)graymapval) % (RGB_COMPONENT_COLOR + 1));
            gry->data[i].gray = grayValue;
        }
        clock_t end = clock();
        *ttt = (((double)(end - start)) / CLOCKS_PER_SEC) * 1000;
    }
    return gry;
}

__global__ void toGRAY(unsigned char *R, unsigned char *G, unsigned char *B, unsigned char *GRAY, long long n)
{
    int tid = blockIdx.x * blockDim.x + threadIdx.x;
    if (tid < n)
    {
        GRAY[tid] = (unsigned char)(((unsigned int)(0.3 * (R[tid]) + 0.59 * (G[tid]) + 0.11 * (B[tid]))) % (256));
    }
}

int main()
{
    char nameppm[] = "JX0.ppm";
    char namepgm[] = "JX0.pgm";
    double serialTime[10];
    printf("Serial Computation Time: \n");
    printf("I\tTime\n");
    for (int i = 0; i <= 9; i++)
    {
        char str[10];
        sprintf(str, "%d", i);
        nameppm[1] = i + '0';
        namepgm[1] = i + '0';
        PPMImage *image;
        PGMImage *grayImage;
        image = readPPM(nameppm);
        double ttt;
        grayImage = changeColorPPM(image, &ttt);
        printf("%d\t%f\n", i, ttt);
        serialTime[i] = ttt;
        writePGM(namepgm, grayImage);
        free(image->data);
        free(image);
        free(grayImage->data);
        free(grayImage);
    }
    long long len;
    long long lenb, mx, mn;
    mx = pow(2, 10);
    mn = pow(2, 0);
    lenb = mn;
    double getTime[11][12];
    getTime[0][0] = 0;
    int ii = 0;
    for (; lenb <= mx; lenb *= 2)
    {

        for (int i = 0; i <= 9; i++)
        {
            unsigned char *R, *G, *B, *GRAY, *dR, *dG, *dB, *dGRAY;
            char str[10];
            sprintf(str, "%d", i);
            nameppm[1] = i + '0';
            namepgm[1] = i + '0';
            PPMImage *img;
            PGMImage *gry;

            img = readPPM(nameppm);
            len = (img->x * img->y);
            gry = (PGMImage *)malloc(sizeof(PGMImage));
            gry->x = img->x;
            gry->y = img->y;
            gry->data = (PGMPixel *)malloc(gry->x * gry->y * sizeof(PGMPixel));

            R = (unsigned char *)malloc(len * sizeof(unsigned char));
            G = (unsigned char *)malloc(len * sizeof(unsigned char));
            B = (unsigned char *)malloc(len * sizeof(unsigned char));
            GRAY = (unsigned char *)malloc(len * sizeof(unsigned char));
            hipMalloc((void **)&dR, len * sizeof(unsigned char));
            hipMalloc((void **)&dG, len * sizeof(unsigned char));
            hipMalloc((void **)&dB, len * sizeof(unsigned char));
            hipMalloc((void **)&dGRAY, len * sizeof(unsigned char));
            for (int j = 0; j < len; j++)
            {
                R[j] = img->data[j].red;
                G[j] = img->data[j].green;
                B[j] = img->data[j].blue;
            }
            hipMemcpy(dR, R, len * sizeof(unsigned char), hipMemcpyHostToDevice);
            hipMemcpy(dG, G, len * sizeof(unsigned char), hipMemcpyHostToDevice);
            hipMemcpy(dB, B, len * sizeof(unsigned char), hipMemcpyHostToDevice);
            int blockSize = lenb;
            int numBlocks = (len + blockSize - 1) / blockSize;
            hipEvent_t start, stop;
            hipEventCreate(&start);
            hipEventCreate(&stop);
            hipEventRecord(start);

            toGRAY<<<numBlocks, blockSize>>>(dR, dG, dB, dGRAY, len);

            hipEventRecord(stop);
            hipEventSynchronize(stop);
            float tot_time = 0;
            hipEventElapsedTime(&tot_time, start, stop);
            getTime[i + 1][0] = i;
            getTime[0][ii + 1] = lenb;
            getTime[i + 1][ii + 1] = tot_time; //FOR PARALLEL TIME GRAPH
            //getTime[i+1][ii+1] = (serialTime[i]/tot_time);  //FOR SPEEDUP GRAPH

            hipMemcpy(GRAY, dGRAY, len * sizeof(unsigned char), hipMemcpyDeviceToHost);
            for (int j = 0; j < len; j++)
            {
                gry->data[j].gray = GRAY[j];
            }
            writePGM(namepgm, gry);
            free(R);
            free(G);
            free(B);
            free(GRAY);
            hipFree(dR);
            hipFree(dG);
            hipFree(dB);
            hipFree(dGRAY);
            free(img->data);
            free(img);
            free(gry->data);
            free(gry);
        }
        ii++;
    }
    printf("Parallel Computation Time: \n");
    for (int i = 0; i < 11; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            if (j == 0)
            {
                printf("%.0f", getTime[i][j]);
            }
            else if (i == 0)
            {
                printf(",%.0f", getTime[i][j]);
            }
            else
                printf(",%f", getTime[i][j]);
        }
        printf("\n");
    }
    printf("Speed Up: \n");
    for (int i = 0; i < 11; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            if (j == 0)
            {
                printf("%.0f", getTime[i][j]);
            }
            else if (i == 0)
            {
                printf(",%.0f", getTime[i][j]);
            }
            else
                printf(",%f", (serialTime[i - 1] / (double)getTime[i][j]));
        }
        printf("\n");
    }
    return 0;
}

	.text
	.file	"PPM2PGM_Parallel.hip"
	.globl	_Z8writePGMPKcP8PGMImage        # -- Begin function _Z8writePGMPKcP8PGMImage
	.type	_Z8writePGMPKcP8PGMImage,@function
_Z8writePGMPKcP8PGMImage:               # @_Z8writePGMPKcP8PGMImage
	.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, %rbx
	movq	%rdi, %r15
	movl	$.L.str, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB0_1
# %bb.2:
	movq	%rax, %r14
	movl	$.L.str.2, %edi
	movl	$3, %esi
	movl	$1, %edx
	movq	%rax, %rcx
	callq	fwrite
	movl	$.L.str.3, %esi
	movl	$.L.str.4, %edx
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movl	(%rbx), %edx
	movl	4(%rbx), %ecx
	movl	$.L.str.5, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movl	$.L.str.6, %esi
	movq	%r14, %rdi
	movl	$255, %edx
	xorl	%eax, %eax
	callq	fprintf
	movq	8(%rbx), %rdi
	movslq	(%rbx), %rsi
	movslq	4(%rbx), %rdx
	movq	%r14, %rcx
	callq	fwrite
	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
.LBB0_1:
	.cfi_def_cfa_offset 32
	movq	stderr(%rip), %rdi
	movl	$.L.str.1, %esi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z8writePGMPKcP8PGMImage, .Lfunc_end0-_Z8writePGMPKcP8PGMImage
	.cfi_endproc
                                        # -- End function
	.globl	_Z21__device_stub__toGRAYPhS_S_S_x # -- Begin function _Z21__device_stub__toGRAYPhS_S_S_x
	.type	_Z21__device_stub__toGRAYPhS_S_S_x,@function
_Z21__device_stub__toGRAYPhS_S_S_x:     # @_Z21__device_stub__toGRAYPhS_S_S_x
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$144, %rsp
	.cfi_def_cfa_offset 192
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	24(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%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	$_Z6toGRAYPhS_S_S_x, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$160, %rsp
	.cfi_adjust_cfa_offset -160
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z21__device_stub__toGRAYPhS_S_S_x, .Lfunc_end1-_Z21__device_stub__toGRAYPhS_S_S_x
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI2_0:
	.quad	0x3fd3333333333333              # double 0.29999999999999999
.LCPI2_1:
	.quad	0x3fe2e147ae147ae1              # double 0.58999999999999997
.LCPI2_2:
	.quad	0x3fbc28f5c28f5c29              # double 0.11
.LCPI2_3:
	.quad	0x412e848000000000              # double 1.0E+6
.LCPI2_4:
	.quad	0x408f400000000000              # double 1000
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$1320, %rsp                     # imm = 0x528
	.cfi_def_cfa_offset 1376
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movabsq	$30804399575029834, %rax        # imm = 0x6D70702E30584A
	leaq	24(%rsp), %rcx
	movq	%rax, (%rcx)
	movabsq	$30794503970379850, %rax        # imm = 0x6D67702E30584A
	leaq	72(%rsp), %r14
	movq	%rax, (%r14)
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	xorl	%r15d, %r15d
.LBB2_1:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_3 Depth 2
	movl	$.L.str.9, %esi
	leaq	256(%rsp), %rdi
	movl	%r15d, %edx
	xorl	%eax, %eax
	callq	sprintf
	movl	%r15d, %eax
	orb	$48, %al
	movb	%al, 25(%rsp)
	movb	%al, 73(%rsp)
	leaq	24(%rsp), %rdi
	callq	_ZL7readPPMPKc
	movq	%rax, %r13
	movl	$16, %edi
	callq	malloc
	movq	%rax, %r12
	movl	(%r13), %eax
	movl	%eax, (%r12)
	movl	4(%r13), %ecx
	movl	%ecx, 4(%r12)
	imull	%eax, %ecx
	movslq	%ecx, %rdi
	callq	malloc
	movq	%rax, %rbp
	movq	%rax, 8(%r12)
	callq	clock
	movq	%rax, %rbx
	movl	4(%r13), %ecx
	imull	(%r13), %ecx
	testl	%ecx, %ecx
	movsd	.LCPI2_0(%rip), %xmm2           # xmm2 = mem[0],zero
	movsd	.LCPI2_1(%rip), %xmm3           # xmm3 = mem[0],zero
	movsd	.LCPI2_2(%rip), %xmm4           # xmm4 = mem[0],zero
	jle	.LBB2_4
# %bb.2:                                # %.lr.ph.i
                                        #   in Loop: Header=BB2_1 Depth=1
	movq	8(%r13), %rax
	movl	%ecx, %ecx
	addq	$2, %rax
	xorl	%edx, %edx
.LBB2_3:                                #   Parent Loop BB2_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movzbl	-2(%rax), %esi
	xorps	%xmm0, %xmm0
	cvtsi2sd	%esi, %xmm0
	mulsd	%xmm2, %xmm0
	movzbl	-1(%rax), %esi
	xorps	%xmm1, %xmm1
	cvtsi2sd	%esi, %xmm1
	mulsd	%xmm3, %xmm1
	addsd	%xmm0, %xmm1
	movzbl	(%rax), %esi
	xorps	%xmm0, %xmm0
	cvtsi2sd	%esi, %xmm0
	mulsd	%xmm4, %xmm0
	addsd	%xmm1, %xmm0
	cvttsd2si	%xmm0, %rsi
	movb	%sil, (%rbp,%rdx)
	incq	%rdx
	addq	$3, %rax
	cmpq	%rdx, %rcx
	jne	.LBB2_3
.LBB2_4:                                # %_ZL14changeColorPPMP8PPMImagePd.exit
                                        #   in Loop: Header=BB2_1 Depth=1
	callq	clock
	subq	%rbx, %rax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI2_3(%rip), %xmm0
	mulsd	.LCPI2_4(%rip), %xmm0
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movl	$.L.str.10, %edi
	movl	%r15d, %esi
	movb	$1, %al
	callq	printf
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movsd	%xmm0, 176(%rsp,%r15,8)
	movq	%r14, %rdi
	movq	%r12, %rsi
	callq	_Z8writePGMPKcP8PGMImage
	movq	8(%r13), %rdi
	callq	free
	movq	%r13, %rdi
	callq	free
	movq	%rbp, %rdi
	callq	free
	movq	%r12, %rdi
	callq	free
	incq	%r15
	cmpq	$10, %r15
	jne	.LBB2_1
# %bb.5:
	movq	$0, 256(%rsp)
	movl	$1, %ecx
	xorl	%esi, %esi
.LBB2_6:                                # %.preheader141
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_7 Depth 2
                                        #       Child Loop BB2_9 Depth 3
	movslq	%ecx, %rax
	leaq	-1(%rax), %rdx
	movq	%rdx, 128(%rsp)                 # 8-byte Spill
	movq	%rax, 136(%rsp)                 # 8-byte Spill
	movl	%eax, %edx
	movabsq	$4294967296, %rax               # imm = 0x100000000
	orq	%rax, %rdx
	movq	%rdx, 120(%rsp)                 # 8-byte Spill
	movq	%rcx, 104(%rsp)                 # 8-byte Spill
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rcx, %xmm0
	movsd	%xmm0, 112(%rsp)                # 8-byte Spill
	incq	%rsi
	xorl	%r14d, %r14d
	movq	%rsi, 80(%rsp)                  # 8-byte Spill
.LBB2_7:                                #   Parent Loop BB2_6 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB2_9 Depth 3
	movl	$.L.str.9, %esi
	leaq	166(%rsp), %rdi
	movl	%r14d, %edx
	xorl	%eax, %eax
	callq	sprintf
	movq	%r14, 8(%rsp)                   # 8-byte Spill
	movl	%r14d, %eax
	orb	$48, %al
	movb	%al, 25(%rsp)
	movb	%al, 73(%rsp)
	leaq	24(%rsp), %rdi
	callq	_ZL7readPPMPKc
	movq	%rax, %r14
	movl	(%rax), %ebx
	movl	4(%rax), %r15d
	movl	%r15d, %eax
	imull	%ebx, %eax
	movl	%eax, 16(%rsp)                  # 4-byte Spill
	movslq	%eax, %rbp
	movl	$16, %edi
	callq	malloc
	movq	%rax, %r12
	movl	%ebx, (%rax)
	movl	%r15d, 4(%rax)
	movq	%rbp, %rdi
	callq	malloc
	movq	%r12, 152(%rsp)                 # 8-byte Spill
	movq	%rax, 88(%rsp)                  # 8-byte Spill
	movq	%rax, 8(%r12)
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, %r13
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, %r12
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, %r15
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, 144(%rsp)                 # 8-byte Spill
	leaq	64(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	leaq	56(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	leaq	48(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	leaq	40(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	testl	%ebp, %ebp
	jle	.LBB2_10
# %bb.8:                                # %.lr.ph
                                        #   in Loop: Header=BB2_7 Depth=2
	movl	16(%rsp), %eax                  # 4-byte Reload
	movl	$1, %ecx
	xorl	%edx, %edx
.LBB2_9:                                #   Parent Loop BB2_6 Depth=1
                                        #     Parent Loop BB2_7 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movq	8(%r14), %rsi
	movb	-1(%rsi,%rcx), %sil
	movb	%sil, (%r13,%rdx)
	movq	8(%r14), %rsi
	movb	(%rsi,%rcx), %sil
	movb	%sil, (%r12,%rdx)
	movq	8(%r14), %rsi
	movb	1(%rsi,%rcx), %sil
	movb	%sil, (%r15,%rdx)
	incq	%rdx
	addq	$3, %rcx
	cmpq	%rdx, %rax
	jne	.LBB2_9
.LBB2_10:                               # %._crit_edge
                                        #   in Loop: Header=BB2_7 Depth=2
	movq	64(%rsp), %rdi
	movq	%r13, %rsi
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	56(%rsp), %rdi
	movq	%r12, %rsi
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	48(%rsp), %rdi
	movq	%r15, %rsi
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	128(%rsp), %rax                 # 8-byte Reload
	addq	%rbp, %rax
	cqto
	idivq	136(%rsp)                       # 8-byte Folded Reload
	movq	%rax, %rbx
	leaq	96(%rsp), %rdi
	callq	hipEventCreate
	leaq	32(%rsp), %rdi
	callq	hipEventCreate
	movq	96(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	%ebx, %edi
	movabsq	$4294967296, %rax               # imm = 0x100000000
	orq	%rax, %rdi
	movl	$1, %esi
	movq	120(%rsp), %rdx                 # 8-byte Reload
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_12
# %bb.11:                               #   in Loop: Header=BB2_7 Depth=2
	movq	64(%rsp), %rdi
	movq	56(%rsp), %rsi
	movq	48(%rsp), %rdx
	movq	40(%rsp), %rcx
	movq	%rbp, %r8
	callq	_Z21__device_stub__toGRAYPhS_S_S_x
.LBB2_12:                               #   in Loop: Header=BB2_7 Depth=2
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	32(%rsp), %rdi
	callq	hipEventSynchronize
	movl	$0, 20(%rsp)
	movq	96(%rsp), %rsi
	movq	32(%rsp), %rdx
	leaq	20(%rsp), %rdi
	callq	hipEventElapsedTime
	movq	8(%rsp), %rax                   # 8-byte Reload
	cvtsi2sd	%eax, %xmm0
	incq	%rax
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	leaq	(%rax,%rax,2), %rax
	shlq	$5, %rax
	addq	%rsp, %rax
	addq	$256, %rax                      # imm = 0x100
	movsd	%xmm0, (%rax)
	movq	80(%rsp), %rcx                  # 8-byte Reload
	movsd	112(%rsp), %xmm0                # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movsd	%xmm0, 256(%rsp,%rcx,8)
	xorps	%xmm0, %xmm0
	cvtss2sd	20(%rsp), %xmm0
	movsd	%xmm0, (%rax,%rcx,8)
	movq	40(%rsp), %rsi
	movq	144(%rsp), %rbx                 # 8-byte Reload
	movq	%rbx, %rdi
	movq	%rbp, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%ebp, %ebp
	movq	88(%rsp), %rbp                  # 8-byte Reload
	jle	.LBB2_14
# %bb.13:                               # %.lr.ph146.preheader
                                        #   in Loop: Header=BB2_7 Depth=2
	movl	16(%rsp), %edx                  # 4-byte Reload
	movq	%rbp, %rdi
	movq	%rbx, %rsi
	callq	memcpy@PLT
.LBB2_14:                               # %._crit_edge147
                                        #   in Loop: Header=BB2_7 Depth=2
	leaq	72(%rsp), %rdi
	movq	152(%rsp), %rbp                 # 8-byte Reload
	movq	%rbp, %rsi
	callq	_Z8writePGMPKcP8PGMImage
	movq	%r13, %rdi
	callq	free
	movq	%r12, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	%rbx, %rdi
	callq	free
	movq	64(%rsp), %rdi
	callq	hipFree
	movq	56(%rsp), %rdi
	callq	hipFree
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	8(%r14), %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	88(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%rbp, %rdi
	callq	free
	movq	8(%rsp), %r14                   # 8-byte Reload
	cmpq	$10, %r14
	movq	80(%rsp), %rsi                  # 8-byte Reload
	jne	.LBB2_7
# %bb.15:                               #   in Loop: Header=BB2_6 Depth=1
	movq	104(%rsp), %rcx                 # 8-byte Reload
	addq	%rcx, %rcx
	cmpq	$11, %rsi
	jne	.LBB2_6
# %bb.16:
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	xorl	%ebx, %ebx
	leaq	256(%rsp), %r13
.LBB2_17:                               # %.preheader140
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_18 Depth 2
	leaq	(%rbx,%rbx,2), %rax
	shlq	$5, %rax
	leaq	(%rsp,%rax), %r14
	addq	$256, %r14                      # imm = 0x100
	xorl	%r15d, %r15d
	xorl	%r12d, %r12d
.LBB2_18:                               #   Parent Loop BB2_17 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	testq	%r15, %r15
	je	.LBB2_19
# %bb.25:                               #   in Loop: Header=BB2_18 Depth=2
	testq	%rbx, %rbx
	je	.LBB2_26
# %bb.27:                               #   in Loop: Header=BB2_18 Depth=2
	leaq	(%r15,%r13), %rax
	movl	$.L.str.14, %edi
	jmp	.LBB2_28
.LBB2_19:                               #   in Loop: Header=BB2_18 Depth=2
	movl	$.L.str.12, %edi
	movq	%r14, %rax
	jmp	.LBB2_28
.LBB2_26:                               #   in Loop: Header=BB2_18 Depth=2
	leaq	(%rsp,%r12,8), %rax
	addq	$256, %rax                      # imm = 0x100
	movl	$.L.str.13, %edi
.LBB2_28:                               #   in Loop: Header=BB2_18 Depth=2
	movsd	(%rax), %xmm0                   # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	incq	%r12
	addq	$8, %r15
	cmpq	$96, %r15
	jne	.LBB2_18
# %bb.20:                               #   in Loop: Header=BB2_17 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%rbx
	addq	$96, %r13
	cmpq	$11, %rbx
	jne	.LBB2_17
# %bb.21:
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	leaq	256(%rsp), %rbx
	xorl	%r14d, %r14d
.LBB2_22:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_23 Depth 2
	leaq	(%r14,%r14,2), %rax
	shlq	$5, %rax
	leaq	(%rsp,%rax), %r15
	addq	$256, %r15                      # imm = 0x100
	xorl	%r12d, %r12d
.LBB2_23:                               #   Parent Loop BB2_22 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	testq	%r12, %r12
	je	.LBB2_24
# %bb.31:                               #   in Loop: Header=BB2_23 Depth=2
	testq	%r14, %r14
	je	.LBB2_32
# %bb.33:                               #   in Loop: Header=BB2_23 Depth=2
	movsd	168(%rsp,%r14,8), %xmm0         # xmm0 = mem[0],zero
	divsd	(%rbx,%r12,8), %xmm0
	movl	$.L.str.14, %edi
	jmp	.LBB2_34
.LBB2_24:                               #   in Loop: Header=BB2_23 Depth=2
	movsd	(%r15), %xmm0                   # xmm0 = mem[0],zero
	movl	$.L.str.12, %edi
	jmp	.LBB2_34
.LBB2_32:                               #   in Loop: Header=BB2_23 Depth=2
	movsd	256(%rsp,%r12,8), %xmm0         # xmm0 = mem[0],zero
	movl	$.L.str.13, %edi
.LBB2_34:                               #   in Loop: Header=BB2_23 Depth=2
	movb	$1, %al
	callq	printf
	incq	%r12
	cmpq	$12, %r12
	jne	.LBB2_23
# %bb.29:                               #   in Loop: Header=BB2_22 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r14
	addq	$96, %rbx
	cmpq	$11, %r14
	jne	.LBB2_22
# %bb.30:
	xorl	%eax, %eax
	addq	$1320, %rsp                     # imm = 0x528
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.type	_ZL7readPPMPKc,@function        # -- Begin function _ZL7readPPMPKc
_ZL7readPPMPKc:                         # @_ZL7readPPMPKc
	.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	$32, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movl	$.L.str.17, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB3_14
# %bb.1:
	movq	%rax, %r14
	leaq	16(%rsp), %rdi
	movl	$16, %esi
	movq	%rax, %rdx
	callq	fgets
	testq	%rax, %rax
	je	.LBB3_15
# %bb.2:
	cmpb	$80, 16(%rsp)
	jne	.LBB3_16
# %bb.3:
	cmpb	$54, 17(%rsp)
	jne	.LBB3_16
# %bb.4:
	movl	$16, %edi
	callq	malloc
	testq	%rax, %rax
	je	.LBB3_17
# %bb.5:
	movq	%rax, %r15
.LBB3_6:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_7 Depth 2
	movq	%r14, %rdi
	callq	getc
	cmpl	$35, %eax
	jne	.LBB3_8
.LBB3_7:                                # %.preheader44
                                        #   Parent Loop BB3_6 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%r14, %rdi
	callq	getc
	cmpl	$10, %eax
	jne	.LBB3_7
	jmp	.LBB3_6
.LBB3_8:                                # %._crit_edge
	movl	%eax, %edi
	movq	%r14, %rsi
	callq	ungetc
	movq	%r15, %rcx
	addq	$4, %rcx
	movl	$.L.str.20, %esi
	movq	%r14, %rdi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$2, %eax
	jne	.LBB3_20
# %bb.9:
	leaq	12(%rsp), %rdx
	movl	$.L.str.9, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$1, %eax
	jne	.LBB3_21
# %bb.10:
	cmpl	$255, 12(%rsp)
	jne	.LBB3_22
.LBB3_11:                               # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	callq	fgetc
	cmpl	$10, %eax
	jne	.LBB3_11
# %bb.12:
	movslq	(%r15), %rax
	movslq	4(%r15), %r12
	leaq	(%rax,%rax,2), %r13
	movq	%r13, %rdi
	imulq	%r12, %rdi
	callq	malloc
	movq	%rax, 8(%r15)
	movq	%rax, %rdi
	movq	%r13, %rsi
	movq	%r12, %rdx
	movq	%r14, %rcx
	callq	fread
	movslq	4(%r15), %rcx
	cmpq	%rcx, %rax
	jne	.LBB3_23
# %bb.13:
	movq	%r14, %rdi
	callq	fclose
	movq	%r15, %rax
	addq	$32, %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
.LBB3_14:
	.cfi_def_cfa_offset 80
	movq	stderr(%rip), %rdi
	movl	$.L.str.1, %esi
	jmp	.LBB3_24
.LBB3_15:
	movq	%rbx, %rdi
	callq	perror
	jmp	.LBB3_19
.LBB3_16:
	movq	stderr(%rip), %rcx
	movl	$.L.str.18, %edi
	movl	$36, %esi
	jmp	.LBB3_18
.LBB3_17:
	movq	stderr(%rip), %rcx
	movl	$.L.str.19, %edi
	movl	$26, %esi
.LBB3_18:
	movl	$1, %edx
	callq	fwrite
	jmp	.LBB3_19
.LBB3_20:
	movq	stderr(%rip), %rdi
	movl	$.L.str.21, %esi
	jmp	.LBB3_24
.LBB3_21:
	movq	stderr(%rip), %rdi
	movl	$.L.str.22, %esi
	jmp	.LBB3_24
.LBB3_22:
	movq	stderr(%rip), %rdi
	movl	$.L.str.23, %esi
	jmp	.LBB3_24
.LBB3_23:
	movq	stderr(%rip), %rdi
	movl	$.L.str.24, %esi
.LBB3_24:
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	fprintf
.LBB3_19:
	movl	$1, %edi
	callq	exit
.Lfunc_end3:
	.size	_ZL7readPPMPKc, .Lfunc_end3-_ZL7readPPMPKc
	.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	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z6toGRAYPhS_S_S_x, %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_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB5_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"wb"
	.size	.L.str, 3

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Unable to open file '%s'\n"
	.size	.L.str.1, 26

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"P5\n"
	.size	.L.str.2, 4

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"# Created by %s\n"
	.size	.L.str.3, 17

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

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

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

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

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

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

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"%.0f"
	.size	.L.str.12, 5

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	",%.0f"
	.size	.L.str.13, 6

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	",%f"
	.size	.L.str.14, 4

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"rb"
	.size	.L.str.17, 3

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"Invalid image format (must be 'P6')\n"
	.size	.L.str.18, 37

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"Unable to allocate memory\n"
	.size	.L.str.19, 27

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"%d %d"
	.size	.L.str.20, 6

	.type	.L.str.21,@object               # @.str.21
.L.str.21:
	.asciz	"Invalid image size (error loading '%s')\n"
	.size	.L.str.21, 41

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"Invalid rgb component (error loading '%s')\n"
	.size	.L.str.22, 44

	.type	.L.str.23,@object               # @.str.23
.L.str.23:
	.asciz	"'%s' does not have 8-bits components\n"
	.size	.L.str.23, 38

	.type	.L.str.24,@object               # @.str.24
.L.str.24:
	.asciz	"Error loading image '%s'\n"
	.size	.L.str.24, 26

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z6toGRAYPhS_S_S_x"
	.size	.L__unnamed_1, 19

	.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	"Serial Computation Time: "
	.size	.Lstr, 26

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"I\tTime"
	.size	.Lstr.1, 7

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Parallel Computation Time: "
	.size	.Lstr.2, 28

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"Speed Up: "
	.size	.Lstr.3, 11

	.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__toGRAYPhS_S_S_x
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z6toGRAYPhS_S_S_x
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z6toGRAYPhS_S_S_x      ; -- Begin function _Z6toGRAYPhS_S_S_x
	.globl	_Z6toGRAYPhS_S_S_x
	.p2align	8
	.type	_Z6toGRAYPhS_S_S_x,@function
_Z6toGRAYPhS_S_S_x:                     ; @_Z6toGRAYPhS_S_S_x
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x34
	s_load_b64 s[2:3], s[0:1], 0x20
	s_waitcnt lgkmcnt(0)
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s4, v[0:1]
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_gt_i64_e32 vcc_lo, s[2:3], v[1:2]
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b256 s[0:7], s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v3, vcc_lo, s2, v1
	v_add_co_ci_u32_e32 v4, vcc_lo, s3, v2, vcc_lo
	global_load_u8 v0, v[3:4], off
	v_add_co_u32 v3, vcc_lo, s0, v1
	v_add_co_ci_u32_e32 v4, vcc_lo, s1, v2, vcc_lo
	s_mov_b32 s0, 0xae147ae1
	s_mov_b32 s1, 0x3fe2e147
	global_load_u8 v5, v[3:4], off
	v_add_co_u32 v3, vcc_lo, s4, v1
	v_add_co_ci_u32_e32 v4, vcc_lo, s5, v2, vcc_lo
	global_load_u8 v7, v[3:4], off
	s_waitcnt vmcnt(2)
	v_cvt_f64_u32_e32 v[3:4], v0
	v_add_co_u32 v0, vcc_lo, s6, v1
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v2, vcc_lo
	s_waitcnt vmcnt(1)
	v_cvt_f64_u32_e32 v[5:6], v5
	s_delay_alu instid0(VALU_DEP_4)
	v_mul_f64 v[3:4], v[3:4], s[0:1]
	s_mov_b32 s0, 0x33333333
	s_mov_b32 s1, 0x3fd33333
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_fma_f64 v[3:4], v[5:6], s[0:1], v[3:4]
	s_waitcnt vmcnt(0)
	v_cvt_f64_u32_e32 v[5:6], v7
	s_mov_b32 s0, 0xc28f5c29
	s_mov_b32 s1, 0x3fbc28f5
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_fma_f64 v[3:4], v[5:6], s[0:1], v[3:4]
	s_delay_alu instid0(VALU_DEP_1)
	v_cvt_u32_f64_e32 v3, v[3:4]
	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 _Z6toGRAYPhS_S_S_x
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 296
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 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	_Z6toGRAYPhS_S_S_x, .Lfunc_end0-_Z6toGRAYPhS_S_S_x
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 284
; 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: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           8
        .value_kind:     by_value
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         44
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         52
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         54
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         56
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         58
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         60
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         62
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         104
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 296
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z6toGRAYPhS_S_S_x
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z6toGRAYPhS_S_S_x.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
