// File: /workspace/github_c_makefile_star2_O3/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 0xa0b77
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xeead3
cmpb $-0x3, %al
jne 0xa0b77
leaq 0x919c4(%rip), %rsi     # 0x132531
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9393
movq 0x18(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0xeec86
movq %rax, %rbp
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeec86
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 0xe8f88
leaq 0x97607(%rip), %rsi     # 0x1381cc
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %r13, 0x10(%rsp)
movq %r13, %rsi
movq %r14, 0x8(%rsp)
movq %r14, %rdx
callq 0xf1db1
testl %eax, %eax
je 0xa0c17
addq $0x8, %r15
shrq $0x3, %r15
testq %rbp, %rbp
js 0xa0c1f
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
movq 0x18(%rdi), %rdx
subq %rsi, %rdx
callq 0xf1388
movq %rax, %r13
jmp 0xa0ca1
xorl %r13d, %r13d
jmp 0xa0cc6
movq 0x8(%rsp), %rbp
imulq %r15, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0xf09c5
movq %rax, %r13
movq (%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0xa0ca1
movq %rsp, %r14
cmpq %rbp, 0x18(%r13)
jge 0xa0ca1
movq %r14, %rsi
callq 0xeeb4b
leal 0x1(%rax), %ecx
movzbl %al, %r12d
cmpb $0x1, %cl
ja 0xa0c85
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeeb4b
movzbl %al, %edx
incq %rdx
movq %r13, %rdi
movl %r12d, %esi
callq 0xf0eba
jmp 0xa0c90
movq %r13, %rdi
movl %r12d, %esi
callq 0xf0bd6
movq (%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
movq %rsp, %r14
jl 0xa0c4c
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 0xf2b5b
addq $0x10, %rsp
movq %r13, %rdi
callq 0xf052b
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq