// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movl $0x88, %esi
callq 0xe969b
movq %rax, %r14
movq %rbx, (%rax)
movq (%r15), %rax
xorl %ebp, %ebp
cmpl $0x0, 0x20(%rax)
je 0x10f347
movq 0x70(%r14), %rdi
callq 0xebe1d
movq 0x80(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
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 %rbx, %rdi
callq 0xf241d
testl %eax, %eax
je 0x10f313
movq 0x10(%r15), %rcx
cmpl $0x0, (%rcx)
jne 0x10f36d
cmpb $0x0, 0x96(%rbx)
je 0x10f313
movq (%r15), %rax
movq %rax, 0x10(%r14)
movq 0x8(%r15), %rdx
movq %rdx, 0x18(%r14)
movl 0x1c(%r15), %edx
testb $0x1, %dl
je 0x10f38a
movb $0x1, 0x62(%r14)
testb $0x2, %dl
je 0x10f394
movb $0x1, 0x49(%r14)
movq 0xd0(%rcx), %rdx
testq %rdx, %rdx
je 0x10f43d
movl 0x78(%rdx), %esi
testl %esi, %esi
jle 0x10f43d
cmpb $0x0, 0xcd(%rbx)
je 0x10f43d
movl $0x1, 0x3c(%r14)
cmpl $0x2, %esi
je 0x10f3f6
cmpl $0x1, %esi
jne 0x10f43d
movb $0x0, 0x48(%r14)
movsd 0x80(%rdx), %xmm0
addsd 0x82ed(%rip), %xmm0     # 0x1176d0
cvttsd2si %xmm0, %rsi
movl %esi, 0x40(%r14)
movsd 0x88(%rdx), %xmm0
jmp 0x10f42c
movb $0x1, 0x48(%r14)
movsd 0x80(%rdx), %xmm0
movsd 0x5eb5(%rip), %xmm1     # 0x1152c0
divsd %xmm1, %xmm0
addsd 0x82b9(%rip), %xmm0     # 0x1176d0
cvttsd2si %xmm0, %rsi
movl %esi, 0x40(%r14)
movsd 0x88(%rdx), %xmm0
divsd %xmm1, %xmm0
addsd 0x829c(%rip), %xmm0     # 0x1176d0
cvttsd2si %xmm0, %rdx
movl %edx, 0x44(%r14)
cmpl $0x0, 0x3c(%r14)
je 0x10f45d
movl 0x40(%r14), %edx
cmpl 0x44(%r14), %edx
jne 0x10f45d
cmpb $0x0, 0x48(%r14)
jne 0x10f45d
movl $0x0, 0x3c(%r14)
cmpl $0x0, 0x3c(%r14)
je 0x10f486
movl 0x40(%r14), %edx
testl %edx, %edx
je 0x10f47e
movl 0x44(%r14), %esi
leal (%rdx,%rdx,4), %edi
cmpl %edi, %esi
ja 0x10f47e
leal (%rsi,%rsi,4), %esi
cmpl %esi, %edx
jbe 0x10f486
movl $0x0, 0x3c(%r14)
movq %rcx, 0x8(%r14)
cmpb $0x0, 0xd6(%rbx)
je 0x10f5ee
movq 0x8(%rax), %rcx
movl %ecx, 0x20(%r14)
movl 0x24(%rax), %edx
movl 0x8(%rax), %esi
imull %edx, %esi
movl %esi, 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 %edx, 0x2c(%r14)
imull %edx, %ecx
movl %ecx, 0x30(%r14)
movb $0x0, 0x60(%r14)
cmpb $0x0, 0x12d(%rbx)
jne 0x10f524
movl $0x9, 0x130(%rbx)
movb $0x1, 0x12d(%rbx)
leaq 0x422f6(%rip), %rsi     # 0x1517e8
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0x10f524
movq %rax, %rdi
callq 0xea786
testq %rax, %rax
movl $0x6, %ecx
cmovnsl %eax, %ecx
cmpq $0xb, %rax
movl $0xa, %eax
cmovll %ecx, %eax
movl %eax, 0x130(%rbx)
movl 0x130(%rbx), %eax
movl %eax, 0x38(%r14)
movq 0x10(%r15), %rax
movq 0xd0(%rax), %rax
testq %rax, %rax
je 0x10f552
cmpb $0x0, 0x68(%rax)
je 0x10f552
addq $0x68, %rax
movupd (%rax), %xmm0
movupd %xmm0, 0x50(%r14)
movq 0x10(%r15), %rax
movq 0xd0(%rax), %rax
testq %rax, %rax
je 0x10f586
cmpb $0x0, 0xe(%rax)
je 0x10f586
movb $0x1, %cl
movb %cl, 0x61(%r14)
movl 0x98(%rax), %edx
movl %edx, 0x64(%r14)
movl 0x9c(%rax), %eax
movl %eax, 0x68(%r14)
movb %cl, 0x60(%r14)
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeba56
movq %rax, 0x70(%r14)
movq (%r14), %rdi
movl $0x40, %esi
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r15
movq 0x8(%r14), %rdi
leaq 0x4225c(%rip), %rsi     # 0x151810
movl $0x8, %edx
callq 0xefb82
movslq 0x20(%r14), %rsi
movq %r15, %rdi
callq 0xf1629
movslq 0x24(%r14), %rsi
movq %r15, %rdi
callq 0xf1629
cmpb $0x0, 0x62(%r14)
je 0x10f60a
movq %r15, %rdi
movl $0x1, %esi
callq 0xf11be
xorl %eax, %eax
jmp 0x10f631
movq 0x18(%rax), %rcx
testq %rcx, %rcx
jle 0x10f497
cmpq 0x8(%rax), %rcx
jge 0x10f497
jmp 0x10f49b
xorl %eax, %eax
cmpq $0x0, 0x18(%r14)
setne %al
leal 0x8(,%rax,8), %esi
movq %r15, %rdi
callq 0xf11be
movslq 0x2c(%r14), %rax
leaq 0x421ea(%rip), %rcx     # 0x151818
movb (%rax,%rcx), %al
movzbl %al, %esi
movq %r15, %rdi
callq 0xf11be
movl $0xd, %esi
movq %r15, %rdi
callq 0xf152a
movq %r14, %rdi
movq %r15, %rsi
movl $0x49484452, %edx       # imm = 0x49484452
callq 0x10fb02
cmpb $0x0, 0x49(%r14)
je 0x10f687
movq %r15, %rdi
xorl %esi, %esi
callq 0xf152a
movl $0x186a0, %esi          # imm = 0x186A0
movq %r15, %rdi
callq 0xf1629
movq %r14, %rdi
movq %r15, %rsi
movl $0x67414d41, %edx       # imm = 0x67414D41
callq 0x10fb02
cmpl $0x0, 0x3c(%r14)
je 0x10f6cd
movq %r15, %rdi
xorl %esi, %esi
callq 0xf152a
movl 0x40(%r14), %esi
movq %r15, %rdi
callq 0xf1629
movl 0x44(%r14), %esi
movq %r15, %rdi
callq 0xf1629
movzbl 0x48(%r14), %esi
movq %r15, %rdi
callq 0xf11be
movq %r14, %rdi
movq %r15, %rsi
movl $0x70485973, %edx       # imm = 0x70485973
callq 0x10fb02
cmpb $0x0, 0x50(%r14)
je 0x10f772
movq (%r14), %rax
cmpb $0x0, 0xde(%rax)
je 0x10f772
movq %r14, %r12
addq $0x50, %r12
movq %r15, %rdi
xorl %esi, %esi
callq 0xf152a
leaq 0x10(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0xeb2df
cmpl $0x0, (%r13)
je 0x10f772
movslq 0x14(%rsp), %rsi
movq %r15, %rdi
callq 0xf15b0
movb 0x18(%rsp), %al
incb %al
movzbl %al, %esi
movq %r15, %rdi
callq 0xf11be
movzbl 0x1c(%rsp), %esi
movq %r15, %rdi
callq 0xf11be
movzbl 0x20(%rsp), %esi
movq %r15, %rdi
callq 0xf11be
movzbl 0x24(%rsp), %esi
movq %r15, %rdi
callq 0xf11be
movzbl 0x28(%rsp), %esi
movq %r15, %rdi
callq 0xf11be
movq %r14, %rdi
movq %r15, %rsi
movl $0x74494d45, %edx       # imm = 0x74494D45
callq 0x10fb02
cmpb $0x0, 0x61(%r14)
je 0x10f7bf
movq %r15, %rdi
xorl %esi, %esi
callq 0xf152a
leaq 0x42096(%rip), %rsi     # 0x151820
movl $0x10, %edx
movq %r15, %rdi
callq 0xefb82
movslq 0x64(%r14), %rsi
movq %r15, %rdi
callq 0xf1699
movslq 0x68(%r14), %rsi
movq %r15, %rdi
callq 0xf1699
movq %r14, %rdi
movq %r15, %rsi
movl $0x68745350, %edx       # imm = 0x68745350
callq 0x10fb02
movq %rbx, 0x8(%rsp)
cmpb $0x0, 0x60(%r14)
je 0x10f817
movq %r15, %rdi
xorl %esi, %esi
callq 0xf152a
leaq 0x2e016(%rip), %rsi     # 0x13d7f2
movl $0x8, %edx
movq %r15, %rdi
callq 0xefb82
movq %r15, %rdi
xorl %esi, %esi
callq 0xf11be
leaq 0x3b0cf(%rip), %rsi     # 0x14a8c9
movl $0x5, %edx
movq %r15, %rdi
callq 0xefb82
movq %r14, %rdi
movq %r15, %rsi
movl $0x74455874, %edx       # imm = 0x74455874
callq 0x10fb02
movq %r15, %rdi
xorl %esi, %esi
callq 0xf152a
movq (%r14), %r12
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rbx
movapd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movq %r14, (%rbx)
movq %r15, 0x8(%rbx)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf19d3
movq %rax, %rbp
movq %rbx, 0xa8(%rax)
leaq 0x35f(%rip), %rax       # 0x10fbbc
movq %rax, 0xb0(%rbp)
movl 0x38(%r14), %eax
cmpl $0xa, %eax
movl $0xa, %ecx
cmovbl %eax, %ecx
leaq 0x41fb6(%rip), %rax     # 0x151830
movl $0x1000, %edx           # imm = 0x1000
orl (%rax,%rcx,4), %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x10de63
movq %rax, %r13
cmpb $0x0, 0x62(%r14)
movslq 0x20(%r14), %rax
movq %rbp, (%rsp)
je 0x10f8ad
addq $0x7, %rax
shrq $0x3, %rax
movq %rax, 0x78(%r14)
jmp 0x10f8c7
movslq 0x2c(%r14), %rcx
imulq %rax, %rcx
movq %rcx, 0x78(%r14)
cmpq $0x0, 0x18(%r14)
je 0x10f8c7
addq %rcx, %rcx
movq %rcx, 0x78(%r14)
movl 0x24(%r14), %eax
testl %eax, %eax
jle 0x10fa51
xorl %r12d, %r12d
leaq 0x41f7f(%rip), %rbp     # 0x15185c
movl %r12d, %ebx
notl %ebx
addl %eax, %ebx
cmpl $0x0, 0x34(%r14)
cmovel %r12d, %ebx
movl $0x1, %edx
movq %r13, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x10dec3
cmpb $0x0, 0x62(%r14)
je 0x10f91f
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x10f94a
movq 0x78(%r14), %rdx
xorl %esi, %esi
callq 0x18140
jmp 0x10f95d
cmpq $0x0, 0x18(%r14)
je 0x10f9b1
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x10f9c6
movq 0x78(%r14), %rdx
xorl %esi, %esi
callq 0x18140
jmp 0x10f9d9
movq (%r14), %rdi
movq 0x78(%r14), %rsi
callq 0xe969b
movq %rax, 0x80(%r14)
cmpl $0x0, 0x20(%r14)
jle 0x10fa2c
xorl %eax, %eax
movq 0x10(%r14), %rcx
movq 0x28(%rcx), %rdx
movl 0x28(%r14), %esi
imull %ebx, %esi
movl 0x24(%rcx), %ecx
imull %eax, %ecx
addl %esi, %ecx
movslq %ecx, %rcx
cmpb $0x0, (%rdx,%rcx)
jns 0x10f9a7
movl %eax, %ecx
notb %cl
andb $0x7, %cl
movl $0x1, %edx
shll %cl, %edx
movq 0x80(%r14), %rcx
movl %eax, %esi
shrl $0x3, %esi
orb %dl, (%rcx,%rsi)
incl %eax
cmpl 0x20(%r14), %eax
jl 0x10f96a
jmp 0x10fa2c
movq 0x10(%r14), %rax
movslq 0x28(%r14), %rcx
movslq %ebx, %rsi
imulq %rcx, %rsi
addq 0x28(%rax), %rsi
jmp 0x10fa33
movq (%r14), %rdi
movq 0x78(%r14), %rsi
callq 0xe969b
movq %rax, 0x80(%r14)
cmpl $0x0, 0x30(%r14)
jle 0x10fa2c
movslq %ebx, %rax
xorl %ecx, %ecx
movslq 0x28(%r14), %rdx
imulq %rax, %rdx
movq 0x10(%r14), %rsi
movq 0x80(%r14), %rdi
addq 0x28(%rsi), %rdx
movb (%rcx,%rdx), %dl
movb %dl, (%rdi,%rcx,2)
movslq 0x28(%r14), %rdx
imulq %rax, %rdx
movq 0x18(%r14), %rsi
movq 0x80(%r14), %rdi
addq 0x28(%rsi), %rdx
movb (%rcx,%rdx), %dl
movb %dl, 0x1(%rdi,%rcx,2)
incq %rcx
movslq 0x30(%r14), %rdx
cmpq %rdx, %rcx
jl 0x10f9e5
movq 0x80(%r14), %rsi
movq 0x78(%r14), %rdx
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x10dec3
incl %r12d
movl 0x24(%r14), %eax
cmpl %eax, %r12d
jl 0x10f8dd
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
movl $0x4, %ecx
callq 0x10dec3
movl %eax, %r12d
cmpl $0x1, %eax
jne 0x10fabd
cmpq $0x0, 0x18(%r15)
movq 0x8(%rsp), %rbx
movq (%rsp), %rbp
jg 0x10fa81
cmpl $0x0, 0x20(%rsp)
jne 0x10fa91
movq %r14, %rdi
movq %r15, %rsi
movl $0x49444154, %edx       # imm = 0x49444154
callq 0x10fb02
movq %r13, %rdi
callq 0x10deea
movq %rbp, %rdi
callq 0xf0b13
movq %r15, %rdi
xorl %esi, %esi
callq 0xf152a
movq %r14, %rdi
movq %r15, %rsi
movl $0x49454e44, %edx       # imm = 0x49454E44
callq 0x10fb02
jmp 0x10fad3
movq %r13, %rdi
callq 0x10deea
movq (%rsp), %rdi
callq 0xf0b13
movq 0x8(%rsp), %rbx
movq %r15, %rdi
callq 0xf0b13
movl $0x1, %ebp
cmpl $0x1, %r12d
je 0x10f313
leaq 0x41d04(%rip), %rsi     # 0x1517f5
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x10f313