// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq %rdx, 0x10(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %rbp
movq %rcx, 0x18(%rsp)
leaq (,%rcx,8), %rbx
movq %r8, %r14
shlq $0x4, %r14
subq %r8, %r14
subq %r8, %r14
movq %rbx, %rsi
movq %r14, %rdx
callq 0xf1db1
testl %eax, %eax
je 0x2458d
xorl %ecx, %ecx
testl %r15d, %r15d
setne %cl
addl $0x3, %ecx
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0xf266a
movq %rax, %rbx
testl %r15d, %r15d
je 0x24597
movq 0x10(%rsp), %rcx
addq $-0xe, %rcx
movabsq $-0xffff0001000000, %rax # imm = 0xFF0000FFFF000000
movq %rax, 0x30(%rsp)
movq %rcx, 0x40(%rsp)
movq $0x0, 0x10(%rsp)
jmp 0x245ca
xorl %r14d, %r14d
xorl %ebx, %ebx
jmp 0x24767
movq %r12, %rax
imulq 0x18(%rsp), %rax
addq 0x10(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x38(%rsp), %rax
leaq 0x14(%rax), %rcx
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xec55a
testq %r12, %r12
jle 0x24728
movq $0x0, 0x20(%rsp)
movl %r15d, 0x2c(%rsp)
movq %r12, 0x50(%rsp)
movq %rbp, 0x70(%rsp)
cmpq $0x0, 0x18(%rsp)
jle 0x2470d
movq 0x20(%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, 0x48(%rsp)
xorl %r14d, %r14d
testl %r15d, %r15d
jne 0x2466a
movq 0x18(%rbp), %rdi
movq 0x58(%rsp), %rax
leaq (%rax,%r14), %rsi
callq 0xeead3
movzbl %al, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movq 0x38(%rsp), %rdx
movl 0x14(%rdx,%rcx,4), %ecx
movl %ecx, 0x30(%rsp)
andl $0xf, %eax
movl 0x14(%rdx,%rax,4), %eax
movl %eax, 0x34(%rsp)
movq 0x60(%rsp), %rax
movq %r14, 0x68(%rsp)
addq %r14, %rax
leaq (%rax,%rax), %rcx
shlq $0x4, %rax
subq %rcx, %rax
addq 0x40(%rsp), %rax
movq %rax, 0x80(%rsp)
xorl %r13d, %r13d
movq 0x18(%rbp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xeeb4b
movq 0x78(%rsp), %rcx
leaq (%rcx,%r13), %r12
movl $0x8, %r15d
movzbl %al, %r14d
movq 0x48(%rsp), %rbp
decq %r15
xorl %eax, %eax
btl %r15d, %r14d
setb %al
movl 0x30(%rsp,%rax,4), %ecx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xf24bf
incq %rbp
testq %r15, %r15
jne 0x246bb
incq %r13
cmpq $0xe, %r13
movq 0x70(%rsp), %rbp
jne 0x24692
movq 0x68(%rsp), %r14
incq %r14
addq $0x8, 0x48(%rsp)
cmpq 0x18(%rsp), %r14
movl 0x2c(%rsp), %r15d
jne 0x24633
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x50(%rsp), %r12
movq %rcx, 0x20(%rsp)
cmpq %r12, %rcx
jne 0x245eb
movl 0xc0(%rsp), %ebp
testl %r15d, %r15d
je 0x24755
movl $0x0, (%rsp)
movl $0x8, %ecx
movl $0xe, %r8d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xf3654
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0xf22dc
movl $0x1, %r14d
movq %rbx, %rdi
callq 0xf228d
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq