// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/macrsrc.c
// Function: do_crsr_CURS_resource
static void do_crsr_CURS_resource(deark *c, lctx *d, struct rsrctypeinfo *rti,
	struct rsrcinstanceinfo *rii, i64 pos1, i64 dlen)
{
	struct fmtutil_macbitmap_info *bi = NULL;
	de_bitmap *img_color = NULL;
	de_bitmap *img_bw = NULL;
	de_bitmap *img_mask = NULL;
	de_finfo *fi = NULL;
	i64 pos = pos1;
	i64 pixmap_offs = 0;
	i64 pixdata_offs = 0;
	i64 n;
	i64 colortable_size = 0;
	int is_crsr = (rti->fcc.id==CODE_crsr);

	if(dlen<68) goto done;
	fi = de_finfo_create(c);

	if(is_crsr) {
		// TODO: Do we need special handling for type=0x8000 (b/w cursor)?
		n = de_getu16be_p(&pos);
		de_dbg(c, "cursor type: 0x%04x", (unsigned int)n);
		if(n!=0x8000 && n!=0x8001) {
			de_err(c, "Invalid or unsupported 'crsr' cursor type");;
			goto done;
		}
		pixmap_offs = de_getu32be_p(&pos);
		de_dbg(c, "offset to pixel map: %d", (int)pixmap_offs);
		pixdata_offs = de_getu32be_p(&pos);
		de_dbg(c, "offset to pixel data: %d", (int)pixdata_offs);
		pos += 10; // other fields
	}

	de_dbg(c, "b/w foreground at %"I64_FMT, pos);
	img_bw = de_bitmap_create(c, 16, 16, 2);
	de_convert_image_bilevel(c->infile, pos, 2, img_bw, DE_CVTF_WHITEISZERO);
	pos += 32;

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

	// I'm assuming the hotspot is a QuickDraw Point structure.
	fi->hotspot_y = (int)de_geti16be_p(&pos);
	fi->hotspot_x = (int)de_geti16be_p(&pos);
	fi->has_hotspot = 1;
	de_dbg(c, "hotspot: (%d,%d)", fi->hotspot_x, fi->hotspot_y);

	de_bitmap_apply_mask(img_bw, img_mask, 0);
	set_resource_filename(c, d, fi, rti, rii, (is_crsr?"crsr_bw":NULL));
	de_bitmap_write_to_file_finfo(img_bw, fi, 0);
	if(!is_crsr) goto done;

	bi = de_malloc(c, sizeof(struct fmtutil_macbitmap_info));
	if(pixmap_offs >= dlen) goto done;
	pos = pos1+pixmap_offs;
	fmtutil_macbitmap_read_baseaddr(c, c->infile, bi, pos);
	pos += 4;
	fmtutil_macbitmap_read_rowbytes_and_bounds(c, c->infile, bi, pos);
	pos += 10;
	fmtutil_macbitmap_read_pixmap_only_fields(c, c->infile, bi, pos);
	pos += 36;
	if(!de_good_image_dimensions(c, bi->npwidth, bi->height)) goto done;

	if((i64)bi->pmTable != pixdata_offs + bi->rowbytes * bi->height) {
		de_warn(c, "Unexpected color table offset. "
			"Cursor might not be decoded correctly.");
	}
	if(bi->pmTable>0 && bi->pmTable<dlen) {
		pos = pos1 + (i64)bi->pmTable;
	}
	else {
		pos = pos1 + pixdata_offs + bi->rowbytes * bi->height;
	}
	if(!fmtutil_macbitmap_read_colortable(c, c->infile, bi, pos, &colortable_size)) {
		goto done;
	}

	img_color = de_bitmap_create2(c, bi->npwidth, bi->pdwidth, bi->height, 4);

	if(pixdata_offs >= dlen) goto done;
	pos = pos1 + pixdata_offs;
	de_dbg(c, "color pixels at %"I64_FMT, pos);
	de_convert_image_paletted(c->infile, pos, bi->pixelsize, bi->rowbytes,
		bi->pal, img_color, 0);
	de_bitmap_apply_mask(img_color, img_mask, 0);

	set_resource_filename(c, d, fi, rti, rii, NULL);
	if(bi->hdpi>=1.0 && bi->vdpi>=1.0) {
		fi->density.code = DE_DENSITY_DPI;
		fi->density.xdens = bi->hdpi;
		fi->density.ydens = bi->vdpi;
	}
	de_bitmap_write_to_file_finfo(img_color, fi, DE_CREATEFLAG_OPT_IMAGE);

done:
	de_free(c, bi);
	de_bitmap_destroy(img_color);
	de_bitmap_destroy(img_bw);
	de_bitmap_destroy(img_mask);
	de_finfo_destroy(c, fi);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %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 -0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x63727372, 0x4(%rax)  # imm = 0x63727372
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x84(%rbp)
cmpq $0x44, -0x30(%rbp)
jge 0xd38e0
jmp 0xd3dd0
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x58(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0xd39b8
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x60(%rbp), %rsi
callq 0x165210
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rax
movl %eax, %edx
leaq 0xe705c(%rip), %rsi     # 0x1ba97c
movb $0x0, %al
callq 0x15b0d0
cmpq $0x8000, -0x78(%rbp)    # imm = 0x8000
je 0xd3952
cmpq $0x8001, -0x78(%rbp)    # imm = 0x8001
je 0xd3952
movq -0x8(%rbp), %rdi
leaq 0xe704a(%rip), %rsi     # 0x1ba990
movb $0x0, %al
callq 0x15bfb0
jmp 0xd3dd0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1654b0
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rax
movl %eax, %edx
leaq 0xe7042(%rip), %rsi     # 0x1ba9ba
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1654b0
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rax
movl %eax, %edx
leaq 0xe702d(%rip), %rsi     # 0x1ba9d2
movb $0x0, %al
callq 0x15b0d0
movq -0x60(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rdx
leaq 0xe7024(%rip), %rsi     # 0x1ba9eb
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x10, %edx
movl $0x2, %ecx
movq %rdx, %rsi
callq 0x16b750
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x48(%rbp), %rcx
movl $0x2, %edx
movl $0x1, %r8d
callq 0x16bee0
movq -0x60(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rdx
leaq 0xf27a4(%rip), %rsi     # 0x1c61c7
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x10, %edx
movl $0x1, %ecx
movq %rdx, %rsi
callq 0x16b750
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x50(%rbp), %rcx
movl $0x2, %edx
xorl %r8d, %r8d
callq 0x16bee0
movq -0x60(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1653b0
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x9c(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1653b0
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x98(%rax)
movq -0x58(%rbp), %rax
movb $0x1, 0xe(%rax)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movl 0x98(%rax), %edx
movq -0x58(%rbp), %rax
movl 0x9c(%rax), %ecx
leaq 0xe6f33(%rip), %rsi     # 0x1baa01
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
xorl %edx, %edx
callq 0x16cf60
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
movl -0x84(%rbp), %r10d
xorl %eax, %eax
movl %eax, %r9d
leaq 0xe6f07(%rip), %rax     # 0x1baa12
cmpl $0x0, %r10d
cmovneq %rax, %r9
callq 0xd46c0
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
xorl %edx, %edx
callq 0x16afa0
cmpl $0x0, -0x84(%rbp)
jne 0xd3b35
jmp 0xd3dd0
movq -0x8(%rbp), %rdi
movl $0x480, %esi            # imm = 0x480
callq 0x15b780
movq %rax, -0x38(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x30(%rbp), %rax
jl 0xd3b56
jmp 0xd3dd0
movq -0x28(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x175920
movq -0x60(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x1759a0
movq -0x60(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x175ba0
movq -0x60(%rbp), %rax
addq $0x24, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0xd3bf4
jmp 0xd3dd0
movq -0x38(%rbp), %rax
movl 0x68(%rax), %eax
movq -0x70(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq (%rdx), %rdx
movq -0x38(%rbp), %rsi
imulq 0x20(%rsi), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
je 0xd3c29
movq -0x8(%rbp), %rdi
leaq 0xe6df8(%rip), %rsi     # 0x1baa1a
movb $0x0, %al
callq 0x15bc10
movq -0x38(%rbp), %rax
cmpl $0x0, 0x68(%rax)
jbe 0xd3c54
movq -0x38(%rbp), %rax
movl 0x68(%rax), %eax
cmpq -0x30(%rbp), %rax
jge 0xd3c54
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
movl 0x68(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
jmp 0xd3c73
movq -0x28(%rbp), %rax
addq -0x70(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rdx
imulq 0x20(%rdx), %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0x60(%rbp), %rcx
leaq -0x80(%rbp), %r8
callq 0x175eb0
cmpl $0x0, %eax
jne 0xd3c9a
jmp 0xd3dd0
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, -0x40(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x30(%rbp), %rax
jl 0xd3cd4
jmp 0xd3dd0
movq -0x28(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rdx
leaq 0xe6d71(%rip), %rsi     # 0x1baa60
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%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 -0x40(%rbp), %r9
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x40(%rbp), %rdi
movq -0x50(%rbp), %rsi
xorl %edx, %edx
callq 0x16cf60
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
xorl %eax, %eax
movl %eax, %r9d
callq 0xd46c0
movq -0x38(%rbp), %rax
movsd 0x58(%rax), %xmm0
movsd 0xcd44c(%rip), %xmm1    # 0x1a11b8
ucomisd %xmm1, %xmm0
jb 0xd3dbe
movq -0x38(%rbp), %rax
movsd 0x60(%rax), %xmm0
movsd 0xcd435(%rip), %xmm1    # 0x1a11b8
ucomisd %xmm1, %xmm0
jb 0xd3dbe
movq -0x58(%rbp), %rax
movl $0x2, 0x78(%rax)
movq -0x38(%rbp), %rax
movsd 0x58(%rax), %xmm0
movq -0x58(%rbp), %rax
movsd %xmm0, 0x80(%rax)
movq -0x38(%rbp), %rax
movsd 0x60(%rax), %xmm0
movq -0x58(%rbp), %rax
movsd %xmm0, 0x88(%rax)
movq -0x40(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl $0x2, %edx
callq 0x16afa0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b820
movq -0x40(%rbp), %rdi
callq 0x16af60
movq -0x48(%rbp), %rdi
callq 0x16af60
movq -0x50(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x15d260
addq $0x90, %rsp
popq %rbp
retq
nop