// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x2
popq %r9
movq %r14, %rcx
movq %rbx, %r8
callq 0xebba5
testl %eax, %eax
je 0x457da
movq 0xf0(%rsp), %r8
cmpl $0x1, 0x18(%rsp)
jne 0x457f0
movq 0x18(%r15), %rdi
leaq 0xd5d29(%rip), %rcx     # 0x11b4f0
movq %r14, %rsi
movq %rbx, %rdx
xorl %r9d, %r9d
callq 0xe4e0b
jmp 0x45939
leaq 0xc95d6(%rip), %rsi     # 0x10edb7
movq %r15, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x45939
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
xorl %r13d, %r13d
testl %ebp, %ebp
leaq 0xce863(%rip), %rax     # 0x114069
leaq 0xcaa96(%rip), %rsi     # 0x1102a3
cmoveq %rax, %rsi
setne %r13b
movq %r15, %rdi
movq %r8, %rdx
xorl %ecx, %ecx
callq 0xe4e62
movl %ebp, 0x4(%rsp)
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
callq 0xe5f08
incq %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0xe5f08
pushq $0x1
popq %rsi
movq %rbp, %rdi
callq 0xe5f08
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 0xe5bc5
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 0xe5bc5
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 0xe5bc5
cmpl $0x0, 0x4(%rsp)
je 0x458fe
movq %rbp, %rdi
xorl %esi, %esi
callq 0xe5bc5
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0xe5f08
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0xe5f08
jmp 0x45909
pushq $0x5
popq %rsi
movq %rbp, %rdi
callq 0xe5d20
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xe5f7f
pushq $0x16
popq %rsi
movq %rbp, %rdi
callq 0xe5f7f
movq 0x18(%r15), %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0xe45f4
movq %rbp, %rdi
callq 0xe5591
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq