// File: /workspace/github_c_makefile_star2_O1/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, %r13
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 0xf6870
cmpq $0x10, %rbx
movq %rax, %rbx
jl 0xd0171
sarq $0x4, 0x20(%rsp)
xorl %edx, %edx
movl $0x0, 0x14(%rsp)
movq %r13, (%rsp)
movq %rbx, 0x8(%rsp)
incq %rdx
movq %r15, %rdi
leaq 0x746e6(%rip), %rsi     # 0x1444be
movq %rdx, 0x38(%rsp)
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%r15), %rdi
movl (%r13), %edx
movq %r12, %rsi
callq 0xef614
movq %rax, %rbp
movq %rbx, %rdi
callq 0xf6883
testl %ebp, %ebp
jns 0xcfe1f
movq %rbx, %rdi
leaq 0x6f4db(%rip), %rsi     # 0x13f2f5
callq 0xf7566
btl $0x1e, %ebp
jae 0xcfe34
movq %rbx, %rdi
leaq 0x6f4d7(%rip), %rsi     # 0x13f306
callq 0xf7566
btl $0x1d, %ebp
jae 0xcfe49
movq %rbx, %rdi
leaq 0x6f4d2(%rip), %rsi     # 0x13f316
callq 0xf7566
testl $0x7000000, %ebp        # imm = 0x7000000
jne 0xcfe60
movq %rbx, %rdi
leaq 0x4270c(%rip), %rsi     # 0x112567
callq 0xf7566
movl %ebp, %r14d
andl $0xffffff, %r14d        # imm = 0xFFFFFF
cmpl $0x20001, %r14d         # imm = 0x20001
jg 0xcfe97
cmpl $0x10001, %r14d         # imm = 0x10001
je 0xcfebb
cmpl $0x10002, %r14d         # imm = 0x10002
je 0xcfecd
cmpl $0x20001, %r14d         # imm = 0x20001
jne 0xcfee5
leaq 0x6f4e0(%rip), %rsi     # 0x13f375
jmp 0xcfedd
cmpl $0x20002, %r14d         # imm = 0x20002
je 0xcfec4
cmpl $0x20003, %r14d         # imm = 0x20003
je 0xcfed6
leaq 0x6f47b(%rip), %rsi     # 0x13f32b
cmpl $0x30000, %r14d         # imm = 0x30000
je 0xcfedd
jmp 0xcfee5
leaq 0x6f483(%rip), %rsi     # 0x13f345
jmp 0xcfedd
leaq 0x6f4c5(%rip), %rsi     # 0x13f390
jmp 0xcfedd
leaq 0x6f489(%rip), %rsi     # 0x13f35d
jmp 0xcfedd
leaq 0x6f4cf(%rip), %rsi     # 0x13f3ac
movq %rbx, %rdi
callq 0xf7566
addl $0xfffefffd, %r14d      # imm = 0xFFFEFFFD
cmpl $-0x3, %r14d
ja 0xcfef6
incl 0x78(%r13)
movq %rbx, %rdi
callq 0xf74bd
movq %r15, %rdi
leaq 0x6f4c2(%rip), %rsi     # 0x13f3ca
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r15), %rdi
movl (%r13), %edx
leaq 0x4(%r12), %rsi
callq 0xef614
movq %rax, %rbp
movq %r15, %rdi
leaq 0x6f4b2(%rip), %rsi     # 0x13f3e5
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r15), %rdi
movl (%r13), %edx
addq $0x8, %r12
movq %r12, %rsi
callq 0xef614
movq %rax, %rbx
testq %rax, %rax
movq %rbp, 0x30(%rsp)
je 0xcff8a
movq 0x70(%r13), %rax
testq %rax, %rax
leaq 0x40(%rsp), %rdi
je 0xcff71
testb $0x1, 0x8(%rax)
jne 0xcffa8
movl $0x50, %edx
leaq 0x5beec(%rip), %rsi     # 0x12be69
movq %rdi, %rbp
callq 0xe8ded
movq %rbx, %r13
jmp 0xcffc8
movl $0x50, %edx
leaq 0x40(%rsp), %rbp
movq %rbp, %rdi
leaq 0x6f456(%rip), %rsi     # 0x13f3f4
callq 0xe8ded
xorl %r13d, %r13d
jmp 0xcffc8
movq 0x18(%rax), %r13
addq %rbx, %r13
movl $0x50, %esi
leaq 0x6f453(%rip), %rdx     # 0x13f40e
movq %rdi, %rbp
movq %r13, %rcx
xorl %eax, %eax
callq 0xe8d67
movq %r15, %rdi
leaq 0x6f450(%rip), %rsi     # 0x13f422
movl %ebx, %edx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe9226
testq %rbx, %rbx
jle 0xd003d
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movq %r15, %rdi
leaq 0x6f46e(%rip), %rsi     # 0x13f468
movl %r13d, %edx
movq 0x30(%rsp), %rbp
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe93cf
movq (%rsp), %rax
movq 0x70(%rax), %rax
testq %rax, %rax
movq 0x8(%rsp), %rbx
je 0xd0023
testb $0x1, 0x8(%rax)
jne 0xd0029
movq (%rsp), %r13
jmp 0xd0046
movq 0x20(%rax), %rbx
testq %rbx, %rbx
sete %al
testq %r13, %r13
setle %cl
orb %al, %cl
je 0xd00b6
movq (%rsp), %r13
movq 0x8(%rsp), %rbx
movq 0x18(%r15), %rdi
movl (%r13), %edx
leaq 0x4(%r12), %rsi
callq 0xef570
movq %r15, %rdi
leaq 0x6f3d6(%rip), %rsi     # 0x13f438
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r15), %rdi
movl (%r13), %edx
leaq 0x6(%r12), %rsi
callq 0xef570
movq %r15, %rdi
leaq 0x6f3c9(%rip), %rsi     # 0x13f450
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
addq $0x8, %r12
movq 0x38(%rsp), %rdx
cmpq %rdx, 0x20(%rsp)
jne 0xcfdcb
jmp 0xd0171
movq %r13, %r8
addq %rbp, %r8
movq 0x18(%rbx), %r9
cmpq %r9, %r8
jle 0xd00ff
cmpl $0x0, 0x14(%rsp)
movq (%rsp), %r13
movq 0x8(%rsp), %rbx
jne 0xd0046
movq %r15, %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
leaq 0x6f3a6(%rip), %rcx     # 0x13f491
xorl %eax, %eax
callq 0xcdc5c
movl $0x1, 0x14(%rsp)
jmp 0xd0046
movl $0x3, %ecx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x443e8(%rip), %rdx     # 0x1144f9
callq 0xf025e
testl %eax, %eax
je 0xd013f
movq %r15, %rdi
movq (%rsp), %rsi
movq 0x18(%rsp), %rdx
leaq 0x6f3ae(%rip), %rcx     # 0x13f4db
movq %r13, %r8
movq %rsi, %r13
xorl %eax, %eax
callq 0xcdc5c
jmp 0xd0041
cmpl $-0x2, %r14d
leaq 0x6f3e1(%rip), %rcx     # 0x13f52b
leaq 0x6f3e7(%rip), %rax     # 0x13f538
cmovbq %rax, %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xf0321
jmp 0xd003d
movq %rbx, %rdi
callq 0xf6bb0
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq