// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/ico.c
// Function: decode_win1_icon
static int decode_win1_icon(deark *c, win1ctx *d, i64 pos1)
{
	de_bitmap *mask = NULL;
	de_bitmap *img = NULL;
	de_finfo *fi = NULL;
	i64 npwidth, h;
	i64 pdwidth;
	i64 rowspan;
	i64 i, j;
	i64 pos = pos1;
	int has_inv_bkgd = 0;
	int hotspot_x = 0;
	int hotspot_y = 0;
	int retval = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	if(pos1+12 > c->infile->len) goto done;

	de_dbg(c, "%s at %"I64_FMT, d->type_name, pos);
	de_dbg_indent(c, 1);

	if(d->is_cur) {
		hotspot_x = (int)de_getu16le(pos);
		hotspot_y = (int)de_getu16le(pos+2);
		de_dbg(c, "hotspot: %d,%d", hotspot_x, hotspot_y);
	}
	pos += 4;

	npwidth = de_getu16le_p(&pos);
	h = de_getu16le_p(&pos);
	de_dbg_dimensions(c, npwidth, h);
	if(!de_good_image_dimensions(c, npwidth, h)) goto done;

	rowspan = de_getu16le_p(&pos);
	de_dbg(c, "bytes/row: %d", (int)rowspan);
	pdwidth = rowspan*8;

	if(d->is_cur) {
		unsigned int csColor;
		csColor = (unsigned int)de_getu16le(pos);
		de_dbg(c, "csColor: 0x%04x", csColor);
	}
	pos += 2;

	mask = de_bitmap_create2(c, npwidth, pdwidth, h, 1);
	img = de_bitmap_create2(c, npwidth, pdwidth, h, 4);
	de_dbg(c, "mask at %"I64_FMT, pos);
	de_convert_image_bilevel(c->infile, pos, rowspan, mask, 0);
	pos += rowspan*h;
	de_dbg(c, "foreground at %"I64_FMT, pos);
	de_convert_image_bilevel(c->infile, pos, rowspan, img, 0);
	pos += rowspan*h;

	// This whole loop does nothing, except handle inverse-background-color
	// pixels. But we have to do something, because such pixels are not
	// uncommon.
	for(j=0; j<h; j++) {
		for(i=0; i<pdwidth; i++) {
			u8 fgclr, maskclr;
			u32 newclr;

			maskclr = DE_COLOR_K(de_bitmap_getpixel(mask, i, j));
			if(maskclr==0) continue;
			fgclr = DE_COLOR_K(de_bitmap_getpixel(img, i, j));
			if(fgclr==0) continue;

			newclr = get_inv_bkgd_replacement_clr(i, j);
			de_bitmap_setpixel_gray(mask, i, j, 255-DE_COLOR_A(newclr));
			de_bitmap_setpixel_rgb(img, i, j, DE_MAKE_OPAQUE(newclr));
			if(i<npwidth) {
				has_inv_bkgd = 1;
			}
		}
	}
	if(has_inv_bkgd) {
		warn_inv_bkgd(c);
	}

	de_bitmap_apply_mask(img, mask, DE_BITMAPFLAG_WHITEISTRNS);

	fi = de_finfo_create(c);

	if(d->is_cur) {
		fi->has_hotspot = 1;
		fi->hotspot_x = hotspot_x;
		fi->hotspot_y = hotspot_y;
	}

	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_OPT_IMAGE);
	d->bytes_consumed = pos - pos1;
	retval = 1;

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %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 -0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movl $0x0, -0x78(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x7c(%rbp), %rsi
callq 0x15b580
movq -0x18(%rbp), %rax
addq $0xc, %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xacfbb
jmp 0xad372
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x68(%rbp), %rcx
leaq 0x10f72a(%rip), %rsi    # 0x1bc6fc
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0xad035
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
callq 0x165280
movl %eax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
addq $0x2, %rsi
callq 0x165280
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rdi
movl -0x70(%rbp), %edx
movl -0x74(%rbp), %ecx
leaq 0xfbf1a(%rip), %rsi     # 0x1a8f48
movb $0x0, %al
callq 0x15b0d0
movq -0x68(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x68(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x68(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0xad097
jmp 0xad372
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x68(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0x114bee(%rip), %rsi    # 0x1c1cab
movb $0x0, %al
callq 0x15b0d0
movq -0x50(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0xad103
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
callq 0x165280
movl %eax, -0x80(%rbp)
movq -0x8(%rbp), %rdi
movl -0x80(%rbp), %edx
leaq 0x106e6d(%rip), %rsi    # 0x1b3f69
movb $0x0, %al
callq 0x15b0d0
movq -0x68(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x40(%rbp), %rcx
movl $0x1, %r8d
callq 0x16b7f0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x40(%rbp), %rcx
movl $0x4, %r8d
callq 0x16b7f0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rdx
leaq 0x11906b(%rip), %rsi    # 0x1c61c7
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x20(%rbp), %rcx
xorl %r8d, %r8d
callq 0x16bee0
movq -0x50(%rbp), %rax
imulq -0x40(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rdx
leaq 0x10d850(%rip), %rsi    # 0x1ba9ef
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x28(%rbp), %rcx
xorl %r8d, %r8d
callq 0x16bee0
movq -0x50(%rbp), %rax
imulq -0x40(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0xad2ef
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x48(%rbp), %rax
jge 0xad2dc
movq -0x20(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x16b540
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x82(%rbp)
movzbl -0x82(%rbp), %eax
cmpl $0x0, %eax
jne 0xad22f
jmp 0xad2cb
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x16b540
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x81(%rbp)
movzbl -0x81(%rbp), %eax
cmpl $0x0, %eax
jne 0xad25c
jmp 0xad2cb
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0xacd70
movl %eax, -0x88(%rbp)
movq -0x20(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl -0x88(%rbp), %eax
shrl $0x18, %eax
andl $0xff, %eax
movzbl %al, %ecx
movl $0xff, %eax
subl %ecx, %eax
movzbl %al, %ecx
callq 0x16b1c0
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl -0x88(%rbp), %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
callq 0x16b310
movq -0x58(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0xad2c9
movl $0x1, -0x6c(%rbp)
jmp 0xad2cb
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xad1f1
jmp 0xad2de
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xad1db
cmpl $0x0, -0x6c(%rbp)
je 0xad2fe
movq -0x8(%rbp), %rdi
callq 0xacd40
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x1, %edx
callq 0x16cf60
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0xad349
movq -0x30(%rbp), %rax
movb $0x1, 0xe(%rax)
movl -0x70(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x98(%rax)
movl -0x74(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x9c(%rax)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x2, %edx
callq 0x16afa0
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x1, -0x78(%rbp)
movq -0x28(%rbp), %rdi
callq 0x16af60
movq -0x20(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movl -0x7c(%rbp), %esi
callq 0x15b5a0
movl -0x78(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)