// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/misc2.c
// Function: do_iconmgr_icon
static void do_iconmgr_icon(deark *c, struct iconmgr_ica_ctx *d,
	struct iconmgr_icon_ctx *md)
{
	de_bitmap *img = NULL;
	de_bitmap *mask = NULL;
	de_finfo *fi = NULL;
	i64 pos;
	i64 bpp, xpos, ypos;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "icon at %"I64_FMT, md->icon_pos);
	de_dbg_indent(c, 1);
	pos = md->icon_pos;

	// Try to detect if we've gone off the rails.
	// TODO: Are bit depths other than 4 possible?
	bpp = de_getbyte_p(&pos);
	xpos = de_getu16le_p(&pos);
	ypos = de_getu16le_p(&pos);
	if(bpp!=4 || xpos>=4096 || ypos>=4096) {
		d->need_errmsg = 1;
		goto done;
	}

	if(d->ver==2) {
		md->name = ucstring_create(c);
		dbuf_read_to_ucstring(c->infile, pos, 26, md->name, DE_CONVFLAG_STOP_AT_NUL,
			d->input_encoding);
		de_dbg(c, "name: \"%s\"", ucstring_getpsz_d(md->name));
		pos += 26;
	}

	de_dbg(c, "bitmap at %"I64_FMT, pos);
	img = de_bitmap_create(c, 32, 32, 4);
	de_convert_image_paletted(c->infile, pos, 4, 16, d->pal, img, 0);
	pos += 512;

	de_dbg(c, "mask at %"I64_FMT, pos);
	mask = de_bitmap_create(c, 32, 32, 1);
	de_convert_image_bilevel(c->infile, pos, 4, mask, 0);
	pos += 128;

	de_bitmap_apply_mask(img, mask, DE_BITMAPFLAG_WHITEISTRNS);

	fi = de_finfo_create(c);
	if(c->filenames_from_file && ucstring_isnonempty(md->name)) {
		de_finfo_set_name_from_ucstring(c, fi, md->name, 0);
	}

	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_FLIP_IMAGE|DE_CREATEFLAG_OPT_IMAGE);
	md->total_len = pos - md->icon_pos;

done:
	de_bitmap_destroy(img);
	de_bitmap_destroy(mask);
	de_finfo_destroy(c, fi);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x54(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
leaq 0x114bea(%rip), %rsi    # 0x1b0d25
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x50(%rbp)
cmpq $0x4, -0x40(%rbp)
jne 0x9c1b8
cmpq $0x1000, -0x48(%rbp)    # imm = 0x1000
jge 0x9c1b8
cmpq $0x1000, -0x50(%rbp)    # imm = 0x1000
jl 0x9c1c5
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x9c38d
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x2, %eax
jne 0x9c247
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movl 0x4(%rax), %r9d
movl $0x1a, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x172650
movq -0x60(%rbp), %rdi
movq %rax, %rdx
leaq 0x111f9c(%rip), %rsi    # 0x1ae1d0
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x1a, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x125cb1(%rip), %rsi    # 0x1c1f07
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x20, %edx
movl $0x4, %ecx
movq %rdx, %rsi
callq 0x16b750
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x10(%rbp), %r8
addq $0x18, %r8
movq -0x20(%rbp), %r9
movl $0x4, %edx
movl $0x10, %ecx
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x38(%rbp), %rax
addq $0x200, %rax            # imm = 0x200
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x129f03(%rip), %rsi    # 0x1c61c7
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x20, %edx
movl $0x1, %ecx
movq %rdx, %rsi
callq 0x16b750
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x28(%rbp), %rcx
movl $0x4, %edx
xorl %r8d, %r8d
callq 0x16bee0
movq -0x38(%rbp), %rax
addq $0x80, %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl $0x1, %edx
callq 0x16cf60
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movzbl 0xd0(%rax), %eax
cmpl $0x0, %eax
je 0x9c368
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1717f0
cmpl $0x0, %eax
je 0x9c368
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x6, %edx
callq 0x16afa0
movq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rax
subq (%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rdi
callq 0x16af60
movq -0x28(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x15b5a0
addq $0x70, %rsp
popq %rbp
retq
nop