// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/megapaint.c
// Function: de_run_bld
static void de_run_bld(deark *c, de_module_params *mparams)
{
	i64 w_raw, h_raw;
	i64 w, h;
	i64 rowspan;
	i64 pos = 0;
	int is_compressed;
	dbuf *unc_pixels = NULL;

	if(c->module_disposition==DE_MODDISP_AUTODETECT &&
		de_getbyte(0)==0xfd)
	{
		// Unfortunately, Megapaint and BSAVE may both use extension .bld.
		// And compressed Megapaint files with a width between 514 and 769
		// start with 0xfd, like BSAVE files do.
		// TODO: Figure out if it's possible to distinguish them.
		de_info(c, "Note: This might be a BLOAD/BSAVE file. "
			"Try \"-m bsave\" if there are problems.");
	}

	w_raw = de_geti16be_p(&pos);
	h_raw = de_geti16be_p(&pos);
	is_compressed = (w_raw<0);
	w = is_compressed ? ((-w_raw)+1) : (w_raw+1);
	h = h_raw+1;
	de_dbg_dimensions(c, w, h);
	de_dbg(c, "compressed: %d", is_compressed);
	if(!de_good_image_dimensions(c, w, h)) goto done;
	rowspan = (w+7)/8;

	if(is_compressed) {
		unc_pixels = dbuf_create_membuf(c, h*rowspan, 1);
		while(1) {
			u8 b1;
			i64 count;

			if(pos >= c->infile->len) break;
			if(unc_pixels->len >= h*rowspan) break;

			b1 = de_getbyte_p(&pos);
			if(b1==0x00 || b1==0xff) {
				count = 1+(i64)de_getbyte_p(&pos);
				dbuf_write_run(unc_pixels, b1, count);
			}
			else {
				dbuf_writebyte(unc_pixels, b1);
			}
		}
	}
	else {
		unc_pixels = dbuf_open_input_subfile(c->infile, pos, c->infile->len-pos);
	}

	de_convert_and_write_image_bilevel2(unc_pixels, 0, w, h, rowspan,
		DE_CVTF_WHITEISZERO, NULL, 0);

done:
	dbuf_close(unc_pixels);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x1, 0x24(%rax)
jne 0xed36a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x165060
movzbl %al, %eax
cmpl $0xfd, %eax
jne 0xed36a
movq -0x8(%rbp), %rdi
leaq 0xd0b85(%rip), %rsi     # 0x1bdee8
movb $0x0, %al
callq 0x15c0c0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1653b0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1653b0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
setl %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xed3ba
xorl %eax, %eax
subq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0xed3c6
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %edx
leaq 0xd6687(%rip), %rsi     # 0x1c3a80
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0xed41b
jmp 0xed548
movq -0x28(%rbp), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xed4f2
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
imulq -0x38(%rbp), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0xed46d
jmp 0xed4f0
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rcx
imulq -0x38(%rbp), %rcx
cmpq %rcx, %rax
jl 0xed485
jmp 0xed4f0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, -0x51(%rbp)
movzbl -0x51(%rbp), %eax
cmpl $0x0, %eax
je 0xed4ad
movzbl -0x51(%rbp), %eax
cmpl $0xff, %eax
jne 0xed4de
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rdi
movb -0x51(%rbp), %al
movq -0x60(%rbp), %rdx
movzbl %al, %esi
callq 0x168800
jmp 0xed4eb
movq -0x50(%rbp), %rdi
movzbl -0x51(%rbp), %esi
callq 0x168380
jmp 0xed456
jmp 0xed517
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
subq -0x40(%rbp), %rdx
callq 0x1691b0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %r8
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %r9d
xorl %eax, %eax
movq $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x16c000
movq -0x50(%rbp), %rdi
callq 0x1676a0
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)