// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/afcp.c
// Function: do_afcprec_thumbnail
static int do_afcprec_thumbnail(deark *c, struct afcpctx *d, struct afcprecord *ar)
{
	unsigned int recver;
	i64 pos = ar->dpos;
	i64 w, h;
	i64 foundpos = 0;
	struct de_fourcc imgtype;
	const char *ext;
	int ret;
	int retval = 0;

	if(ar->dlen<10) goto done;
	recver = (unsigned int)dbuf_getu16x(c->infile, pos, d->is_le);
	pos += 2;
	de_dbg(c, "record version: %u", recver);
	if(recver!=1) goto done;

	w = dbuf_getu16x(c->infile, pos, d->is_le);
	pos += 2;
	h = dbuf_getu16x(c->infile, pos, d->is_le);
	pos += 2;
	de_dbg(c, "reported dimensions: %d"DE_CHAR_TIMES"%d", (int)w, (int)h);
	// Nothing after this point is adequately documented in the spec.

	dbuf_read_fourcc(c->infile, pos, &imgtype, 4, 0);
	pos += 4;
	de_dbg(c, "image type: '%s'", imgtype.id_dbgstr);

	ret = dbuf_search(c->infile, (const u8*)"\xff\xd8\xff", 3, pos,
		ar->dpos + ar->dlen - pos, &foundpos);
	if(!ret) {
		de_dbg(c, "[failed to find thumbnail image data]");
		goto done;
	}
	if(ar->id4cc.id==CODE_PrVw) ext = "afcppreview.jpg";
	else ext = "afcpthumb.jpg";
	dbuf_create_file_from_slice(c->infile, foundpos, ar->dpos + ar->dlen - foundpos, ext,
		NULL, DE_CREATEFLAG_IS_AUX);

	retval = 1;
done:
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x80(%rbp)
movq -0x18(%rbp), %rax
cmpq $0xa, (%rax)
jge 0x2f1b1
jmp 0x2f356
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %edx
callq 0x165820
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %edx
leaq 0x171169(%rip), %rsi    # 0x1a034e
movb $0x0, %al
callq 0x15b0d0
cmpl $0x1, -0x1c(%rbp)
je 0x2f1f7
jmp 0x2f356
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %edx
callq 0x165820
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %edx
callq 0x165820
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x38(%rbp), %rax
movl %eax, %ecx
leaq 0x171105(%rip), %rsi    # 0x1a0361
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
leaq -0x70(%rbp), %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0x169860
movq -0x28(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x70(%rbp), %rdx
addq $0x10, %rdx
leaq 0x1710de(%rip), %rsi    # 0x1a037d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %r8
movq -0x18(%rbp), %rax
addq (%rax), %r8
subq -0x28(%rbp), %r8
leaq 0x1710c2(%rip), %rsi    # 0x1a038e
movl $0x3, %edx
leaq -0x40(%rbp), %r9
callq 0x169370
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jne 0x2f2f7
movq -0x8(%rbp), %rdi
leaq 0x1710a4(%rip), %rsi    # 0x1a0392
movb $0x0, %al
callq 0x15b0d0
jmp 0x2f356
movq -0x18(%rbp), %rax
cmpl $0x50725677, 0x14(%rax) # imm = 0x50725677
jne 0x2f311
leaq 0x1710ad(%rip), %rax    # 0x1a03b8
movq %rax, -0x78(%rbp)
jmp 0x2f31c
leaq 0x1710b0(%rip), %rax    # 0x1a03c8
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x18(%rbp), %rax
addq (%rax), %rdx
subq -0x40(%rbp), %rdx
movq -0x78(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
movl $0x1, %r9d
callq 0x166be0
movl $0x1, -0x80(%rbp)
movl -0x80(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)