// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/unifont.c
// Function: de_run_unifont_hex
static void de_run_unifont_hex(deark *c, de_module_params *mparams)
{
	struct de_bitmap_font *font = NULL;
	i64 char_array_numalloc = 0;
	char linebuf[256];
	struct de_linereader *lr = NULL;
	int ok = 0;

	font = de_create_bitmap_font(c);
	font->has_unicode_codepoints = 1;
	font->prefer_unicode = 1;
	font->nominal_height = 16;
	char_array_numalloc = 1024;
	font->char_array = de_mallocarray(c, char_array_numalloc, sizeof(struct de_bitmap_font_char));

	lr = de_linereader_create(c, c->infile);

	while(de_linereader_readnextline(c, lr, linebuf, sizeof(linebuf), 0)) {
		i64 idx;
		struct de_bitmap_font_char *ch;
		i64 fdata_len;
		char *dptr; // Pointer into linebuf, to the char after the ":"

		if(font->num_chars>=17*65536) goto done;

		idx = font->num_chars;
		if(idx >= char_array_numalloc) {
			i64 new_numalloc = char_array_numalloc*2;
			font->char_array = de_reallocarray(c, font->char_array,
				char_array_numalloc, sizeof(struct de_bitmap_font_char),
				new_numalloc);
			char_array_numalloc = new_numalloc;
		}
		ch = &font->char_array[idx];

		dptr = de_strchr(linebuf, ':');
		if(!dptr) goto done;
		*dptr = '\0';
		dptr++;

		fdata_len = (i64)de_strlen(dptr);
		ch->codepoint_unicode = (i32)de_strtoll(linebuf, NULL, 16);
		if(ch->codepoint_unicode<0 || ch->codepoint_unicode>=17*65536) goto done;

		ch->width = (int)((fdata_len/32)*8);
		ch->height = 16;
		de_dbg2(c, "char[%d] U+%04X %d"DE_CHAR_TIMES"%d",
			(int)font->num_chars, (unsigned int)ch->codepoint_unicode,
			ch->width, ch->height);
		if(ch->width<8 || ch->width>32) goto done;
		ch->rowspan = (ch->width+7)/8;
		ch->bitmap = de_malloc(c, ch->rowspan * ch->height);
		decode_fontdata(c, dptr, ch);

		font->num_chars++;
		if(ch->width > font->nominal_width) {
			font->nominal_width = ch->width;
		}
	}

	de_dbg(c, "number of characters: %d", (int)font->num_chars);
	if(font->num_chars<1) goto done;
	if(font->nominal_width<1) goto done;

	de_font_bitmap_font_to_image(c, font, NULL, 0);
	ok = 1;

done:
	if(!ok) {
		de_err(c, "Error parsing HEX font file (offset %"I64_FMT")", lr->f_pos);
	}
	de_linereader_destroy(c, lr);
	if(font) {
		if(font->char_array) {
			i64 k;
			for(k=0; k<font->num_chars; k++) {
				de_free(c, font->char_array[k].bitmap);
			}
			de_free(c, font->char_array);
		}
		font->char_array = NULL;
		de_destroy_bitmap_font(c, font);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x128(%rbp)
movl $0x0, -0x12c(%rbp)
movq -0x8(%rbp), %rdi
callq 0x16f6f0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movb $0x1, 0x11(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0x12(%rax)
movq -0x18(%rbp), %rax
movl $0x10, 0x4(%rax)
movq $0x400, -0x20(%rbp)     # imm = 0x400
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x28, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x153dc0
movq %rax, -0x128(%rbp)
movq -0x8(%rbp), %rdi
movq -0x128(%rbp), %rsi
leaq -0x120(%rbp), %rdx
movl $0x100, %ecx            # imm = 0x100
xorl %r8d, %r8d
callq 0x153e00
cmpl $0x0, %eax
je 0x153c98
movq -0x18(%rbp), %rax
cmpq $0x110000, 0x18(%rax)   # imm = 0x110000
jl 0x153a58
jmp 0x153ce9
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rax
cmpq -0x20(%rbp), %rax
jl 0x153ab9
movq -0x20(%rbp), %rax
shlq %rax
movq %rax, -0x158(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x20(%rbp), %rdx
movq -0x158(%rbp), %r8
movl $0x28, %ecx
callq 0x15c4d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x158(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x138(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x140(%rbp)
leaq -0x120(%rbp), %rdi
movl $0x3a, %esi
callq 0x15abe0
movq %rax, -0x150(%rbp)
cmpq $0x0, -0x150(%rbp)
jne 0x153afa
jmp 0x153ce9
movq -0x150(%rbp), %rax
movb $0x0, (%rax)
movq -0x150(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rdi
callq 0x190e0
movq %rax, -0x148(%rbp)
leaq -0x120(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x10, %edx
callq 0x18fa00
movl %eax, %ecx
movq -0x140(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x140(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jl 0x153b67
movq -0x140(%rbp), %rax
cmpl $0x110000, 0x4(%rax)    # imm = 0x110000
jl 0x153b6c
jmp 0x153ce9
movq -0x148(%rbp), %rax
movl $0x20, %ecx
cqto
idivq %rcx
shlq $0x3, %rax
movl %eax, %ecx
movq -0x140(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x140(%rbp), %rax
movl $0x10, 0xc(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
movq -0x140(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x140(%rbp), %rax
movl 0x8(%rax), %r8d
movq -0x140(%rbp), %rax
movl 0xc(%rax), %r9d
leaq 0x7d745(%rip), %rsi     # 0x1d1315
movb $0x0, %al
callq 0x15b350
movq -0x140(%rbp), %rax
cmpl $0x8, 0x8(%rax)
jl 0x153bf1
movq -0x140(%rbp), %rax
cmpl $0x20, 0x8(%rax)
jle 0x153bf6
jmp 0x153ce9
movq -0x140(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x7, %eax
movl $0x8, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
movq -0x140(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x140(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x140(%rbp), %rax
movslq 0xc(%rax), %rax
imulq %rax, %rsi
callq 0x15b780
movq %rax, %rcx
movq -0x140(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x150(%rbp), %rsi
movq -0x140(%rbp), %rdx
callq 0x153ee0
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x18(%rax)
movq -0x140(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jle 0x153c93
movq -0x140(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
jmp 0x153a1d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0x56e6c(%rip), %rsi     # 0x1aab19
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpq $0x1, 0x18(%rax)
jge 0x153cc1
jmp 0x153ce9
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jge 0x153ccc
jmp 0x153ce9
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x16fe80
movl $0x1, -0x12c(%rbp)
cmpl $0x0, -0x12c(%rbp)
jne 0x153d0f
movq -0x8(%rbp), %rdi
movq -0x128(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x7d624(%rip), %rsi     # 0x1d132c
movb $0x0, %al
callq 0x15bfb0
movq -0x8(%rbp), %rdi
movq -0x128(%rbp), %rsi
callq 0x153fc0
cmpq $0x0, -0x18(%rbp)
je 0x153daf
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x153d96
movq $0x0, -0x160(%rbp)
movq -0x160(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x153d85
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x160(%rbp), %rcx
addq %rcx, %rax
movq 0x20(%rax), %rsi
callq 0x15b820
movq -0x160(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x160(%rbp)
jmp 0x153d40
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x15b820
movq -0x18(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x16f730
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nopl (%rax,%rax)