// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/tiff.c
// Function: handler_mpentry
static void handler_mpentry(deark *c, lctx *d, const struct taginfo *tg, const struct tagnuminfo *tni)
{
	i64 num_entries;
	i64 k;
	i64 pos = tg->val_offset;
	de_ucstring *s = NULL;
	struct mpfctx_struct mpfctx;

	de_zeromem(&mpfctx, sizeof(struct mpfctx_struct));
	d->mpf_main_image_count = 0;
	// Length is supposed to be 16x{NumberOfImages; tag 45057}. We'll just assume
	// it's correct.
	num_entries = tg->total_size/16;

	s = ucstring_create(c);
	for(k=0; k<num_entries; k++) {
		i64 n;
		i64 imgoffs_rel, imgoffs_abs;
		i64 imgsize;
		u32 attrs;
		u32 dataformat;
		u32 typecode;
		char offset_descr[80];

		de_dbg(c, "entry #%d", (int)(k+1));
		de_dbg_indent(c, 1);

		attrs = (u32)dbuf_getu32x(c->infile, pos, d->is_le);
		dataformat = (attrs&0x07000000)>>24;
		typecode = attrs&0x00ffffff;
		ucstring_empty(s);
		if(attrs&0x80000000U) ucstring_append_flags_item(s, "dependent parent");
		if(attrs&0x40000000U) ucstring_append_flags_item(s, "dependent child");
		if(attrs&0x20000000U) ucstring_append_flags_item(s, "representative image");
		if(dataformat==0) ucstring_append_flags_item(s, "JPEG");
		if(typecode==0x030000U) ucstring_append_flags_item(s, "baseline MP primary image");
		if(typecode==0x010001U) ucstring_append_flags_item(s, "large thumbnail class 1");
		if(typecode==0x010002U) ucstring_append_flags_item(s, "large thumbnail class 2");
		if(typecode==0x020001U) ucstring_append_flags_item(s, "multi-frame image panorama");
		if(typecode==0x020002U) ucstring_append_flags_item(s, "multi-frame image disparity");
		if(typecode==0x020003U) ucstring_append_flags_item(s, "multi-frame image multi-angle");

		if(typecode==0x010001U || typecode==0x010002U) {
			mpfctx.is_thumb = 1;
		}
		else {
			// Count that number of non-thumbnail images
			d->mpf_main_image_count++;
			mpfctx.is_thumb = 0;
		}

		de_dbg(c, "image attribs: 0x%08x (%s)", (unsigned int)attrs,
			ucstring_getpsz(s));

		imgsize = dbuf_getu32x(c->infile, pos+4, d->is_le);
		de_dbg(c, "image size: %u", (unsigned int)imgsize);

		imgoffs_rel = dbuf_getu32x(c->infile, pos+8, d->is_le);
		// This is relative to beginning of the payload data (the TIFF header)
		// of the MPF segment, except that 0 is a special case.
		if(imgoffs_rel==0) {
			imgoffs_abs = 0;
			de_strlcpy(offset_descr, "refers to the first image", sizeof(offset_descr));
		}
		else if(d->in_params && (d->in_params->flags&0x01)) {
			imgoffs_abs = d->in_params->offset_in_parent+imgoffs_rel;
			de_snprintf(offset_descr, sizeof(offset_descr), "absolute offset %"I64_FMT,
				imgoffs_abs);
		}
		else {
			imgoffs_abs = imgoffs_rel;
			de_strlcpy(offset_descr, "?", sizeof(offset_descr));
		}
		de_dbg(c, "image offset: %u (%s)", (unsigned int)imgoffs_rel, offset_descr);

		if(imgoffs_rel>0) {
			mpfctx.imgoffs_abs = imgoffs_abs;
			mpfctx.imgsize = imgsize;
			try_to_extract_mpf_image(c, d, tg->pg, &mpfctx);
		}

		n = dbuf_getu16x(c->infile, pos+12, d->is_le);
		de_dbg(c, "dep. image #1 entry: %u", (unsigned int)n);
		n = dbuf_getu16x(c->infile, pos+14, d->is_le);
		de_dbg(c, "dep. image #2 entry: %u", (unsigned int)n);
		de_dbg_indent(c, -1);
		pos += 16;
	}
	ucstring_destroy(s);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq 0x20(%rdx), %r12
movl $0x0, 0x78(%rsi)
movq %rdx, 0x28(%rsp)
movq 0x30(%rdx), %rbx
leaq 0xf(%rbx), %rax
testq %rbx, %rbx
cmovnsq %rbx, %rax
movq %rax, 0x20(%rsp)
callq 0xf6410
movq %rax, %r13
cmpq $0x10, %rbx
jl 0xcf559
sarq $0x4, 0x20(%rsp)
xorl %edx, %edx
movl $0x0, 0xc(%rsp)
movq %r13, (%rsp)
movq %r14, 0x18(%rsp)
incq %rdx
movq %r15, %rdi
leaq 0x757d8(%rip), %rsi     # 0x14499e
movq %rdx, 0x38(%rsp)
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%r15), %rdi
movl (%r14), %edx
movq %r12, %rsi
callq 0xeeff3
movq %rax, %rbp
movq %r13, %rdi
callq 0xf6423
testl %ebp, %ebp
jns 0xcf20c
movq %r13, %rdi
leaq 0x704ee(%rip), %rsi     # 0x13f6f5
callq 0xf7105
btl $0x1e, %ebp
jae 0xcf221
movq %r13, %rdi
leaq 0x704ea(%rip), %rsi     # 0x13f706
callq 0xf7105
btl $0x1d, %ebp
jae 0xcf236
movq %r13, %rdi
leaq 0x704e5(%rip), %rsi     # 0x13f716
callq 0xf7105
testl $0x7000000, %ebp        # imm = 0x7000000
jne 0xcf24d
movq %r13, %rdi
leaq 0x4332f(%rip), %rsi     # 0x112577
callq 0xf7105
movl %ebp, %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl $0x20001, %eax          # imm = 0x20001
jg 0xcf279
cmpl $0x10001, %eax          # imm = 0x10001
je 0xcf2a8
cmpl $0x10002, %eax          # imm = 0x10002
je 0xcf2bd
cmpl $0x20001, %eax          # imm = 0x20001
jne 0xcf295
leaq 0x704fe(%rip), %rsi     # 0x13f775
jmp 0xcf2dc
cmpl $0x20002, %eax          # imm = 0x20002
je 0xcf2b4
cmpl $0x20003, %eax          # imm = 0x20003
je 0xcf2d5
leaq 0x7049d(%rip), %rsi     # 0x13f72b
cmpl $0x30000, %eax          # imm = 0x30000
je 0xcf2dc
addl $0xfffeffff, %eax       # imm = 0xFFFEFFFF
leaq 0x7068a(%rip), %rcx     # 0x13f92b
cmpl $0x2, %eax
jae 0xcf2e4
jmp 0xcf2ef
movq %r13, %rdi
leaq 0x70493(%rip), %rsi     # 0x13f745
jmp 0xcf2c7
leaq 0x704d5(%rip), %rsi     # 0x13f790
jmp 0xcf2dc
movq %r13, %rdi
leaq 0x70496(%rip), %rsi     # 0x13f75d
callq 0xf7105
leaq 0x70658(%rip), %rcx     # 0x13f92b
jmp 0xcf2ef
leaq 0x704d0(%rip), %rsi     # 0x13f7ac
movq %r13, %rdi
callq 0xf7105
incl 0x78(%r14)
leaq 0x70649(%rip), %rcx     # 0x13f938
movq %rcx, 0x30(%rsp)
movq %r13, %rdi
callq 0xf705c
movq %r15, %rdi
leaq 0x704c4(%rip), %rsi     # 0x13f7ca
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r15), %rdi
movl (%r14), %edx
leaq 0x4(%r12), %rsi
callq 0xeeff3
movq %rax, %rbp
movq %r15, %rdi
leaq 0x704b5(%rip), %rsi     # 0x13f7e5
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r15), %rdi
movl (%r14), %edx
addq $0x8, %r12
movq %r12, %rsi
callq 0xeeff3
testq %rax, %rax
je 0xcf381
movq %rax, %rbx
movq 0x70(%r14), %rax
testq %rax, %rax
leaq 0x40(%rsp), %rdi
je 0xcf368
testb $0x1, 0x8(%rax)
jne 0xcf3b5
movl $0x50, %edx
leaq 0x5cddd(%rip), %rsi     # 0x12c151
movq %rdi, %r14
callq 0xe8219
movq %rbx, %r13
jmp 0xcf3d5
movl $0x50, %edx
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
leaq 0x7045f(%rip), %rsi     # 0x13f7f4
callq 0xe8219
movq %r15, %rdi
leaq 0x7047e(%rip), %rsi     # 0x13f822
xorl %edx, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xe8617
jmp 0xcf4f0
movq 0x18(%rax), %r13
addq %rbx, %r13
movl $0x50, %esi
leaq 0x70446(%rip), %rdx     # 0x13f80e
movq %rdi, %r14
movq %r13, %rcx
xorl %eax, %eax
callq 0xe8193
movq %r15, %rdi
leaq 0x70443(%rip), %rsi     # 0x13f822
movl %ebx, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0xe8617
testq %rbx, %rbx
jle 0xcf48c
movq 0x28(%rsp), %rax
movq (%rax), %rbx
movq %r15, %rdi
leaq 0x70462(%rip), %rsi     # 0x13f868
movl %r13d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe87e4
movq 0x18(%rsp), %r14
movq 0x70(%r14), %rax
testq %rax, %rax
je 0xcf4ec
testb $0x1, 0x8(%rax)
je 0xcf4ec
movq %rbx, 0x10(%rsp)
movq 0x20(%rax), %rbx
testq %rbx, %rbx
sete %al
testq %r13, %r13
setle %cl
orb %al, %cl
jne 0xcf4ec
movq %r13, %r8
addq %rbp, %r8
movq 0x18(%rbx), %r9
cmpq %r9, %r8
jle 0xcf497
cmpl $0x0, 0xc(%rsp)
movq (%rsp), %r13
jne 0xcf4f0
movq %r15, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
leaq 0x70416(%rip), %rcx     # 0x13f891
xorl %eax, %eax
callq 0xcd067
movl $0x1, 0xc(%rsp)
jmp 0xcf4f0
movq (%rsp), %r13
movq 0x18(%rsp), %r14
jmp 0xcf4f0
movl $0x3, %ecx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x45100(%rip), %rdx     # 0x1145a9
callq 0xefc85
testl %eax, %eax
je 0xcf4d0
movq %r15, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
leaq 0x70417(%rip), %rcx     # 0x13f8db
movq %r13, %r8
xorl %eax, %eax
callq 0xcd067
jmp 0xcf4ec
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq 0x30(%rsp), %rcx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xefd48
movq (%rsp), %r13
movq 0x18(%r15), %rdi
movl (%r14), %edx
leaq 0x4(%r12), %rsi
callq 0xeef43
movq %r15, %rdi
leaq 0x7032d(%rip), %rsi     # 0x13f838
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r15), %rdi
movl (%r14), %edx
leaq 0x6(%r12), %rsi
callq 0xeef43
movq %r15, %rdi
leaq 0x70321(%rip), %rsi     # 0x13f850
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
addq $0x8, %r12
movq 0x38(%rsp), %rdx
cmpq %rdx, 0x20(%rsp)
jne 0xcf1b9
movq %r13, %rdi
callq 0xf6753
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq