// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/awbm.c
// Function: do_v1_image
static int do_v1_image(deark *c, lctx *d, i64 pos,
	i64 w_blocks, i64 h_blocks, int special, unsigned int createflags)
{
	de_bitmap *img = NULL;
	i64 w, h;
	i64 i, j;
	i64 colors_start=0, bitmap_start;
	i64 fgpos;
	int retval = 0;
	de_color pal2[2];

	w = 8 * w_blocks;
	h = EPA_CH * h_blocks;
	if(!de_good_image_dimensions(c, w, h)) goto done;
	img = de_bitmap_create(c, w, h, special?4:3);

	if(special) {
		// The first block is not stored, and is assumed to be blank.
		// To compensate, pretend the image starts 1 block before it does.
		// (There's special code later to make the first block blank.)
		bitmap_start = pos - EPA_CH;
	}
	else {
		colors_start = pos;
		bitmap_start = colors_start + w_blocks*h_blocks;
		de_copy_std_palette(DE_PALID_PC16, 0, 0, d->pal, 16, 0);
	}

	if(special) {
		// Blue on black
		pal2[0] = DE_STOCKCOLOR_BLACK;
		pal2[1] = DE_MAKE_RGB(0x00,0x00,0xff);
	}

	// Read the bitmap, "character by character"
	for(j=0; j<h_blocks; j++) {
		for(i=0; i<w_blocks; i++) {
			i64 i2, j2;

			if(!special) { // Read the color attributes for this block
				u8 attr;

				attr = de_getbyte(colors_start + j*w_blocks + i);
				pal2[0] = d->pal[(UI)((attr&0xf0)>>4)];
				pal2[1] = d->pal[(UI)(attr&0x0f)];
			}

			fgpos = bitmap_start + j*w_blocks*EPA_CH + i*EPA_CH;

			for(j2=0; j2<EPA_CH; j2++) { // For each 8-pixel row of a block...
				u8 n;

				n = de_getbyte_p(&fgpos);

				for(i2=0; i2<8; i2++) { // For each pixel in a row...
					u8 b;

					b = (n>>(7-i2)) & 0x01;
					de_bitmap_setpixel_rgba(img, i*8+i2, j*EPA_CH+j2, pal2[(UI)b]);
				}
			}
		}
	}

	if(special) {
		de_bitmap_rect(img, 0, 0, 8, EPA_CH, DE_STOCKCOLOR_TRANSPARENT, 0);
	}

	de_bitmap_write_to_file(img, NULL, createflags);
	retval = 1;
done:
	de_bitmap_destroy(img);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r9d, %r15d
movq %r8, %r14
movq %rdx, 0x10(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, %r12
movq %rcx, 0x18(%rsp)
leaq (,%rcx,8), %rbx
movq %r8, 0x20(%rsp)
shlq $0x4, %r14
subq %r8, %r14
subq %r8, %r14
movq %rbx, %rsi
movq %r14, %rdx
callq 0xf241d
testl %eax, %eax
je 0x245c6
xorl %ecx, %ecx
testl %r15d, %r15d
setne %cl
addl $0x3, %ecx
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0xf2cab
movq %rax, %rbx
testl %r15d, %r15d
je 0x245d0
movq 0x10(%rsp), %rax
addq $-0xe, %rax
movq %rax, 0x48(%rsp)
movq $0x0, 0x10(%rsp)
jmp 0x24605
xorl %r14d, %r14d
xorl %ebx, %ebx
jmp 0x247b3
movq 0x20(%rsp), %rax
imulq 0x18(%rsp), %rax
addq 0x10(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x40(%rsp), %rax
leaq 0x14(%rax), %rcx
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xecbbc
testl %r15d, %r15d
je 0x24619
movabsq $-0xffff0001000000, %rax # imm = 0xFF0000FFFF000000
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x20(%rsp)
jle 0x24774
movq $0x0, 0x28(%rsp)
movl %r15d, 0x34(%rsp)
movq %r12, 0x70(%rsp)
cmpq $0x0, 0x18(%rsp)
jle 0x2475c
movq 0x28(%rsp), %rcx
movq %rcx, %rdx
imulq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rax
movq %rdx, 0x60(%rsp)
addq %rdx, %rax
movq %rax, 0x58(%rsp)
leaq (%rcx,%rcx), %rax
shlq $0x4, %rcx
subq %rax, %rcx
movq %rcx, 0x78(%rsp)
movq $0x0, 0x50(%rsp)
xorl %r14d, %r14d
testl %r15d, %r15d
jne 0x246b8
movq 0x18(%r12), %rdi
movq 0x58(%rsp), %rax
leaq (%rax,%r14), %rsi
callq 0xef0d5
movzbl %al, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movq 0x40(%rsp), %rdx
movl 0x14(%rdx,%rcx,4), %ecx
movl %ecx, 0x38(%rsp)
andl $0xf, %eax
movl 0x14(%rdx,%rax,4), %eax
movl %eax, 0x3c(%rsp)
movq 0x60(%rsp), %rax
movq %r14, 0x68(%rsp)
addq %r14, %rax
leaq (%rax,%rax), %rcx
shlq $0x4, %rax
subq %rcx, %rax
addq 0x48(%rsp), %rax
movq %rax, 0x80(%rsp)
xorl %r13d, %r13d
movq 0x18(%r12), %rdi
leaq 0x80(%rsp), %rsi
callq 0xef14d
movq 0x78(%rsp), %rcx
leaq (%rcx,%r13), %r12
movl $0x8, %r15d
movzbl %al, %r14d
movq 0x50(%rsp), %rbp
decq %r15
xorl %eax, %eax
btl %r15d, %r14d
setb %al
movl 0x38(%rsp,%rax,4), %ecx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xf2b02
incq %rbp
testq %r15, %r15
jne 0x2470a
incq %r13
cmpq $0xe, %r13
movq 0x70(%rsp), %r12
jne 0x246e0
movq 0x68(%rsp), %r14
incq %r14
addq $0x8, 0x50(%rsp)
cmpq 0x18(%rsp), %r14
movl 0x34(%rsp), %r15d
jne 0x24680
movq 0x28(%rsp), %rcx
incq %rcx
movq %rcx, 0x28(%rsp)
cmpq 0x20(%rsp), %rcx
jne 0x24638
movl 0xc0(%rsp), %ebp
testl %r15d, %r15d
je 0x247a1
movl $0x0, (%rsp)
movl $0x8, %ecx
movl $0xe, %r8d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xf3cb3
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0xf2923
movl $0x1, %r14d
movq %rbx, %rdi
callq 0xf28d4
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq