// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
leaq -0xf0(%rbp), %rdx
movl $0x2, %r9d
callq 0x172d70
cmpl $0x0, %eax
jne 0x628fd
movq -0x8(%rbp), %rdi
leaq 0x14520b(%rip), %rsi    # 0x1a7afc
movb $0x0, %al
callq 0x15bfb0
jmp 0x62aa4
cmpl $0x1, -0xf0(%rbp)
jne 0x6292e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq 0x18(%rbp), %r8
leaq 0x1515b7(%rip), %rcx    # 0x1b3ed8
xorl %r9d, %r9d
callq 0x166be0
jmp 0x62aa4
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %ecx
leaq 0x14a3c2(%rip), %rsi    # 0x1accfe
leaq 0x146601(%rip), %rax    # 0x1a8f44
cmpl $0x0, %ecx
cmovneq %rax, %rsi
movq 0x18(%rbp), %rdx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168a10
movq -0x38(%rbp), %rdi
movl -0x24(%rbp), %edx
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movslq %eax, %rsi
callq 0x168a10
movq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x168a10
movq -0xd0(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0xff, -0x40(%rbp)
jle 0x629b0
movq $0x0, -0x40(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0xff, -0x48(%rbp)
jle 0x629cd
movq $0x0, -0x48(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0xff, -0x50(%rbp)
jle 0x629ea
movq $0x0, -0x50(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x629fc
movq -0x60(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rax
movzbl %al, %esi
callq 0x168380
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rax
movzbl %al, %esi
callq 0x168380
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rax
movzbl %al, %esi
callq 0x168380
cmpl $0x0, -0x24(%rbp)
je 0x62a59
movq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0x168380
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x168a10
movq -0x38(%rbp), %rdi
movq 0x10(%rbp), %rsi
callq 0x168a10
jmp 0x62a67
movq -0x38(%rbp), %rdi
movl $0x5, %esi
callq 0x1685f0
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x168c30
movq -0x38(%rbp), %rdi
movl $0x16, %esi
callq 0x168c30
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x166070
movq -0x38(%rbp), %rdi
callq 0x1676a0
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)