// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/macrsrc.c
// Function: do_cicn_resource
static void do_cicn_resource(deark *c, lctx *d, struct rsrctypeinfo *rti,
	struct rsrcinstanceinfo *rii, i64 dpos, i64 dlen)
{
	struct fmtutil_macbitmap_info *bi_fgcolor = NULL;
	struct fmtutil_macbitmap_info *bi_mask = NULL;
	struct fmtutil_macbitmap_info *bi_bw = NULL;
	de_bitmap *img_fgcolor = NULL;
	de_bitmap *img_mask = NULL;
	de_bitmap *img_bw = NULL;
	de_finfo *fi = NULL;
	i64 fgcolor_bitssize;
	i64 mask_bitssize;
	i64 bw_bitssize = 0;
	int bw_exists = 0;
	i64 colortable_size = 0;
	i64 pos = dpos;
	int needmsg = 1;
	int ok = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	bi_fgcolor = de_malloc(c, sizeof(struct fmtutil_macbitmap_info));
	bi_mask = de_malloc(c, sizeof(struct fmtutil_macbitmap_info));
	bi_bw = de_malloc(c, sizeof(struct fmtutil_macbitmap_info));
	fi = de_finfo_create(c);

	de_dbg(c, "[color pixmap header]");
	de_dbg_indent(c, 1);
	fmtutil_macbitmap_read_baseaddr(c, c->infile, bi_fgcolor, pos);
	pos += 4;
	fmtutil_macbitmap_read_rowbytes_and_bounds(c, c->infile, bi_fgcolor, pos);
	if(!bi_fgcolor->pixmap_flag) goto done;
	pos += 10;
	fmtutil_macbitmap_read_pixmap_only_fields(c, c->infile, bi_fgcolor, pos);
	pos += 36;

	// Some images have 0 in one or both of these fields.
	if(bi_fgcolor->cmpcount==0) {
		bi_fgcolor->cmpcount = 1;
	}
	if(bi_fgcolor->cmpsize==0) {
		bi_fgcolor->cmpsize = bi_fgcolor->pixelsize;
	}

	de_dbg_indent(c, -1);

	de_dbg(c, "[mask bitmap header]");
	de_dbg_indent(c, 1);
	fmtutil_macbitmap_read_baseaddr(c, c->infile, bi_mask, pos);
	pos += 4;
	fmtutil_macbitmap_read_rowbytes_and_bounds(c, c->infile, bi_mask, pos);
	pos += 10;
	de_dbg_indent(c, -1);

	de_dbg(c, "[b/w bitmap header]");
	de_dbg_indent(c, 1);
	fmtutil_macbitmap_read_baseaddr(c, c->infile, bi_bw, pos);
	pos += 4;
	fmtutil_macbitmap_read_rowbytes_and_bounds(c, c->infile, bi_bw, pos);
	pos += 10;
	de_dbg_indent(c, -1);

	pos += 4; // "icon data"

	// This is said to be optional, but I don't know how to tell if it exists.
	if(bi_bw->rowbytes && bi_bw->height && bi_bw->npwidth) {
		bw_exists = 1;
	}

	if(!de_good_image_dimensions_noerr(c, bi_fgcolor->npwidth, bi_fgcolor->height)) goto done;
	if(!de_good_image_dimensions_noerr(c, bi_mask->npwidth, bi_mask->height)) goto done;
	if(bw_exists && !de_good_image_dimensions_noerr(c, bi_bw->npwidth, bi_bw->height)) goto done;

	if(bi_fgcolor->pixeltype!=0) goto done;
	// TODO: pixsize=8 w/ cmpsize=4 is possibly legal
	if(bi_fgcolor->pixelsize!=bi_fgcolor->cmpsize) goto done;
	if(bi_fgcolor->cmpcount!=1) goto done;
	if(bi_fgcolor->pixelsize!=1 && bi_fgcolor->pixelsize!=2 &&
		bi_fgcolor->pixelsize!=4 && bi_fgcolor->pixelsize!=8)
	{
		goto done;
	}

	mask_bitssize = bi_mask->rowbytes * bi_mask->height;
	if(bw_exists) bw_bitssize = bi_bw->rowbytes * bi_bw->height;
	fgcolor_bitssize =  bi_fgcolor->rowbytes * bi_fgcolor->height;

	if(pos+mask_bitssize > dpos+dlen) goto done;
	de_dbg(c, "mask bitmap at %"I64_FMT", len=%"I64_FMT, pos, mask_bitssize);
	img_mask = de_bitmap_create2(c, bi_mask->npwidth, bi_mask->pdwidth, bi_mask->height, 1);
	de_convert_image_bilevel(c->infile, pos, bi_mask->rowbytes, img_mask, 0);
	pos += mask_bitssize;

	if(bw_exists) {
		if(pos+bw_bitssize > dpos+dlen) goto done;
		de_dbg(c, "bw bitmap at %"I64_FMT", len=%"I64_FMT, pos, bw_bitssize);
		img_bw = de_bitmap_create2(c, bi_bw->npwidth, bi_bw->pdwidth, bi_bw->height, 2);
		de_convert_image_bilevel(c->infile, pos, bi_bw->rowbytes, img_bw,
			DE_CVTF_WHITEISZERO);
		de_bitmap_apply_mask(img_bw, img_mask, 0);
		set_resource_filename(c, d, fi, rti, rii, "cicn_bw");
		de_bitmap_write_to_file_finfo(img_bw, fi, DE_CREATEFLAG_OPT_IMAGE);
		pos += bw_bitssize;
	}
	else {
		de_dbg(c, "[assuming there is no bw bitmap]");
	}

	if(!fmtutil_macbitmap_read_colortable(c, c->infile, bi_fgcolor,
		pos, &colortable_size))
	{
		goto done;
	}
	pos += colortable_size;

	if(pos+fgcolor_bitssize > dpos+dlen) goto done;
	de_dbg(c, "color bitmap at %"I64_FMT", len=%"I64_FMT, pos, fgcolor_bitssize);
	img_fgcolor = de_bitmap_create2(c, bi_fgcolor->npwidth, bi_fgcolor->pdwidth, bi_fgcolor->height, 4);
	de_convert_image_paletted(c->infile, pos, bi_fgcolor->pixelsize, bi_fgcolor->rowbytes,
		bi_fgcolor->pal, img_fgcolor, 0);
	de_bitmap_apply_mask(img_fgcolor, img_mask, 0);
	if(bi_fgcolor->hdpi>=1.0 && bi_fgcolor->vdpi>=1.0) {
		fi->density.code = DE_DENSITY_DPI;
		fi->density.xdens = bi_fgcolor->hdpi;
		fi->density.ydens = bi_fgcolor->vdpi;
	}
	set_resource_filename(c, d, fi, rti, rii, NULL);
	de_bitmap_write_to_file_finfo(img_fgcolor, fi, DE_CREATEFLAG_OPT_IMAGE);
	//pos += fgcolor_bitssize;
	ok = 1;

done:
	if(!ok && needmsg) {
		// TODO: There are a small but significant number of 'cicn' resources that
		// appear to use a completely different format than the one I know about.
		// (Or it could be some sort of systematic corruption.)
		de_err(c, "Failed to parse 'cicn' icon resource at %"I64_FMT, dpos);
	}
	de_bitmap_destroy(img_fgcolor);
	de_bitmap_destroy(img_mask);
	de_bitmap_destroy(img_bw);
	de_free(c, bi_fgcolor);
	de_free(c, bi_mask);
	de_free(c, bi_bw);
	de_finfo_destroy(c, fi);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x98(%rbp)
movl $0x1, -0x9c(%rbp)
movl $0x0, -0xa0(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xa4(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x480, %esi            # imm = 0x480
callq 0x15b780
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl $0x480, %esi            # imm = 0x480
callq 0x15b780
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl $0x480, %esi            # imm = 0x480
callq 0x15b780
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
leaq 0xe6b6f(%rip), %rsi     # 0x1baa74
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0x98(%rbp), %rcx
callq 0x175920
movq -0x98(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0x98(%rbp), %rcx
callq 0x1759a0
movq -0x38(%rbp), %rax
cmpl $0x0, 0x6c(%rax)
jne 0xd3f73
jmp 0xd4587
movq -0x98(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0x98(%rbp), %rcx
callq 0x175ba0
movq -0x98(%rbp), %rax
addq $0x24, %rax
movq %rax, -0x98(%rbp)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0xd3fca
movq -0x38(%rbp), %rax
movq $0x1, 0x48(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0xd3fe5
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
leaq 0xe6a8c(%rip), %rsi     # 0x1baa8a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x40(%rbp), %rdx
movq -0x98(%rbp), %rcx
callq 0x175920
movq -0x98(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x40(%rbp), %rdx
movq -0x98(%rbp), %rcx
callq 0x1759a0
movq -0x98(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
leaq 0xe6a17(%rip), %rsi     # 0x1baa9f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x48(%rbp), %rdx
movq -0x98(%rbp), %rcx
callq 0x175920
movq -0x98(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x48(%rbp), %rdx
movq -0x98(%rbp), %rcx
callq 0x1759a0
movq -0x98(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x98(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x98(%rbp)
movq -0x48(%rbp), %rax
cmpq $0x0, (%rax)
je 0xd4143
movq -0x48(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0xd4143
movq -0x48(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0xd4143
movl $0x1, -0x84(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rdx
callq 0x16a7f0
cmpl $0x0, %eax
jne 0xd4166
jmp 0xd4587
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rdx
callq 0x16a7f0
cmpl $0x0, %eax
jne 0xd4189
jmp 0xd4587
cmpl $0x0, -0x84(%rbp)
je 0xd41b5
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rdx
callq 0x16a7f0
cmpl $0x0, %eax
jne 0xd41b5
jmp 0xd4587
movq -0x38(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0xd41c5
jmp 0xd4587
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
cmpq 0x50(%rcx), %rax
je 0xd41dc
jmp 0xd4587
movq -0x38(%rbp), %rax
cmpq $0x1, 0x48(%rax)
je 0xd41ec
jmp 0xd4587
movq -0x38(%rbp), %rax
cmpq $0x1, 0x40(%rax)
je 0xd421d
movq -0x38(%rbp), %rax
cmpq $0x2, 0x40(%rax)
je 0xd421d
movq -0x38(%rbp), %rax
cmpq $0x4, 0x40(%rax)
je 0xd421d
movq -0x38(%rbp), %rax
cmpq $0x8, 0x40(%rax)
je 0xd421d
jmp 0xd4587
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x40(%rbp), %rcx
imulq 0x20(%rcx), %rax
movq %rax, -0x78(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0xd424e
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
imulq 0x20(%rcx), %rax
movq %rax, -0x80(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
imulq 0x20(%rcx), %rax
movq %rax, -0x70(%rbp)
movq -0x98(%rbp), %rax
addq -0x78(%rbp), %rax
movq -0x28(%rbp), %rcx
addq -0x30(%rbp), %rcx
cmpq %rcx, %rax
jle 0xd427f
jmp 0xd4587
movq -0x8(%rbp), %rdi
movq -0x98(%rbp), %rdx
movq -0x78(%rbp), %rcx
leaq 0xe681e(%rip), %rsi     # 0x1baab3
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rcx
movl $0x1, %r8d
callq 0x16b7f0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x98(%rbp), %rsi
movq -0x40(%rbp), %rax
movq (%rax), %rdx
movq -0x58(%rbp), %rcx
xorl %r8d, %r8d
callq 0x16bee0
movq -0x78(%rbp), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0xd43e7
movq -0x98(%rbp), %rax
addq -0x80(%rbp), %rax
movq -0x28(%rbp), %rcx
addq -0x30(%rbp), %rcx
cmpq %rcx, %rax
jle 0xd4325
jmp 0xd4587
movq -0x8(%rbp), %rdi
movq -0x98(%rbp), %rdx
movq -0x80(%rbp), %rcx
leaq 0xe6794(%rip), %rsi     # 0x1baacf
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rcx
movl $0x2, %r8d
callq 0x16b7f0
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x98(%rbp), %rsi
movq -0x48(%rbp), %rax
movq (%rax), %rdx
movq -0x60(%rbp), %rcx
movl $0x1, %r8d
callq 0x16bee0
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
xorl %edx, %edx
callq 0x16cf60
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
leaq 0xe672d(%rip), %r9      # 0x1baae9
callq 0xd46c0
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
movl $0x2, %edx
callq 0x16afa0
movq -0x80(%rbp), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0xd43f9
movq -0x8(%rbp), %rdi
leaq 0xe66ff(%rip), %rsi     # 0x1baaf1
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0x98(%rbp), %rcx
leaq -0x90(%rbp), %r8
callq 0x175eb0
cmpl $0x0, %eax
jne 0xd4426
jmp 0xd4587
movq -0x90(%rbp), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
addq -0x70(%rbp), %rax
movq -0x28(%rbp), %rcx
addq -0x30(%rbp), %rcx
cmpq %rcx, %rax
jle 0xd4458
jmp 0xd4587
movq -0x8(%rbp), %rdi
movq -0x98(%rbp), %rdx
movq -0x70(%rbp), %rcx
leaq 0xe66a4(%rip), %rsi     # 0x1bab12
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rcx
movl $0x4, %r8d
callq 0x16b7f0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x98(%rbp), %rsi
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rdx
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq -0x38(%rbp), %r8
addq $0x80, %r8
movq -0x50(%rbp), %r9
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
xorl %edx, %edx
callq 0x16cf60
movq -0x38(%rbp), %rax
movsd 0x58(%rax), %xmm0
movsd 0xcccbd(%rip), %xmm1    # 0x1a11b8
ucomisd %xmm1, %xmm0
jb 0xd454d
movq -0x38(%rbp), %rax
movsd 0x60(%rax), %xmm0
movsd 0xccca6(%rip), %xmm1    # 0x1a11b8
ucomisd %xmm1, %xmm0
jb 0xd454d
movq -0x68(%rbp), %rax
movl $0x2, 0x78(%rax)
movq -0x38(%rbp), %rax
movsd 0x58(%rax), %xmm0
movq -0x68(%rbp), %rax
movsd %xmm0, 0x80(%rax)
movq -0x38(%rbp), %rax
movsd 0x60(%rax), %xmm0
movq -0x68(%rbp), %rax
movsd %xmm0, 0x88(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
xorl %eax, %eax
movl %eax, %r9d
callq 0xd46c0
movq -0x50(%rbp), %rdi
movq -0x68(%rbp), %rsi
movl $0x2, %edx
callq 0x16afa0
movl $0x1, -0xa0(%rbp)
cmpl $0x0, -0xa0(%rbp)
jne 0xd45af
cmpl $0x0, -0x9c(%rbp)
je 0xd45af
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0xe6587(%rip), %rsi     # 0x1bab2f
movb $0x0, %al
callq 0x15bfb0
movq -0x50(%rbp), %rdi
callq 0x16af60
movq -0x58(%rbp), %rdi
callq 0x16af60
movq -0x60(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movl -0xa4(%rbp), %esi
callq 0x15b5a0
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)