// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x19(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
movl $0x1, %r9d
callq 0x39eb0
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movzbl -0x19(%rbp), %eax
cmpl $0x1, %eax
je 0x37516
jmp 0x378ec
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
shrl $0x4, %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %edx
leaq 0x16a4fa(%rip), %rsi    # 0x1a1a3e
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x38(%rbp)
je 0x37562
cmpl $0x4, -0x38(%rbp)
je 0x37562
cmpl $0x8, -0x38(%rbp)
je 0x37562
jmp 0x378ec
movl -0x34(%rbp), %eax
andl $0xf, %eax
movl %eax, -0x3c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %edx
leaq 0x16a4b0(%rip), %rsi    # 0x1a1a29
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x3c(%rbp)
je 0x37597
cmpl $0x4, -0x3c(%rbp)
je 0x37597
cmpl $0x8, -0x3c(%rbp)
je 0x37597
jmp 0x378ec
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
shrl $0x4, %eax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl -0x40(%rbp), %edx
leaq 0x16a474(%rip), %rsi    # 0x1a1a39
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x40(%rbp)
je 0x375e3
cmpl $0x4, -0x40(%rbp)
je 0x375e3
cmpl $0x8, -0x40(%rbp)
je 0x375e3
jmp 0x378ec
movl -0x34(%rbp), %eax
andl $0xf, %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %edx
leaq 0x16a454(%rip), %rsi    # 0x1a1a4e
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x44(%rbp)
je 0x37618
cmpl $0x4, -0x44(%rbp)
je 0x37618
cmpl $0x8, -0x44(%rbp)
je 0x37618
jmp 0x378ec
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165210
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0x16a41f(%rip), %rsi    # 0x1a1a5d
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jge 0x378ea
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x38(%rcx), %rcx
movq -0x28(%rbp), %rdx
addq 0x40(%rdx), %rcx
cmpq %rcx, %rax
jl 0x37679
jmp 0x378ec
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rcx
leaq 0x16a3de(%rip), %rsi    # 0x1a1a6c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165210
movl %eax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movl -0x60(%rbp), %edx
leaq 0x16a3b7(%rip), %rsi    # 0x1a1a7c
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165210
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
andl $0xf, %eax
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rdi
movl -0x74(%rbp), %edx
leaq 0x16a391(%rip), %rsi    # 0x1a1a88
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movl -0x40(%rbp), %eax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165210
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rax
movl %eax, %edx
leaq 0x16a365(%rip), %rsi    # 0x1a1aa0
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jge 0x378cb
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x38(%rcx), %rcx
movq -0x28(%rbp), %rdx
addq 0x40(%rdx), %rcx
cmpq %rcx, %rax
jl 0x37789
jmp 0x378ec
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rax
movl %eax, %edx
leaq 0x16a317(%rip), %rsi    # 0x1a1ab1
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movl -0x44(%rbp), %eax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, -0x38(%rbp)
jbe 0x377fd
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rsi
movl -0x38(%rbp), %edx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x165a80
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rdx
leaq 0x190ffc(%rip), %rsi    # 0x1c87e9
movb $0x0, %al
callq 0x15b0d0
leaq -0x80(%rbp), %rdi
callq 0x15f830
movl -0x38(%rbp), %eax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, -0x3c(%rbp)
jbe 0x37854
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rsi
movl -0x3c(%rbp), %edx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x165a80
movq %rax, -0x88(%rbp)
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rdx
leaq 0x16b256(%rip), %rsi    # 0x1a2a97
movb $0x0, %al
callq 0x15b0d0
leaq -0x88(%rbp), %rdi
callq 0x15f830
movl -0x3c(%rbp), %eax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x18(%rax), %eax
cmpl $0x0, %eax
je 0x378ac
movl -0x60(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jne 0x378ac
cmpq $0x1, -0x68(%rbp)
jne 0x378ac
movq -0x8(%rbp), %rdi
leaq 0x16a232(%rip), %rsi    # 0x1a1abc
movb $0x0, %al
callq 0x15b0d0
movq -0x80(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x88(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x3774a
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x3764d
jmp 0x378ec
movq -0x10(%rbp), %rax
cmpb $0x0, 0x18(%rax)
je 0x3790b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x3a090
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x15b5a0
addq $0x90, %rsp
popq %rbp
retq