// File: /workspace/github_c_makefile_star2_O1/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 %rcx, %r13
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %rbx
movq (%rdx), %rsi
movq 0x10(%rdx), %rcx
movq %rcx, %rax
subq %rsi, %rax
jle 0xa71de
movq 0x8(%r14), %rdx
cmpq %rdx, %rsi
jge 0xa71e0
subq %rdx, %rcx
movq %rcx, %rax
jmp 0xa71e0
xorl %eax, %eax
cmpq $0x1d, %rax
jge 0xa71ee
xorl %r14d, %r14d
jmp 0xa72d8
movq 0x18(%rbx), %rdi
movl 0x4(%r12), %edx
callq 0xef614
cmpq $0x1, %rax
jne 0xa722d
movq (%r14), %rcx
movq 0x10(%r14), %rax
movq %rcx, (%rsp)
leaq 0x1c(%rcx), %rbp
movq %rbp, (%r14)
movq %rax, %r15
subq %rbp, %r15
jle 0xa7248
movq 0x8(%r14), %rcx
cmpq %rcx, %rbp
jge 0xa724b
subq %rcx, %rax
movq %rax, %r15
jmp 0xa724b
leaq 0x8c1ee(%rip), %rsi     # 0x133422
xorl %r14d, %r14d
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
jmp 0xa72d8
xorl %r15d, %r15d
cmpw $0x409, (%r13)          # imm = 0x409
jne 0xa7277
leaq 0x8c1f2(%rip), %r14     # 0x13344d
cmpl $0x1, 0x8c(%rbx)
je 0xa727e
leaq 0x8c1f6(%rip), %rsi     # 0x133461
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9ef5
jmp 0xa727e
leaq 0x8c233(%rip), %r14     # 0x1334b1
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0xf037a
movq %rax, %r14
cmpw $0x409, (%r13)          # imm = 0x409
jne 0xa72c6
cmpb $0x0, 0x14(%r12)
je 0xa72c6
cmpq $0xb, %r15
jl 0xa72c6
movq 0x18(%rbx), %rdi
leaq 0x8d763(%rip), %rdx     # 0x134a18
movl $0xb, %ecx
movq %rbp, %rsi
callq 0xf025e
testl %eax, %eax
je 0xa72ee
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0xefabc
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf0b13
movq 0x18(%rbx), %rdi
movl $0x14, %edx
movq %rbp, %rsi
movq %r14, %rcx
callq 0xefabc
leaq 0x8d71b(%rip), %rsi     # 0x134a24
movl $0x12, %edx
movq %r14, %rdi
callq 0xefb82
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
addq $0x30, %rsi
addq $-0x14, %r15
jmp 0xa72cd