// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x74(%rbp)
movq -0x20(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x40(%rbp)
imulq $0xe, -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x2c87d
jmp 0x2cae3
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl -0x2c(%rbp), %r8d
movl $0x3, %ecx
movl $0x4, %eax
cmpl $0x0, %r8d
cmovnel %eax, %ecx
callq 0x16b750
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x2c8bb
movq -0x18(%rbp), %rax
subq $0xe, %rax
movq %rax, -0x68(%rbp)
jmp 0x2c8f9
movq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
imulq -0x28(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rcx
addq $0x14, %rcx
movl $0x14, %edi
xorl %r9d, %r9d
xorl %eax, %eax
movl %eax, %edx
movl $0x10, %r8d
movl %r9d, %esi
callq 0x161320
cmpl $0x0, -0x2c(%rbp)
je 0x2c90d
movl $0xff000000, -0x7c(%rbp) # imm = 0xFF000000
movl $0xff0000ff, -0x78(%rbp) # imm = 0xFF0000FF
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x2caa1
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x2ca8e
cmpl $0x0, -0x2c(%rbp)
jne 0x2c99a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rax
imulq -0x20(%rbp), %rax
addq %rax, %rsi
addq -0x50(%rbp), %rsi
callq 0x165060
movb %al, -0x91(%rbp)
movq -0x10(%rbp), %rax
movzbl -0x91(%rbp), %ecx
andl $0xf0, %ecx
sarl $0x4, %ecx
movl %ecx, %ecx
movl 0x14(%rax,%rcx,4), %eax
movl %eax, -0x7c(%rbp)
movq -0x10(%rbp), %rax
movzbl -0x91(%rbp), %ecx
andl $0xf, %ecx
movl %ecx, %ecx
movl 0x14(%rax,%rcx,4), %eax
movl %eax, -0x78(%rbp)
movq -0x68(%rbp), %rax
movq -0x58(%rbp), %rcx
imulq -0x20(%rbp), %rcx
imulq $0xe, %rcx, %rcx
addq %rcx, %rax
imulq $0xe, -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq $0x0, -0x90(%rbp)
cmpq $0xe, -0x90(%rbp)
jge 0x2ca7b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x70(%rbp), %rsi
callq 0x165170
movb %al, -0x92(%rbp)
movq $0x0, -0x88(%rbp)
cmpq $0x8, -0x88(%rbp)
jge 0x2ca62
movzbl -0x92(%rbp), %eax
movl $0x7, %ecx
subq -0x88(%rbp), %rcx
sarl %cl, %eax
andl $0x1, %eax
movb %al, -0x93(%rbp)
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
shlq $0x3, %rsi
addq -0x88(%rbp), %rsi
imulq $0xe, -0x58(%rbp), %rdx
addq -0x90(%rbp), %rdx
movzbl -0x93(%rbp), %eax
movl %eax, %eax
movl -0x7c(%rbp,%rax,4), %ecx
callq 0x16b350
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x2c9f5
jmp 0x2ca64
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x2c9c5
jmp 0x2ca7d
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2c92b
jmp 0x2ca90
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2c915
cmpl $0x0, -0x2c(%rbp)
je 0x2cacc
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movl $0x8, %ecx
movl $0xe, %r8d
xorl %r9d, %r9d
movq %rdx, %rsi
movl $0x0, (%rsp)
callq 0x16cdc0
movq -0x38(%rbp), %rdi
movl 0x10(%rbp), %edx
xorl %eax, %eax
movl %eax, %esi
callq 0x16afd0
movl $0x1, -0x74(%rbp)
movq -0x38(%rbp), %rdi
callq 0x16af60
movl -0x74(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)