// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/printptnr.c
// Function: do_image_cmpr3
static void do_image_cmpr3(deark *c, lctx *d, struct page_ctx *pg, i64 pos1,
	i64 *bytes_consumed)
{
	de_bitmap *img = NULL;
	i64 pos = pos1;
	i64 nibble_count;
	i64 nibble_idx;
	i64 pixel_idx;
	u8 b;

	img = de_bitmap_create(c, pg->width, pg->height, 1);

	// Start with an all-white image:
	de_bitmap_rect(img, 0, 0, pg->width, pg->height, DE_STOCKCOLOR_WHITE, 0);

	nibble_count = de_getu16le(pos);
	de_dbg(c, "cmpr data len: %d nibbles", (int)nibble_count);
	pos += 2;

	*bytes_consumed = 2 + (nibble_count+1)/2;

	b = 0;
	pixel_idx = 0;
	for(nibble_idx=0; nibble_idx<nibble_count; nibble_idx++) {
		i64 count;
		int isblack;
		u8 nibble_val;
		i64 k;

		if((nibble_idx&0x1) == 0) {
			b = de_getbyte(pos++);
			nibble_val = b>>4;
		}
		else {
			nibble_val = b&0x0f;
		}

		count = (i64)(nibble_val&0x7);
		isblack = (nibble_val>=8);

		for(k=0; k<count; k++) {
			if(isblack)
				de_bitmap_setpixel_gray(img, pixel_idx%pg->width, pixel_idx/pg->width, 0x00);
			pixel_idx++;
		}
	}

	do_write_image_frombitmap(c, d, pg, img);
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
movl $0x1, %ecx
callq 0x16b750
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %r8
xorl %eax, %eax
movl %eax, %edx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %eax, %eax
movq %rdx, %rsi
movl $0x0, (%rsp)
callq 0x16cdc0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x165280
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
leaq 0xb36b2(%rip), %rsi     # 0x1c39c6
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
addq $0x2, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movb $0x0, -0x51(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x11043b
movq -0x48(%rbp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
jne 0x1103a2
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
callq 0x165060
movb %al, -0x51(%rbp)
movzbl -0x51(%rbp), %eax
sarl $0x4, %eax
movb %al, -0x65(%rbp)
jmp 0x1103ac
movzbl -0x51(%rbp), %eax
andl $0xf, %eax
movb %al, -0x65(%rbp)
movzbl -0x65(%rbp), %eax
andl $0x7, %eax
cltq
movq %rax, -0x60(%rbp)
movzbl -0x65(%rbp), %eax
cmpl $0x8, %eax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x64(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0x110428
cmpl $0x0, -0x64(%rbp)
je 0x11040e
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
cqto
idivq (%rcx)
movq %rdx, %rsi
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
cqto
idivq (%rcx)
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x16b1c0
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x1103d3
jmp 0x11042a
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x11035b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x110500
movq -0x30(%rbp), %rdi
callq 0x16af60
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)