// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq 0x28(%rdx), %rax
movq %rax, 0x40(%rsp)
movq 0x38(%rax), %r13
leaq 0x3c(%rsp), %rsi
callq 0xe8a1c
movq 0x10(%r14), %rdi
movq 0x28(%r14), %rax
movq 0x38(%rax), %rsi
callq 0xeed0d
movq %rax, %r12
movl %r12d, %ecx
andl $0xffffff, %ecx         # imm = 0xFFFFFF
movl %r12d, %edx
shrl $0x18, %edx
leaq 0xea666(%rip), %rsi     # 0x115b38
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
addq $0x4, %r13
movq %r13, 0x10(%rsp)
andl $0xff000000, %r12d      # imm = 0xFF000000
cmpl $0x1000000, %r12d       # imm = 0x1000000
jne 0x2b85b
movq 0x10(%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0xeeb4b
movb %al, 0xf(%rsp)
movzbl %al, %r12d
movl %r12d, %edx
shrl $0x4, %edx
leaq 0xea6da(%rip), %rsi     # 0x115bf6
movq %rbx, %rdi
movl %edx, 0x1c(%rsp)
xorl %eax, %eax
callq 0xe8617
cmpb $0x10, %r12b
jb 0x2b542
cmpl $0x8, 0x1c(%rsp)
je 0x2b542
cmpl $0x4, 0x1c(%rsp)
jne 0x2b85b
andl $0xf, %r12d
leaq 0xea694(%rip), %rsi     # 0x115be1
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
testb $0xb, 0xf(%rsp)
setne %al
cmpl $0x8, %r12d
setne %cl
testb %cl, %al
jne 0x2b85b
movq 0x10(%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0xeeb4b
movzbl %al, %r13d
movl %r13d, %edx
shrl $0x4, %edx
leaq 0xea661(%rip), %rsi     # 0x115bf1
movq %rbx, %rdi
movq %rdx, 0x20(%rsp)
xorl %eax, %eax
callq 0xe8617
cmpb $0x10, %r13b
jb 0x2b5b7
cmpl $0x8, 0x20(%rsp)
je 0x2b5b7
cmpl $0x4, 0x20(%rsp)
jne 0x2b85b
movl %r13d, %ebp
andl $0xf, %ebp
leaq 0xea642(%rip), %rsi     # 0x115c06
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
testb $0xb, %r13b
setne %al
cmpl $0x8, %ebp
setne %cl
testb %cl, %al
jne 0x2b85b
movq 0x10(%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0xeeb91
movq %rax, %r13
leaq 0xea618(%rip), %rsi     # 0x115c15
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe8617
movq %r13, 0x48(%rsp)
testq %r13, %r13
jle 0x2b85b
movq 0x20(%rsp), %rax
addl $0x2, %eax
movq %rax, 0x20(%rsp)
movl %ebp, %eax
movq %rax, 0x70(%rsp)
movl 0x1c(%rsp), %eax
movq %rax, 0x68(%rsp)
movl %r12d, %eax
movq %rax, 0x60(%rsp)
xorl %edx, %edx
movq 0x10(%rsp), %rcx
movq 0x40(%rsp), %rsi
movq 0x40(%rsi), %rax
addq 0x38(%rsi), %rax
cmpq %rax, %rcx
jge 0x2b85b
movq %rbx, %rdi
leaq 0xea5c0(%rip), %rsi     # 0x115c24
movq %rdx, 0x50(%rsp)
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x10(%r14), %rdi
leaq 0x10(%rsp), %r13
movq %r13, %rsi
callq 0xeeb91
movq %rbx, %rdi
leaq 0xea59c(%rip), %rsi     # 0x115c34
movq %rax, 0x58(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x10(%r14), %rdi
movq %r13, %rsi
callq 0xeeb91
andl $0xf, %eax
movq %rbx, %rdi
leaq 0xea581(%rip), %rsi     # 0x115c40
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x20(%rsp), %rax
addq %rax, 0x10(%rsp)
movq 0x10(%r14), %rdi
movq %r13, %rsi
callq 0xeeb91
movq %rax, %rbp
movq %rbx, %rdi
leaq 0xea56d(%rip), %rsi     # 0x115c58
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
testq %rbp, %rbp
jle 0x2b83b
xorl %r13d, %r13d
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movq 0x40(%rsp), %rcx
movq 0x40(%rcx), %rax
addq 0x38(%rcx), %rax
cmpq %rax, 0x10(%rsp)
jge 0x2b85b
movq %rbx, %rdi
leaq 0xea53b(%rip), %rsi     # 0x115c69
movl %r13d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x10(%rsp), %rsi
addq 0x70(%rsp), %rsi
movq %rsi, 0x10(%rsp)
cmpb $0x10, 0xf(%rsp)
jb 0x2b795
movq 0x10(%r14), %rdi
movl 0x1c(%rsp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xef0f6
movq %rax, 0x30(%rsp)
movq %rbx, %rdi
leaq 0x111401(%rip), %rsi    # 0x13cb7d
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
leaq 0x30(%rsp), %rdi
callq 0xebad2
movq 0x10(%rsp), %rsi
addq 0x68(%rsp), %rsi
movq %rsi, 0x10(%rsp)
testl %r12d, %r12d
je 0x2b7dd
movq 0x10(%r14), %rdi
movl %r12d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xef0f6
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
leaq 0xeb4b7(%rip), %rsi     # 0x116c7b
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
leaq 0x28(%rsp), %rdi
callq 0xebad2
movq 0x10(%rsp), %rsi
addq 0x60(%rsp), %rsi
movq %rsi, 0x10(%rsp)
cmpb $0x0, 0x18(%r15)
je 0x2b822
cmpq $0x1, %rbp
jne 0x2b822
movq 0x58(%rsp), %rax
cmpl %eax, 0x1c(%r15)
jne 0x2b822
movq %rbx, %rdi
leaq 0xea46b(%rip), %rsi     # 0x115c74
xorl %eax, %eax
callq 0xe8617
movq 0x30(%rsp), %rax
movq %rax, 0x20(%r15)
movq 0x28(%rsp), %rax
movq %rax, 0x28(%r15)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
incq %r13
cmpq %r13, %rbp
jne 0x2b700
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x50(%rsp), %rdx
incq %rdx
cmpq 0x48(%rsp), %rdx
jne 0x2b63f
cmpb $0x0, 0x18(%r15)
je 0x2b902
movq 0x20(%r15), %r12
testq %r12, %r12
jle 0x2b902
movq 0x28(%r15), %r15
cmpq $0x18, %r15
jl 0x2b902
movq 0x10(%r14), %r13
leaq 0xa(%r12), %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0xeead3
movl %eax, %ebp
addq $0xb, %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0xeead3
movl %ebp, %ecx
xorb $0x4d, %cl
movl %eax, %edx
xorb $0x4d, %dl
orb %cl, %dl
je 0x2b8bf
xorb $0x49, %bpl
xorb $0x49, %al
orb %bpl, %al
jne 0x2b902
addq $-0xa, %r15
leaq 0xea3b6(%rip), %rsi     # 0x115c80
movq %rbx, %rdi
movq %r14, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xf7a67
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movl 0x3c(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq