// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/gemfont.c
// Function: do_characters
static int do_characters(deark *c, lctx *d)
{
	i64 i;
	i64 row;
	i64 n;
	struct de_bitmap_font_char *ch;
	i64 char_startpos;
	u8 *font_data = NULL;
	i64 form_nbytes;
	int retval = 0;

	de_dbg(c, "reading characters");
	de_dbg_indent(c, 1);

	form_nbytes = d->form_width_bytes * d->form_height_pixels;
	if(d->font_data_pos + form_nbytes > c->infile->len) {
		de_err(c, "Font data goes beyond end of file");
		goto done;
	}
	font_data = de_malloc(c, form_nbytes);
	de_read(font_data, d->font_data_pos, form_nbytes);

	if(d->byte_swap_flag) {
		i64 z;

		if(d->form_width_bytes%2) {
			de_err(c, "Bad form width");
			goto done;
		}

		for(z=0; z+1<form_nbytes; z+=2) {
			u8 tmpb;

			tmpb = font_data[z];
			font_data[z] = font_data[z+1];
			font_data[z+1] = tmpb;
		}
	}

	for(i=0; i<d->font->num_chars; i++) {
		ch = &d->font->char_array[i];
		char_startpos = de_getu16le(d->char_offset_table_pos + 2*i);
		n = de_getu16le(d->char_offset_table_pos + 2*(i+1));
		ch->width = (int)(n - char_startpos);
		ch->height = d->font->nominal_height;
		ch->codepoint_nonunicode = (i32)(d->first_index+i);
		de_dbg2(c, "char[%d] #%d offset=%d width=%d", (int)i, (int)ch->codepoint_nonunicode,
			 (int)char_startpos, ch->width);
		if(ch->width<1 || ch->width>d->max_char_cell_width || ch->width>512) continue;

		ch->rowspan = (ch->width+7)/8;
		ch->bitmap = de_malloc(c, ch->height * ch->rowspan);

		for(row=0; row<ch->height; row++) {
			de_copy_bits(font_data + row*d->form_width_bytes, char_startpos,
				ch->bitmap + row*ch->rowspan, 0, (i64)ch->width);
		}

		if(ch->width > d->font->nominal_width) {
			// Track the maximum character width.
			d->font->nominal_width = ch->width;
		}
	}
	retval = 1;

done:
	de_dbg_indent(c, -1);
	de_free(c, font_data);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x13acfd(%rip), %rsi    # 0x1aad27
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rcx
imulq 0x40(%rcx), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
addq -0x48(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x70085
movq -0x8(%rbp), %rdi
leaq 0x13acc1(%rip), %rsi    # 0x1aad3a
movb $0x0, %al
callq 0x15bfb0
jmp 0x7033f
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x15b780
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdx
movq -0x48(%rbp), %rcx
callq 0x164d10
movq -0x10(%rbp), %rax
cmpb $0x0, 0x48(%rax)
je 0x7014a
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl $0x2, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x700f0
movq -0x8(%rbp), %rdi
leaq 0x13ac78(%rip), %rsi    # 0x1aad5c
movb $0x0, %al
callq 0x15bfb0
jmp 0x7033f
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
cmpq -0x48(%rbp), %rax
jge 0x70148
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x59(%rbp)
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movb 0x1(%rax,%rcx), %dl
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movb %dl, (%rax,%rcx)
movb -0x59(%rbp), %dl
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movb %dl, 0x1(%rax,%rcx)
movq -0x58(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x58(%rbp)
jmp 0x700f8
jmp 0x7014a
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
cmpq 0x18(%rcx), %rax
jge 0x70338
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x18(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rax
shlq %rax
addq %rax, %rsi
callq 0x165280
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rax
addq $0x1, %rax
shlq %rax
addq %rax, %rsi
callq 0x165280
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
subq -0x38(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl 0x4(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
addq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movl (%rax), %ecx
movq -0x38(%rbp), %rax
movl %eax, %r8d
movq -0x30(%rbp), %rax
movl 0x8(%rax), %r9d
leaq 0x13ab47(%rip), %rsi    # 0x1aad6b
movb $0x0, %al
callq 0x15b350
movq -0x30(%rbp), %rax
cmpl $0x1, 0x8(%rax)
jl 0x70254
movq -0x30(%rbp), %rax
movslq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jg 0x70254
movq -0x30(%rbp), %rax
cmpl $0x200, 0x8(%rax)       # imm = 0x200
jle 0x70259
jmp 0x70327
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x7, %eax
movl $0x8, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movslq 0xc(%rax), %rsi
movq -0x30(%rbp), %rax
imulq 0x18(%rax), %rsi
callq 0x15b780
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movslq 0xc(%rcx), %rcx
cmpq %rcx, %rax
jge 0x70303
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
imulq 0x38(%rcx), %rax
addq %rax, %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdx
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
imulq 0x18(%rcx), %rax
addq %rax, %rdx
movq -0x30(%rbp), %rax
movslq 0x8(%rax), %r8
xorl %eax, %eax
movl %eax, %ecx
callq 0x15e500
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x702a3
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
cmpl (%rcx), %eax
jle 0x70325
movq -0x30(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %ecx, (%rax)
jmp 0x70327
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x70152
movl $0x1, -0x4c(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15b820
movl -0x4c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)