// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/pcf.c
// Function: handler_bdf_encodings
static void handler_bdf_encodings(deark *c, lctx *d, struct table_entry *te)
{
	i64 pos = te->offset;
	int saved_indent_level;
	unsigned int min_char_or_byte2, max_char_or_byte2;
	unsigned int min_byte1, max_byte1;
	unsigned int default_char;
	unsigned int byte1_count, byte2_count;
	int is_singlebyte_encoding;
	i64 ncodepoints;
	i64 k;

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

	if(!read_and_check_format_field(c, d, te, pos)) goto done;
	pos += 4;
	if(te->fmt.gross_format != GFMT_DEFAULT) goto done;

	min_char_or_byte2 = (unsigned int)dbuf_getu16x(c->infile, pos, te->fmt.is_le);
	de_dbg(c, "min_char_or_byte2: %u", min_char_or_byte2);
	pos += 2;
	max_char_or_byte2 = (unsigned int)dbuf_getu16x(c->infile, pos, te->fmt.is_le);
	de_dbg(c, "max_char_or_byte2: %u", max_char_or_byte2);
	pos += 2;
	min_byte1 = (unsigned int)dbuf_getu16x(c->infile, pos, te->fmt.is_le);
	de_dbg(c, "min_byte1: %u", min_byte1);
	pos += 2;
	max_byte1 = (unsigned int)dbuf_getu16x(c->infile, pos, te->fmt.is_le);
	de_dbg(c, "max_byte1: %u", max_byte1);
	pos += 2;
	default_char = (unsigned int)dbuf_getu16x(c->infile, pos, te->fmt.is_le);
	de_dbg(c, "default_char: %u", default_char);
	pos += 2;

	if(min_char_or_byte2>max_char_or_byte2) goto done;
	if(min_byte1>max_byte1) goto done;

	is_singlebyte_encoding = (min_byte1==0 && max_byte1==0);
	de_dbg(c, "encoding type: %s", is_singlebyte_encoding?"single byte":"double byte");

	byte1_count = max_byte1-min_byte1+1;
	byte2_count = max_char_or_byte2-min_char_or_byte2+1;
	ncodepoints = (i64)byte1_count * (i64)byte2_count;
	de_dbg(c, "number of codepoints in table: %"I64_FMT, ncodepoints);

	d->has_encodings_table = 1;

	for(k=0; k<ncodepoints; k++) {
		unsigned int glyph_number;
		i32 codepoint;

		if(pos+2 > te->offset+te->size) break;
		glyph_number = (unsigned int)dbuf_getu16x(c->infile, pos, te->fmt.is_le);

		if(is_singlebyte_encoding) {
			codepoint = (i32)k + (i32)min_char_or_byte2;
		}
		else {
			unsigned int tmp_hi, tmp_lo;
			tmp_hi = ((unsigned int)k)/byte2_count;
			tmp_lo = ((unsigned int)k)%byte2_count;
			codepoint = (i32)(((min_char_or_byte2+tmp_hi)<<8) |
				(min_byte1+tmp_lo));
		}

		if(glyph_number!=0xffff && glyph_number<d->num_chars) {
			d->chars[glyph_number].codepoint = codepoint;
		}

		if(c->debug_level>=2) {
			char gstr[40];
			if(glyph_number==0xffff) {
				de_strlcpy(gstr, "no char", sizeof(gstr));
			}
			else {
				de_snprintf(gstr, sizeof(gstr), "char[%u]", glyph_number);
			}
			de_dbg2(c, "[%d]: codepoint %d = %s", (int)k, (int)codepoint, gstr);
		}

		pos += 2;
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %rbp
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
movq 0x30(%rdx), %r13
leaq 0x14(%rsp), %rsi
callq 0xe958c
leaq 0x7894c(%rip), %rsi     # 0x139a8a
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0xc13e2
testl %eax, %eax
je 0xc1170
cmpl $0x0, 0x4(%rbp)
je 0xc118b
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4(%r13), %rsi
movq 0x18(%rbx), %rdi
movl 0x10(%rbp), %edx
callq 0xef570
movq %rax, %r14
leaq 0x78900(%rip), %rsi     # 0x139aa5
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0x6(%r13), %rsi
movq 0x18(%rbx), %rdi
movl 0x10(%rbp), %edx
callq 0xef570
movq %rax, %r15
leaq 0x788ef(%rip), %rsi     # 0x139abb
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0x8(%r13), %rsi
movq 0x18(%rbx), %rdi
movl 0x10(%rbp), %edx
callq 0xef570
leaq 0x788e1(%rip), %rsi     # 0x139ad1
movq %rbx, %rdi
movq %rax, (%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0xa(%r13), %rsi
movq 0x18(%rbx), %rdi
movl 0x10(%rbp), %edx
callq 0xef570
leaq 0x788c8(%rip), %rsi     # 0x139adf
movq %rbx, %rdi
movq %rax, %r12
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0xc(%r13), %rsi
movq 0x18(%rbx), %rdi
movl 0x10(%rbp), %edx
callq 0xef570
leaq 0x788b0(%rip), %rsi     # 0x139aed
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %r14, 0x18(%rsp)
subl %r14d, %r15d
jb 0xc1170
cmpl %r12d, (%rsp)
ja 0xc1170
movq %r12, %rax
movq %r12, %r14
movq (%rsp), %r12
orl %r12d, %eax
movq %rax, 0x30(%rsp)
leaq 0x78896(%rip), %rax     # 0x139b10
leaq 0x7889b(%rip), %rdx     # 0x139b1c
cmoveq %rax, %rdx
leaq 0x78872(%rip), %rsi     # 0x139afe
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
subl %r12d, %r14d
incl %r14d
incl %r15d
movq %r15, 0x28(%rsp)
imulq %r14, %r15
leaq 0x78879(%rip), %rsi     # 0x139b28
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x8(%rsp), %rax
movb $0x1, 0x50(%rax)
movq %r15, 0x20(%rsp)
testq %r15, %r15
je 0xc1170
addq $0xe, %r13
decq 0x20(%rsp)
xorl %r12d, %r12d
movq %rbp, 0x38(%rsp)
leaq 0x2(%r13), %r15
movq 0x28(%rbp), %r14
addq 0x30(%rbp), %r14
cmpq %r14, %r15
jg 0xc13bc
movq 0x18(%rbx), %rdi
movl 0x10(%rbp), %edx
movq %r13, %rsi
callq 0xef570
movq %rax, %rcx
cmpl $0x0, 0x30(%rsp)
je 0xc132b
movl %r12d, %eax
xorl %edx, %edx
divl 0x28(%rsp)
movl %edx, %ebp
addl 0x18(%rsp), %eax
shll $0x8, %eax
addl (%rsp), %ebp
orl %eax, %ebp
jmp 0xc1334
movq 0x18(%rsp), %rax
leal (%rax,%r12), %ebp
cmpl $0xffff, %ecx           # imm = 0xFFFF
je 0xc135a
movl %ecx, %eax
movq 0x8(%rsp), %rdx
cmpq 0x10(%rdx), %rax
jge 0xc135a
movq 0x8(%rsp), %rdx
movq 0x18(%rdx), %rdx
leaq (%rax,%rax,2), %rax
movl %ebp, 0x4(%rdx,%rax,8)
cmpl $0x2, (%rbx)
movq %r15, %r13
jl 0xc13bc
cmpl $0xffff, %ecx           # imm = 0xFFFF
jne 0xc1385
movl $0x28, %edx
leaq 0x40(%rsp), %r13
movq %r13, %rdi
leaq 0x787cd(%rip), %rsi     # 0x139b4b
callq 0xe8ded
jmp 0xc13a0
movl $0x28, %esi
leaq 0x40(%rsp), %r13
movq %r13, %rdi
leaq 0x787ba(%rip), %rdx     # 0x139b53
xorl %eax, %eax
callq 0xe8d67
movq %rbx, %rdi
leaq 0x787b2(%rip), %rsi     # 0x139b5c
movl %r12d, %edx
movl %ebp, %ecx
movq %r13, %r8
xorl %eax, %eax
callq 0xe93cf
movq %r15, %r13
cmpq %r14, %r15
movq 0x38(%rsp), %rbp
jg 0xc1170
leaq 0x1(%r12), %rax
cmpq %r12, 0x20(%rsp)
movq %rax, %r12
jne 0xc12e4
jmp 0xc1170