// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq $0x0, (%rsp)
cmpl $0x1, 0x24(%rdi)
jne 0xa142b
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xef0d5
cmpb $-0x3, %al
jne 0xa142b
leaq 0x90d8c(%rip), %rsi     # 0x1321ad
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9ef5
movq 0x18(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0xef295
movq %rax, %rbp
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef295
movq %rax, %r14
movq %rbp, %r12
shrq $0x3f, %r12
movq %rbp, %r15
negq %r15
cmovsq %rbp, %r15
leaq 0x1(%r15), %r13
incq %r14
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xe9b05
leaq 0x969c7(%rip), %rsi     # 0x137e40
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movq %r13, 0x10(%rsp)
movq %r13, %rsi
movq %r14, 0x8(%rsp)
movq %r14, %rdx
callq 0xf241d
testl %eax, %eax
je 0xa14cb
addq $0x8, %r15
shrq $0x3, %r15
testq %rbp, %rbp
js 0xa14d3
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
movq 0x18(%rdi), %rdx
subq %rsi, %rdx
callq 0xf1988
movq %rax, %r13
jmp 0xa1555
xorl %r13d, %r13d
jmp 0xa157a
movq 0x8(%rsp), %rbp
imulq %r15, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0xf0faf
movq %rax, %r13
movq (%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0xa1555
movq %rsp, %r14
cmpq %rbp, 0x18(%r13)
jge 0xa1555
movq %r14, %rsi
callq 0xef14d
leal 0x1(%rax), %ecx
movzbl %al, %r12d
cmpb $0x1, %cl
ja 0xa1539
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef14d
movzbl %al, %edx
incq %rdx
movq %r13, %rdi
movl %r12d, %esi
callq 0xf14bd
jmp 0xa1544
movq %r13, %rdi
movl %r12d, %esi
callq 0xf11be
movq (%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
movq %rsp, %r14
jl 0xa1500
movq %r13, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %r15, %r8
movl $0x1, %r9d
pushq $0x0
pushq $0x0
callq 0xf319c
addq $0x10, %rsp
movq %r13, %rdi
callq 0xf0b13
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq