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

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

	if(!read_and_check_format_field(c, d, te, pos)) goto done;
	pos += 4;

	nmetrics = dbuf_getu16x(c->infile, pos, te->fmt.is_le);
	pos += 2;
	de_dbg(c, "number of metrics: %d", (int)nmetrics);

	d->num_chars = nmetrics;

	// Allocate chars array, and set defaults
	d->chars = de_mallocarray(c, d->num_chars, sizeof(struct char_info));
	for(k=0; k<d->num_chars; k++) {
		d->chars[k].codepoint = DE_CODEPOINT_INVALID;
	}

	for(k=0; k<d->num_chars; k++) {
		int leftsb, rightsb;
		int char_width;
		int char_desc;
		unsigned int char_attr;
		struct char_info *ci;

		de_dbg2(c, "char[%d]", (int)k);
		de_dbg_indent(c, 1);
		ci = &d->chars[k];

		if(te->fmt.gross_format==GFMT_COMPRESSED_METRICS) {
			leftsb = (int)de_getbyte_p(&pos) - 0x80;
			rightsb = (int)de_getbyte_p(&pos) - 0x80;
			char_width = (int)de_getbyte_p(&pos) - 0x80;
			ci->ascent = (int)de_getbyte_p(&pos) - 0x80;
			char_desc = (int)de_getbyte_p(&pos) - 0x80;
			char_attr = 0;
		}
		else {
			leftsb = (int)dbuf_geti16x(c->infile, pos, te->fmt.is_le); pos += 2;
			rightsb = (int)dbuf_geti16x(c->infile, pos, te->fmt.is_le); pos += 2;
			char_width = (int)dbuf_geti16x(c->infile, pos, te->fmt.is_le); pos += 2;
			ci->ascent = (int)dbuf_geti16x(c->infile, pos, te->fmt.is_le); pos += 2;
			char_desc = (int)dbuf_geti16x(c->infile, pos, te->fmt.is_le); pos += 2;
			char_attr = (int)dbuf_getu16x(c->infile, pos, te->fmt.is_le); pos += 2;
		}

		if(c->debug_level>=2) {
			de_dbg2(c, "bearing (l, r): %d, %d", leftsb, rightsb);
			de_dbg2(c, "width: %d", char_width);
			de_dbg2(c, "ascent, descent: %d, %d", ci->ascent, char_desc);
			de_dbg2(c, "attributes: %u", char_attr);
		}
		ci->width_raw = rightsb - leftsb;
		ci->height_raw = ci->ascent + char_desc;
		if(c->debug_level>=2) {
			de_dbg(c, "raw bitmap dimensions: %d"DE_CHAR_TIMES"%d",
				ci->width_raw, ci->height_raw);
		}

		// TODO: Are these calculations correct?
		ci->extraspace_l = (i16)leftsb;
		if(ci->extraspace_l<0) ci->extraspace_l=0;
		ci->extraspace_r = (i16)(char_width - ci->width_raw - (int)ci->extraspace_l);
		if(ci->extraspace_r<0) ci->extraspace_r=0;

		de_dbg_indent(c, -1);
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %r13
movq 0x30(%rdx), %r14
movq %r14, (%rsp)
leaq 0x14(%rsp), %rsi
callq 0xe8a1c
leaq 0x79de2(%rip), %rsi     # 0x139cd4
movq %r13, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r13, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xc0832
testl %eax, %eax
je 0xc02fa
leaq 0x4(%r14), %rsi
movq 0x18(%r13), %rdi
movl 0x10(%r12), %edx
callq 0xeef43
movq %rax, %r15
addq $0x6, %r14
movq %r14, (%rsp)
leaq 0x79da3(%rip), %rsi     # 0x139ce9
movq %r13, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, 0x10(%rbp)
movl $0x18, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0xe96f7
movq %rax, 0x18(%rbp)
movq 0x10(%rbp), %rcx
testq %rcx, %rcx
jle 0xc02fa
leaq 0x3(%rcx), %rdx
andq $-0x4, %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
addq $0x4c, %rax
movdqa 0x72616(%rip), %xmm1    # 0x1325b0
movdqa 0x7072e(%rip), %xmm2    # 0x1306d0
movdqa 0x70736(%rip), %xmm3    # 0x1306e0
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x6a846(%rip), %xmm5    # 0x12a800
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %esi
notl %esi
testb $0x1, %sil
je 0xbfff6
movl $0xfffffff, -0x48(%rax) # imm = 0xFFFFFFF
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0xc000c
movl $0xfffffff, -0x30(%rax) # imm = 0xFFFFFFF
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %esi
testb $0x1, %sil
je 0xc004b
movl $0xfffffff, -0x18(%rax) # imm = 0xFFFFFFF
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0xc005c
movl $0xfffffff, (%rax)      # imm = 0xFFFFFFF
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x60, %rax
addq $-0x4, %rdx
jne 0xbffba
testq %rcx, %rcx
jle 0xc02fa
movl $0x8, %ebx
xorl %edx, %edx
movq %r13, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq %rbp, 0x28(%rsp)
movq %r13, %rdi
leaq 0x79c64(%rip), %rsi     # 0x139cff
movq %rdx, 0x40(%rsp)
xorl %eax, %eax
callq 0xe87e4
movq %r13, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbp), %r14
cmpl $0x100, 0x4(%r12)       # imm = 0x100
movq 0x18(%r13), %rdi
movq %r14, 0x18(%rsp)
jne 0xc013e
movq %r14, %rbp
movq %rsp, %r14
movq %r14, %rsi
callq 0xeeb4b
movzbl %al, %r12d
addl $-0x80, %r12d
movq 0x18(%r13), %rdi
movq %r14, %rsi
callq 0xeeb4b
movzbl %al, %eax
addl $-0x80, %eax
movq %rax, 0x8(%rsp)
movq 0x18(%r13), %rdi
movq %r14, %rsi
callq 0xeeb4b
movzbl %al, %r15d
addl $-0x80, %r15d
movq 0x18(%r13), %rdi
movq %r14, %rsi
callq 0xeeb4b
movzbl %al, %eax
addl $-0x80, %eax
movl %eax, 0x8(%rbp,%rbx)
movq 0x18(%r13), %rdi
movq %r14, %rsi
callq 0xeeb4b
movzbl %al, %r14d
addl $-0x80, %r14d
xorl %eax, %eax
jmp 0xc01df
movq (%rsp), %rsi
movl 0x10(%r12), %edx
callq 0xeef8a
movq %r12, %rbp
movq %rax, %r12
movq (%rsp), %rsi
addq $0x2, %rsi
movq %rsi, (%rsp)
movq 0x18(%r13), %rdi
movl 0x10(%rbp), %edx
callq 0xeef8a
movq %rax, 0x8(%rsp)
movq (%rsp), %rsi
addq $0x2, %rsi
movq %rsi, (%rsp)
movq 0x18(%r13), %rdi
movl 0x10(%rbp), %edx
callq 0xeef8a
movq %rax, %r15
movq (%rsp), %rsi
addq $0x2, %rsi
movq %rsi, (%rsp)
movq 0x18(%r13), %rdi
movl 0x10(%rbp), %edx
callq 0xeef8a
movl %eax, 0x8(%r14,%rbx)
movq (%rsp), %rsi
addq $0x2, %rsi
movq %rsi, (%rsp)
movq 0x18(%r13), %rdi
movl 0x10(%rbp), %edx
callq 0xeef8a
movq %rax, %r14
movq (%rsp), %rsi
addq $0x2, %rsi
movq %rsi, (%rsp)
movq 0x18(%r13), %rdi
movl 0x10(%rbp), %edx
callq 0xeef43
addq $0x2, (%rsp)
cmpl $0x1, (%r13)
jle 0xc0286
movq %r13, %rdi
leaq 0x79b14(%rip), %rsi     # 0x139d08
movl %r12d, %edx
movq 0x8(%rsp), %rcx
movq %rax, 0x20(%rsp)
xorl %eax, %eax
callq 0xe87e4
movq %r13, %rdi
leaq 0x5eec5(%rip), %rsi     # 0x11f0d7
movl %r15d, %edx
xorl %eax, %eax
callq 0xe87e4
movq 0x18(%rsp), %rbp
movl 0x8(%rbp,%rbx), %edx
movq %r13, %rdi
leaq 0x79af0(%rip), %rsi     # 0x139d1f
movl %r14d, %ecx
xorl %eax, %eax
callq 0xe87e4
movq %r13, %rdi
leaq 0x79af4(%rip), %rsi     # 0x139d37
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0xe87e4
movq 0x8(%rsp), %rdx
subl %r12d, %edx
addl 0x8(%rbp,%rbx), %r14d
cmpl $0x2, (%r13)
movl %edx, (%rbp,%rbx)
movl %r14d, 0x4(%rbp,%rbx)
jl 0xc02a1
movq %r13, %rdi
leaq 0x79ad0(%rip), %rsi     # 0x139d46
movl %r14d, %ecx
xorl %eax, %eax
callq 0xe8617
movl (%rbp,%rbx), %edx
jmp 0xc02a1
movq 0x8(%rsp), %rdx
subl %r12d, %edx
movq 0x18(%rsp), %rbp
movl %edx, (%rbp,%rbx)
addl 0x8(%rbp,%rbx), %r14d
movl %r14d, 0x4(%rbp,%rbx)
testw %r12w, %r12w
movl $0x0, %eax
cmovsl %eax, %r12d
movw %r12w, 0xc(%rbp,%rbx)
addl %edx, %r12d
subl %r12d, %r15d
testw %r15w, %r15w
cmovsl %eax, %r15d
movw %r15w, 0xe(%rbp,%rbx)
movq 0x38(%rsp), %r13
movq %r13, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x40(%rsp), %rdx
incq %rdx
addq $0x18, %rbx
movq 0x28(%rsp), %rbp
cmpq 0x10(%rbp), %rdx
movq 0x30(%rsp), %r12
jl 0xc0091
movl 0x14(%rsp), %esi
movq %r13, %rdi
callq 0xe8a25
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq