// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/jbf.c
// Function: do_one_thumbnail
static int do_one_thumbnail(deark *c, lctx *d, i64 pos1, i64 imgidx, i64 *bytes_consumed)
{
	i64 payload_len;
	int retval = 0;
	i64 pos = pos1;
	UI filetype_code;
	i64 file_size;
	i64 x;
	i64 tn_w, tn_h;
	struct page_ctx *pg = NULL;
	i64 fn_field_size = 0;

	de_dbg(c, "image #%d at %"I64_FMT, (int)imgidx, pos1);
	de_dbg_indent(c, 1);

	pg = de_malloc(c, sizeof(struct page_ctx));

	pg->fi = de_finfo_create(c);

	if(!read_filename(c, d, pg, pos, &fn_field_size)) {
		goto done;
	}
	pos += fn_field_size;

	if(d->ver_major==2) {
		read_FILETIME(c, d, pg, pos);
		pos += 8;
	}

	if(d->ver_major==2) {
		// The original file type (not the format of the thumbnail)
		filetype_code = (UI)de_getu32le_p(&pos);
		de_dbg(c, "original file type: 0x%02x (%s)", filetype_code, get_type_name(filetype_code));
	}
	else if(d->ver_major==1 && d->ver_minor<3) {
		pos += 4; // TODO: FOURCC
	}

	tn_w = de_getu16le(pos);
	pos += 4;
	tn_h = de_getu16le(pos);
	pos += 4;
	de_dbg(c, "original dimensions: %d"DE_CHAR_TIMES"%d", (int)tn_w, (int)tn_h);

	pos += 4; // color depth

	if(d->ver_major==2) {
		pos += 4; // (uncompressed size?)
	}

	file_size = de_getu32le_p(&pos);
	de_dbg(c, "original file size: %"I64_FMT, file_size);

	if(d->ver_major==1) {
		read_unix_time(c, d, pg, pos);
		pos += 4;

		pos += 4; // TODO: image index
	}

	if(d->ver_major==2) {
		// first 4 bytes of 12-byte "thumbnail signature"
		x = de_getu32le_p(&pos);
		if(x==0) { // truncated entry
			de_dbg(c, "thumbnail not present");
			retval = 1;
			goto done;
		}

		pos += 8; // remaining 8 byte of signature

		payload_len = de_getu32le_p(&pos);
		de_dbg(c, "payload len: %"I64_FMT, payload_len);

		if(pos + payload_len > c->infile->len) {
			de_err(c, "Bad payload length (%"I64_FMT") or unsupported format", payload_len);
			goto done;
		}

		dbuf_create_file_from_slice(c->infile, pos, payload_len, pg->thumbnail_ext, pg->fi, 0);
		pos += payload_len;
	}
	else { // ver_major==1
		i64 thumbnail_size;
		if(!read_bitmap_v1(c, d, pg, pos, &thumbnail_size)) {
			goto done;
		}
		pos += thumbnail_size;
	}

	retval = 1;
done:
	*bytes_consumed = pos - pos1;
	de_finfo_destroy(c, pg->fi);
	ucstring_destroy(pg->fname);
	de_free(c, pg);
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
movq -0x18(%rbp), %rcx
leaq 0xfc12e(%rip), %rsi     # 0x1b6071
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x18, %esi
callq 0x15b780
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x40(%rbp), %rcx
leaq -0x78(%rbp), %r8
callq 0xba2a0
cmpl $0x0, %eax
jne 0xb9fa0
jmp 0xba24c
movq -0x78(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, 0x4(%rax)
jne 0xb9fd7
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x40(%rbp), %rcx
callq 0xba4c0
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, 0x4(%rax)
jne 0xba031
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165580
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x44(%rbp), %edi
callq 0xba540
movq -0x90(%rbp), %rdi
movl -0x84(%rbp), %edx
movq %rax, %rcx
leaq 0xfc05a(%rip), %rsi     # 0x1b6082
movb $0x0, %al
callq 0x15b0d0
jmp 0xba053
movq -0x10(%rbp), %rax
cmpl $0x1, 0x4(%rax)
jne 0xba051
movq -0x10(%rbp), %rax
cmpl $0x3, 0x8(%rax)
jae 0xba051
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
jmp 0xba053
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x165280
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x165280
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %edx
movq -0x68(%rbp), %rax
movl %eax, %ecx
leaq 0xfbff6(%rip), %rsi     # 0x1b60a2
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, 0x4(%rax)
jne 0xba0d5
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165580
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0xf2400(%rip), %rsi     # 0x1ac4f9
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpl $0x1, 0x4(%rax)
jne 0xba137
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x40(%rbp), %rcx
callq 0xba5f0
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, 0x4(%rax)
jne 0xba219
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165580
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0xba17f
movq -0x8(%rbp), %rdi
leaq 0xfbf52(%rip), %rsi     # 0x1b60be
movb $0x0, %al
callq 0x15b0d0
movl $0x1, -0x34(%rbp)
jmp 0xba24c
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0xfbf25(%rip), %rsi     # 0x1b60d4
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xba1e4
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0xfbf0a(%rip), %rsi     # 0x1b60e5
movb $0x0, %al
callq 0x15bfb0
jmp 0xba24c
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x70(%rbp), %rax
movq (%rax), %r8
xorl %r9d, %r9d
callq 0x166be0
movq -0x30(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0xba245
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x40(%rbp), %rcx
leaq -0x80(%rbp), %r8
callq 0xba670
cmpl $0x0, %eax
jne 0xba239
jmp 0xba24c
movq -0x80(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x1, -0x34(%rbp)
movq -0x40(%rbp), %rcx
subq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rax
movq (%rax), %rsi
callq 0x15d260
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x34(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nop