// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq 0xd1c(%rip), %rsi       # 0x2004
callq 0x1140
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x130f
leaq 0xd09(%rip), %rdi       # 0x2007
callq 0x1150
movl $0x0, -0x4(%rbp)
jmp 0x15c2
leaq 0xcf9(%rip), %rdi       # 0x200f
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x1160
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x135c
movq 0x2cc2(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcd6(%rip), %rsi       # 0x2016
movb $0x0, %al
callq 0x10c0
movq -0x30(%rbp), %rdi
callq 0x1070
movl $0x0, -0x4(%rbp)
jmp 0x15c2
movq -0x38(%rbp), %rdi
callq 0x10f0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x139c
movq 0x2c82(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcb7(%rip), %rsi       # 0x2037
movb $0x0, %al
callq 0x10c0
movq -0x30(%rbp), %rdi
callq 0x1070
movl $0x0, -0x4(%rbp)
jmp 0x15c2
movq -0x38(%rbp), %rdi
movq 0x2c39(%rip), %rsi      # 0x3fe0
movl $0xc8, %edx
callq 0x1080
movq %rax, %rdi
callq 0x10b0
cmpl $0x0, %eax
je 0x13eb
movq 0x2c33(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc88(%rip), %rsi       # 0x2057
movb $0x0, %al
callq 0x10c0
movq -0x30(%rbp), %rdi
callq 0x1070
movl $0x0, -0x4(%rbp)
jmp 0x15c2
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x1130
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
movl $0x8, %r8d
movl $0x2, %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x1170
cmpq $0x0, -0x28(%rbp)
je 0x1467
movl $0xffffffff, -0x80(%rbp) # imm = 0xFFFFFFFF
leaq 0xc2d(%rip), %rax       # 0x2072
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq -0x80(%rbp), %rdx
movl $0x1, %ecx
callq 0x10e0
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x1030
imull $0x3, -0x14(%rbp), %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x1110
movq %rax, -0x48(%rbp)
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x155c
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x153b
movl -0x84(%rbp), %eax
movl -0x88(%rbp), %ecx
imull -0x14(%rbp), %ecx
addl %ecx, %eax
imull $0x3, %eax, %eax
movl %eax, -0x8c(%rbp)
movq -0x48(%rbp), %rdi
imull $0x3, -0x84(%rbp), %eax
cltq
addq %rax, %rdi
movq -0x20(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movb (%rax,%rcx), %sil
movq -0x20(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x20(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl %sil, %esi
movzbl %dl, %edx
movzbl (%rax,%rcx), %ecx
callq 0x1280
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x14ab
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x1060
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1492
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10a0
cmpq $0x0, -0x30(%rbp)
je 0x1579
movq -0x30(%rbp), %rdi
callq 0x1070
cmpq $0x0, -0x40(%rbp)
je 0x1597
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0xffff, %edx           # imm = 0xFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1090
cmpq $0x0, -0x38(%rbp)
je 0x15ab
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1100
cmpq $0x0, -0x48(%rbp)
je 0x15bb
movq -0x48(%rbp), %rdi
callq 0x1040
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nop