// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x7, -0x48(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2d(%rax)
je 0x4c2d7
movq -0x10(%rbp), %rax
movq 0x440(%rax), %rax
addq $0x1, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x448(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x48(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x48(%rbp)
jmp 0x4c364
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2c(%rax)
jne 0x4c336
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x140, %edx            # imm = 0x140
callq 0x4cc30
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x3, %rax
movl $0x4, %ecx
cqto
idivq %rcx
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
cqto
idivq -0x50(%rbp)
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x4cca0
movq %rax, -0x30(%rbp)
jmp 0x4c362
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x140, %edx            # imm = 0x140
callq 0x4cc30
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0xc8, %edx
callq 0x4cca0
movq %rax, -0x30(%rbp)
jmp 0x4c364
movq -0x10(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0x4c3aa
movq -0x10(%rbp), %rax
movzbl 0x1c(%rax), %esi
leaq -0x20(%rbp), %rcx
movl $0xa, %edi
xorl %eax, %eax
movl %eax, %edx
movl $0x4, %r8d
xorl %r9d, %r9d
callq 0x161320
movq -0x10(%rbp), %rax
movzbl 0x1d(%rax), %edx
movl $0x14, %edi
xorl %esi, %esi
callq 0x161150
movl %eax, -0x20(%rbp)
jmp 0x4c3df
movq $0x0, -0x38(%rbp)
cmpq $0x4, -0x38(%rbp)
jge 0x4c3dd
movq -0x38(%rbp), %rcx
leaq 0x1587ac(%rip), %rax    # 0x1a4b70
movl (%rax,%rcx,4), %ecx
movq -0x38(%rbp), %rax
movl %ecx, -0x20(%rbp,%rax,4)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x4c3b2
jmp 0x4c3df
movq -0x28(%rbp), %rax
addq $0x3, %rax
movl $0x4, %ecx
cqto
idivq %rcx
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x4c421
jmp 0x4c534
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2c(%rax)
je 0x4c4fa
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
imulq -0x50(%rbp), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x60(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x4c4c8
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
shlq $0xd, %rdx
addq %rdx, %rsi
movq -0x40(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
imulq -0x50(%rbp), %rax
addq %rax, %rsi
movq -0x50(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x166070
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x4c46c
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rcx
leaq -0x20(%rbp), %r8
movq -0x58(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x60(%rbp), %rdi
callq 0x1676a0
jmp 0x4c525
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rcx
leaq -0x20(%rbp), %r8
movq -0x58(%rbp), %r9
movl $0x2, %edx
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x58(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x16afd0
movq -0x58(%rbp), %rdi
callq 0x16af60
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)