// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/cfb.c
// Function: thumbsdb_msrgba_special_extract
static int thumbsdb_msrgba_special_extract(deark *c, lctx *d, struct dir_entry_info *dei,
	i64 hdrsize, dbuf *outf)
{
	i64 inf_pos;
	i64 idseg_len;

	if(dei->stream_size<hdrsize+16+2+22) return 0;
	inf_pos = hdrsize+16; // Also skip past the 16-byte extra header

	// SOI
	copy_any_stream_to_dbuf(c, d, dei, inf_pos, 2, outf);
	inf_pos += 2;

	// Special APP1 segment to record both headers, and identify the format.
	dbuf_write(outf, (const u8*)"\xff\xe1", 2);
	idseg_len = 2 + 12 + 1 + hdrsize + 16;
	dbuf_writeu16be(outf, idseg_len);
	dbuf_write(outf, (const u8*)"Deark_MSRGBA\0", 13);
	copy_any_stream_to_dbuf(c, d, dei, 0, hdrsize+16, outf);

	// DQT
	thumbsdb_msrgba_write_dqt(c, outf);
	// TODO: Do we ever need another quantization table?

	// SOF0
	// TODO?: This code is fragile. We could parse the JPEG data, instead of
	// just hoping it is laid out like we expect.
	copy_any_stream_to_dbuf(c, d, dei, inf_pos, 22, outf);
	inf_pos += 22;

	// DHT
	dbuf_write(outf, (const u8*)"\xff\xc4\x00\xd2\x00", 5);
	fmtutil_write_std_jpeg_dht(outf, 0);
	dbuf_writebyte(outf, 0x10);
	fmtutil_write_std_jpeg_dht(outf, 1);

	// The rest of the file
	copy_any_stream_to_dbuf(c, d, dei, inf_pos, dei->stream_size-inf_pos, outf);

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
addq $0x10, %rcx
addq $0x2, %rcx
addq $0x16, %rcx
cmpq %rcx, %rax
jge 0x54fd5
movl $0x0, -0x4(%rbp)
jmp 0x55114
movq -0x28(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %r9
movl $0x2, %r8d
callq 0x543d0
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x150c76(%rip), %rsi    # 0x1a5c8d
movl $0x2, %edx
callq 0x166170
movq -0x28(%rbp), %rax
addq $0xf, %rax
addq $0x10, %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x168a50
movq -0x30(%rbp), %rdi
leaq 0x150443(%rip), %rsi    # 0x1a548c
movl $0xd, %edx
callq 0x166170
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %r8
addq $0x10, %r8
movq -0x30(%rbp), %r9
xorl %eax, %eax
movl %eax, %ecx
callq 0x543d0
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x55200
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %r9
movl $0x16, %r8d
callq 0x543d0
movq -0x38(%rbp), %rax
addq $0x16, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x1503e3(%rip), %rsi    # 0x1a549a
movl $0x5, %edx
callq 0x166170
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x176570
movq -0x30(%rbp), %rdi
movl $0x10, %esi
callq 0x168380
movq -0x30(%rbp), %rdi
movl $0x1, %esi
callq 0x176570
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %r8
subq -0x38(%rbp), %r8
movq -0x30(%rbp), %r9
callq 0x543d0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)