// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/bmff.c
// Function: do_box_iloc
static void do_box_iloc(deark *c, lctx *d, struct de_boxesctx *bctx)
{
	u8 version;
	struct de_boxdata *curbox = bctx->curbox;
	i64 pos = curbox->payload_pos;
	unsigned int u;
	unsigned int offset_size, length_size, base_offset_size, index_size;
	i64 item_count;
	i64 k;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	do_read_version_and_flags(c, d, bctx, &version, NULL, 1);
	pos += 4;

	// TODO: Support more versions
	if(version!=1) goto done;

	u = (unsigned int)dbuf_getbyte_p(bctx->f, &pos);
	offset_size = u>>4;
	de_dbg(c, "offset size: %u", offset_size);
	if(offset_size!=0 && offset_size!=4 && offset_size!=8) goto done;
	length_size = u&0xf;
	de_dbg(c, "length size: %u", length_size);
	if(length_size!=0 && length_size!=4 && length_size!=8) goto done;

	u = (unsigned int)dbuf_getbyte_p(bctx->f, &pos);
	base_offset_size = u>>4;
	de_dbg(c, "base offset size: %u", base_offset_size);
	if(base_offset_size!=0 && base_offset_size!=4 && base_offset_size!=8) goto done;
	index_size =  u&0xf;
	de_dbg(c, "index size: %u", index_size);
	if(index_size!=0 && index_size!=4 && index_size!=8) goto done;

	item_count = dbuf_getu16be_p(bctx->f, &pos);
	de_dbg(c, "item count: %d", (int)item_count);

	for(k=0; k<item_count; k++) {
		unsigned int item_id;
		i64 extent_count;
		i64 e;
		unsigned int cnstr_meth;

		if(pos >= curbox->payload_pos+curbox->payload_len) goto done;
		de_dbg(c, "item[%d] at %"I64_FMT, (int)k, pos);
		de_dbg_indent(c, 1);
		item_id = (unsigned int)dbuf_getu16be_p(bctx->f, &pos);
		de_dbg(c, "item id: %u", item_id);

		u = (unsigned int)dbuf_getu16be_p(bctx->f, &pos);
		cnstr_meth = u&0xf;
		de_dbg(c, "construction method: %u", cnstr_meth);

		pos += 2; // data reference index
		pos += base_offset_size;

		extent_count = dbuf_getu16be_p(bctx->f, &pos);
		de_dbg(c, "extent count: %d", (int)extent_count);

		for(e=0; e<extent_count; e++) {
			i64 xoffs = 0;
			i64 xlen = 0;

			if(pos >= curbox->payload_pos+curbox->payload_len) goto done;
			de_dbg(c, "extent[%d]", (int)e);
			de_dbg_indent(c, 1);
			pos += index_size;

			if(offset_size>0) {
				xoffs = dbuf_getint_ext(bctx->f, pos, offset_size, 0, 0);
				de_dbg(c, "offset: %"I64_FMT, xoffs);
				de_sanitize_offset(&xoffs);
			}
			pos += offset_size;

			if(length_size>0) {
				xlen = dbuf_getint_ext(bctx->f, pos, length_size, 0, 0);
				de_dbg(c, "length: %"I64_FMT, xlen);
				de_sanitize_length(&xlen);
			}
			pos += length_size;

			if(d->exif_item_id_known && item_id==d->exif_item_id && extent_count==1) {
				de_dbg(c, "[Exif item]");
				d->exif_item_offs = xoffs;
				d->exif_item_len = xlen;
			}

			de_dbg_indent(c, -1);
		}

		de_dbg_indent(c, -1);
	}

done:
	if(d->exif_item_id_known) {
		extract_exif_item(c, d, bctx->f);
	}
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %rbx
movq 0x28(%rdx), %r13
movq 0x38(%r13), %r14
leaq 0x4c(%rsp), %rsi
callq 0xdebae
leaq 0x2f(%rsp), %r12
movq %rbx, %rdi
movq %rbp, 0x20(%rsp)
movq %rbp, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x2c79e
addq $0x4, %r14
movq %r14, 0x10(%rsp)
cmpb $0x1, (%r12)
jne 0x2a9ae
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe3f2a
movb %al, 0xf(%rsp)
movzbl %al, %r12d
movl %r12d, %edx
shrl $0x4, %edx
leaq 0xde27e(%rip), %rsi     # 0x108b66
movq %rbx, %rdi
movl %edx, 0x1c(%rsp)
xorl %eax, %eax
callq 0xde896
cmpb $0x10, %r12b
jb 0x2a90e
cmpl $0x8, 0x1c(%rsp)
je 0x2a90e
cmpl $0x4, 0x1c(%rsp)
jne 0x2a9ae
andl $0xf, %r12d
leaq 0xde238(%rip), %rsi     # 0x108b51
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
testb $0xb, 0xf(%rsp)
setne %al
cmpl $0x8, %r12d
setne %cl
testb %cl, %al
jne 0x2a9ae
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe3f2a
movzbl %al, %r14d
movl %r14d, %edx
shrl $0x4, %edx
leaq 0xde204(%rip), %rsi     # 0x108b61
movq %rbx, %rdi
movq %rdx, 0x30(%rsp)
xorl %eax, %eax
callq 0xde896
cmpb $0x10, %r14b
jb 0x2a980
cmpl $0x8, 0x30(%rsp)
je 0x2a980
cmpl $0x4, 0x30(%rsp)
jne 0x2a9ae
movl %r14d, %ebp
andl $0xf, %ebp
leaq 0xde1e9(%rip), %rsi     # 0x108b76
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
testb $0xb, %r14b
setne %al
cmpl $0x8, %ebp
setne %cl
testb %cl, %al
je 0x2aa74
cmpb $0x0, 0x18(%r15)
je 0x2aa56
movq 0x20(%r15), %r12
testq %r12, %r12
jle 0x2aa56
movq 0x28(%r15), %r15
cmpq $0x18, %r15
jl 0x2aa56
movq 0x20(%rsp), %rax
movq 0x10(%rax), %r13
leaq 0xa(%r12), %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0xe3eb4
movl %eax, %ebp
addq $0xb, %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0xe3eb4
movl %ebp, %ecx
xorb $0x4d, %cl
movl %eax, %edx
xorb $0x4d, %dl
orb %cl, %dl
je 0x2aa17
xorb $0x49, %bpl
xorb $0x49, %al
orb %bpl, %al
jne 0x2aa56
addq $-0xa, %r15
leaq 0xde1ce(%rip), %rsi     # 0x108bf0
movq %rbx, %rdi
movq %r14, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xec1b2
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movl 0x4c(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe3f6e
movq %rax, %r14
leaq 0xde0f4(%rip), %rsi     # 0x108b85
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x30(%rsp), %rax
addl $0x2, %eax
movq %rax, 0x30(%rsp)
movl %ebp, %eax
movq %rax, 0x80(%rsp)
movl 0x1c(%rsp), %eax
movq %rax, 0x78(%rsp)
movl %r12d, %eax
movq %rax, 0x70(%rsp)
testq %r14, %r14
movl $0x0, %eax
cmovleq %rax, %r14
leaq 0xde0bb(%rip), %rsi     # 0x108b94
xorl %edx, %edx
movq %r14, 0x50(%rsp)
cmpq %r14, %rdx
je 0x2a9ae
movq 0x10(%rsp), %rcx
movq 0x40(%r13), %rax
addq 0x38(%r13), %rax
cmpq %rax, %rcx
jge 0x2a9ae
movq %rbx, %rdi
movq %rdx, 0x58(%rsp)
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq 0x20(%rsp), %r14
movq 0x10(%r14), %rdi
leaq 0x10(%rsp), %rbp
movq %rbp, %rsi
callq 0xe3f6e
movq %rbx, %rdi
leaq 0xde06b(%rip), %rsi     # 0x108ba4
movq %rax, 0x60(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x10(%r14), %rdi
movq %rbp, %rsi
callq 0xe3f6e
andl $0xf, %eax
movq %rbx, %rdi
leaq 0xde050(%rip), %rsi     # 0x108bb0
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x30(%rsp), %rax
addq %rax, 0x10(%rsp)
movq 0x10(%r14), %rdi
movq %rbp, %rsi
callq 0xe3f6e
movq %rax, %rbp
movq %rbx, %rdi
leaq 0xde03c(%rip), %rsi     # 0x108bc8
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
testq %rbp, %rbp
movl $0x0, %r14d
movq %rbp, 0x68(%rsp)
cmovgq %rbp, %r14
xorl %ebp, %ebp
cmpq %rbp, %r14
je 0x2acee
andq $0x0, 0x40(%rsp)
andq $0x0, 0x38(%rsp)
movq 0x40(%r13), %rax
addq 0x38(%r13), %rax
cmpq %rax, 0x10(%rsp)
jge 0x2a9ae
movq %rbx, %rdi
leaq 0xddffe(%rip), %rsi     # 0x108bd9
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq 0x10(%rsp), %rsi
addq 0x80(%rsp), %rsi
movq %rsi, 0x10(%rsp)
cmpb $0x10, 0xf(%rsp)
jb 0x2ac47
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
movl 0x1c(%rsp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xe424b
movq %rax, 0x40(%rsp)
movq %rbx, %rdi
leaq 0x104c1f(%rip), %rsi    # 0x12f84d
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
leaq 0x40(%rsp), %rdi
callq 0xe10b1
movq 0x10(%rsp), %rsi
addq 0x78(%rsp), %rsi
movq %rsi, 0x10(%rsp)
testl %r12d, %r12d
je 0x2ac94
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
movl %r12d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xe424b
movq %rax, 0x38(%rsp)
movq %rbx, %rdi
leaq 0xdef70(%rip), %rsi     # 0x109beb
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
leaq 0x38(%rsp), %rdi
callq 0xe10b1
movq 0x10(%rsp), %rsi
addq 0x70(%rsp), %rsi
movq %rsi, 0x10(%rsp)
cmpb $0x0, 0x18(%r15)
je 0x2acdb
cmpq $0x1, 0x68(%rsp)
jne 0x2acdb
movq 0x60(%rsp), %rax
cmpl %eax, 0x1c(%r15)
jne 0x2acdb
movq %rbx, %rdi
leaq 0xddf22(%rip), %rsi     # 0x108be4
xorl %eax, %eax
callq 0xde896
movq 0x40(%rsp), %rax
movq %rax, 0x20(%r15)
movq 0x38(%rsp), %rax
movq %rax, 0x28(%r15)
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
incq %rbp
jmp 0x2aba9
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x58(%rsp), %rdx
incq %rdx
movq 0x50(%rsp), %r14
leaq 0xdde87(%rip), %rsi     # 0x108b94
jmp 0x2aae0