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

int xpixels = 17500;
int ypixels = 10000;
int currentPixel = 0;		//counter for calculations
int totalPixels;			//the total number of pixels in the image 

double xmin;			// ranges for the calculating the mandelbrot set on
double xmax;			//	real-imaginary plane, where x is real and y
double ymin;			//	is imaginary
double ymax;			//

char outputFile[] = "imagecuda.ppm";		// output file

/*
 * complex_number_t
 * Replresents a complex number by storing separate doubles for real and
 * imaginary parts for the complex number.
*/
typedef struct complex_number_t{
    double real;
    double imag;
}complex_number_t;

/*
 * pixel_t
 * Represents a pixel in the PPM file by storing the RGB components.
*/
typedef struct pixel_t{
    int r;
    int g;
    int b;
} pixel_t;

/*convert_to_2d
 *Finds index in a 1D array given 2D indices
 *INPUTS
 *	x - column position
 *	y - row position
 *	scale - number of elements per row
 *OUTPUT
 *	long index in the 1D array
*/
unsigned int convert_to_2d(int x, int y, int scale) {
    return y * scale + x;
}

/*
 *calculate_mandlebrot_set
 *Determines if a given position on the complex plane diverages or not and
 *outputs the results to an  array.
 *INPUTS
 *  *pixelArray - poinjter to array representing output image
 *	xpixels - number of horizontal in output image
 *	ypixels - number or vertical pixels in output image
 *  xmin - smaller cartesian coordinate of the output image in x direction
 *	xmax - larger cartesian coordinate of the output image in x direction
 *  ymin - smaller cartesian coordinate of the output image in y direction
 *	ymax - larger cartesian coordinate of the output image in y direction
 *	
*/
__global__ void calculate_mandlebrot_set( pixel_t *pixelArray, int xpixels, int ypixels, double xmin, double xmax, double ymin, double ymax){
    int MAX_ITERATIONS = 1000;
    double xincrement = (xmax - xmin) / xpixels; // macro to find increments
    double yincrement = (ymax - ymin) / ypixels;

	complex_number_t z;
	z.real = 0;
	z.imag = 0;
	
	complex_number_t c;
	pixel_t *currPixel;

	if (threadIdx.x + blockDim.x * blockIdx.x < xpixels && threadIdx.y + blockDim.y * blockIdx.y < ypixels){
		int myIndex = (threadIdx.x + blockDim.x * blockIdx.x) +  xpixels* (threadIdx.y + blockDim.y * blockIdx.y);
		currPixel = pixelArray + myIndex;
		
		int x = threadIdx.x + blockDim.x * blockIdx.x;             // x posiiton of the pixel
		int y = threadIdx.y + blockDim.y * blockIdx.y;             // y position of the piel
		
		c.real = xmin + x * xincrement;
		c.imag = ymin + y * yincrement;
		
		int toBreak = 0;
		int i = 0;
		while( i < MAX_ITERATIONS ){
			complex_number_t temp;

			temp.real = z.real;
			temp.imag = z.imag;
			z.real = (z.real * z.real) - (z.imag * z.imag) + c.real; //real
			z.imag = (2*temp.real*temp.imag) + c.imag; // imaginary
			if(z.real*z.real + z.imag*z.imag > 4){
				//save colors to Pixel Array
				(*currPixel).r = (int) 255 * (((1.0*i) / (1.0 * MAX_ITERATIONS)));
				(*currPixel).g = (int) 255 * sqrt(((1.0*i) / (1.0 * MAX_ITERATIONS)));
				(*currPixel).b = (int) 50 * sqrt(1.0 - ((1.0*i) / (1.0 * MAX_ITERATIONS)));
				toBreak = 1;
				break; //end calculation for current pixel
			}
			i++;
		}
		// never escapes
		if(!toBreak){
			(*currPixel).r = 0;
			(*currPixel).g = 0;
			(*currPixel).b = 0;
		}
	}
}

/*
 *calcMandelbrotCuda
 *A helper function that launches calculate_mandlebrot_set kernel.
 *INPUTS
 *	*data - a pointer to and array of pixel_t for data output
*/
cudaError_t calcMandelbrotCuda(pixel_t *data ){
	cudaError_t cudaStatus;

    // int d_xpixels, d_ypixels;
    // double d_xmin, d_xmax, d_ymin, d_ymax;
	
	
	dim3 threadsPerBlock(32,32,1);
	dim3 numBlocks((xpixels - 1) / threadsPerBlock.x + 1, (ypixels - 1) / threadsPerBlock.y + 1, 1);
	
	cudaStatus = cudaSetDevice(0);
	if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?"); goto Error; }

    // d_xpixels = xpixels;
    // d_ypixels = ypixels;

    // d_xmin = xmin;
    // d_xmax = xmax;
    // d_ymin = ymin;
    // d_ymax = ymax;

	printf("threads.x = %d, threads.y = %d\n", threadsPerBlock.x, threadsPerBlock.y);
	printf("blocks.x = %d, blocks.y = %d\n", numBlocks.x, numBlocks.y);

	calculate_mandlebrot_set<<<numBlocks, threadsPerBlock>>>(data, xpixels, ypixels, xmin, xmax, ymin, ymax);
	cudaStatus = cudaGetLastError();
	if (cudaStatus != cudaSuccess) { fprintf(stderr, "calculate_mandlebrot_set launch failed: %s\n", cudaGetErrorString(cudaStatus)); goto Error; }
	
	cudaDeviceSynchronize();

    Error:
    return cudaStatus;
}

int main(int argc, char * argv[]){
    if (!(argc == 5 || argc == 2)){
        printf("ERROR: incorrect number of arguments. Run with argument --help for help.\n");
        return 0;
    }
    
    if (argc == 2){
        // char *strHelp = "--help";
        if (!strcmp(argv[1], "--help")){
            printf("USAGE: CompiledMandelbrot [xmin] [xmax] [ymin] [ymax]\n");
        }
        else{
            printf("ERROR: Unrecognized argument. Run with argument --help for help.\n");
        }
        return 0;
    }
	    
	//set range of mandlebrot set from arguments
    xmin = atof(argv[1]);
    xmax = atof(argv[2]);
    ymin = atof(argv[3]);
    ymax = atof(argv[4]);
    
    if (ymin >= ymax){
        printf("ERROR: Invalid range for imaginary axis.\n");
        return 0;
    }
    
    if (xmin >= xmax){
        printf("ERROR: Invalid range for real axis.\n");
        return 0;
    }
    
	//initialize some values
	totalPixels	= xpixels * ypixels;
	
	//initialize output file with PPm header
    FILE *fp;
    fp = fopen(outputFile, "w+");
    fprintf(fp, "P3 \n%d %d \n255\n\n", xpixels, ypixels);
    fclose(fp);



	//varaibles for timing
    struct timespec start, finish;
    double elapsed;

    printf("Timing Calculations...\n");
    clock_gettime(CLOCK_MONOTONIC, &start);

	


	//do cuda here

    pixel_t *pixelArray = (pixel_t*)(malloc(xpixels*ypixels*sizeof(pixel_t)));  //for cudaMemcpy version
    pixel_t *d_pixelArray;  //for cudaMemcpy version

    // pixel_t *pixelArray;  //for cudaMallocManaged version

	cudaMalloc(&d_pixelArray, totalPixels * sizeof(pixel_t));  //for cudaMemcpy version
	// cudaMallocManaged(&pixelArray, totalPixels * sizeof(pixel_t)); //for culaMallocManaged version

	// cudaError_t cudaStatus = calcMandelbrotCuda(pixelArray);  //for cudaMallocManaged version
    cudaError_t cudaStatus = calcMandelbrotCuda(d_pixelArray); //for cudaMemcpy version

    //calculate elapsed time
    clock_gettime(CLOCK_MONOTONIC, &finish);
    elapsed = (finish.tv_sec - start.tv_sec);
    elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
    printf("Calculations took %f seconds\n", elapsed);

    printf("\nTiming cudaMemCpy...\n");  //for cudaMemcpy version
    clock_gettime(CLOCK_MONOTONIC, &start);  //for cudaMemcpy version
    cudaMemcpy(pixelArray, d_pixelArray, totalPixels*sizeof(pixel_t), cudaMemcpyDeviceToHost); //for cudaMemcpy version
    clock_gettime(CLOCK_MONOTONIC, &finish);  //for cudaMemcpy version
    elapsed = (finish.tv_sec - start.tv_sec);  //for cudaMemcpy version
    elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;  //for cudaMemcpy version
    printf("cudaMemCpy took %f seconds\n", elapsed);  //for cudaMemcpy version


	//start file output timing
    printf("\nStarting file output...\n");
    clock_gettime(CLOCK_MONOTONIC, &start);
	
    fp = fopen(outputFile, "a");
    int x;
    int y;

    for (y = 0; y < ypixels; y++){
        for (x = 0; x < xpixels; x++){
            pixel_t* currPixel;
            currPixel = pixelArray + convert_to_2d(x, y, xpixels);		//get one pixel at a time
            fprintf(fp, " %d %d %d    ",(*currPixel).r,(*currPixel).g,(*currPixel).b);		//outpit pixel in PPM format
			
        }
        fprintf(fp, "\n");		//next line in PPM file

	}
    fclose(fp);
    //stop timing
    clock_gettime(CLOCK_MONOTONIC, &finish);
    elapsed = (finish.tv_sec - start.tv_sec);
    elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
    printf("File output took %f seconds.\n", elapsed );
    
	
	//convert the PPM file to a PNG file to save space
    execl("/usr/bin/convert", "/usr/bin/convert", "image.ppm", "image.png", (char *)NULL);
    

}
	.file	"tmpxft_002d22a3_00000000-6_mandelbrotcalculatorcuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2045:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2045:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z13convert_to_2diii
	.type	_Z13convert_to_2diii, @function
_Z13convert_to_2diii:
.LFB2040:
	.cfi_startproc
	endbr64
	imull	%edx, %esi
	leal	(%rsi,%rdi), %eax
	ret
	.cfi_endproc
.LFE2040:
	.size	_Z13convert_to_2diii, .-_Z13convert_to_2diii
	.globl	_Z57__device_stub__Z24calculate_mandlebrot_setP7pixel_tiiddddP7pixel_tiidddd
	.type	_Z57__device_stub__Z24calculate_mandlebrot_setP7pixel_tiiddddP7pixel_tiidddd, @function
_Z57__device_stub__Z24calculate_mandlebrot_setP7pixel_tiiddddP7pixel_tiidddd:
.LFB2067:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	56(%rsp), %rcx
	leaq	64(%rsp), %rdi
	movl	%esi, 36(%rsp)
	leaq	76(%rsp), %rsi
	movl	%edx, 32(%rsp)
	leaq	48(%rsp), %rdx
	movsd	%xmm0, 24(%rsp)
	movsd	%xmm1, 16(%rsp)
	movsd	%xmm2, 8(%rsp)
	movsd	%xmm3, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	36(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	movq	%rsp, %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L3
	pushq	56(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z24calculate_mandlebrot_setP7pixel_tiidddd(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 208
	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 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L3:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L5
	call	__stack_chk_fail@PLT
.L5:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2067:
	.size	_Z57__device_stub__Z24calculate_mandlebrot_setP7pixel_tiiddddP7pixel_tiidddd, .-_Z57__device_stub__Z24calculate_mandlebrot_setP7pixel_tiiddddP7pixel_tiidddd
	.globl	_Z24calculate_mandlebrot_setP7pixel_tiidddd
	.type	_Z24calculate_mandlebrot_setP7pixel_tiidddd, @function
_Z24calculate_mandlebrot_setP7pixel_tiidddd:
.LFB2068:
	.cfi_startproc
	endbr64
	jmp	_Z57__device_stub__Z24calculate_mandlebrot_setP7pixel_tiiddddP7pixel_tiidddd
	.cfi_endproc
.LFE2068:
	.size	_Z24calculate_mandlebrot_setP7pixel_tiidddd, .-_Z24calculate_mandlebrot_setP7pixel_tiidddd
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?"
.LC1:
	.string	"threads.x = %d, threads.y = %d\n"
.LC2:
	.string	"blocks.x = %d, blocks.y = %d\n"
.LC3:
	.string	"calculate_mandlebrot_set launch failed: %s\n"
	.text
	.globl	_Z18calcMandelbrotCudaP7pixel_t
	.type	_Z18calcMandelbrotCudaP7pixel_t, @function
_Z18calcMandelbrotCudaP7pixel_t:
.LFB2041:
	.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	%rdi, %r12
	xorl	%edi, %edi
	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
	movl	ypixels(%rip), %r13d
	movl	xpixels(%rip), %ebp
	movl	$1, 16(%rsp)
	movl	$1, 28(%rsp)
	call	cudaSetDevice@PLT
	testl	%eax, %eax
	je	.L10
	movq	stderr(%rip), %rdi
	movl	%eax, %ebx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC0(%rip), %rdx
	call	__fprintf_chk@PLT
	jmp	.L9
.L10:
	decl	%r13d
	decl	%ebp
	movl	$32, %ecx
	movl	$32, %edx
	shrl	$5, %r13d
	shrl	$5, %ebp
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	incl	%r13d
	incl	%ebp
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	%r13d, %ecx
	movl	%ebp, %edx
	movl	$2, %edi
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebp, 20(%rsp)
	movl	16(%rsp), %ecx
	xorl	%r9d, %r9d
	movl	%r13d, 24(%rsp)
	movl	28(%rsp), %esi
	xorl	%r8d, %r8d
	movabsq	$137438953504, %rdx
	movq	20(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L12
	movsd	ymax(%rip), %xmm3
	movsd	ymin(%rip), %xmm2
	movq	%r12, %rdi
	movsd	xmax(%rip), %xmm1
	movsd	xmin(%rip), %xmm0
	movl	ypixels(%rip), %edx
	movl	xpixels(%rip), %esi
	call	_Z57__device_stub__Z24calculate_mandlebrot_setP7pixel_tiiddddP7pixel_tiidddd
.L12:
	call	cudaGetLastError@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L13
	movl	%eax, %edi
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC3(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L9
.L13:
	call	cudaDeviceSynchronize@PLT
.L11:
.L9:
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	movl	%ebx, %eax
	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
.LFE2041:
	.size	_Z18calcMandelbrotCudaP7pixel_t, .-_Z18calcMandelbrotCudaP7pixel_t
	.section	.rodata.str1.1
.LC4:
	.string	"ERROR: incorrect number of arguments. Run with argument --help for help.\n"
.LC5:
	.string	"--help"
.LC6:
	.string	"USAGE: CompiledMandelbrot [xmin] [xmax] [ymin] [ymax]\n"
.LC7:
	.string	"ERROR: Unrecognized argument. Run with argument --help for help.\n"
.LC8:
	.string	"ERROR: Invalid range for imaginary axis.\n"
.LC9:
	.string	"ERROR: Invalid range for real axis.\n"
.LC10:
	.string	"w+"
.LC11:
	.string	"P3 \n%d %d \n255\n\n"
.LC12:
	.string	"Timing Calculations...\n"
.LC14:
	.string	"Calculations took %f seconds\n"
.LC15:
	.string	"\nTiming cudaMemCpy...\n"
.LC16:
	.string	"cudaMemCpy took %f seconds\n"
.LC17:
	.string	"\nStarting file output...\n"
.LC18:
	.string	"a"
.LC19:
	.string	" %d %d %d    "
.LC20:
	.string	"\n"
.LC21:
	.string	"File output took %f seconds.\n"
.LC22:
	.string	"image.png"
.LC23:
	.string	"image.ppm"
.LC24:
	.string	"/usr/bin/convert"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2042:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	cmpl	$5, %edi
	je	.L16
	leaq	.LC4(%rip), %rsi
	cmpl	$2, %edi
	jne	.L40
.L16:
	movq	8(%rbx), %rax
	cmpl	$2, %edi
	jne	.L18
	leaq	.LC5(%rip), %rsi
	movq	%rax, %rdi
	call	strcmp@PLT
	testl	%eax, %eax
	jne	.L19
	leaq	.LC6(%rip), %rsi
.L40:
	movl	$2, %edi
.L41:
	call	__printf_chk@PLT
	jmp	.L17
.L19:
	leaq	.LC7(%rip), %rsi
	jmp	.L42
.L18:
	movq	%rax, %rdi
	call	atof@PLT
	movq	16(%rbx), %rdi
	movsd	%xmm0, xmin(%rip)
	movsd	%xmm0, 24(%rsp)
	call	atof@PLT
	movq	24(%rbx), %rdi
	movsd	%xmm0, xmax(%rip)
	movsd	%xmm0, 16(%rsp)
	call	atof@PLT
	movq	32(%rbx), %rdi
	movsd	%xmm0, ymin(%rip)
	movsd	%xmm0, 8(%rsp)
	call	atof@PLT
	movsd	8(%rsp), %xmm3
	movsd	16(%rsp), %xmm1
	leaq	.LC8(%rip), %rsi
	movsd	24(%rsp), %xmm2
	movsd	%xmm0, ymax(%rip)
	comisd	%xmm0, %xmm3
	jnb	.L42
	comisd	%xmm1, %xmm2
	jb	.L38
	leaq	.LC9(%rip), %rsi
.L42:
	movl	$2, %edi
	xorl	%eax, %eax
	jmp	.L41
.L38:
	movl	xpixels(%rip), %eax
	imull	ypixels(%rip), %eax
	leaq	56(%rsp), %r12
	xorl	%r14d, %r14d
	leaq	outputFile(%rip), %r13
	leaq	.LC10(%rip), %rsi
	movq	%r13, %rdi
	movl	%eax, totalPixels(%rip)
	call	fopen@PLT
	movl	xpixels(%rip), %ecx
	movl	$2, %esi
	movl	ypixels(%rip), %r8d
	leaq	.LC11(%rip), %rdx
	movq	%rax, %rbx
	movq	%rax, %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	%rbx, %rdi
	leaq	40(%rsp), %rbx
	call	fclose@PLT
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbx, %rsi
	movl	$1, %edi
	call	clock_gettime@PLT
	movl	xpixels(%rip), %edi
	imull	ypixels(%rip), %edi
	movslq	%edi, %rdi
	imulq	$12, %rdi, %rdi
	call	malloc@PLT
	movslq	totalPixels(%rip), %rsi
	leaq	32(%rsp), %rdi
	movq	%rax, %rbp
	imulq	$12, %rsi, %rsi
	call	cudaMalloc@PLT
	movq	32(%rsp), %rdi
	call	_Z18calcMandelbrotCudaP7pixel_t
	movq	%r12, %rsi
	movl	$1, %edi
	call	clock_gettime@PLT
	movq	56(%rsp), %rax
	subq	40(%rsp), %rax
	leaq	.LC14(%rip), %rsi
	cvtsi2sdq	%rax, %xmm1
	movq	64(%rsp), %rax
	subq	48(%rsp), %rax
	movl	$2, %edi
	cvtsi2sdq	%rax, %xmm0
	divsd	.LC13(%rip), %xmm0
	movb	$1, %al
	addsd	%xmm1, %xmm0
	call	__printf_chk@PLT
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbx, %rsi
	movl	$1, %edi
	call	clock_gettime@PLT
	movslq	totalPixels(%rip), %rdx
	movq	32(%rsp), %rsi
	movq	%rbp, %rdi
	movl	$2, %ecx
	imulq	$12, %rdx, %rdx
	call	cudaMemcpy@PLT
	movq	%r12, %rsi
	movl	$1, %edi
	call	clock_gettime@PLT
	movq	56(%rsp), %rax
	subq	40(%rsp), %rax
	leaq	.LC16(%rip), %rsi
	cvtsi2sdq	%rax, %xmm1
	movq	64(%rsp), %rax
	subq	48(%rsp), %rax
	movl	$2, %edi
	cvtsi2sdq	%rax, %xmm0
	divsd	.LC13(%rip), %xmm0
	movb	$1, %al
	addsd	%xmm1, %xmm0
	call	__printf_chk@PLT
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbx, %rsi
	movl	$1, %edi
	call	clock_gettime@PLT
	movq	%r13, %rdi
	leaq	.LC18(%rip), %rsi
	leaq	.LC19(%rip), %r13
	call	fopen@PLT
	movq	%rax, %rbx
.L24:
	cmpl	%r14d, ypixels(%rip)
	jle	.L43
	xorl	%r15d, %r15d
.L26:
	movl	xpixels(%rip), %eax
	cmpl	%r15d, %eax
	jle	.L44
	imull	%r14d, %eax
	movq	%r13, %rdx
	movl	$2, %esi
	movq	%rbx, %rdi
	addl	%r15d, %eax
	incl	%r15d
	cltq
	imulq	$12, %rax, %rax
	addq	%rbp, %rax
	movl	8(%rax), %r9d
	movl	4(%rax), %r8d
	movl	(%rax), %ecx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L26
.L44:
	leaq	.LC20(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	incl	%r14d
	movl	$2, %esi
	call	__fprintf_chk@PLT
	jmp	.L24
.L43:
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	%r12, %rsi
	movl	$1, %edi
	call	clock_gettime@PLT
	movq	56(%rsp), %rax
	subq	40(%rsp), %rax
	leaq	.LC21(%rip), %rsi
	cvtsi2sdq	%rax, %xmm1
	movq	64(%rsp), %rax
	subq	48(%rsp), %rax
	movl	$2, %edi
	cvtsi2sdq	%rax, %xmm0
	divsd	.LC13(%rip), %xmm0
	movb	$1, %al
	addsd	%xmm1, %xmm0
	call	__printf_chk@PLT
	leaq	.LC24(%rip), %rdi
	xorl	%r8d, %r8d
	xorl	%eax, %eax
	leaq	.LC22(%rip), %rcx
	leaq	.LC23(%rip), %rdx
	movq	%rdi, %rsi
	call	execl@PLT
.L17:
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	addq	$88, %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
.LFE2042:
	.size	main, .-main
	.section	.rodata.str1.1
.LC25:
	.string	"_Z24calculate_mandlebrot_setP7pixel_tiidddd"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2070:
	.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	.LC25(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z24calculate_mandlebrot_setP7pixel_tiidddd(%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
.LFE2070:
	.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
	.globl	outputFile
	.data
	.align 8
	.type	outputFile, @object
	.size	outputFile, 14
outputFile:
	.string	"imagecuda.ppm"
	.globl	ymax
	.bss
	.align 8
	.type	ymax, @object
	.size	ymax, 8
ymax:
	.zero	8
	.globl	ymin
	.align 8
	.type	ymin, @object
	.size	ymin, 8
ymin:
	.zero	8
	.globl	xmax
	.align 8
	.type	xmax, @object
	.size	xmax, 8
xmax:
	.zero	8
	.globl	xmin
	.align 8
	.type	xmin, @object
	.size	xmin, 8
xmin:
	.zero	8
	.globl	totalPixels
	.align 4
	.type	totalPixels, @object
	.size	totalPixels, 4
totalPixels:
	.zero	4
	.globl	currentPixel
	.align 4
	.type	currentPixel, @object
	.size	currentPixel, 4
currentPixel:
	.zero	4
	.globl	ypixels
	.data
	.align 4
	.type	ypixels, @object
	.size	ypixels, 4
ypixels:
	.long	10000
	.globl	xpixels
	.align 4
	.type	xpixels, @object
	.size	xpixels, 4
xpixels:
	.long	17500
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC13:
	.long	0
	.long	1104006501
	.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 <unistd.h>
#include <math.h>
#include <time.h>
#include <string.h>

int xpixels = 17500;
int ypixels = 10000;
int currentPixel = 0;		//counter for calculations
int totalPixels;			//the total number of pixels in the image 

double xmin;			// ranges for the calculating the mandelbrot set on
double xmax;			//	real-imaginary plane, where x is real and y
double ymin;			//	is imaginary
double ymax;			//

char outputFile[] = "imagecuda.ppm";		// output file

/*
 * complex_number_t
 * Replresents a complex number by storing separate doubles for real and
 * imaginary parts for the complex number.
*/
typedef struct complex_number_t{
    double real;
    double imag;
}complex_number_t;

/*
 * pixel_t
 * Represents a pixel in the PPM file by storing the RGB components.
*/
typedef struct pixel_t{
    int r;
    int g;
    int b;
} pixel_t;

/*convert_to_2d
 *Finds index in a 1D array given 2D indices
 *INPUTS
 *	x - column position
 *	y - row position
 *	scale - number of elements per row
 *OUTPUT
 *	long index in the 1D array
*/
unsigned int convert_to_2d(int x, int y, int scale) {
    return y * scale + x;
}

/*
 *calculate_mandlebrot_set
 *Determines if a given position on the complex plane diverages or not and
 *outputs the results to an  array.
 *INPUTS
 *  *pixelArray - poinjter to array representing output image
 *	xpixels - number of horizontal in output image
 *	ypixels - number or vertical pixels in output image
 *  xmin - smaller cartesian coordinate of the output image in x direction
 *	xmax - larger cartesian coordinate of the output image in x direction
 *  ymin - smaller cartesian coordinate of the output image in y direction
 *	ymax - larger cartesian coordinate of the output image in y direction
 *	
*/
__global__ void calculate_mandlebrot_set( pixel_t *pixelArray, int xpixels, int ypixels, double xmin, double xmax, double ymin, double ymax){
    int MAX_ITERATIONS = 1000;
    double xincrement = (xmax - xmin) / xpixels; // macro to find increments
    double yincrement = (ymax - ymin) / ypixels;

	complex_number_t z;
	z.real = 0;
	z.imag = 0;
	
	complex_number_t c;
	pixel_t *currPixel;

	if (threadIdx.x + blockDim.x * blockIdx.x < xpixels && threadIdx.y + blockDim.y * blockIdx.y < ypixels){
		int myIndex = (threadIdx.x + blockDim.x * blockIdx.x) +  xpixels* (threadIdx.y + blockDim.y * blockIdx.y);
		currPixel = pixelArray + myIndex;
		
		int x = threadIdx.x + blockDim.x * blockIdx.x;             // x posiiton of the pixel
		int y = threadIdx.y + blockDim.y * blockIdx.y;             // y position of the piel
		
		c.real = xmin + x * xincrement;
		c.imag = ymin + y * yincrement;
		
		int toBreak = 0;
		int i = 0;
		while( i < MAX_ITERATIONS ){
			complex_number_t temp;

			temp.real = z.real;
			temp.imag = z.imag;
			z.real = (z.real * z.real) - (z.imag * z.imag) + c.real; //real
			z.imag = (2*temp.real*temp.imag) + c.imag; // imaginary
			if(z.real*z.real + z.imag*z.imag > 4){
				//save colors to Pixel Array
				(*currPixel).r = (int) 255 * (((1.0*i) / (1.0 * MAX_ITERATIONS)));
				(*currPixel).g = (int) 255 * sqrt(((1.0*i) / (1.0 * MAX_ITERATIONS)));
				(*currPixel).b = (int) 50 * sqrt(1.0 - ((1.0*i) / (1.0 * MAX_ITERATIONS)));
				toBreak = 1;
				break; //end calculation for current pixel
			}
			i++;
		}
		// never escapes
		if(!toBreak){
			(*currPixel).r = 0;
			(*currPixel).g = 0;
			(*currPixel).b = 0;
		}
	}
}

/*
 *calcMandelbrotCuda
 *A helper function that launches calculate_mandlebrot_set kernel.
 *INPUTS
 *	*data - a pointer to and array of pixel_t for data output
*/
hipError_t calcMandelbrotCuda(pixel_t *data ){
	hipError_t cudaStatus;

    // int d_xpixels, d_ypixels;
    // double d_xmin, d_xmax, d_ymin, d_ymax;
	
	
	dim3 threadsPerBlock(32,32,1);
	dim3 numBlocks((xpixels - 1) / threadsPerBlock.x + 1, (ypixels - 1) / threadsPerBlock.y + 1, 1);
	
	cudaStatus = hipSetDevice(0);
	if (cudaStatus != hipSuccess) { fprintf(stderr, "hipSetDevice failed!  Do you have a CUDA-capable GPU installed?"); goto Error; }

    // d_xpixels = xpixels;
    // d_ypixels = ypixels;

    // d_xmin = xmin;
    // d_xmax = xmax;
    // d_ymin = ymin;
    // d_ymax = ymax;

	printf("threads.x = %d, threads.y = %d\n", threadsPerBlock.x, threadsPerBlock.y);
	printf("blocks.x = %d, blocks.y = %d\n", numBlocks.x, numBlocks.y);

	calculate_mandlebrot_set<<<numBlocks, threadsPerBlock>>>(data, xpixels, ypixels, xmin, xmax, ymin, ymax);
	cudaStatus = hipGetLastError();
	if (cudaStatus != hipSuccess) { fprintf(stderr, "calculate_mandlebrot_set launch failed: %s\n", hipGetErrorString(cudaStatus)); goto Error; }
	
	hipDeviceSynchronize();

    Error:
    return cudaStatus;
}

int main(int argc, char * argv[]){
    if (!(argc == 5 || argc == 2)){
        printf("ERROR: incorrect number of arguments. Run with argument --help for help.\n");
        return 0;
    }
    
    if (argc == 2){
        // char *strHelp = "--help";
        if (!strcmp(argv[1], "--help")){
            printf("USAGE: CompiledMandelbrot [xmin] [xmax] [ymin] [ymax]\n");
        }
        else{
            printf("ERROR: Unrecognized argument. Run with argument --help for help.\n");
        }
        return 0;
    }
	    
	//set range of mandlebrot set from arguments
    xmin = atof(argv[1]);
    xmax = atof(argv[2]);
    ymin = atof(argv[3]);
    ymax = atof(argv[4]);
    
    if (ymin >= ymax){
        printf("ERROR: Invalid range for imaginary axis.\n");
        return 0;
    }
    
    if (xmin >= xmax){
        printf("ERROR: Invalid range for real axis.\n");
        return 0;
    }
    
	//initialize some values
	totalPixels	= xpixels * ypixels;
	
	//initialize output file with PPm header
    FILE *fp;
    fp = fopen(outputFile, "w+");
    fprintf(fp, "P3 \n%d %d \n255\n\n", xpixels, ypixels);
    fclose(fp);



	//varaibles for timing
    struct timespec start, finish;
    double elapsed;

    printf("Timing Calculations...\n");
    clock_gettime(CLOCK_MONOTONIC, &start);

	


	//do cuda here

    pixel_t *pixelArray = (pixel_t*)(malloc(xpixels*ypixels*sizeof(pixel_t)));  //for cudaMemcpy version
    pixel_t *d_pixelArray;  //for cudaMemcpy version

    // pixel_t *pixelArray;  //for cudaMallocManaged version

	hipMalloc(&d_pixelArray, totalPixels * sizeof(pixel_t));  //for cudaMemcpy version
	// cudaMallocManaged(&pixelArray, totalPixels * sizeof(pixel_t)); //for culaMallocManaged version

	// cudaError_t cudaStatus = calcMandelbrotCuda(pixelArray);  //for cudaMallocManaged version
    hipError_t cudaStatus = calcMandelbrotCuda(d_pixelArray); //for cudaMemcpy version

    //calculate elapsed time
    clock_gettime(CLOCK_MONOTONIC, &finish);
    elapsed = (finish.tv_sec - start.tv_sec);
    elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
    printf("Calculations took %f seconds\n", elapsed);

    printf("\nTiming cudaMemCpy...\n");  //for cudaMemcpy version
    clock_gettime(CLOCK_MONOTONIC, &start);  //for cudaMemcpy version
    hipMemcpy(pixelArray, d_pixelArray, totalPixels*sizeof(pixel_t), hipMemcpyDeviceToHost); //for cudaMemcpy version
    clock_gettime(CLOCK_MONOTONIC, &finish);  //for cudaMemcpy version
    elapsed = (finish.tv_sec - start.tv_sec);  //for cudaMemcpy version
    elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;  //for cudaMemcpy version
    printf("cudaMemCpy took %f seconds\n", elapsed);  //for cudaMemcpy version


	//start file output timing
    printf("\nStarting file output...\n");
    clock_gettime(CLOCK_MONOTONIC, &start);
	
    fp = fopen(outputFile, "a");
    int x;
    int y;

    for (y = 0; y < ypixels; y++){
        for (x = 0; x < xpixels; x++){
            pixel_t* currPixel;
            currPixel = pixelArray + convert_to_2d(x, y, xpixels);		//get one pixel at a time
            fprintf(fp, " %d %d %d    ",(*currPixel).r,(*currPixel).g,(*currPixel).b);		//outpit pixel in PPM format
			
        }
        fprintf(fp, "\n");		//next line in PPM file

	}
    fclose(fp);
    //stop timing
    clock_gettime(CLOCK_MONOTONIC, &finish);
    elapsed = (finish.tv_sec - start.tv_sec);
    elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
    printf("File output took %f seconds.\n", elapsed );
    
	
	//convert the PPM file to a PNG file to save space
    execl("/usr/bin/convert", "/usr/bin/convert", "image.ppm", "image.png", (char *)NULL);
    

}
	.text
	.file	"mandelbrotcalculatorcuda.hip"
	.globl	_Z13convert_to_2diii            # -- Begin function _Z13convert_to_2diii
	.type	_Z13convert_to_2diii,@function
_Z13convert_to_2diii:                   # @_Z13convert_to_2diii
	.cfi_startproc
# %bb.0:
                                        # kill: def $esi killed $esi def $rsi
                                        # kill: def $edi killed $edi def $rdi
	imull	%edx, %esi
	leal	(%rsi,%rdi), %eax
	retq
.Lfunc_end0:
	.size	_Z13convert_to_2diii, .Lfunc_end0-_Z13convert_to_2diii
	.cfi_endproc
                                        # -- End function
	.globl	_Z39__device_stub__calculate_mandlebrot_setP7pixel_tiidddd # -- Begin function _Z39__device_stub__calculate_mandlebrot_setP7pixel_tiidddd
	.type	_Z39__device_stub__calculate_mandlebrot_setP7pixel_tiidddd,@function
_Z39__device_stub__calculate_mandlebrot_setP7pixel_tiidddd: # @_Z39__device_stub__calculate_mandlebrot_setP7pixel_tiidddd
	.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	$160, %rsp
	.cfi_def_cfa_offset 208
	.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	4(%rsp), %rcx
	movl	%esi, (%rcx)
	movq	%rsp, %rsi
	movl	%edx, (%rsi)
	leaq	48(%rsp), %rdx
	movsd	%xmm0, (%rdx)
	leaq	40(%rsp), %rdi
	movsd	%xmm1, (%rdi)
	leaq	32(%rsp), %r8
	movsd	%xmm2, (%r8)
	leaq	24(%rsp), %r9
	movsd	%xmm3, (%r9)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rdi, 32(%rbx)
	movq	%r8, 40(%rbx)
	movq	%r9, 48(%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	$_Z24calculate_mandlebrot_setP7pixel_tiidddd, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$176, %rsp
	.cfi_adjust_cfa_offset -176
	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	_Z39__device_stub__calculate_mandlebrot_setP7pixel_tiidddd, .Lfunc_end1-_Z39__device_stub__calculate_mandlebrot_setP7pixel_tiidddd
	.cfi_endproc
                                        # -- End function
	.globl	_Z18calcMandelbrotCudaP7pixel_t # -- Begin function _Z18calcMandelbrotCudaP7pixel_t
	.type	_Z18calcMandelbrotCudaP7pixel_t,@function
_Z18calcMandelbrotCudaP7pixel_t:        # @_Z18calcMandelbrotCudaP7pixel_t
	.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	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdi, %rbx
	movl	xpixels(%rip), %r14d
	movl	ypixels(%rip), %r15d
	xorl	%edi, %edi
	callq	hipSetDevice
	testl	%eax, %eax
	jne	.LBB2_1
# %bb.2:
	decl	%r14d
	shrl	$5, %r14d
	incl	%r14d
	decl	%r15d
	shrl	$5, %r15d
	incl	%r15d
	movq	%r15, %r12
	shlq	$32, %r12
	orq	%r14, %r12
	movl	$.L.str.1, %edi
	movl	$32, %esi
	movl	$32, %edx
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.2, %edi
	movl	%r14d, %esi
	movl	%r15d, %edx
	xorl	%eax, %eax
	callq	printf
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movq	%r12, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_4
# %bb.3:
	movl	xpixels(%rip), %esi
	movl	ypixels(%rip), %edx
	movsd	xmin(%rip), %xmm0               # xmm0 = mem[0],zero
	movsd	xmax(%rip), %xmm1               # xmm1 = mem[0],zero
	movsd	ymin(%rip), %xmm2               # xmm2 = mem[0],zero
	movsd	ymax(%rip), %xmm3               # xmm3 = mem[0],zero
	movq	%rbx, %rdi
	callq	_Z39__device_stub__calculate_mandlebrot_setP7pixel_tiidddd
.LBB2_4:
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB2_5
# %bb.6:
	callq	hipDeviceSynchronize
	xorl	%ebp, %ebp
.LBB2_7:
	movl	%ebp, %eax
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.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
.LBB2_1:
	.cfi_def_cfa_offset 48
	movl	%eax, %ebp
	movq	stderr(%rip), %rcx
	movl	$.L.str, %edi
	movl	$63, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	jmp	.LBB2_7
.LBB2_5:
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.3, %esi
	movq	%rbx, %rdi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB2_7
.Lfunc_end2:
	.size	_Z18calcMandelbrotCudaP7pixel_t, .Lfunc_end2-_Z18calcMandelbrotCudaP7pixel_t
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI3_0:
	.quad	0x41cdcd6500000000              # double 1.0E+9
	.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	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$64, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	cmpl	$2, %edi
	je	.LBB3_11
# %bb.1:
	cmpl	$5, %edi
	jne	.LBB3_13
# %bb.2:
	movq	8(%rbx), %rdi
	callq	atof
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movsd	%xmm0, xmin(%rip)
	movq	16(%rbx), %rdi
	callq	atof
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	movsd	%xmm0, xmax(%rip)
	movq	24(%rbx), %rdi
	callq	atof
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	movsd	%xmm0, ymin(%rip)
	movq	32(%rbx), %rdi
	callq	atof
	movsd	%xmm0, ymax(%rip)
	movsd	16(%rsp), %xmm1                 # 8-byte Reload
                                        # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	jae	.LBB3_14
# %bb.3:
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	ucomisd	(%rsp), %xmm0                   # 8-byte Folded Reload
	jae	.LBB3_16
# %bb.4:
	movl	ypixels(%rip), %eax
	imull	xpixels(%rip), %eax
	movl	%eax, totalPixels(%rip)
	movl	$outputFile, %edi
	movl	$.L.str.10, %esi
	callq	fopen
	movq	%rax, %rbx
	movl	xpixels(%rip), %edx
	movl	ypixels(%rip), %ecx
	movl	$.L.str.11, %esi
	movq	%rax, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movq	%rbx, %rdi
	callq	fclose
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	leaq	24(%rsp), %r14
	movl	$1, %edi
	movq	%r14, %rsi
	callq	clock_gettime
	movslq	xpixels(%rip), %rax
	movslq	ypixels(%rip), %rcx
	imulq	%rax, %rcx
	shlq	$2, %rcx
	leaq	(%rcx,%rcx,2), %rdi
	callq	malloc
	movq	%rax, %rbx
	movslq	totalPixels(%rip), %rax
	shlq	$2, %rax
	leaq	(%rax,%rax,2), %rsi
	leaq	40(%rsp), %r12
	movq	%r12, %rdi
	callq	hipMalloc
	movq	(%r12), %rdi
	callq	_Z18calcMandelbrotCudaP7pixel_t
	leaq	48(%rsp), %r15
	movl	$1, %edi
	movq	%r15, %rsi
	callq	clock_gettime
	movq	(%r15), %rax
	movq	8(%r15), %rcx
	subq	(%r14), %rax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	subq	8(%r14), %rcx
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rcx, %xmm0
	divsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	movl	$.L.str.13, %edi
	movb	$1, %al
	callq	printf
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movl	$1, %edi
	movq	%r14, %rsi
	callq	clock_gettime
	movq	(%r12), %rsi
	movslq	totalPixels(%rip), %rax
	shlq	$2, %rax
	leaq	(%rax,%rax,2), %rdx
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movl	$1, %edi
	movq	%r15, %rsi
	callq	clock_gettime
	movq	(%r15), %rax
	movq	8(%r15), %rcx
	subq	(%r14), %rax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	subq	8(%r14), %rcx
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rcx, %xmm0
	divsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	movl	$.L.str.15, %edi
	movb	$1, %al
	callq	printf
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	$1, %edi
	movq	%r14, %rsi
	callq	clock_gettime
	movl	$outputFile, %edi
	movl	$.L.str.17, %esi
	callq	fopen
	movq	%rax, %r14
	cmpl	$0, ypixels(%rip)
	jle	.LBB3_10
# %bb.5:                                # %.preheader.preheader
	xorl	%ebp, %ebp
.LBB3_6:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_8 Depth 2
	movl	xpixels(%rip), %eax
	testl	%eax, %eax
	jle	.LBB3_9
# %bb.7:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB3_6 Depth=1
	xorl	%r15d, %r15d
.LBB3_8:                                # %.lr.ph
                                        #   Parent Loop BB3_6 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	imull	%ebp, %eax
	addl	%r15d, %eax
	leaq	(%rax,%rax,2), %rax
	movl	(%rbx,%rax,4), %edx
	movl	4(%rbx,%rax,4), %ecx
	movl	8(%rbx,%rax,4), %r8d
	movl	$.L.str.18, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	incl	%r15d
	movl	xpixels(%rip), %eax
	cmpl	%eax, %r15d
	jl	.LBB3_8
.LBB3_9:                                # %._crit_edge
                                        #   in Loop: Header=BB3_6 Depth=1
	movl	$10, %edi
	movq	%r14, %rsi
	callq	fputc@PLT
	incl	%ebp
	cmpl	ypixels(%rip), %ebp
	jl	.LBB3_6
.LBB3_10:                               # %._crit_edge42
	movq	%r14, %rdi
	callq	fclose
	leaq	48(%rsp), %rbx
	movl	$1, %edi
	movq	%rbx, %rsi
	callq	clock_gettime
	movq	(%rbx), %rax
	movq	8(%rbx), %rcx
	subq	24(%rsp), %rax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	subq	32(%rsp), %rcx
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rcx, %xmm0
	divsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	movl	$.L.str.20, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.21, %edi
	movl	$.L.str.21, %esi
	movl	$.L.str.22, %edx
	movl	$.L.str.23, %ecx
	xorl	%r8d, %r8d
	xorl	%eax, %eax
	callq	execl
	jmp	.LBB3_18
.LBB3_11:
	movq	8(%rbx), %rdi
	movl	$.L.str.5, %esi
	callq	strcmp
	testl	%eax, %eax
	je	.LBB3_15
# %bb.12:
	movl	$.Lstr.7, %edi
	jmp	.LBB3_17
.LBB3_13:
	movl	$.Lstr, %edi
	jmp	.LBB3_17
.LBB3_14:
	movl	$.Lstr.5, %edi
	jmp	.LBB3_17
.LBB3_15:
	movl	$.Lstr.6, %edi
	jmp	.LBB3_17
.LBB3_16:
	movl	$.Lstr.4, %edi
.LBB3_17:
	callq	puts@PLT
.LBB3_18:
	xorl	%eax, %eax
	addq	$64, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	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	$_Z24calculate_mandlebrot_setP7pixel_tiidddd, %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	xpixels,@object                 # @xpixels
	.data
	.globl	xpixels
	.p2align	2, 0x0
xpixels:
	.long	17500                           # 0x445c
	.size	xpixels, 4

	.type	ypixels,@object                 # @ypixels
	.globl	ypixels
	.p2align	2, 0x0
ypixels:
	.long	10000                           # 0x2710
	.size	ypixels, 4

	.type	currentPixel,@object            # @currentPixel
	.bss
	.globl	currentPixel
	.p2align	2, 0x0
currentPixel:
	.long	0                               # 0x0
	.size	currentPixel, 4

	.type	totalPixels,@object             # @totalPixels
	.globl	totalPixels
	.p2align	2, 0x0
totalPixels:
	.long	0                               # 0x0
	.size	totalPixels, 4

	.type	xmin,@object                    # @xmin
	.globl	xmin
	.p2align	3, 0x0
xmin:
	.quad	0x0000000000000000              # double 0
	.size	xmin, 8

	.type	xmax,@object                    # @xmax
	.globl	xmax
	.p2align	3, 0x0
xmax:
	.quad	0x0000000000000000              # double 0
	.size	xmax, 8

	.type	ymin,@object                    # @ymin
	.globl	ymin
	.p2align	3, 0x0
ymin:
	.quad	0x0000000000000000              # double 0
	.size	ymin, 8

	.type	ymax,@object                    # @ymax
	.globl	ymax
	.p2align	3, 0x0
ymax:
	.quad	0x0000000000000000              # double 0
	.size	ymax, 8

	.type	outputFile,@object              # @outputFile
	.data
	.globl	outputFile
outputFile:
	.asciz	"imagecuda.ppm"
	.size	outputFile, 14

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

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"hipSetDevice failed!  Do you have a CUDA-capable GPU installed?"
	.size	.L.str, 64

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"threads.x = %d, threads.y = %d\n"
	.size	.L.str.1, 32

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"blocks.x = %d, blocks.y = %d\n"
	.size	.L.str.2, 30

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"calculate_mandlebrot_set launch failed: %s\n"
	.size	.L.str.3, 44

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"--help"
	.size	.L.str.5, 7

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

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

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Calculations took %f seconds\n"
	.size	.L.str.13, 30

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"cudaMemCpy took %f seconds\n"
	.size	.L.str.15, 28

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"a"
	.size	.L.str.17, 2

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	" %d %d %d    "
	.size	.L.str.18, 14

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"File output took %f seconds.\n"
	.size	.L.str.20, 30

	.type	.L.str.21,@object               # @.str.21
.L.str.21:
	.asciz	"/usr/bin/convert"
	.size	.L.str.21, 17

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"image.ppm"
	.size	.L.str.22, 10

	.type	.L.str.23,@object               # @.str.23
.L.str.23:
	.asciz	"image.png"
	.size	.L.str.23, 10

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z24calculate_mandlebrot_setP7pixel_tiidddd"
	.size	.L__unnamed_1, 44

	.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	"ERROR: incorrect number of arguments. Run with argument --help for help."
	.size	.Lstr, 73

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Timing Calculations..."
	.size	.Lstr.1, 23

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"\nTiming cudaMemCpy..."
	.size	.Lstr.2, 22

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"\nStarting file output..."
	.size	.Lstr.3, 25

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"ERROR: Invalid range for real axis."
	.size	.Lstr.4, 36

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"ERROR: Invalid range for imaginary axis."
	.size	.Lstr.5, 41

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"USAGE: CompiledMandelbrot [xmin] [xmax] [ymin] [ymax]"
	.size	.Lstr.6, 54

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"ERROR: Unrecognized argument. Run with argument --help for help."
	.size	.Lstr.7, 65

	.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 _Z39__device_stub__calculate_mandlebrot_setP7pixel_tiidddd
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym outputFile
	.addrsig_sym _Z24calculate_mandlebrot_setP7pixel_tiidddd
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z24calculate_mandlebrot_setP7pixel_tiidddd ; -- Begin function _Z24calculate_mandlebrot_setP7pixel_tiidddd
	.globl	_Z24calculate_mandlebrot_setP7pixel_tiidddd
	.p2align	8
	.type	_Z24calculate_mandlebrot_setP7pixel_tiidddd,@function
_Z24calculate_mandlebrot_setP7pixel_tiidddd: ; @_Z24calculate_mandlebrot_setP7pixel_tiidddd
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x3c
	s_load_b64 s[12:13], s[0:1], 0x8
	v_and_b32_e32 v3, 0x3ff, v0
	s_add_u32 s2, s0, 48
	s_addc_u32 s3, s1, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s14, s4, v[3:4]
	s_mov_b32 s4, exec_lo
	v_cmpx_gt_u32_e64 s12, v1
	s_cbranch_execz .LBB0_12
; %bb.1:
	s_load_b32 s2, s[2:3], 0xc
	v_bfe_u32 v0, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s2, s2, 16
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[6:7], null, s15, s2, v[0:1]
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_gt_u32_e32 vcc_lo, s13, v6
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_12
; %bb.2:
	s_load_b256 s[4:11], s[0:1], 0x10
	v_cvt_f64_i32_e32 v[2:3], s12
	v_cvt_f64_i32_e32 v[7:8], s13
	s_load_b64 s[0:1], s[0:1], 0x0
                                        ; implicit-def: $sgpr3
	s_waitcnt lgkmcnt(0)
	v_add_f64 v[4:5], s[6:7], -s[4:5]
	v_add_f64 v[9:10], s[10:11], -s[8:9]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_div_scale_f64 v[11:12], null, v[2:3], v[2:3], v[4:5]
	v_div_scale_f64 v[13:14], null, v[7:8], v[7:8], v[9:10]
	v_div_scale_f64 v[23:24], vcc_lo, v[4:5], v[2:3], v[4:5]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_rcp_f64_e32 v[15:16], v[11:12]
	v_rcp_f64_e32 v[17:18], v[13:14]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[19:20], -v[11:12], v[15:16], 1.0
	v_fma_f64 v[21:22], -v[13:14], v[17:18], 1.0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[15:16], v[15:16], v[19:20], v[15:16]
	v_fma_f64 v[17:18], v[17:18], v[21:22], v[17:18]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[19:20], -v[11:12], v[15:16], 1.0
	v_fma_f64 v[21:22], -v[13:14], v[17:18], 1.0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_fma_f64 v[15:16], v[15:16], v[19:20], v[15:16]
	v_div_scale_f64 v[19:20], s2, v[9:10], v[7:8], v[9:10]
	v_fma_f64 v[17:18], v[17:18], v[21:22], v[17:18]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f64 v[21:22], v[23:24], v[15:16]
	v_mul_f64 v[25:26], v[19:20], v[17:18]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[11:12], -v[11:12], v[21:22], v[23:24]
	v_fma_f64 v[13:14], -v[13:14], v[25:26], v[19:20]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_div_fmas_f64 v[11:12], v[11:12], v[15:16], v[21:22]
	s_mov_b32 vcc_lo, s2
	v_cvt_f64_i32_e32 v[15:16], v1
	v_div_fmas_f64 v[13:14], v[13:14], v[17:18], v[25:26]
	s_mov_b32 s2, 0
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_div_fixup_f64 v[2:3], v[11:12], v[2:3], v[4:5]
	v_mov_b32_e32 v12, 0
	v_div_fixup_f64 v[4:5], v[13:14], v[7:8], v[9:10]
	v_cvt_f64_i32_e32 v[7:8], v6
	v_dual_mov_b32 v10, 0 :: v_dual_mov_b32 v13, 0
	v_mov_b32_e32 v11, 0
	v_fma_f64 v[2:3], v[2:3], v[15:16], s[4:5]
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[4:5], v[4:5], v[7:8], s[8:9]
	v_mad_u64_u32 v[7:8], null, v6, s12, v[1:2]
	s_delay_alu instid0(VALU_DEP_1)
	v_mad_i64_i32 v[0:1], null, v7, 12, s[0:1]
	v_dual_mov_b32 v6, v10 :: v_dual_mov_b32 v7, v11
.LBB0_3:                                ; =>This Inner Loop Header: Depth=1
	s_or_b32 s3, s3, exec_lo
	s_mov_b32 s4, exec_lo
                                        ; implicit-def: $vgpr8_vgpr9
	v_cmpx_gt_i32_e32 0x3e8, v12
	s_cbranch_execz .LBB0_9
; %bb.4:                                ;   in Loop: Header=BB0_3 Depth=1
	v_mul_f64 v[8:9], v[6:7], v[6:7]
                                        ; implicit-def: $sgpr1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fma_f64 v[8:9], v[10:11], v[10:11], -v[8:9]
	v_add_f64 v[10:11], v[10:11], v[10:11]
	v_add_f64 v[8:9], v[2:3], v[8:9]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[6:7], v[10:11], v[6:7], v[4:5]
	v_mul_f64 v[10:11], v[8:9], v[8:9]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[10:11], v[6:7], v[6:7], v[10:11]
	v_cmp_lt_f64_e64 s0, 4.0, v[10:11]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_saveexec_b32 s5, s0
	s_xor_b32 s5, exec_lo, s5
	s_cbranch_execz .LBB0_6
; %bb.5:                                ;   in Loop: Header=BB0_3 Depth=1
	v_cvt_f64_i32_e32 v[10:11], v12
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_div_scale_f64 v[13:14], null, 0x408f4000, 0x408f4000, v[10:11]
	v_div_scale_f64 v[19:20], vcc_lo, v[10:11], 0x408f4000, v[10:11]
	v_rcp_f64_e32 v[15:16], v[13:14]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[17:18], -v[13:14], v[15:16], 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[15:16], v[15:16], v[17:18], v[15:16]
	v_fma_f64 v[17:18], -v[13:14], v[15:16], 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[15:16], v[15:16], v[17:18], v[15:16]
	v_mul_f64 v[17:18], v[19:20], v[15:16]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[13:14], -v[13:14], v[17:18], v[19:20]
	v_div_fmas_f64 v[13:14], v[13:14], v[15:16], v[17:18]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fixup_f64 v[10:11], v[13:14], 0x408f4000, v[10:11]
	v_add_f64 v[13:14], -v[10:11], 1.0
	v_cmp_gt_f64_e32 vcc_lo, 0x10000000, v[10:11]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_cmp_gt_f64_e64 s1, 0x10000000, v[13:14]
	v_cndmask_b32_e64 v15, 0, 1, vcc_lo
	v_lshlrev_b32_e32 v15, 8, v15
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e64 v16, 0, 1, s1
	v_lshlrev_b32_e32 v17, 8, v16
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_ldexp_f64 v[15:16], v[10:11], v15
	v_mul_f64 v[10:11], 0x406fe000, v[10:11]
	v_ldexp_f64 v[13:14], v[13:14], v17
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_rsq_f64_e32 v[17:18], v[15:16]
	v_rsq_f64_e32 v[19:20], v[13:14]
	s_waitcnt_depctr 0xfff
	v_mul_f64 v[21:22], v[15:16], v[17:18]
	v_mul_f64 v[17:18], v[17:18], 0.5
	v_mul_f64 v[23:24], v[13:14], v[19:20]
	v_mul_f64 v[19:20], v[19:20], 0.5
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[25:26], -v[17:18], v[21:22], 0.5
	v_fma_f64 v[27:28], -v[19:20], v[23:24], 0.5
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_fma_f64 v[21:22], v[21:22], v[25:26], v[21:22]
	v_fma_f64 v[17:18], v[17:18], v[25:26], v[17:18]
	v_fma_f64 v[23:24], v[23:24], v[27:28], v[23:24]
	v_fma_f64 v[19:20], v[19:20], v[27:28], v[19:20]
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_fma_f64 v[25:26], -v[21:22], v[21:22], v[15:16]
	v_fma_f64 v[27:28], -v[23:24], v[23:24], v[13:14]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[21:22], v[25:26], v[17:18], v[21:22]
	v_fma_f64 v[23:24], v[27:28], v[19:20], v[23:24]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[25:26], -v[21:22], v[21:22], v[15:16]
	v_fma_f64 v[27:28], -v[23:24], v[23:24], v[13:14]
	s_delay_alu instid0(VALU_DEP_2)
	v_fma_f64 v[17:18], v[25:26], v[17:18], v[21:22]
	v_cndmask_b32_e64 v21, 0, 0xffffff80, vcc_lo
	v_cndmask_b32_e64 v22, 0, 0xffffff80, s1
	v_cmp_class_f64_e64 vcc_lo, v[15:16], 0x260
	v_fma_f64 v[19:20], v[27:28], v[19:20], v[23:24]
	v_cmp_class_f64_e64 s1, v[13:14], 0x260
	v_ldexp_f64 v[17:18], v[17:18], v21
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_ldexp_f64 v[19:20], v[19:20], v22
	v_dual_cndmask_b32 v16, v18, v16 :: v_dual_cndmask_b32 v15, v17, v15
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cndmask_b32_e64 v14, v20, v14, s1
	v_cndmask_b32_e64 v13, v19, v13, s1
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mul_f64 v[15:16], 0x406fe000, v[15:16]
	s_mov_b32 s1, 1
	v_mul_f64 v[17:18], 0x40490000, v[13:14]
	v_cvt_i32_f64_e32 v13, v[10:11]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cvt_i32_f64_e32 v14, v[15:16]
	v_cvt_i32_f64_e32 v15, v[17:18]
	global_store_b96 v[0:1], v[13:15], off
                                        ; implicit-def: $vgpr13
.LBB0_6:                                ; %Flow73
                                        ;   in Loop: Header=BB0_3 Depth=1
	s_or_saveexec_b32 s5, s5
	v_mov_b32_e32 v10, s1
	s_xor_b32 exec_lo, exec_lo, s5
; %bb.7:                                ;   in Loop: Header=BB0_3 Depth=1
	v_add_nc_u32_e32 v12, 1, v12
	v_mov_b32_e32 v10, v13
; %bb.8:                                ;   in Loop: Header=BB0_3 Depth=1
	s_or_b32 exec_lo, exec_lo, s5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	v_mov_b32_e32 v13, v10
	s_and_not1_b32 s1, s3, exec_lo
	s_and_b32 s0, s0, exec_lo
	s_or_b32 s3, s1, s0
.LBB0_9:                                ; %Flow74
                                        ;   in Loop: Header=BB0_3 Depth=1
	s_or_b32 exec_lo, exec_lo, s4
	v_dual_mov_b32 v11, v9 :: v_dual_mov_b32 v10, v8
	s_and_b32 s0, exec_lo, s3
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s2, s0, s2
	s_and_not1_b32 exec_lo, exec_lo, s2
	s_cbranch_execnz .LBB0_3
; %bb.10:
	s_or_b32 exec_lo, exec_lo, s2
	v_cmp_eq_u32_e32 vcc_lo, 0, v13
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_12
; %bb.11:
	v_mov_b32_e32 v2, 0
	s_delay_alu instid0(VALU_DEP_1)
	v_mov_b32_e32 v3, v2
	v_mov_b32_e32 v4, v2
	global_store_b96 v[0:1], v[2:4], off
.LBB0_12:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z24calculate_mandlebrot_setP7pixel_tiidddd
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 304
		.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 29
		.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	_Z24calculate_mandlebrot_setP7pixel_tiidddd, .Lfunc_end0-_Z24calculate_mandlebrot_setP7pixel_tiidddd
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1252
; NumSgprs: 18
; NumVgprs: 29
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 3
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 29
; 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
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .offset:         16
        .size:           8
        .value_kind:     by_value
      - .offset:         24
        .size:           8
        .value_kind:     by_value
      - .offset:         32
        .size:           8
        .value_kind:     by_value
      - .offset:         40
        .size:           8
        .value_kind:     by_value
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         52
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         60
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         62
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         64
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         66
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         68
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         70
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         112
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 304
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z24calculate_mandlebrot_setP7pixel_tiidddd
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z24calculate_mandlebrot_setP7pixel_tiidddd.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     29
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
