// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %r8, %r13
movq %rcx, %r14
movl %edx, %r12d
movl %esi, %ebp
leaq 0xc7e(%rip), %rsi       # 0x2004
callq 0x1140
testq %rax, %rax
je 0x1555
movq %rax, %r15
leaq 0xc71(%rip), %rdi       # 0x200f
xorl %ebx, %ebx
movl %ebx, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x1160
movq %rax, (%rsp)
testq %rax, %rax
je 0x1563
movq %r14, %rbx
movq %rax, %rdi
callq 0x10f0
testq %rax, %rax
je 0x156d
movq %rax, %r14
movq (%rsp), %rdi
movq 0x2c04(%rip), %rsi      # 0x3fe0
movl $0xc8, %edx
callq 0x1080
movq %rax, %rdi
callq 0x10b0
testl %eax, %eax
jne 0x1577
movq (%rsp), %rdi
movq %r15, 0x18(%rsp)
movq %r15, %rsi
callq 0x1130
movq (%rsp), %rdi
subq $0x8, %rsp
xorl %eax, %eax
movl %eax, %r10d
movq %r14, %rsi
movl %ebp, %edx
movl %r12d, %ecx
movl $0x8, %r8d
movl $0x2, %r9d
pushq %r10
pushq %r10
pushq %rax
callq 0x1180
addq $0x20, %rsp
testq %r13, %r13
je 0x1466
leaq 0x20(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0xc25(%rip), %rax       # 0x2072
movq %rax, 0x8(%rdx)
movq %r13, 0x10(%rdx)
movq (%rsp), %rdi
movq %r14, %rsi
movl $0x1, %ecx
callq 0x10e0
movq (%rsp), %rdi
movq %r14, 0x10(%rsp)
movq %r14, %rsi
callq 0x1030
movl %ebp, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x1110
movq %rax, %r13
movq %rax, 0x8(%rsp)
testl %r12d, %r12d
jle 0x14ed
movl %r12d, %r12d
movl %ebp, %eax
movq %rbx, %r14
addq $0x2, %r14
leaq (%rax,%rax,2), %rbx
xorl %eax, %eax
movl %eax, %r15d
testl %ebp, %ebp
jle 0x14d0
xorl %eax, %eax
movzwl -0x2(%r14,%rax), %ecx
movb (%r14,%rax), %dl
movw %cx, (%r13,%rax)
movb %dl, 0x2(%r13,%rax)
movq %rax, %rcx
addq $0x3, %rcx
movq %rcx, %rax
cmpq %rcx, %rbx
jne 0x14ac
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x1060
movq %r15, %rax
incq %rax
addq %rbx, %r14
movq %rax, %r15
cmpq %r12, %rax
jne 0x14a6
movq (%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10a0
movq 0x18(%rsp), %rdi
callq 0x1070
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0xffff, %edx           # imm = 0xFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1090
cmpq $0x0, (%rsp)
je 0x152f
xorl %eax, %eax
movl %eax, %esi
movq %rsp, %rdi
callq 0x1100
movl $0x1, %ebx
cmpq $0x0, 0x8(%rsp)
je 0x1544
movq %r13, %rdi
callq 0x1040
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xaab(%rip), %rdi       # 0x2007
callq 0x1150
jmp 0x157f
movq %r15, %rdi
callq 0x11f4
jmp 0x1544
movq %r15, %rdi
callq 0x11ca
jmp 0x157f
movq %r15, %rdi
callq 0x11a0
xorl %eax, %eax
jmp 0x1546