// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/macpaint.c
// Function: do_read_bitmap
static void do_read_bitmap(deark *c, lctx *d)
{
	i64 cmpr_bytes_consumed = 0;
	dbuf *unc_pixels = NULL;
	de_finfo *fi = NULL;
	int saved_indent_level;
	i64 ipos;
	struct de_packbits_params *pbparams = NULL;
	struct de_dfilter_in_params dcmpri;
	struct de_dfilter_out_params dcmpro;
	struct de_dfilter_results dres;

	de_dbg_indent_save(c, &saved_indent_level);
	if(!d->is_fmac2com) {
		i64 ver_num;

		de_dbg(c, "header at %"I64_FMT, d->hdr_pos);
		de_dbg_indent(c, 1);
		ver_num = de_getu32be(d->hdr_pos);
		de_dbg(c, "version number: %u", (unsigned int)ver_num);
		if(ver_num!=0 && ver_num!=2 && ver_num!=3) {
			de_warn(c, "Unrecognized version number: %u", (unsigned int)ver_num);
		}

		// We wait until later to read the brush patterns, only so that the patterns
		// won't be the first file extracted.

		de_dbg_indent(c, -1);
	}

	ipos = d->img_data_pos;
	de_dbg(c, "image data at %"I64_FMT, ipos);
	de_dbg_indent(c, 1);
	unc_pixels = dbuf_create_membuf(c, MACPAINT_IMAGE_BYTES, 1);
	dbuf_enable_wbuffer(unc_pixels);

	de_dfilter_init_objects(c, &dcmpri, &dcmpro, &dres);
	dcmpri.f = c->infile;
	dcmpri.pos = ipos;
	dcmpri.len = c->infile->len - ipos;
	dcmpro.f = unc_pixels;
	dcmpro.len_known = 1;
	dcmpro.expected_len = MACPAINT_IMAGE_BYTES;
	pbparams = de_malloc(c, sizeof(struct de_packbits_params));

	decompress_packbits_for_macpaint(c, d, &dcmpri, &dcmpro, &dres);
	dbuf_flush(unc_pixels);

	cmpr_bytes_consumed = dres.bytes_consumed;
	de_dbg(c, "decompressed %"I64_FMT" to %"I64_FMT" bytes", cmpr_bytes_consumed,
		unc_pixels->len);

	if(d->df_known) {
		if(ipos+cmpr_bytes_consumed > d->expected_dfpos+d->expected_dflen) {
			de_warn(c, "Image (ends at %"I64_FMT") goes beyond end of "
				"MacBinary data fork (ends at %"I64_FMT")",
				ipos+cmpr_bytes_consumed, d->expected_dfpos+d->expected_dflen);
		}
	}

	if(unc_pixels->len < MACPAINT_IMAGE_BYTES) {
		de_warn(c, "Image decompressed to %"I64_FMT" bytes, expected %u.",
			unc_pixels->len, (UI)MACPAINT_IMAGE_BYTES);
	}
	else if(d->row_issue_flag) {
		de_warn(c, "Rows not compressed independently. Decompression may have failed.");
	}

	fi = de_finfo_create(c);
	if(d->filename && c->filenames_from_file) {
		de_finfo_set_name_from_ucstring(c, fi, d->filename, 0);
	}

	if(d->mod_time_from_macbinary.is_valid) {
		fi->internal_mod_time = d->mod_time_from_macbinary;
	}

	de_convert_and_write_image_bilevel(unc_pixels, 0,
		MACPAINT_WIDTH, MACPAINT_HEIGHT, MACPAINT_WIDTH/8,
		DE_CVTF_WHITEISZERO, fi, 0);

	dbuf_close(unc_pixels);
	de_finfo_destroy(c, fi);
	de_free(c, pbparams);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x15b580
movq -0x10(%rbp), %rax
cmpb $0x0, 0x4(%rax)
jne 0xd96b4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdx
leaq 0xd5742(%rip), %rsi     # 0x1aed63
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rsi
callq 0x165470
movq %rax, -0xe0(%rbp)
movq -0x8(%rbp), %rdi
movq -0xe0(%rbp), %rax
movl %eax, %edx
leaq 0xc6cab(%rip), %rsi     # 0x1a0311
movb $0x0, %al
callq 0x15b0d0
cmpq $0x0, -0xe0(%rbp)
je 0xd96a6
cmpq $0x2, -0xe0(%rbp)
je 0xd96a6
cmpq $0x3, -0xe0(%rbp)
je 0xd96a6
movq -0x8(%rbp), %rdi
movq -0xe0(%rbp), %rax
movl %eax, %edx
leaq 0xe2069(%rip), %rsi     # 0x1bb708
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xed396(%rip), %rsi     # 0x1c6a65
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0xca80, %esi           # imm = 0xCA80
movl $0x1, %edx
callq 0x168020
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0x78(%rbp), %rdx
leaq -0xd8(%rbp), %rcx
callq 0x1773f0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x78(%rbp)
movb $0x1, -0x70(%rbp)
movq $0xca80, -0x68(%rbp)    # imm = 0xCA80
movq -0x8(%rbp), %rdi
movl $0x4, %esi
callq 0x15b780
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x60(%rbp), %rdx
leaq -0x78(%rbp), %rcx
leaq -0xd8(%rbp), %r8
callq 0xd9dd0
movq -0x20(%rbp), %rdi
callq 0x167c30
movq -0xd0(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rcx
leaq 0xd3947(%rip), %rsi     # 0x1ad0f8
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x5(%rax)
je 0xd980b
movq -0x38(%rbp), %rax
addq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x10(%rbp), %rdx
addq 0x18(%rdx), %rcx
cmpq %rcx, %rax
jle 0xd9809
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
addq -0x18(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
addq 0x18(%rax), %rcx
leaq 0xe1f26(%rip), %rsi     # 0x1bb728
movb $0x0, %al
callq 0x15bc10
jmp 0xd980b
movq -0x20(%rbp), %rax
cmpq $0xca80, 0x18(%rax)     # imm = 0xCA80
jge 0xd983a
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0xe1f45(%rip), %rsi     # 0x1bb771
movl $0xca80, %ecx           # imm = 0xCA80
movb $0x0, %al
callq 0x15bc10
jmp 0xd9858
movq -0x10(%rbp), %rax
cmpb $0x0, 0x7(%rax)
je 0xd9856
movq -0x8(%rbp), %rdi
leaq 0xe1f50(%rip), %rsi     # 0x1bb79f
movb $0x0, %al
callq 0x15bc10
jmp 0xd9858
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0xd9897
movq -0x8(%rbp), %rax
movzbl 0xd0(%rax), %eax
cmpl $0x0, %eax
je 0xd9897
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x30(%rax)
je 0xd98b9
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rdx
movq %rdx, 0x68(%rax)
movq 0x38(%rcx), %rcx
movq %rcx, 0x70(%rax)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %esi
movl $0x240, %edx            # imm = 0x240
movl $0x2d0, %ecx            # imm = 0x2D0
movl $0x48, %r8d
movl $0x1, %r9d
xorl %r10d, %r10d
movq %rax, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x16bf50
movq -0x20(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x15b5a0
addq $0xf0, %rsp
popq %rbp
retq
nopw (%rax,%rax)