// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/grasp.c
// Function: de_run_graspfont_oldfmt
static void de_run_graspfont_oldfmt(deark *c)
{
	i64 reported_filesize;
	i32 first_codepoint;
	struct de_bitmap_font *font = NULL;
	i64 bytes_per_glyph;
	i64 i;
	i64 font_data_size;
	u8 *font_data = NULL;
	i64 glyph_rowspan;
	struct de_encconv_state es;

	font = de_create_bitmap_font(c);

	reported_filesize = de_getu16le(0);
	de_dbg(c, "reported file size: %d", (int)reported_filesize);

	font->has_nonunicode_codepoints = 1;
	font->has_unicode_codepoints = 1;
	font->num_chars = (i64)de_getbyte(2);
	if(font->num_chars==0) font->num_chars=256;
	first_codepoint = (i32)de_getbyte(3);
	font->nominal_width = (int)de_getbyte(4);
	font->nominal_height = (int)de_getbyte(5);
	bytes_per_glyph = (i64)de_getbyte(6);

	de_dbg(c, "number of glyphs: %d, first codepoint: %d", (int)font->num_chars, (int)first_codepoint);
	de_dbg(c, "glyph dimensions: %d"DE_CHAR_TIMES"%d, size in bytes: %d", font->nominal_width,
		font->nominal_height, (int)bytes_per_glyph);

	glyph_rowspan = (font->nominal_width+7)/8;
	if(bytes_per_glyph < glyph_rowspan*font->nominal_height ||
		font->nominal_width<1 || font->nominal_height<1)
	{
		de_err(c, "Bad font metrics");
		goto done;
	}

	font->char_array = de_mallocarray(c, font->num_chars, sizeof(struct de_bitmap_font_char));
	font_data_size = bytes_per_glyph * font->num_chars;
	font_data = de_malloc(c, font_data_size);

	// There's no way to tell what encoding a GRASP font uses, but CP437 is
	// a reasonable guess.
	de_encconv_init(&es, DE_ENCODING_CP437_G);

	de_read(font_data, 7, font_data_size);

	for(i=0; i<font->num_chars; i++) {
		font->char_array[i].width = font->nominal_width;
		font->char_array[i].height = font->nominal_height;
		font->char_array[i].rowspan = glyph_rowspan;

		font->char_array[i].codepoint_nonunicode = first_codepoint + (i32)i;

		font->char_array[i].codepoint_unicode =
			de_char_to_unicode_ex(first_codepoint + (i32)i, &es);

		font->char_array[i].bitmap = &font_data[i*bytes_per_glyph];
	}

	de_font_bitmap_font_to_image(c, font, NULL, 0);

done:
	if(font) {
		if(font->char_array) {
			de_free(c, font->char_array);
		}
		de_destroy_bitmap_font(c, font);
	}
	de_free(c, font_data);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
callq 0x16f6f0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x165280
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl %eax, %edx
leaq 0x1359bd(%rip), %rsi    # 0x1abeec
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
movb $0x1, 0x10(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x11(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x2, %esi
callq 0x165060
movzbl %al, %eax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x7657c
movq -0x20(%rbp), %rax
movq $0x100, 0x18(%rax)      # imm = 0x100
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x3, %esi
callq 0x165060
movzbl %al, %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x165060
movzbl %al, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x5, %esi
callq 0x165060
movzbl %al, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x6, %esi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
movl -0x14(%rbp), %ecx
leaq 0x135907(%rip), %rsi    # 0x1abf03
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %edx
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movl %eax, %r8d
leaq 0x13590b(%rip), %rsi    # 0x1abf2d
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
movl (%rax), %eax
addl $0x7, %eax
movl $0x8, %ecx
cltd
idivl %ecx
cltq
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x20(%rbp), %rdx
movslq 0x4(%rdx), %rdx
imulq %rdx, %rcx
cmpq %rcx, %rax
jl 0x7666c
movq -0x20(%rbp), %rax
cmpl $0x1, (%rax)
jl 0x7666c
movq -0x20(%rbp), %rax
cmpl $0x1, 0x4(%rax)
jge 0x76683
movq -0x8(%rbp), %rdi
leaq 0x1358e2(%rip), %rsi    # 0x1abf59
movb $0x0, %al
callq 0x15bfb0
jmp 0x767dd
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rsi
movl $0x28, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq -0x20(%rbp), %rcx
imulq 0x18(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b780
movq %rax, -0x40(%rbp)
leaq -0x68(%rbp), %rdi
movl $0xd, %esi
callq 0x160150
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rcx
movl $0x7, %edx
callq 0x164d10
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x767ca
movq -0x20(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movq -0x48(%rbp), %rcx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movq %rcx, 0x18(%rax)
movl -0x14(%rbp), %ecx
movq -0x30(%rbp), %rax
addl %eax, %ecx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x14(%rbp), %edi
movq -0x30(%rbp), %rax
addl %eax, %edi
leaq -0x68(%rbp), %rsi
callq 0x160a30
movl %eax, %ecx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
imulq -0x28(%rbp), %rax
addq %rax, %rcx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x766f6
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x16fe80
cmpq $0x0, -0x20(%rbp)
je 0x7680d
movq -0x20(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x76800
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x16f730
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15b820
addq $0x70, %rsp
popq %rbp
retq