// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
andq $0x0, 0x18(%rsp)
movq %rdi, %rbx
cmpl $0x1, 0x24(%rdi)
jne 0x9ae61
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xe3eb4
cmpb $-0x3, %al
jne 0x9ae61
leaq 0x8a3ee(%rip), %rsi     # 0x125245
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf37f
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0xe4026
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe4026
movq %rax, %rbp
movq %r15, %r14
shrq $0x3f, %r14
movq %r15, %r12
negq %r12
movq %r15, 0x10(%rsp)
cmovsq %r15, %r12
leaq 0x1(%r12), %r15
incq %rbp
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0xdf038
leaq 0x90015(%rip), %rsi     # 0x12aecc
xorl %r13d, %r13d
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movq %r15, 0x20(%rsp)
movq %r15, %rsi
movq %rbp, %rdx
callq 0xe6be1
testl %eax, %eax
je 0x9afab
addq $0x8, %r12
shrq $0x3, %r12
cmpq $0x0, 0x10(%rsp)
movq %r12, %r15
js 0x9af0f
movq 0x18(%rbx), %rdi
movq 0x18(%rsp), %rsi
movq 0x18(%rdi), %rdx
subq %rsi, %rdx
callq 0xe6248
movq %rax, %r13
jmp 0x9af88
movq %rbp, 0x10(%rsp)
imulq %r15, %rbp
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe59cd
movq %rax, %r13
leaq 0x18(%rsp), %r12
movq 0x18(%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0x9af83
cmpq %rbp, 0x18(%r13)
jge 0x9af83
movq %r12, %rsi
callq 0xe3f2a
leal 0x1(%rax), %ecx
movzbl %al, %r14d
cmpb $0x1, %cl
ja 0x9af76
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f2a
movzbl %al, %edx
incq %rdx
movq %r13, %rdi
movl %r14d, %esi
callq 0xe5e40
jmp 0x9af2e
movq %r13, %rdi
movl %r14d, %esi
callq 0xe5bc5
jmp 0x9af2e
movq 0x10(%rsp), %rbp
andl $0x0, 0x8(%rsp)
andq $0x0, (%rsp)
pushq $0x1
popq %r9
movq %r13, %rdi
xorl %esi, %esi
movq 0x20(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
callq 0xe78af
movq %r13, %rdi
callq 0xe5591
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq