// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/psd.c
// Function: hrsrc_thumbnail
static void hrsrc_thumbnail(deark *c, lctx *d, zztype *zz, const struct rsrc_info *ri)
{
	i64 fmt;
	const char *ext;
	dbuf *outf = NULL;
	i64 dpos;
	i64 dlen;

	if(zz_avail(zz)<=28) goto done;

	fmt = psd_getu32(zz->pos);
	if(fmt != 1) {
		// fmt != kJpegRGB
		de_dbg(c, "thumbnail in unsupported format (%d) found", (int)fmt);
		goto done;
	}

	zz->pos += 28;
	dpos = zz->pos;
	dlen = zz_avail(zz);

	if(ri->id==0x0409) {
		ext = "psdthumb_rbswap.jpg";
		if(c->extract_policy!=DE_EXTRACTPOLICY_MAINONLY) {
			de_info(c, "Note: This Photoshop thumbnail uses nonstandard colors, "
				"and may not look right.");
		}
	}
	else {
		ext = "psdthumb.jpg";
	}

	outf = dbuf_create_output_file(c, ext, NULL, DE_CREATEFLAG_IS_AUX);

	if(ri->id==0x0409 && d->jpeg_rbswap_mode && dlen>=11 &&
		!dbuf_memcmp(c->infile, dpos, "\xff\xd8\xff\xe0\x00\x10" "JFIF" "\x00", 11))
	{
		// If we were to extract this image as-is, there will be no way to tell
		// later that the red/blue channels are swapped. So, we have this feature
		// to mark it by inserting a custom segment (after the JFIF segment).
		dbuf_copy(c->infile, dpos, 20, outf);
		dbuf_write(outf, (const u8*)"\xff\xe1\x00\x10" "Deark_RB_swap\0", 18);
		dbuf_copy(c->infile, dpos+20, dlen-20, outf);
	}
	else {
		dbuf_copy(c->infile, dpos, dlen, outf);
	}

done:
	dbuf_close(outf);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf40b0
cmpq $0x1c, %rax
jg 0xf4774
jmp 0xf48f1
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x1658c0
movq %rax, -0x28(%rbp)
cmpq $0x1, -0x28(%rbp)
je 0xf47b7
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0xcaab6(%rip), %rsi     # 0x1bf261
movb $0x0, %al
callq 0x15b0d0
jmp 0xf48f1
movq -0x18(%rbp), %rax
movq (%rax), %rcx
addq $0x1c, %rcx
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf40b0
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
cmpl $0x409, %eax            # imm = 0x409
jne 0xf4817
leaq 0xcaa9a(%rip), %rax     # 0x1bf28c
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x1, 0x8c(%rax)
je 0xf4815
movq -0x8(%rbp), %rdi
leaq 0xcaa92(%rip), %rsi     # 0x1bf2a0
movb $0x0, %al
callq 0x15c0c0
jmp 0xf4822
leaq 0xcaad2(%rip), %rax     # 0x1bf2f0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
callq 0x166c60
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
cmpl $0x409, %eax            # imm = 0x409
jne 0xf48d6
movq -0x10(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x0, %eax
je 0xf48d6
cmpq $0xb, -0x48(%rbp)
jl 0xf48d6
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
leaq 0xc9fbf(%rip), %rdx     # 0x1be834
movl $0xb, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0xf48d6
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rcx
movl $0x14, %edx
callq 0x166070
movq -0x38(%rbp), %rdi
leaq 0xc9f97(%rip), %rsi     # 0x1be840
movl $0x12, %edx
callq 0x166170
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
addq $0x14, %rsi
movq -0x48(%rbp), %rdx
subq $0x14, %rdx
movq -0x38(%rbp), %rcx
callq 0x166070
jmp 0xf48ef
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x166070
jmp 0xf48f1
movq -0x38(%rbp), %rdi
callq 0x1676a0
addq $0x50, %rsp
popq %rbp
retq