// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/pcf.c
// Function: handler_bitmaps
static void handler_bitmaps(deark *c, lctx *d, struct table_entry *te)
{
	i64 pos = te->offset;
	i64 nglyphs;
	i64 k;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "bitmap table at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	if(!read_and_check_format_field(c, d, te, pos)) goto done;
	pos += 4;
	d->bitmaps_fmt = te->fmt; // struct copy

	nglyphs = dbuf_getu32x(c->infile, pos, te->fmt.is_le);
	pos += 4;
	de_dbg(c, "glyph count: %d", (int)nglyphs);

	if(nglyphs != d->num_chars) {
		de_warn(c, "Expected %d bitmaps, found %d", (int)d->num_chars, (int)nglyphs);
	}
	if(nglyphs < d->num_chars) {
		d->num_chars = nglyphs;
	}

	for(k=0; k<d->num_chars; k++) {
		d->chars[k].bitmap_offset = (unsigned int)dbuf_getu32x(c->infile, pos, te->fmt.is_le);
		pos += 4;
		if(c->debug_level>=2)
			de_dbg2(c, "char[%d] glyph offset: %u", (int)k, d->chars[k].bitmap_offset);
	}

	for(k=0; k<4; k++) {
		i64 bitmapsize;
		int use_this_one;

		bitmapsize = dbuf_getu32x(c->infile, pos, te->fmt.is_le);
		pos += 4;
		use_this_one = (((unsigned int)k)==te->fmt.glyph_padding_code);
		de_dbg(c, "bitmapsize[if padding=%d]: %u%s", (int)k, (unsigned int)bitmapsize,
			use_this_one?" *":"");
		if(use_this_one) {
			d->bitmaps_data_len = bitmapsize;
		}
	}

	if(pos + d->bitmaps_data_len > te->offset+te->size) {
		// error
		d->bitmaps_data_len = 0;
		goto done;
	}

	if(te->fmt.is_le && te->fmt.scan_unit_value>1) {
		// TODO: Support this
		de_err(c, "Little-endian byte order is not supported");
		goto done;
	}

	de_dbg(c, "bitmaps data at %"I64_FMT", len=%"I64_FMT, pos, d->bitmaps_data_len);
	d->bitmaps_data = de_malloc(c, d->bitmaps_data_len);
	de_read(d->bitmaps_data, pos, d->bitmaps_data_len);
	if(!te->fmt.msbit_first) {
		reverse_bit_order(d->bitmaps_data, d->bitmaps_data_len);
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq 0x30(%rdx), %r12
leaq 0x14(%rsp), %rsi
callq 0xe8a1c
leaq 0x79a23(%rip), %rsi     # 0x139d64
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xc0832
testl %eax, %eax
je 0xc055b
leaq 0x4(%r12), %rsi
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x40(%r14)
movups %xmm0, 0x30(%r14)
movq 0x18(%rbx), %rdi
movl 0x10(%r15), %edx
callq 0xeeff3
movq %rax, %r13
leaq 0x799d8(%rip), %rsi     # 0x139d78
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x10(%r14), %rdx
cmpq %rdx, %r13
je 0xc03d7
leaq 0x799cb(%rip), %rsi     # 0x139d88
movq %rbx, %rdi
movl %r13d, %ecx
xorl %eax, %eax
callq 0xe8faa
movq 0x10(%r14), %rax
cmpq %rax, %r13
jge 0xc03da
movq %r13, 0x10(%r14)
movq %r13, %rax
movq %r14, 0x8(%rsp)
addq $0x8, %r12
testq %rax, %rax
jle 0xc043a
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x8(%rsp), %r13
movq 0x18(%rbx), %rdi
movl 0x10(%r15), %edx
movq %r12, %rsi
callq 0xeeff3
movq 0x18(%r13), %rcx
movl %eax, (%rcx,%r14)
cmpl $0x2, (%rbx)
jl 0xc0424
movq %rbx, %rdi
leaq 0x7998d(%rip), %rsi     # 0x139da6
movl %ebp, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe87e4
incq %rbp
addq $0x18, %r14
addq $0x4, %r12
movq 0x8(%rsp), %r13
cmpq 0x10(%r13), %rbp
jl 0xc03f2
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
movl 0x10(%r15), %edx
movq %r12, %rsi
callq 0xeeff3
movq %rax, %r14
movl 0x8(%r15), %r13d
cmpl %r13d, %ebp
leaq 0x84c53(%rip), %r8      # 0x1450b0
leaq 0x7997c(%rip), %rax     # 0x139de0
cmoveq %rax, %r8
movq %rbx, %rdi
leaq 0x7994e(%rip), %rsi     # 0x139dc0
movl %ebp, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0xe8617
cmpl %r13d, %ebp
jne 0xc048c
movq 0x8(%rsp), %rax
movq %r14, 0x20(%rax)
incq %rbp
addq $0x4, %r12
cmpq $0x4, %rbp
jne 0xc043c
movq 0x8(%rsp), %r14
movq 0x20(%r14), %rcx
leaq (%rcx,%r12), %rax
movq 0x28(%r15), %rdx
addq 0x30(%r15), %rdx
cmpq %rdx, %rax
jle 0xc04c0
movq $0x0, 0x20(%r14)
jmp 0xc055b
cmpl $0x0, 0x10(%r15)
je 0xc04e1
cmpl $0x2, 0x1c(%r15)
jb 0xc04e1
leaq 0x7990e(%rip), %rsi     # 0x139de3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0xc055b
leaq 0x79925(%rip), %rsi     # 0x139e0d
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
movq 0x20(%r14), %rcx
movq %rax, %rsi
movq %r12, %rdx
callq 0xee8d4
cmpl $0x0, 0x14(%r15)
jne 0xc055b
movq 0x20(%r14), %rax
testq %rax, %rax
jle 0xc055b
movq 0x28(%r14), %rcx
xorl %edx, %edx
leaq 0x7952b(%rip), %rsi     # 0x139a60
movzbl (%rcx,%rdx), %edi
movl %edi, %r8d
andl $0xf, %r8d
movb (%r8,%rsi), %r8b
shlb $0x4, %r8b
shrl $0x4, %edi
orb (%rdi,%rsi), %r8b
movb %r8b, (%rcx,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0xc0535
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq