// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %rcx, %r12
movl %edx, %ebp
movl %esi, %r13d
leaq 0xd4e(%rip), %rsi       # 0x2004
callq 0x1140
testq %rax, %rax
je 0x146f
movq %rax, %r15
leaq 0xd41(%rip), %rdi       # 0x200f
xorl %r14d, %r14d
movl %r14d, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x1160
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x147d
movq %r12, 0x18(%rsp)
movq %rax, %r14
movq %rax, %rdi
callq 0x10f0
testq %rax, %rax
je 0x14a6
movq %rax, %r12
movq 0x2cd0(%rip), %rsi      # 0x3fe0
movl $0xc8, %edx
movq %r14, %rdi
callq 0x1080
movq %rax, %rdi
callq 0x10b0
testl %eax, %eax
jne 0x14bb
movq %r14, %rdi
movq %r15, 0x20(%rsp)
movq %r15, %rsi
callq 0x1130
subq $0x8, %rsp
pushq $0x8
popq %r8
pushq $0x2
popq %r9
movq %r14, %rdi
movq %r12, %rsi
movl %r13d, %edx
movl %ebp, %ecx
pushq $0x0
pushq $0x0
pushq $0x0
callq 0x1180
addq $0x20, %rsp
testq %rbx, %rbx
je 0x138d
leaq 0x30(%rsp), %rdx
orl $-0x1, (%rdx)
leaq 0xcfb(%rip), %rax       # 0x2072
movq %rax, 0x8(%rdx)
movq %rbx, 0x10(%rdx)
pushq $0x1
popq %rcx
movq %r14, %rdi
movq %r12, %rsi
callq 0x10e0
movq %r14, 0x8(%rsp)
movq %r14, %rdi
movq %r12, 0x10(%rsp)
movq %r12, %rsi
callq 0x1030
movl %r13d, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x1110
movq %rax, %rbx
xorl %edx, %edx
testl %r13d, %r13d
movl %edx, %ecx
cmovgl %r13d, %ecx
movslq %r13d, %rax
testl %ebp, %ebp
cmovlel %edx, %ebp
movl %ebp, %r13d
leaq (%rcx,%rcx,2), %rbp
movq 0x18(%rsp), %r15
addq $0x2, %r15
leaq (%rax,%rax,2), %r12
movl %edx, %r14d
xorl %eax, %eax
cmpq %r13, %r14
je 0x1416
cmpq %rax, %rbp
je 0x1401
movzwl -0x2(%r15,%rax), %ecx
movb (%r15,%rax), %dl
movw %cx, (%rbx,%rax)
movb %dl, 0x2(%rbx,%rax)
addq $0x3, %rax
jmp 0x13e4
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1060
incq %r14
addq %r12, %r15
jmp 0x13dd
movl %eax, %r15d
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x10a0
movq 0x20(%rsp), %rdi
callq 0x1070
pushq $-0x1
popq %rcx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movl $0xffff, %edx           # imm = 0xFFFF
callq 0x1090
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x1100
movq %rbx, %rdi
callq 0x1040
pushq $0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb91(%rip), %rdi       # 0x2007
callq 0x1150
jmp 0x14df
movq 0x2b74(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb88(%rip), %rdi       # 0x2016
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x1170
movq %r15, %rdi
callq 0x1070
movl %r14d, %eax
jmp 0x1460
movq 0x2b4b(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb80(%rip), %rdi       # 0x2037
pushq $0x1f
jmp 0x14ce
movq 0x2b36(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb8b(%rip), %rdi       # 0x2057
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1170
movq %r15, %rdi
callq 0x1070
xorl %eax, %eax
jmp 0x1460