// File: /workspace/github_c_makefile_star2_O2/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 0x20(%rdx), %r15
andl $0x0, 0x78(%rsi)
movq %rdi, %r12
movq %rdx, 0x18(%rsp)
movq 0x30(%rdx), %rax
pushq $0x10
popq %rcx
cqto
idivq %rcx
movq %rax, %rbx
callq 0xeac74
movq %rax, %rbp
xorl %eax, %eax
testq %rbx, %rbx
cmovleq %rax, %rbx
movq %rbx, 0x30(%rsp)
movl $0x0, 0x4(%rsp)
xorl %eax, %eax
movq %rbp, 0x28(%rsp)
movq %r14, 0x10(%rsp)
cmpq %rax, 0x30(%rsp)
je 0xc7188
movq %rax, 0x38(%rsp)
leal 0x1(%rax), %edx
movq %r12, %rdi
leaq 0x70720(%rip), %rsi     # 0x13754e
xorl %eax, %eax
callq 0xde896
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq 0x18(%r12), %rdi
movl (%r14), %edx
movq %r15, %rsi
callq 0xe41cb
movq %rax, %r13
movq %rbp, %rdi
callq 0xeac85
testl %r13d, %r13d
jns 0xc6e6f
movq %rbp, %rdi
leaq 0x6b55b(%rip), %rsi     # 0x1323c5
callq 0xeb884
btl $0x1e, %r13d
jae 0xc6e85
movq %rbp, %rdi
leaq 0x6b556(%rip), %rsi     # 0x1323d6
callq 0xeb884
btl $0x1d, %r13d
jae 0xc6e9b
movq %rbp, %rdi
leaq 0x6b550(%rip), %rsi     # 0x1323e6
callq 0xeb884
testl $0x7000000, %r13d       # imm = 0x7000000
jne 0xc6eb3
movq %rbp, %rdi
leaq 0x3e6a9(%rip), %rsi     # 0x105557
callq 0xeb884
movl %r13d, %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
leaq 0x6b539(%rip), %rsi     # 0x1323fb
cmpl $0x30000, %eax          # imm = 0x30000
je 0xc6f11
cmpl $0x10002, %eax          # imm = 0x10002
je 0xc6f39
cmpl $0x20001, %eax          # imm = 0x20001
je 0xc6ef8
cmpl $0x20002, %eax          # imm = 0x20002
je 0xc6f0a
cmpl $0x20003, %eax          # imm = 0x20003
je 0xc6f01
cmpl $0x10001, %eax          # imm = 0x10001
jne 0xc6f26
movq %rbp, %rdi
leaq 0x6b51f(%rip), %rsi     # 0x132415
jmp 0xc6f43
leaq 0x6b546(%rip), %rsi     # 0x132445
jmp 0xc6f11
leaq 0x6b574(%rip), %rsi     # 0x13247c
jmp 0xc6f11
leaq 0x6b54f(%rip), %rsi     # 0x132460
movq %rbp, %rdi
callq 0xeb884
incl 0x78(%r14)
leaq 0x6b6e4(%rip), %rcx     # 0x132608
jmp 0xc6f4f
addl $0xfffeffff, %eax       # imm = 0xFFFEFFFF
leaq 0x6b6c9(%rip), %rcx     # 0x1325fb
cmpl $0x2, %eax
jae 0xc6f19
jmp 0xc6f4f
movq %rbp, %rdi
leaq 0x6b4ea(%rip), %rsi     # 0x13242d
callq 0xeb884
leaq 0x6b6ac(%rip), %rcx     # 0x1325fb
movq %rcx, 0x20(%rsp)
movq %rbp, %rdi
callq 0xeb7e4
movq %r12, %rdi
leaq 0x6b534(%rip), %rsi     # 0x13249a
movl %r13d, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r12), %rdi
movl (%r14), %edx
leaq 0x4(%r15), %rsi
callq 0xe41cb
movq %rax, %r13
movq %r12, %rdi
leaq 0x6b524(%rip), %rsi     # 0x1324b5
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r12), %rdi
movl (%r14), %edx
addq $0x8, %r15
movq %r15, %rsi
callq 0xe41cb
movq %rax, %rbx
testq %rax, %rax
je 0xc6fe2
movq 0x70(%r14), %rax
testq %rax, %rax
leaq 0x40(%rsp), %r14
je 0xc6fcb
testb $0x1, 0x8(%rax)
jne 0xc6ffd
movq %r14, %rdi
leaq 0x57f0c(%rip), %rsi     # 0x11eee1
pushq $0x50
popq %rdx
callq 0xde492
movq %rbx, %rbp
jmp 0xc701b
leaq 0x40(%rsp), %r14
movq %r14, %rdi
leaq 0x6b4d3(%rip), %rsi     # 0x1324c4
pushq $0x50
popq %rdx
callq 0xde492
xorl %ebp, %ebp
jmp 0xc701b
movq 0x18(%rax), %rbp
addq %rbx, %rbp
movq %r14, %rdi
pushq $0x50
popq %rsi
leaq 0x6b4cd(%rip), %rdx     # 0x1324de
movq %rbp, %rcx
xorl %eax, %eax
callq 0xde40c
movq %r12, %rdi
leaq 0x6b4cd(%rip), %rsi     # 0x1324f2
movl %ebx, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0xde896
testq %rbx, %rbx
jle 0xc70c1
movq 0x18(%rsp), %rax
movq (%rax), %rbx
movq %r12, %rdi
leaq 0x6b4ec(%rip), %rsi     # 0x132538
movl %ebp, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0xde9f1
movq 0x10(%rsp), %r14
movq 0x70(%r14), %rax
testq %rax, %rax
je 0xc70c6
testb $0x1, 0x8(%rax)
je 0xc70c6
movq %rbx, 0x8(%rsp)
movq 0x20(%rax), %rbx
testq %rbx, %rbx
sete %al
testq %rbp, %rbp
setle %cl
orb %al, %cl
jne 0xc70c6
movq %rbp, %r8
addq %r13, %r8
movq 0x18(%rbx), %r9
cmpq %r9, %r8
jle 0xc712f
cmpl $0x0, 0x4(%rsp)
jne 0xc70c6
movq %r12, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
leaq 0x6b4b0(%rip), %rcx     # 0x132561
xorl %eax, %eax
callq 0xc52e6
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
jmp 0xc70c6
movq 0x10(%rsp), %r14
movq 0x18(%r12), %rdi
movl (%r14), %edx
leaq 0x4(%r15), %rsi
callq 0xe41b1
movq %r12, %rdi
leaq 0x6b427(%rip), %rsi     # 0x132508
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r12), %rdi
movl (%r14), %edx
leaq 0x6(%r15), %rsi
callq 0xe41b1
movq %r12, %rdi
leaq 0x6b41b(%rip), %rsi     # 0x132520
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %r12, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
addq $0x8, %r15
movq 0x38(%rsp), %rax
incq %rax
movq 0x28(%rsp), %rbp
jmp 0xc6e11
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x4040d(%rip), %rdx     # 0x107549
pushq $0x3
popq %rcx
callq 0xe4d48
testl %eax, %eax
je 0xc7169
movq %r12, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
leaq 0x6b451(%rip), %rcx     # 0x1325ab
movq %rbp, %r8
xorl %eax, %eax
callq 0xc52e6
jmp 0xc70c6
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq 0x20(%rsp), %rcx
xorl %r8d, %r8d
pushq $0x1
popq %r9
callq 0xe4e0b
jmp 0xc70c6
movq %rbp, %rdi
callq 0xeaf3c
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq