// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/png.c
// Function: prepare_PNG_extraction_from_APNG
static void prepare_PNG_extraction_from_APNG(deark *c, lctx *d)
{
	dbuf *outf = NULL;
	i64 srcpos;
	int found_IDAT = 0;

	if(!d->is_APNG) return;
	if(!d->extract_from_APNG_enabled) return;
	if(d->extracted_main_APNG_image) return;
	if(d->APNG_prefix) return;
	d->extracted_main_APNG_image = 1;

	d->APNG_prefix = dbuf_create_membuf(c, 0, 0);
#define MAX_APNG_PREFIX_LEN 4000
	dbuf_set_length_limit(d->APNG_prefix, MAX_APNG_PREFIX_LEN+1);

	dbuf_write(d->APNG_prefix, g_png_sig, 8);

	// This is a bit crude, but simple enough. Just read the entire file in a
	// separate pass, copying everything that isn't a special APNG chunk.
	de_dbg2(c, "[extracting main image from APNG]");

	// If an fcTL chunk has not appeared yet, then this is the fallback image
	// that will only be seen if the viewer does not support APNG.
	outf = dbuf_create_output_file(c, (d->found_fcTL ? "png" : "default.png"), NULL, 0);

	dbuf_write(outf, g_png_sig, 8);
	srcpos = 8;
	while(1) {
		i64 ck_dlen;
		i64 ck_len;
		u32 ck_id;
		int copy_chunk_to_default;
		int copy_chunk_to_prefix = 0;

		if(srcpos+12 > c->infile->len) goto done;
		ck_dlen = de_getu32be(srcpos);
		ck_len = ck_dlen + 12;
		ck_id = (u32)de_getu32be(srcpos+4);
		if(srcpos + ck_len > c->infile->len) goto done;

		if(ck_id==CODE_IDAT) {
			found_IDAT = 1;
		}

		if(ck_id==CODE_acTL || ck_id==CODE_fcTL || ck_id==CODE_fdAT) {
			copy_chunk_to_default = 0;
		}
		else {
			copy_chunk_to_default = 1;
		}

		if(d->extract_from_APNG_enabled && !found_IDAT) {
			// This prefix could be written to many frame files, so we want it
			// to be small. We'll be selective about which chunks we put in it
			// (no iCCP -- too big).
			// Note that the main image file will still include all the chunks we
			// filter out.
			switch(ck_id) {
			case CODE_PLTE:
			case CODE_tRNS:
			case CODE_cHRM:
			case CODE_gAMA:
			case CODE_sBIT:
			case CODE_sRGB:
			case CODE_bKGD:
			case CODE_pHYs:
				copy_chunk_to_prefix = 1;
				break;
			case CODE_IHDR:
				if(ck_dlen==13 && d->APNG_prefix_IHDR_pos==0) {
					d->APNG_prefix_IHDR_pos = d->APNG_prefix->len;
					copy_chunk_to_prefix = 1;
				}
				break;
			}
		}

		if(copy_chunk_to_default) {
			dbuf_copy(c->infile, srcpos, ck_len, outf);
		}

		if(copy_chunk_to_prefix) {
			dbuf_copy(c->infile, srcpos, ck_len, d->APNG_prefix);
		}
		srcpos += ck_len;
	}

done:
	if(d->APNG_prefix) {
		if(d->APNG_prefix->len > MAX_APNG_PREFIX_LEN) {
			d->extract_from_APNG_enabled = 0;
		}
	}
	dbuf_close(outf);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x8(%rax)
jne 0x10cdde
jmp 0x10d0b0
movq -0x10(%rbp), %rax
cmpb $0x0, 0xa(%rax)
jne 0x10cded
jmp 0x10d0b0
movq -0x10(%rbp), %rax
cmpb $0x0, 0xb(%rax)
je 0x10cdfc
jmp 0x10d0b0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x10ce0c
jmp 0x10d0b0
movq -0x10(%rbp), %rax
movb $0x1, 0xb(%rax)
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x168020
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movl $0xfa1, %esi            # imm = 0xFA1
callq 0x168120
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
leaq 0xb5fdd(%rip), %rsi     # 0x1c2e2c
movl $0x8, %edx
callq 0x166170
movq -0x8(%rbp), %rdi
leaq 0xb629d(%rip), %rsi     # 0x1c3101
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0xd(%rax), %ecx
leaq 0xb62a5(%rip), %rsi     # 0x1c3123
leaq 0xa7053(%rip), %rax     # 0x1b3ed8
cmpl $0x0, %ecx
cmovneq %rax, %rsi
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq 0xb5f86(%rip), %rsi     # 0x1c2e2c
movl $0x8, %edx
callq 0x166170
movq $0x8, -0x20(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x20(%rbp), %rax
addq $0xc, %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x10ceda
jmp 0x10d080
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x165470
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x4, %rsi
callq 0x165470
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rax
addq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x10cf2e
jmp 0x10d080
cmpl $0x49444154, -0x3c(%rbp) # imm = 0x49444154
jne 0x10cf3e
movl $0x1, -0x24(%rbp)
cmpl $0x6163544c, -0x3c(%rbp) # imm = 0x6163544C
je 0x10cf59
cmpl $0x6663544c, -0x3c(%rbp) # imm = 0x6663544C
je 0x10cf59
cmpl $0x66644154, -0x3c(%rbp) # imm = 0x66644154
jne 0x10cf62
movl $0x0, -0x40(%rbp)
jmp 0x10cf69
movl $0x1, -0x40(%rbp)
movq -0x10(%rbp), %rax
movzbl 0xa(%rax), %eax
cmpl $0x0, %eax
je 0x10d02d
cmpl $0x0, -0x24(%rbp)
jne 0x10d02d
movl -0x3c(%rbp), %eax
movl %eax, -0x48(%rbp)
subl $0x49484452, %eax       # imm = 0x49484452
je 0x10cffc
jmp 0x10cf93
movl -0x48(%rbp), %eax
subl $0x504c5445, %eax       # imm = 0x504C5445
je 0x10cff3
jmp 0x10cf9f
movl -0x48(%rbp), %eax
subl $0x624b4744, %eax       # imm = 0x624B4744
je 0x10cff3
jmp 0x10cfab
movl -0x48(%rbp), %eax
subl $0x6348524d, %eax       # imm = 0x6348524D
je 0x10cff3
jmp 0x10cfb7
movl -0x48(%rbp), %eax
subl $0x67414d41, %eax       # imm = 0x67414D41
je 0x10cff3
jmp 0x10cfc3
movl -0x48(%rbp), %eax
subl $0x70485973, %eax       # imm = 0x70485973
je 0x10cff3
jmp 0x10cfcf
movl -0x48(%rbp), %eax
subl $0x73424954, %eax       # imm = 0x73424954
je 0x10cff3
jmp 0x10cfdb
movl -0x48(%rbp), %eax
subl $0x73524742, %eax       # imm = 0x73524742
je 0x10cff3
jmp 0x10cfe7
movl -0x48(%rbp), %eax
subl $0x74524e53, %eax       # imm = 0x74524E53
jne 0x10d02b
jmp 0x10cff3
movl $0x1, -0x44(%rbp)
jmp 0x10d02b
cmpq $0xd, -0x30(%rbp)
jne 0x10d029
movq -0x10(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x10d029
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movl $0x1, -0x44(%rbp)
jmp 0x10d02b
jmp 0x10d02d
cmpl $0x0, -0x40(%rbp)
je 0x10d04c
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x166070
cmpl $0x0, -0x44(%rbp)
je 0x10d06f
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
callq 0x166070
movq -0x38(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x10ceb8
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x10d0a7
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
cmpq $0xfa0, 0x18(%rax)      # imm = 0xFA0
jle 0x10d0a5
movq -0x10(%rbp), %rax
movb $0x0, 0xa(%rax)
jmp 0x10d0a7
movq -0x18(%rbp), %rdi
callq 0x1676a0
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)