// File: /workspace/github_c_makefile_star2_O2/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, %ebp
movq %r8, %r15
movq %rcx, %r12
movq %rdx, 0x8(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
leaq (,%rcx,8), %rbx
imulq $0xe, %r8, %r14
movq %rbx, %rsi
movq %r14, %rdx
callq 0xe6be1
testl %eax, %eax
je 0x23d71
xorl %ecx, %ecx
testl %ebp, %ebp
setne %cl
addl $0x3, %ecx
movq %r13, 0x30(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0xe7437
movq %rax, %rbx
testl %ebp, %ebp
je 0x23d7b
movq 0x8(%rsp), %rcx
addq $-0xe, %rcx
movabsq $-0xffff0001000000, %rax # imm = 0xFF0000FFFF000000
movq %rax, 0x18(%rsp)
movq %rcx, 0x28(%rsp)
movq $0x0, 0x8(%rsp)
jmp 0x23da8
xorl %r14d, %r14d
xorl %ebx, %ebx
jmp 0x23f32
movq %r15, %rax
imulq %r12, %rax
addq 0x8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x20(%rsp), %rax
leaq 0x14(%rax), %rcx
pushq $0x14
popq %rdi
pushq $0x10
popq %r8
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xe1b11
xorl %ecx, %ecx
testq %r12, %r12
movl $0x0, %eax
cmovgq %r12, %rax
movq %rax, 0x68(%rsp)
testq %r15, %r15
cmovleq %rcx, %r15
movq 0x30(%rsp), %r14
movl %ebp, 0x14(%rsp)
movq %r15, 0x50(%rsp)
movq %r12, 0x48(%rsp)
cmpq %r15, %rcx
je 0x23eff
movq %rcx, %rdx
imulq %r12, %rdx
movq 0x8(%rsp), %rax
movq %rdx, 0x58(%rsp)
addq %rdx, %rax
movq %rax, 0x40(%rsp)
imulq $0xe, %rcx, %rax
movq %rax, 0x78(%rsp)
movq $0x0, 0x38(%rsp)
xorl %r13d, %r13d
movq %rcx, 0x60(%rsp)
cmpq 0x68(%rsp), %r13
je 0x23ef7
testl %ebp, %ebp
jne 0x23e52
movq 0x18(%r14), %rdi
movq 0x40(%rsp), %rax
leaq (%rax,%r13), %rsi
callq 0xe3eb4
movzbl %al, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movq 0x20(%rsp), %rdx
movl 0x14(%rdx,%rcx,4), %ecx
movl %ecx, 0x18(%rsp)
andl $0xf, %eax
movl 0x14(%rdx,%rax,4), %eax
movl %eax, 0x1c(%rsp)
movq 0x58(%rsp), %rax
movq %r13, 0x70(%rsp)
addq %r13, %rax
imulq $0xe, %rax, %rax
addq 0x28(%rsp), %rax
movq %rax, 0x80(%rsp)
xorl %r13d, %r13d
cmpq $0xe, %r13
je 0x23ed1
movq 0x18(%r14), %rdi
leaq 0x80(%rsp), %rsi
callq 0xe3f2a
movq 0x78(%rsp), %rcx
leaq (%rcx,%r13), %r14
movzbl %al, %r15d
movq 0x38(%rsp), %r12
pushq $0x8
popq %rbp
testq %rbp, %rbp
je 0x23ec7
decq %rbp
xorl %eax, %eax
btl %ebp, %r15d
setb %al
movl 0x18(%rsp,%rax,4), %ecx
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xe728c
incq %r12
jmp 0x23e9f
incq %r13
movq 0x30(%rsp), %r14
jmp 0x23e73
movq 0x70(%rsp), %r13
incq %r13
addq $0x8, 0x38(%rsp)
movl 0x14(%rsp), %ebp
movq 0x50(%rsp), %r15
movq 0x48(%rsp), %r12
movq 0x60(%rsp), %rcx
jmp 0x23e11
incq %rcx
jmp 0x23dd5
testl %ebp, %ebp
je 0x23f1d
andl $0x0, (%rsp)
pushq $0x8
popq %rcx
pushq $0xe
popq %r8
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xe8292
movq %rbx, %rdi
xorl %esi, %esi
movl 0xc0(%rsp), %edx
callq 0xe70b7
pushq $0x1
popq %r14
movq %rbx, %rdi
callq 0xe7084
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop