// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r13
movq (%rdx), %rsi
movq 0x10(%rdx), %rax
cmpq %rax, %rsi
jge 0xa67f9
movq %rcx, %r14
movq %rdx, %rbp
movq 0x8(%rdx), %rcx
cmpq %rcx, %rsi
cmovgq %rsi, %rcx
subq %rcx, %rax
cmpq $0x1d, %rax
jge 0xa6811
xorl %ebp, %ebp
movq %rbp, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf052b
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
movl 0x4(%r13), %edx
callq 0xeeff3
cmpq $0x1, %rax
jne 0xa6854
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rsp)
leaq 0x1c(%rcx), %r12
movq %r12, (%rbp)
movq %rax, %r15
subq %r12, %r15
jle 0xa686b
movq 0x8(%rbp), %rcx
cmpq %rcx, %r12
jge 0xa686e
subq %rcx, %rax
movq %rax, %r15
jmp 0xa686e
leaq 0x8cfbf(%rip), %rsi     # 0x13381a
xorl %ebp, %ebp
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
jmp 0xa67fb
xorl %r15d, %r15d
cmpw $0x409, (%r14)          # imm = 0x409
jne 0xa6899
leaq 0x8cfc8(%rip), %rbp     # 0x133845
cmpl $0x1, 0x8c(%rbx)
je 0xa68a0
leaq 0x8cfcc(%rip), %rsi     # 0x133859
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9393
jmp 0xa68a0
leaq 0x8d009(%rip), %rbp     # 0x1338a9
movq %rbx, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0xefda1
movq %rax, %rbp
cmpw $0x409, (%r14)          # imm = 0x409
jne 0xa68e6
cmpb $0x0, 0x14(%r13)
je 0xa68e6
cmpq $0xb, %r15
jl 0xa68e6
movq 0x18(%rbx), %rdi
leaq 0x8c507(%rip), %rdx     # 0x132ddc
movl $0xb, %ecx
movq %r12, %rsi
callq 0xefc85
testl %eax, %eax
je 0xa68fd
movq 0x18(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0xef481
jmp 0xa67fb
movq 0x18(%rbx), %rdi
movl $0x14, %edx
movq %r12, %rsi
movq %rbp, %rcx
callq 0xef481
leaq 0x8c4d0(%rip), %rsi     # 0x132de8
movl $0x12, %edx
movq %rbp, %rdi
callq 0xef547
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
addq $0x30, %rsi
addq $-0x14, %r15
jmp 0xa68ed