// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/exe.c
// Function: do_extract_ico_cur
static void do_extract_ico_cur(deark *c, lctx *d, i64 pos, i64 len,
	int is_cur, i64 hotspot_x, i64 hotspot_y, de_finfo *fi)
{
	dbuf *f;
	i64 w, h;
	i64 ncolors;
	struct de_bmpinfo bi;

	// I guess we have to manufacture an ICO/CUR header?
	// There's usually a GROUP_ICON resource that seems to contain (most of) an
	// ICO header, but I don't know exactly how it's connected to the icon image(s).

	if(!fmtutil_get_bmpinfo(c, c->infile, &bi, pos, len, DE_BMPINFO_ICO_FORMAT)) {
		de_err(c, "Invalid bitmap");
		return;
	}

	if(bi.file_format==DE_BMPINFO_FMT_PNG) {
		dbuf_create_file_from_slice(c->infile, pos, len, "png", fi, 0);
		return;
	}

	f = dbuf_create_output_file(c, is_cur?"cur":"ico", fi, 0);

	// Write the 6-byte file header.
	dbuf_writeu16le(f, 0); // Reserved
	dbuf_writeu16le(f, is_cur?2:1); // Resource ID
	dbuf_writeu16le(f, 1); // Number of icons/cursors

	w = bi.width;
	if(w>255) w=0;
	h = bi.height;
	if(h>255) h=0;
	ncolors = bi.num_colors;
	if(ncolors>255) ncolors = 0;

	if(bi.total_size < len) {
		// Strip off useless padding at the end of the image.
		len = bi.total_size;
	}

	// Write the 16-byte index entry for the one icon/cursor.
	dbuf_writebyte(f, (u8)w);
	dbuf_writebyte(f, (u8)h);
	dbuf_writebyte(f, (u8)ncolors);
	if(is_cur) {
		dbuf_writebyte(f, 0);
		dbuf_writeu16le(f, hotspot_x);
		dbuf_writeu16le(f, hotspot_y);
	}
	else {
		dbuf_write_zeroes(f, 5);
	}
	dbuf_writeu32le(f, len); // Icon/cursor size
	dbuf_writeu32le(f, (i64)6+16); // Icon/cursor file offset

	// Write the non-manufactured part of the file.
	dbuf_copy(c->infile, pos, len, f);
	dbuf_close(f);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, %r12
movq %r8, %r13
movl %ecx, %ebp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0x18(%rdi), %rsi
leaq 0x18(%rsp), %rdx
movq %r14, %rcx
movq %rbx, %r8
movl $0x2, %r9d
callq 0xf7881
testl %eax, %eax
je 0x4794e
movq 0xf0(%rsp), %r8
cmpl $0x1, 0x18(%rsp)
jne 0x47964
movq 0x18(%r15), %rdi
leaq 0xe0bc5(%rip), %rcx     # 0x128500
movq %r14, %rsi
movq %rbx, %rdx
xorl %r9d, %r9d
callq 0xf0321
jmp 0x47ab3
leaq 0xd442a(%rip), %rsi     # 0x11bd7f
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x47ab3
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
xorl %r13d, %r13d
testl %ebp, %ebp
leaq 0xd96cf(%rip), %rax     # 0x121049
leaq 0xd5902(%rip), %rsi     # 0x11d283
cmoveq %rax, %rsi
setne %r13b
movq %r15, %rdi
movq %r8, %rdx
xorl %ecx, %ecx
callq 0xf037a
movl %ebp, 0x4(%rsp)
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
callq 0xf1598
incq %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0xf1598
movl $0x1, %esi
movq %rbp, %rdi
callq 0xf1598
movq 0x38(%rsp), %rsi
movq 0xa8(%rsp), %rax
cmpq %rbx, %rax
cmovlq %rax, %rbx
movq 0x40(%rsp), %r12
movl $0x100, %eax            # imm = 0x100
cmpq %rax, %rsi
movq 0x70(%rsp), %r13
movl $0x0, %eax
cmovgel %eax, %esi
movl $0xff, %eax
andl %eax, %esi
movq %rbp, %rdi
callq 0xf11be
movl $0x100, %eax            # imm = 0x100
cmpq %rax, %r12
movl $0x0, %eax
cmovgel %eax, %r12d
movl $0xff, %eax
andl %eax, %r12d
movq %rbp, %rdi
movl %r12d, %esi
callq 0xf11be
movl $0x100, %eax            # imm = 0x100
cmpq %rax, %r13
movl $0x0, %eax
cmovgel %eax, %r13d
movl $0xff, %eax
andl %eax, %r13d
movq %rbp, %rdi
movl %r13d, %esi
callq 0xf11be
cmpl $0x0, 0x4(%rsp)
je 0x47a74
movq %rbp, %rdi
xorl %esi, %esi
callq 0xf11be
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0xf1598
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0xf1598
jmp 0x47a81
movl $0x5, %esi
movq %rbp, %rdi
callq 0xf1346
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xf1645
movl $0x16, %esi
movq %rbp, %rdi
callq 0xf1645
movq 0x18(%r15), %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0xefabc
movq %rbp, %rdi
callq 0xf0b13
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)