// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/deark-png.c
// Function: de_write_png
int de_write_png(deark *c, struct de_write_image_params *wp)
{
	const char *opt_level;
	int retval = 0;
	struct deark_png_encode_info *pei = NULL;

	pei = de_malloc(c, sizeof(struct deark_png_encode_info));
	pei->c = c;

	if(wp->img->invalid_image_flag) {
		goto done;
	}
	if(!de_good_image_dimensions(c, wp->img->width, wp->img->height)) {
		goto done;
	}

	// Optimization to speed up list mode
	if(wp->f->btype==DBUF_TYPE_NULL && !c->enable_oinfo) {
		goto done;
	}

	pei->img = wp->img;
	pei->imglo = wp->imglo;
	if(wp->flags2 & 0x1) {
		pei->encode_as_bwimg = 1;
	}
	if(wp->flags2 & 0x2) {
		pei->linear_colorspace = 1;
	}

	if(wp->f->fi_copy && wp->f->fi_copy->density.code>0 && c->write_density) {
		pei->has_phys = 1;
		if(wp->f->fi_copy->density.code==1) { // unspecified units
			pei->phys_units = 0;
			pei->xdens = (u32)(wp->f->fi_copy->density.xdens+0.5);
			pei->ydens = (u32)(wp->f->fi_copy->density.ydens+0.5);
		}
		else if(wp->f->fi_copy->density.code==2) { // dpi
			pei->phys_units = 1; // pixels/meter
			pei->xdens = (u32)(0.5+wp->f->fi_copy->density.xdens/0.0254);
			pei->ydens = (u32)(0.5+wp->f->fi_copy->density.ydens/0.0254);
		}
	}

	if(pei->has_phys && pei->xdens==pei->ydens && pei->phys_units==0) {
		// Useless density information. Don't bother to write it.
		pei->has_phys = 0;
	}

	// Detect likely-bogus density settings.
	if(pei->has_phys) {
		if(pei->xdens<=0 || pei->ydens<=0 ||
			(pei->xdens > pei->ydens*5) || (pei->ydens > pei->xdens*5))
		{
			pei->has_phys = 0;
		}
	}

	pei->outf = wp->f;
	if(!c->padpix && wp->img->unpadded_width>0 && wp->img->unpadded_width<wp->img->width) {
		pei->width = (int)wp->img->unpadded_width;
	}
	else {
		pei->width = (int)wp->img->width;
	}
	pei->src_rowspan = (int)(wp->img->width * wp->img->bytes_per_pixel);
	pei->height = (int)wp->img->height;
	pei->flip = (wp->createflags & DE_CREATEFLAG_FLIP_IMAGE)?1:0;
	pei->num_chans = wp->img->bytes_per_pixel;
	pei->samples_per_row = pei->width * pei->num_chans;
	pei->include_text_chunk_software = 0;

	if(!c->pngcprlevel_valid) {
		c->pngcmprlevel = 9; // default
		c->pngcprlevel_valid = 1;

		opt_level = de_get_ext_option(c, "pngcmprlevel");
		if(opt_level) {
			i64 opt_level_n = de_atoi64(opt_level);
			if(opt_level_n>10) {
				c->pngcmprlevel = 10;
			}
			else if(opt_level_n<0) {
				c->pngcmprlevel = 6;
			}
			else {
				c->pngcmprlevel = (unsigned int)opt_level_n;
			}
		}
	}
	pei->level = c->pngcmprlevel;

	if(wp->f->fi_copy && wp->f->fi_copy->internal_mod_time.is_valid) {
		pei->internal_mod_time = wp->f->fi_copy->internal_mod_time;
	}

	if(wp->f->fi_copy && wp->f->fi_copy->has_hotspot) {
		pei->has_hotspot = 1;
		pei->hotspot_x = wp->f->fi_copy->hotspot_x;
		pei->hotspot_y = wp->f->fi_copy->hotspot_y;
		// Leave a hint as to where our custom Hotspot chunk came from.
		pei->include_text_chunk_software = 1;
	}

	pei->crco = de_crcobj_create(c, DE_CRCOBJ_CRC32_IEEE);

	if(!do_generate_png(pei, wp->img->bitmap)) {
		de_err(c, "PNG write failed");
		goto done;
	}

	retval = 1;

done:
	if(pei) {
		de_crcobj_destroy(pei->crco);
		de_free(c, pei->tmprow);
		de_free(c, pei);
	}
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
movq %rdi, %r12
movl $0x88, %esi
callq 0xdecaf
movq %rax, %r14
movq %r12, (%rax)
movq (%r15), %rax
xorl %ebp, %ebp
cmpl $0x0, 0x20(%rax)
je 0x101fff
movq 0x70(%r14), %rdi
callq 0xe0e8d
movq 0x80(%r14), %rsi
movq %r12, %rdi
callq 0xded32
movq %r12, %rdi
movq %r14, %rsi
callq 0xded32
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq %r12, %rdi
callq 0xe6be1
testl %eax, %eax
je 0x101fcb
movq 0x10(%r15), %rcx
cmpl $0x0, (%rcx)
jne 0x102027
cmpb $0x0, 0x96(%r12)
je 0x101fcb
movq (%r15), %rax
movq %rax, 0x10(%r14)
movq 0x8(%r15), %rdx
movq %rdx, 0x18(%r14)
movl 0x1c(%r15), %edx
testb $0x1, %dl
je 0x102044
movb $0x1, 0x62(%r14)
testb $0x2, %dl
je 0x10204e
movb $0x1, 0x49(%r14)
movq 0xd0(%rcx), %rdx
testq %rdx, %rdx
je 0x1020f9
movl 0x78(%rdx), %esi
testl %esi, %esi
jle 0x1020f9
cmpb $0x0, 0xcd(%r12)
je 0x1020f9
movl $0x1, 0x3c(%r14)
cmpl $0x2, %esi
je 0x1020b2
cmpl $0x1, %esi
jne 0x1020f9
movb $0x0, 0x48(%r14)
movsd 0x80(%rdx), %xmm0
addsd 0x86a1(%rip), %xmm0     # 0x10a740
cvttsd2si %xmm0, %rsi
movl %esi, 0x40(%r14)
movsd 0x88(%rdx), %xmm0
jmp 0x1020e8
movb $0x1, 0x48(%r14)
movsd 0x80(%rdx), %xmm0
movsd 0x6219(%rip), %xmm1     # 0x1082e0
divsd %xmm1, %xmm0
addsd 0x866d(%rip), %xmm0     # 0x10a740
cvttsd2si %xmm0, %rsi
movl %esi, 0x40(%r14)
movsd 0x88(%rdx), %xmm0
divsd %xmm1, %xmm0
addsd 0x8650(%rip), %xmm0     # 0x10a740
cvttsd2si %xmm0, %rdx
movl %edx, 0x44(%r14)
cmpl $0x0, 0x3c(%r14)
je 0x102139
movl 0x40(%r14), %edx
movl 0x44(%r14), %esi
cmpl %esi, %edx
jne 0x102122
cmpb $0x0, 0x48(%r14)
sete %dil
testl %edx, %edx
sete %r8b
orb %dil, %r8b
je 0x102126
jmp 0x102134
testl %edx, %edx
je 0x102134
leal (%rdx,%rdx,4), %edi
cmpl %edi, %esi
ja 0x102134
leal (%rsi,%rsi,4), %esi
cmpl %esi, %edx
jbe 0x102139
andl $0x0, 0x3c(%r14)
movq %rcx, 0x8(%r14)
cmpb $0x0, 0xd6(%r12)
je 0x102298
movl 0x8(%rax), %edx
movl %edx, 0x20(%r14)
movl %edx, %ecx
movl 0x24(%rax), %esi
imull %esi, %edx
movl %edx, 0x28(%r14)
movl 0x10(%rax), %eax
movl %eax, 0x24(%r14)
movl 0x18(%r15), %eax
shrl $0x2, %eax
andl $0x1, %eax
movl %eax, 0x34(%r14)
movl %esi, 0x2c(%r14)
imull %esi, %ecx
movl %ecx, 0x30(%r14)
movb $0x0, 0x60(%r14)
cmpb $0x0, 0x12d(%r12)
pushq $0xa
popq %r13
jne 0x1021dd
movl $0x9, 0x130(%r12)
movb $0x1, 0x12d(%r12)
leaq 0x42754(%rip), %rsi     # 0x144903
movq %r12, %rdi
callq 0xdfa87
testq %rax, %rax
je 0x1021dd
movq %rax, %rdi
callq 0xdfb3b
testq %rax, %rax
pushq $0x6
popq %rcx
cmovnsl %eax, %ecx
cmpq $0xb, %rax
cmovgel %r13d, %ecx
movl %ecx, 0x130(%r12)
movl 0x130(%r12), %eax
movl %eax, 0x38(%r14)
movq 0x10(%r15), %rax
movq 0xd0(%rax), %rax
testq %rax, %rax
je 0x102238
cmpb $0x0, 0x68(%rax)
je 0x10221d
addq $0x68, %rax
movupd (%rax), %xmm0
movupd %xmm0, 0x50(%r14)
movq 0x10(%r15), %rax
movq 0xd0(%rax), %rax
testq %rax, %rax
je 0x102238
cmpb $0x0, 0xe(%rax)
je 0x102238
movb $0x1, %cl
movb %cl, 0x61(%r14)
movq 0x98(%rax), %rax
movq %rax, 0x64(%r14)
movb %cl, 0x60(%r14)
pushq $0x10
popq %rsi
movq %r12, %rdi
callq 0xe0ae9
movq %rax, 0x70(%r14)
movq (%r14), %rdi
pushq $0x40
popq %rsi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %rbp
movq 0x8(%r14), %rdi
leaq 0x426ce(%rip), %rsi     # 0x144930
pushq $0x8
popq %rdx
callq 0xe46ba
movslq 0x20(%r14), %rsi
movq %rbp, %rdi
callq 0xe5f65
movslq 0x24(%r14), %rsi
movq %rbp, %rdi
callq 0xe5f65
cmpb $0x0, 0x62(%r14)
je 0x1022bb
pushq $0x1
popq %rsi
movq %rbp, %rdi
callq 0xe5bc5
xorl %eax, %eax
jmp 0x1022e2
movq 0x18(%rax), %rcx
testq %rcx, %rcx
jle 0x10214c
movq 0x8(%rax), %rdx
cmpq %rdx, %rcx
jge 0x10214c
movl %ecx, 0x20(%r14)
jmp 0x102155
xorl %eax, %eax
cmpq $0x0, 0x18(%r14)
setne %al
leal 0x8(,%rax,8), %esi
movq %rbp, %rdi
callq 0xe5bc5
movslq 0x2c(%r14), %rax
leaq 0x42659(%rip), %rcx     # 0x144938
movb (%rax,%rcx), %al
movzbl %al, %esi
movq %rbp, %rdi
callq 0xe5bc5
pushq $0xd
popq %rsi
movq %rbp, %rdi
callq 0xe5eb2
movq %r14, %rdi
movq %rbp, %rsi
movl $0x49484452, %edx       # imm = 0x49484452
callq 0x10278e
cmpb $0x0, 0x49(%r14)
je 0x102336
movq %rbp, %rdi
xorl %esi, %esi
callq 0xe5eb2
movl $0x186a0, %esi          # imm = 0x186A0
movq %rbp, %rdi
callq 0xe5f65
movq %r14, %rdi
movq %rbp, %rsi
movl $0x67414d41, %edx       # imm = 0x67414D41
callq 0x10278e
cmpl $0x0, 0x3c(%r14)
je 0x10237c
movq %rbp, %rdi
xorl %esi, %esi
callq 0xe5eb2
movl 0x40(%r14), %esi
movq %rbp, %rdi
callq 0xe5f65
movl 0x44(%r14), %esi
movq %rbp, %rdi
callq 0xe5f65
movzbl 0x48(%r14), %esi
movq %rbp, %rdi
callq 0xe5bc5
movq %r14, %rdi
movq %rbp, %rsi
movl $0x70485973, %edx       # imm = 0x70485973
callq 0x10278e
cmpb $0x0, 0x50(%r14)
je 0x102427
movq (%r14), %rax
cmpb $0x0, 0xde(%rax)
je 0x102427
movq %r12, %r15
movq %r14, %rbx
addq $0x50, %rbx
movq %rbp, %rdi
xorl %esi, %esi
callq 0xe5eb2
leaq 0x18(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe04a6
cmpl $0x0, (%r12)
je 0x102424
movslq 0x1c(%rsp), %rsi
movq %rbp, %rdi
callq 0xe5f1e
movb 0x20(%rsp), %al
incb %al
movzbl %al, %esi
movq %rbp, %rdi
callq 0xe5bc5
movzbl 0x24(%rsp), %esi
movq %rbp, %rdi
callq 0xe5bc5
movzbl 0x28(%rsp), %esi
movq %rbp, %rdi
callq 0xe5bc5
movzbl 0x2c(%rsp), %esi
movq %rbp, %rdi
callq 0xe5bc5
movzbl 0x30(%rsp), %esi
movq %rbp, %rdi
callq 0xe5bc5
movq %r14, %rdi
movq %rbp, %rsi
movl $0x74494d45, %edx       # imm = 0x74494D45
callq 0x10278e
movq %r15, %r12
cmpb $0x0, 0x61(%r14)
je 0x102472
movq %rbp, %rdi
xorl %esi, %esi
callq 0xe5eb2
leaq 0x42501(%rip), %rsi     # 0x144940
pushq $0x10
popq %rdx
movq %rbp, %rdi
callq 0xe46ba
movslq 0x64(%r14), %rsi
movq %rbp, %rdi
callq 0xe5fb0
movslq 0x68(%r14), %rsi
movq %rbp, %rdi
callq 0xe5fb0
movq %r14, %rdi
movq %rbp, %rsi
movl $0x68745350, %edx       # imm = 0x68745350
callq 0x10278e
cmpb $0x0, 0x60(%r14)
je 0x1024c1
movq %rbp, %rdi
xorl %esi, %esi
callq 0xe5eb2
leaq 0x2e438(%rip), %rsi     # 0x1308c2
pushq $0x8
popq %rdx
movq %rbp, %rdi
callq 0xe46ba
movq %rbp, %rdi
xorl %esi, %esi
callq 0xe5bc5
leaq 0x3b503(%rip), %rsi     # 0x13d9a9
pushq $0x5
popq %rdx
movq %rbp, %rdi
callq 0xe46ba
movq %r14, %rdi
movq %rbp, %rsi
movl $0x74455874, %edx       # imm = 0x74455874
callq 0x10278e
movq %rbp, %rdi
xorl %esi, %esi
callq 0xe5eb2
movq (%r14), %rbx
leaq 0x18(%rsp), %r15
andq $0x0, 0x10(%r15)
movq %r14, (%r15)
movq %rbp, 0x8(%r15)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe6277
movq %rax, %rsi
movq %r15, 0xa8(%rax)
leaq 0x34f(%rip), %rax       # 0x10284b
movq %rax, 0xb0(%rsi)
movl 0x38(%r14), %eax
cmpl $0xa, %eax
cmovbl %eax, %r13d
leaq 0x4243b(%rip), %rax     # 0x144950
movl $0x1000, %edx           # imm = 0x1000
orl (%rax,%r13,4), %edx
movq %rbx, %rdi
movq %rsi, 0x8(%rsp)
callq 0x100ba4
movq %rax, %r13
cmpb $0x0, 0x62(%r14)
movslq 0x20(%r14), %rax
movq %r12, 0x10(%rsp)
movq %rbp, (%rsp)
je 0x102550
addq $0x7, %rax
shrq $0x3, %rax
movq %rax, 0x78(%r14)
jmp 0x10256a
movslq 0x2c(%r14), %rcx
imulq %rax, %rcx
movq %rcx, 0x78(%r14)
cmpq $0x0, 0x18(%r14)
je 0x10256a
addq %rcx, %rcx
movq %rcx, 0x78(%r14)
pushq $0x1
popq %rbp
xorl %r15d, %r15d
pushq $0x1
popq %rbx
movl 0x24(%r14), %r12d
cmpl %r12d, %r15d
jge 0x1026da
movl %r15d, %eax
notl %eax
addl %eax, %r12d
cmpl $0x0, 0x34(%r14)
cmovel %r15d, %r12d
movq %r13, %rdi
leaq 0x423e1(%rip), %rsi     # 0x14497c
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x100c02
cmpb $0x0, 0x62(%r14)
je 0x1025c5
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x1025ef
movq 0x78(%r14), %rdx
xorl %esi, %esi
callq 0x18140
jmp 0x102602
cmpq $0x0, 0x18(%r14)
movslq %r12d, %r12
je 0x10264d
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x10265f
movq 0x78(%r14), %rdx
xorl %esi, %esi
callq 0x18140
jmp 0x102672
movq (%r14), %rdi
movq 0x78(%r14), %rsi
callq 0xdecaf
movq %rax, 0x80(%r14)
xorl %eax, %eax
cmpl 0x20(%r14), %eax
jge 0x1026bd
movq 0x10(%r14), %rcx
movq 0x28(%rcx), %rdx
movl 0x28(%r14), %esi
imull %r12d, %esi
movl 0x24(%rcx), %ecx
imull %eax, %ecx
addl %esi, %ecx
movslq %ecx, %rcx
cmpb $0x0, (%rdx,%rcx)
jns 0x102649
movl %eax, %ecx
notb %cl
andb $0x7, %cl
movl %ebp, %edx
shll %cl, %edx
movq 0x80(%r14), %rcx
movl %eax, %esi
shrl $0x3, %esi
orb %dl, (%rcx,%rsi)
incl %eax
jmp 0x102604
movq 0x10(%r14), %rax
movslq 0x28(%r14), %rsi
imulq %r12, %rsi
addq 0x28(%rax), %rsi
jmp 0x1026c4
movq (%r14), %rdi
movq 0x78(%r14), %rsi
callq 0xdecaf
movq %rax, 0x80(%r14)
xorl %eax, %eax
movslq 0x30(%r14), %rcx
cmpq %rcx, %rax
jge 0x1026bd
movslq 0x28(%r14), %rcx
imulq %r12, %rcx
movq 0x10(%r14), %rdx
addq 0x28(%rdx), %rcx
movq 0x80(%r14), %rdx
movb (%rax,%rcx), %cl
movb %cl, (%rdx,%rax,2)
movslq 0x28(%r14), %rcx
imulq %r12, %rcx
movq 0x18(%r14), %rdx
addq 0x28(%rdx), %rcx
movq 0x80(%r14), %rdx
movb (%rax,%rcx), %cl
movb %cl, 0x1(%rdx,%rax,2)
incq %rax
jmp 0x102674
movq 0x80(%r14), %rsi
movq 0x78(%r14), %rdx
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x100c02
incl %r15d
jmp 0x102573
pushq $0x4
popq %rcx
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x100c02
movl %eax, %ebx
cmpl $0x1, %eax
jne 0x10274a
movq (%rsp), %rax
cmpq $0x0, 0x18(%rax)
movq 0x10(%rsp), %r12
jg 0x102707
cmpl $0x0, 0x28(%rsp)
jne 0x102718
movq %r14, %rdi
movq (%rsp), %rsi
movl $0x49444154, %edx       # imm = 0x49444154
callq 0x10278e
movq %r13, %rdi
callq 0x100c27
movq 0x8(%rsp), %rdi
callq 0xe5591
movq (%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0xe5eb2
movq %r14, %rdi
movq %r15, %rsi
movl $0x49454e44, %edx       # imm = 0x49454E44
callq 0x10278e
jmp 0x102765
movq %r13, %rdi
callq 0x100c27
movq 0x8(%rsp), %rdi
callq 0xe5591
movq 0x10(%rsp), %r12
movq (%rsp), %r15
movq %r15, %rdi
callq 0xe5591
cmpl $0x1, %ebx
je 0x101fcb
leaq 0x42193(%rip), %rsi     # 0x144910
xorl %ebp, %ebp
movq %r12, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x101fcb