// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
movq %rbx, (%rax)
movq (%r15), %rax
xorl %ebp, %ebp
cmpl $0x0, 0x20(%rax)
je 0x10f2ef
movq 0x70(%r14), %rdi
callq 0xeb858
movq 0x80(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
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 0xf1db1
testl %eax, %eax
je 0x10f2bb
movq 0x10(%r15), %rcx
cmpl $0x0, (%rcx)
jne 0x10f315
cmpb $0x0, 0x96(%rbx)
je 0x10f2bb
movq (%r15), %rax
movq %rax, 0x10(%r14)
movq 0x8(%r15), %rdx
movq %rdx, 0x18(%r14)
movl 0x1c(%r15), %edx
testb $0x1, %dl
je 0x10f332
movb $0x1, 0x62(%r14)
testb $0x2, %dl
je 0x10f33c
movb $0x1, 0x49(%r14)
movq 0xd0(%rcx), %rdx
testq %rdx, %rdx
je 0x10f3e5
movl 0x78(%rdx), %esi
testl %esi, %esi
jle 0x10f3e5
cmpb $0x0, 0xcd(%rbx)
je 0x10f3e5
movl $0x1, 0x3c(%r14)
cmpl $0x2, %esi
je 0x10f39e
cmpl $0x1, %esi
jne 0x10f3e5
movb $0x0, 0x48(%r14)
movsd 0x80(%rdx), %xmm0
addsd 0x84a5(%rip), %xmm0     # 0x117830
cvttsd2si %xmm0, %rsi
movl %esi, 0x40(%r14)
movsd 0x88(%rdx), %xmm0
jmp 0x10f3d4
movb $0x1, 0x48(%r14)
movsd 0x80(%rdx), %xmm0
movsd 0x5fbd(%rip), %xmm1     # 0x115370
divsd %xmm1, %xmm0
addsd 0x8471(%rip), %xmm0     # 0x117830
cvttsd2si %xmm0, %rsi
movl %esi, 0x40(%r14)
movsd 0x88(%rdx), %xmm0
divsd %xmm1, %xmm0
addsd 0x8454(%rip), %xmm0     # 0x117830
cvttsd2si %xmm0, %rdx
movl %edx, 0x44(%r14)
cmpl $0x0, 0x3c(%r14)
je 0x10f428
movl 0x40(%r14), %edx
movl 0x44(%r14), %esi
cmpl %esi, %edx
jne 0x10f40e
cmpb $0x0, 0x48(%r14)
sete %dil
testl %edx, %edx
sete %r8b
orb %dil, %r8b
je 0x10f412
jmp 0x10f420
testl %edx, %edx
je 0x10f420
leal (%rdx,%rdx,4), %edi
cmpl %edi, %esi
ja 0x10f420
leal (%rsi,%rsi,4), %esi
cmpl %esi, %edx
jbe 0x10f428
movl $0x0, 0x3c(%r14)
movq %rcx, 0x8(%r14)
cmpb $0x0, 0xd6(%rbx)
je 0x10f585
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(%rbx)
jne 0x10f4c4
movl $0x9, 0x130(%rbx)
movb $0x1, 0x12d(%rbx)
leaq 0x42981(%rip), %rsi     # 0x151e13
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x10f4c4
movq %rax, %rdi
callq 0xe9c2f
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 0x10f51d
cmpb $0x0, 0x68(%rax)
je 0x10f502
addq $0x68, %rax
movupd (%rax), %xmm0
movupd %xmm0, 0x50(%r14)
movq 0x10(%r15), %rax
movq 0xd0(%rax), %rax
testq %rax, %rax
je 0x10f51d
cmpb $0x0, 0xe(%rax)
je 0x10f51d
movb $0x1, %cl
movb %cl, 0x61(%r14)
movq 0x98(%rax), %rax
movq %rax, 0x64(%r14)
movb %cl, 0x60(%r14)
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeb499
movq %rax, 0x70(%r14)
movq (%r14), %rdi
movl $0x40, %esi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r15
movq 0x8(%r14), %rdi
leaq 0x428f5(%rip), %rsi     # 0x151e40
movl $0x8, %edx
callq 0xef547
movslq 0x20(%r14), %rsi
movq %r15, %rdi
callq 0xf1026
movslq 0x24(%r14), %rsi
movq %r15, %rdi
callq 0xf1026
cmpb $0x0, 0x62(%r14)
je 0x10f5a8
movq %r15, %rdi
movl $0x1, %esi
callq 0xf0bd6
xorl %eax, %eax
jmp 0x10f5cf
movq 0x18(%rax), %rcx
testq %rcx, %rcx
jle 0x10f439
movq 0x8(%rax), %rdx
cmpq %rdx, %rcx
jge 0x10f439
movl %ecx, 0x20(%r14)
jmp 0x10f442
xorl %eax, %eax
cmpq $0x0, 0x18(%r14)
setne %al
leal 0x8(,%rax,8), %esi
movq %r15, %rdi
callq 0xf0bd6
movslq 0x2c(%r14), %rax
leaq 0x4287c(%rip), %rcx     # 0x151e48
movb (%rax,%rcx), %al
movzbl %al, %esi
movq %r15, %rdi
callq 0xf0bd6
movl $0xd, %esi
movq %r15, %rdi
callq 0xf0f27
movq %r14, %rdi
movq %r15, %rsi
movl $0x49484452, %edx       # imm = 0x49484452
callq 0x10fa9d
cmpb $0x0, 0x49(%r14)
je 0x10f625
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0f27
movl $0x186a0, %esi          # imm = 0x186A0
movq %r15, %rdi
callq 0xf1026
movq %r14, %rdi
movq %r15, %rsi
movl $0x67414d41, %edx       # imm = 0x67414D41
callq 0x10fa9d
cmpl $0x0, 0x3c(%r14)
je 0x10f66b
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0f27
movl 0x40(%r14), %esi
movq %r15, %rdi
callq 0xf1026
movl 0x44(%r14), %esi
movq %r15, %rdi
callq 0xf1026
movzbl 0x48(%r14), %esi
movq %r15, %rdi
callq 0xf0bd6
movq %r14, %rdi
movq %r15, %rsi
movl $0x70485973, %edx       # imm = 0x70485973
callq 0x10fa9d
cmpb $0x0, 0x50(%r14)
je 0x10f710
movq (%r14), %rax
cmpb $0x0, 0xde(%rax)
je 0x10f710
movq %r14, %r12
addq $0x50, %r12
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0f27
leaq 0x18(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0xead46
cmpl $0x0, (%r13)
je 0x10f710
movslq 0x1c(%rsp), %rsi
movq %r15, %rdi
callq 0xf0fad
movb 0x20(%rsp), %al
incb %al
movzbl %al, %esi
movq %r15, %rdi
callq 0xf0bd6
movzbl 0x24(%rsp), %esi
movq %r15, %rdi
callq 0xf0bd6
movzbl 0x28(%rsp), %esi
movq %r15, %rdi
callq 0xf0bd6
movzbl 0x2c(%rsp), %esi
movq %r15, %rdi
callq 0xf0bd6
movzbl 0x30(%rsp), %esi
movq %r15, %rdi
callq 0xf0bd6
movq %r14, %rdi
movq %r15, %rsi
movl $0x74494d45, %edx       # imm = 0x74494D45
callq 0x10fa9d
cmpb $0x0, 0x61(%r14)
je 0x10f75d
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0f27
leaq 0x42728(%rip), %rsi     # 0x151e50
movl $0x10, %edx
movq %r15, %rdi
callq 0xef547
movslq 0x64(%r14), %rsi
movq %r15, %rdi
callq 0xf1096
movslq 0x68(%r14), %rsi
movq %r15, %rdi
callq 0xf1096
movq %r14, %rdi
movq %r15, %rsi
movl $0x68745350, %edx       # imm = 0x68745350
callq 0x10fa9d
movq %rbx, 0x10(%rsp)
cmpb $0x0, 0x60(%r14)
je 0x10f7b5
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0f27
leaq 0x2e478(%rip), %rsi     # 0x13dbf2
movl $0x8, %edx
movq %r15, %rdi
callq 0xef547
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0bd6
leaq 0x3b6e1(%rip), %rsi     # 0x14ae79
movl $0x5, %edx
movq %r15, %rdi
callq 0xef547
movq %r14, %rdi
movq %r15, %rsi
movl $0x74455874, %edx       # imm = 0x74455874
callq 0x10fa9d
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0f27
movq (%r14), %r13
leaq 0x18(%rsp), %rbx
movq $0x0, 0x10(%rbx)
movq %r14, (%rbx)
movq %r15, 0x8(%rbx)
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf13d3
movq %rax, %r12
movq %rbx, 0xa8(%rax)
leaq 0x2ee(%rip), %rax       # 0x10fae1
movq %rax, 0xb0(%r12)
movl 0x38(%r14), %eax
cmpl $0xa, %eax
movl $0xa, %ecx
cmovbl %eax, %ecx
leaq 0x4264f(%rip), %rax     # 0x151e60
movl $0x1000, %edx           # imm = 0x1000
orl (%rax,%rcx,4), %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x10dd3f
movq %rax, %r13
cmpb $0x0, 0x62(%r14)
movslq 0x20(%r14), %rax
movq %r12, 0x8(%rsp)
je 0x10f845
addq $0x7, %rax
shrq $0x3, %rax
movq %rax, 0x78(%r14)
jmp 0x10f85f
movslq 0x2c(%r14), %rcx
imulq %rax, %rcx
movq %rcx, 0x78(%r14)
cmpq $0x0, 0x18(%r14)
je 0x10f85f
addq %rcx, %rcx
movq %rcx, 0x78(%r14)
movl 0x24(%r14), %eax
testl %eax, %eax
jle 0x10f9ec
xorl %r12d, %r12d
leaq 0x42617(%rip), %rbp     # 0x151e8c
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 0x10dd9f
cmpb $0x0, 0x62(%r14)
je 0x10f8b7
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x10f8e2
movq 0x78(%r14), %rdx
xorl %esi, %esi
callq 0x18140
jmp 0x10f8f5
cmpq $0x0, 0x18(%r14)
je 0x10f94c
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x10f961
movq 0x78(%r14), %rdx
xorl %esi, %esi
callq 0x18140
jmp 0x10f974
movq (%r14), %rdi
movq 0x78(%r14), %rsi
callq 0xe8b2b
movq %rax, 0x80(%r14)
movl 0x20(%r14), %ecx
testl %ecx, %ecx
jle 0x10f9c7
xorl %eax, %eax
movq 0x10(%r14), %rdx
movq 0x28(%rdx), %rsi
movl 0x28(%r14), %edi
imull %ebx, %edi
movl 0x24(%rdx), %edx
imull %eax, %edx
addl %edi, %edx
movslq %edx, %rdx
cmpb $0x0, (%rsi,%rdx)
jns 0x10f944
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)
movl 0x20(%r14), %ecx
incl %eax
cmpl %ecx, %eax
jl 0x10f903
jmp 0x10f9c7
movq 0x10(%r14), %rax
movslq 0x28(%r14), %rcx
movslq %ebx, %rsi
imulq %rcx, %rsi
addq 0x28(%rax), %rsi
jmp 0x10f9ce
movq (%r14), %rdi
movq 0x78(%r14), %rsi
callq 0xe8b2b
movq %rax, 0x80(%r14)
cmpl $0x0, 0x30(%r14)
jle 0x10f9c7
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 0x10f980
movq 0x80(%r14), %rsi
movq 0x78(%r14), %rdx
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x10dd9f
incl %r12d
movl 0x24(%r14), %eax
cmpl %eax, %r12d
jl 0x10f875
xorl %ebp, %ebp
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
movl $0x4, %ecx
callq 0x10dd9f
cmpl $0x1, %eax
jne 0x10fa68
cmpq $0x0, 0x18(%r15)
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r12
jg 0x10fa1c
cmpl $0x0, 0x28(%rsp)
jne 0x10fa2c
movq %r14, %rdi
movq %r15, %rsi
movl $0x49444154, %edx       # imm = 0x49444154
callq 0x10fa9d
movq %r13, %rdi
callq 0x10ddc6
movq %r12, %rdi
callq 0xf052b
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0f27
movq %r14, %rdi
movq %r15, %rsi
movl $0x49454e44, %edx       # imm = 0x49454E44
callq 0x10fa9d
movq %r15, %rdi
callq 0xf052b
movl $0x1, %ebp
jmp 0x10f2bb
movq %r13, %rdi
callq 0x10ddc6
movq 0x8(%rsp), %rdi
callq 0xf052b
movq %r15, %rdi
callq 0xf052b
leaq 0x42397(%rip), %rsi     # 0x151e20
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x10f2bb