// File: /workspace/github_c_makefile_star2_O1/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 0xc(%rsp), %rsi
callq 0xe958c
leaq 0x78af7(%rip), %rsi     # 0x1399c4
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xc13e2
testl %eax, %eax
je 0xc10f4
leaq 0x4(%r12), %rsi
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x40(%r14)
movups %xmm0, 0x30(%r14)
movq (%rsp), %rbx
movq 0x18(%rbx), %rdi
movl 0x10(%r15), %edx
callq 0xef614
movq %rax, %r13
leaq 0x78aa4(%rip), %rsi     # 0x1399d8
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x10(%r14), %rdx
cmpq %rdx, %r13
je 0xc0f5f
leaq 0x78a97(%rip), %rsi     # 0x1399e8
movq (%rsp), %rdi
movl %r13d, %ecx
xorl %eax, %eax
callq 0xe9b27
cmpq 0x10(%r14), %r13
jge 0xc0f69
movq %r13, 0x10(%r14)
addq $0x8, %r12
cmpq $0x0, 0x10(%r14)
jle 0xc0fc1
xorl %r13d, %r13d
xorl %ebp, %ebp
movq (%rsp), %rbx
movq 0x18(%rbx), %rdi
movl 0x10(%r15), %edx
movq %r12, %rsi
callq 0xef614
movq 0x18(%r14), %rcx
movl %eax, (%rcx,%r13)
cmpl $0x2, (%rbx)
jl 0xc0fb0
movq (%rsp), %rdi
leaq 0x78a61(%rip), %rsi     # 0x139a06
movl %ebp, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe93cf
incq %rbp
addq $0x18, %r13
addq $0x4, %r12
cmpq 0x10(%r14), %rbp
jl 0xc0f7d
xorl %ebp, %ebp
movq %r15, 0x10(%rsp)
movq (%rsp), %rbx
movq 0x18(%rbx), %rdi
movl 0x10(%r15), %edx
movq %r12, %rsi
callq 0xef614
movq %rax, %r13
movq %r15, %rax
movq %r14, %r15
movl 0x8(%rax), %r14d
cmpl %r14d, %ebp
leaq 0x83bdd(%rip), %r8      # 0x144bd0
leaq 0x78a46(%rip), %rax     # 0x139a40
cmoveq %rax, %r8
movq %rbx, %rdi
leaq 0x78a18(%rip), %rsi     # 0x139a20
movl %ebp, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0xe9226
cmpl %r14d, %ebp
movq %r15, %r14
jne 0xc1020
movq %r13, 0x20(%r14)
incq %rbp
addq $0x4, %r12
cmpq $0x4, %rbp
movq 0x10(%rsp), %r15
jne 0xc0fcc
movq 0x20(%r14), %rcx
leaq (%rcx,%r12), %rax
movq 0x28(%r15), %rdx
addq 0x30(%r15), %rdx
cmpq %rdx, %rax
jle 0xc1054
movq $0x0, 0x20(%r14)
jmp 0xc10f4
cmpl $0x0, 0x10(%r15)
je 0xc1076
cmpl $0x2, 0x1c(%r15)
jb 0xc1076
leaq 0x789da(%rip), %rsi     # 0x139a43
movq (%rsp), %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0xc10f4
leaq 0x789f0(%rip), %rsi     # 0x139a6d
movq (%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0xe969b
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
movq 0x20(%r14), %rcx
movq %rax, %rsi
movq %r12, %rdx
callq 0xeeee8
cmpl $0x0, 0x14(%r15)
jne 0xc10f4
movq 0x20(%r14), %rax
testq %rax, %rax
jle 0xc10f4
movq 0x28(%r14), %rcx
xorl %edx, %edx
leaq 0x78b72(%rip), %rsi     # 0x139c40
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 0xc10ce
movl 0xc(%rsp), %esi
movq (%rsp), %rdi
callq 0xe9595
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq