// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/bsave.c
// Function: do_4color
static void do_4color(deark *c, lctx *d)
{
	// TODO: This may not be the right palette.
	static const de_color default_palette[4] = { 0x000000, 0x55ffff, 0xff55ff, 0xffffff };
	de_color palette[4];
	i64 w, h;
	i64 i,j;
	i64 pos;
	i64 src_rowspan;
	de_bitmap *img = NULL;

	pos = BSAVE_HDRSIZE;

	if(d->has_dimension_fields) {
		// 11-byte header that includes width & height
		w = (d->metrics.maybe_bitsperrow + 1)/2; // width = number of bits??
		h = d->metrics.maybe_nrows;
		pos+=4;
	}
	else if(!d->interlaced) {
		w = get_width(c, d, 320);
		src_rowspan = (w+3)/4;
		h = d->data_size / src_rowspan;
		h = get_height(c, d, h);
	}
	else {
		w = get_width(c, d, 320);
		h = get_height(c, d, 200);	// TODO: Calculate this?
	}

	// Set the palette
	if(d->have_pcpaint_palinfo) {
		de_copy_std_palette(DE_PALID_CGA, (int)d->pcpaint_pal_num, 0, palette, 4, 0);
		palette[0] = de_get_std_palette_entry(DE_PALID_PC16, 0, (int)d->pcpaint_border_col);
	}
	else {
		for(i=0; i<4; i++) {
			palette[i] = default_palette[i];
		}
	}

	src_rowspan = (w+3)/4;
	de_dbg_dimensions(c, w, h);
	if(!de_good_image_dimensions(c, w, h)) goto done;
	img = de_bitmap_create(c, w, h, 3);

	if(d->interlaced) {
		dbuf *tmpf;

		tmpf = dbuf_create_membuf(c, h*src_rowspan, 0x1);
		for(j=0; j<h; j++) {
			// interlaced -- Odd rows appear offset by 8192 bytes
			dbuf_copy(c->infile, pos + ((j%2)*8192) +
				(j/2)*src_rowspan, src_rowspan, tmpf);
		}

		de_convert_image_paletted(tmpf, 0, 2, src_rowspan, palette, img, 0);
		dbuf_close(tmpf);
	}
	else {
		de_convert_image_paletted(c->infile, pos, 2, src_rowspan, palette, img, 0);
	}

	de_bitmap_write_to_file(img, NULL, 0);
done:
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r12
movq %rdi, %r14
cmpb $0x0, 0x2d(%rsi)
je 0x393fe
movq 0x440(%r12), %rax
movq 0x448(%r12), %r15
leaq 0x1(%rax), %rcx
shrq $0x3f, %rcx
leaq (%rax,%rcx), %r13
incq %r13
sarq %r13
movl $0xb, %ebx
jmp 0x39454
movb 0x2c(%r12), %bl
movl $0x140, %esi            # imm = 0x140
movq %r14, %rdi
callq 0x39b51
movq %rax, %r13
testb %bl, %bl
je 0x39421
movl $0xc8, %esi
movq %r14, %rdi
jmp 0x39447
leaq 0x3(%r13), %rax
movq %r13, %rcx
addq $0x6, %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
sarq $0x2, %rcx
movq 0x10(%r12), %rax
cqto
idivq %rcx
movq %r14, %rdi
movq %rax, %rsi
callq 0x39b9a
movq %rax, %r15
movl $0x7, %ebx
cmpl $0x0, 0x18(%r12)
je 0x39496
movzbl 0x1c(%r12), %esi
leaq 0x10(%rsp), %rbp
movl $0x4, %r8d
movl $0xa, %edi
xorl %edx, %edx
movq %rbp, %rcx
xorl %r9d, %r9d
callq 0xec55a
movzbl 0x1d(%r12), %edx
movl $0x14, %edi
xorl %esi, %esi
callq 0xec464
movl %eax, (%rbp)
jmp 0x394a2
movaps 0xdfa33(%rip), %xmm0    # 0x118ed0
movaps %xmm0, 0x10(%rsp)
leaq 0x3(%r13), %rax
leaq 0x6(%r13), %rbp
testq %rax, %rax
cmovnsq %rax, %rbp
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xe8f88
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xf1db1
testl %eax, %eax
je 0x395ad
sarq $0x2, %rbp
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
movl $0x3, %ecx
callq 0xf266a
movq %rax, %r13
cmpb $0x0, 0x2c(%r12)
je 0x395b2
movq %r13, 0x28(%rsp)
movq %rbp, %rsi
imulq %r15, %rsi
movq %r14, %rdi
movl $0x1, %edx
callq 0xf09c5
testq %r15, %r15
movq %r14, %rcx
movq %rbp, %r14
jle 0x3957a
movq %r15, %r12
movq %rbx, %rdx
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %rcx, 0x30(%rsp)
movq 0x18(%rcx), %rdi
movq %r13, %rcx
movl $0x2000, %esi           # imm = 0x2000
andq %rsi, %rcx
orq %rdx, %rcx
movq %rbx, %rsi
shrq %rsi
imulq %r14, %rsi
addq %rcx, %rsi
movq %rdx, %rbp
movq %r14, %rdx
movq %rax, %r15
movq %rax, %rcx
callq 0xef481
movq %rbp, %rdx
movq 0x30(%rsp), %rcx
movq %r15, %rax
incq %rbx
movl $0x2000, %esi           # imm = 0x2000
addq %rsi, %r13
cmpq %rbx, %r12
jne 0x3952f
movl $0x0, (%rsp)
leaq 0x10(%rsp), %r8
movl $0x2, %edx
movq %rax, %rdi
xorl %esi, %esi
movq %rax, %rbx
movq %r14, %rcx
movq 0x28(%rsp), %r13
movq %r13, %r9
callq 0xf2f79
movq %rbx, %rdi
callq 0xf052b
jmp 0x395d5
xorl %r13d, %r13d
jmp 0x395e1
movq 0x18(%r14), %rdi
movl $0x0, (%rsp)
leaq 0x10(%rsp), %r8
movl $0x2, %edx
movq %rbx, %rsi
movq %rbp, %rcx
movq %r13, %r9
callq 0xf2f79
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf22dc
movq %r13, %rdi
callq 0xf228d
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq