// File: /workspace/github_c_makefile_star2_O3/w4kfu[P]HommI/Pal/main.c
// Function: writeImage
int writeImage(char* filename, int width, int height, unsigned char *buffer, char* title)
{
	FILE *fp;
	png_structp png_ptr;
	png_infop info_ptr;
	png_bytep row;

	if (!(fp = fopen(filename, "wb")))
	{
		perror("fopen()");
		return 0;
	}
	if (!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)))
	{
		fprintf(stderr, "Could not allocate write struct\n");
		fclose(fp);
		return 0;
	}
	if (!(info_ptr = png_create_info_struct(png_ptr)))
	{
		fprintf(stderr, "Could not allocate info struct\n");
		fclose(fp);
		return 0;
	}
	if (setjmp(png_jmpbuf(png_ptr)))
	{
		fprintf(stderr, "Error during png creation\n");
		fclose(fp);
		return 0;
	}
	png_init_io(png_ptr, fp);

	png_set_IHDR(png_ptr, info_ptr, width, height,
			8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
			PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

	if (title != NULL)
	{
		png_text title_text;
		title_text.compression = PNG_TEXT_COMPRESSION_NONE;
		title_text.key = "Title";
		title_text.text = title;
		png_set_text(png_ptr, info_ptr, &title_text, 1);
	}

	png_write_info(png_ptr, info_ptr);

	row = (png_bytep) malloc(3 * width * sizeof(png_byte));

	int x, y;
	for (y = 0; y < height; y++)
	{
		for (x = 0; x < width; x++)
		{
			int a = (x + (y * width)) * 3;
			setRGB(&(row[x*3]), buffer[a], buffer[a + 1], buffer[a + 2]);
		}
		png_write_row(png_ptr, row);
	}

	png_write_end(png_ptr, NULL);

	if (fp != NULL)
		fclose(fp);
	if (info_ptr != NULL)
		png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
	if (png_ptr != NULL)
		png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
	if (row != NULL)
		free(row);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r15
movq %rcx, %r14
movl %edx, %r13d
movl %esi, 0xc(%rsp)
leaq 0xc7c(%rip), %rsi       # 0x2004
callq 0x1140
testq %rax, %rax
je 0x1558
movq %rax, %rbp
leaq 0xc6f(%rip), %rdi       # 0x200f
xorl %ebx, %ebx
movl %ebx, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x1160
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x1566
movq %r14, %rbx
movq %rax, %r14
movq %rax, %rdi
callq 0x10f0
testq %rax, %rax
je 0x1570
movq %rax, %r12
movq 0x2c02(%rip), %rsi      # 0x3fe0
movl $0xc8, %edx
movq %r14, %rdi
callq 0x1080
movq %rax, %rdi
callq 0x10b0
testl %eax, %eax
jne 0x157a
movq %r14, %rdi
movq %rbp, 0x20(%rsp)
movq %rbp, %rsi
callq 0x1130
subq $0x8, %rsp
xorl %eax, %eax
movl %eax, %r10d
movq %r14, %rdi
movq %r12, %rsi
movl 0x14(%rsp), %edx
movl %r13d, %ecx
movl $0x8, %r8d
movl $0x2, %r9d
pushq %r10
pushq %r10
pushq %rax
callq 0x1180
addq $0x20, %rsp
testq %r15, %r15
je 0x146a
leaq 0x30(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0xc20(%rip), %rax       # 0x2072
movq %rax, 0x8(%rdx)
movq %r15, 0x10(%rdx)
movq %r14, %rdi
movq %r12, %rsi
movl $0x1, %ecx
callq 0x10e0
movq %r14, %rdi
movq %r12, 0x18(%rsp)
movq %r12, %rsi
callq 0x1030
movl 0xc(%rsp), %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x1110
movq %rax, %r15
movq %rax, 0x10(%rsp)
testl %r13d, %r13d
jle 0x14f4
movl 0xc(%rsp), %eax
movl %r13d, %r13d
movq %rbx, %r12
addq $0x2, %r12
leaq (%rax,%rax,2), %rbx
xorl %eax, %eax
movl %eax, %ebp
cmpl $0x0, 0xc(%rsp)
jle 0x14d8
xorl %eax, %eax
movzwl -0x2(%r12,%rax), %ecx
movb (%r12,%rax), %dl
movw %cx, (%r15,%rax)
movb %dl, 0x2(%r15,%rax)
movq %rax, %rcx
addq $0x3, %rcx
movq %rcx, %rax
cmpq %rcx, %rbx
jne 0x14b5
movq %r14, %rdi
movq %r15, %rsi
callq 0x1060
movq %rbp, %rax
incq %rax
addq %rbx, %r12
movq %rax, %rbp
cmpq %r13, %rax
jne 0x14ac
xorl %eax, %eax
movl %eax, %r13d
movq %r14, %rdi
movq %r13, %rsi
callq 0x10a0
movq 0x20(%rsp), %rdi
callq 0x1070
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movl $0xffff, %edx           # imm = 0xFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1090
leaq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x1100
movl $0x1, %ebx
cmpq $0x0, 0x10(%rsp)
je 0x1547
movq %r15, %rdi
callq 0x1040
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xaa8(%rip), %rdi       # 0x2007
callq 0x1150
jmp 0x1582
movq %rbp, %rdi
callq 0x11f4
jmp 0x1547
movq %rbp, %rdi
callq 0x11ca
jmp 0x1582
movq %rbp, %rdi
callq 0x11a0
xorl %eax, %eax
jmp 0x1549