// File: /workspace/github_c_makefile_star2_O1/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 $0x98, %rsp
movq %rdx, %r14
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
movq 0x28(%rdx), %rax
movq %rax, 0x58(%rsp)
movq 0x38(%rax), %rax
movq %rax, 0x18(%rsp)
leaq 0x54(%rsp), %rsi
callq 0xe958c
movq 0x10(%r14), %rdi
movq %r14, 0x28(%rsp)
movq 0x28(%r14), %rax
movq 0x38(%rax), %rsi
callq 0xef32a
movq %rax, %r14
movl %r14d, %ecx
andl $0xffffff, %ecx         # imm = 0xFFFFFF
movl %r14d, %edx
shrl $0x18, %edx
leaq 0xea576(%rip), %rsi     # 0x115a88
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
addq $0x4, 0x18(%rsp)
andl $0xff000000, %r14d      # imm = 0xFF000000
cmpl $0x1000000, %r14d       # imm = 0x1000000
jne 0x2b91b
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x18(%rsp), %rsi
callq 0xef14d
movb %al, 0xf(%rsp)
movzbl %al, %r12d
movl %r12d, %edx
shrl $0x4, %edx
leaq 0xea5e3(%rip), %rsi     # 0x115b46
movq 0x10(%rsp), %rdi
movl %edx, 0x24(%rsp)
xorl %eax, %eax
callq 0xe9226
cmpb $0x10, %r12b
jb 0x2b58b
cmpl $0x8, 0x24(%rsp)
je 0x2b58b
cmpl $0x4, 0x24(%rsp)
jne 0x2b91b
andl $0xf, %r12d
leaq 0xea59b(%rip), %rsi     # 0x115b31
movq 0x10(%rsp), %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
testb $0xb, 0xf(%rsp)
setne %al
cmpl $0x8, %r12d
setne %cl
testb %cl, %al
jne 0x2b91b
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x18(%rsp), %rsi
callq 0xef14d
movzbl %al, %r14d
movl %r14d, %edx
shrl $0x4, %edx
leaq 0xea561(%rip), %rsi     # 0x115b41
movq 0x10(%rsp), %rdi
movq %rdx, 0x30(%rsp)
xorl %eax, %eax
callq 0xe9226
cmpb $0x10, %r14b
jb 0x2b609
cmpl $0x8, 0x30(%rsp)
je 0x2b609
cmpl $0x4, 0x30(%rsp)
jne 0x2b91b
movl %r14d, %ebp
andl $0xf, %ebp
leaq 0xea540(%rip), %rsi     # 0x115b56
movq 0x10(%rsp), %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
testb $0xb, %r14b
setne %al
cmpl $0x8, %ebp
setne %cl
testb %cl, %al
jne 0x2b91b
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x18(%rsp), %rsi
callq 0xef193
movq %rax, %rbx
leaq 0xea50f(%rip), %rsi     # 0x115b65
movq 0x10(%rsp), %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, 0x68(%rsp)
testq %rbx, %rbx
jle 0x2b91b
movq 0x30(%rsp), %rax
addl $0x2, %eax
movq %rax, 0x30(%rsp)
movl %ebp, %eax
movq %rax, 0x90(%rsp)
movl 0x24(%rsp), %eax
movq %rax, 0x88(%rsp)
movl %r12d, %eax
movq %rax, 0x80(%rsp)
xorl %edx, %edx
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %rcx
movq 0x58(%rsp), %rsi
movq 0x40(%rsi), %rax
addq 0x38(%rsi), %rax
cmpq %rax, %rcx
movq %rdx, 0x70(%rsp)
jge 0x2b8f9
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
leaq 0xea49e(%rip), %rsi     # 0x115b74
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x28(%rsp), %r14
movq 0x10(%r14), %rdi
leaq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0xef193
movq %rbx, %rdi
leaq 0xea47a(%rip), %rsi     # 0x115b84
movq %rax, 0x78(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0xef193
andl $0xf, %eax
movq %rbx, %rdi
leaq 0xea45f(%rip), %rsi     # 0x115b90
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x30(%rsp), %rax
addq %rax, 0x18(%rsp)
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0xef193
movq %rax, %r14
movq %rbx, %rdi
leaq 0xea44b(%rip), %rsi     # 0x115ba8
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movq %r14, 0x60(%rsp)
testq %r14, %r14
jle 0x2b8e5
xorl %r14d, %r14d
movl %r12d, %ebx
xorl %eax, %eax
movq %rax, 0x48(%rsp)
movq %rax, 0x40(%rsp)
movq 0x18(%rsp), %r12
movq 0x58(%rsp), %rax
movq 0x40(%rax), %r13
addq 0x38(%rax), %r13
cmpq %r13, %r12
setl %r15b
jge 0x2b916
movq %rbp, %rdi
leaq 0xea409(%rip), %rsi     # 0x115bb9
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rsp), %rsi
addq 0x90(%rsp), %rsi
movq %rsi, 0x18(%rsp)
cmpb $0x10, 0xf(%rsp)
jb 0x2b81c
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
movl 0x24(%rsp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xef6f2
movq %rax, 0x48(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x110f75(%rip), %rsi    # 0x13c77d
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
leaq 0x48(%rsp), %rdi
callq 0xec0d1
movq 0x18(%rsp), %rsi
addq 0x88(%rsp), %rsi
movq %rsi, 0x18(%rsp)
testl %ebx, %ebx
je 0x2b86c
movl %ebx, %edx
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xef6f2
movq %rax, 0x40(%rsp)
movq 0x10(%rsp), %rdi
leaq 0xeb373(%rip), %rsi     # 0x116bcb
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
leaq 0x40(%rsp), %rdi
callq 0xec0d1
movq 0x80(%rsp), %rax
addq %rax, 0x18(%rsp)
movq 0x38(%rsp), %rax
cmpb $0x0, 0x18(%rax)
je 0x2b8c0
cmpq $0x1, 0x60(%rsp)
jne 0x2b8c0
movq 0x78(%rsp), %rcx
cmpl %ecx, 0x1c(%rax)
jne 0x2b8c0
movq 0x10(%rsp), %rdi
leaq 0xea322(%rip), %rsi     # 0x115bc4
xorl %eax, %eax
callq 0xe9226
movq 0x38(%rsp), %rcx
movq 0x48(%rsp), %rax
movq %rax, 0x20(%rcx)
movq 0x40(%rsp), %rax
movq %rax, 0x28(%rcx)
movq 0x10(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpq %r13, %r12
movl %ebx, %r12d
jge 0x2b8fc
incq %r14
cmpq %r14, 0x60(%rsp)
jne 0x2b778
movq 0x10(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movb $0x1, %r15b
jmp 0x2b8fc
xorl %r15d, %r15d
testb %r15b, %r15b
movq 0x70(%rsp), %rdx
je 0x2b91b
incq %rdx
cmpq 0x68(%rsp), %rdx
jne 0x2b6a7
jmp 0x2b91b
movl %ebx, %r12d
jmp 0x2b8fc
movq 0x38(%rsp), %rax
cmpb $0x0, 0x18(%rax)
je 0x2b9d0
movq 0x20(%rax), %r12
testq %r12, %r12
jle 0x2b9d0
movq 0x28(%rax), %r14
cmpq $0x18, %r14
jl 0x2b9d0
movq 0x28(%rsp), %rax
movq 0x10(%rax), %r13
leaq 0xa(%r12), %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0xef0d5
movl %eax, %ebp
addq $0xb, %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0xef0d5
movl %ebp, %ecx
xorb $0x4d, %cl
movl %eax, %edx
xorb $0x4d, %dl
orb %cl, %dl
je 0x2b988
xorb $0x49, %bpl
xorb $0x49, %al
orb %bpl, %al
jne 0x2b9d0
addq $-0xa, %r14
leaq 0xea23d(%rip), %rsi     # 0x115bd0
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0xf7eab
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movl 0x54(%rsp), %esi
movq 0x10(%rsp), %rdi
callq 0xe9595
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq