// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/bintext.c
// Function: de_run_artworx_adf
static void de_run_artworx_adf(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	struct de_char_context *charctx = NULL;
	i64 data_start;
	i64 data_len;

	d = de_malloc(c, sizeof(lctx));

	// TODO: ADF files can probably have SAUCE records, so we should read
	// the SAUCE data if present. But there does not seem to be a defined
	// SAUCE file type for ADF.

	charctx = de_create_charctx(c, 0);
	charctx->prefer_image_output = 1;

	data_start = 1+192+4096;
	data_len = c->infile->len - data_start;
	if(data_len<0) goto done;

	d->csctx.width_in_chars = 80;
	d->csctx.height_in_chars = data_len / (d->csctx.width_in_chars*2);

	de_dbg(c, "guessed width: %d chars", (int)d->csctx.width_in_chars);
	de_dbg(c, "calculated height: %d chars", (int)d->csctx.height_in_chars);
	if(d->csctx.height_in_chars<1) goto done;
	d->has_palette = 0;
	d->has_font = 1;
	d->compression = 0;
	d->has_512chars = 0;
	d->csctx.nonblink = 1;

	do_read_palette(c, d, charctx, 1, 1);

	{
		// TODO: This duplicates a lot of the xbin code.

		d->font = de_create_bitmap_font(c);
		d->font->has_nonunicode_codepoints = 1;
		d->font->has_unicode_codepoints = 1;
		d->font->prefer_unicode = 0;
		d->font->num_chars = 256;
		d->font_height = 16;
		d->font_data_len = d->font->num_chars * d->font_height;

		do_read_font_data(c, d, 1+192);

		if(d->is_standard_font) {
			charctx->suppress_custom_font_warning = 1;
		}

		if(!do_generate_font(c, d)) goto done;

		if(c->extract_level>=2 && d->font) {
			de_font_decide_output_fmt(c);
			if(c->font_fmt_req!=DE_FONTFMT_IMAGE) {
				d->font->force_fontfile_output = 1;
			}
			do_extract_font(c, d);
		}

		charctx->font = d->font;
	}

	d->csctx.inf = c->infile;
	d->csctx.inf_pos = data_start;
	d->csctx.inf_len = data_len;
	fmtutil_char_simple_run(c, &d->csctx, charctx);

done:
	de_free_charctx(c, charctx);
	free_lctx(c, d);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
movl $0x70, %esi
callq 0xe8b2b
movq %rax, %r12
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf3eb8
movq %rax, %r15
movb $0x1, (%rax)
movq 0x18(%rbx), %rax
movq 0x18(%rax), %r13
cmpq $0x10c1, %r13           # imm = 0x10C1
jl 0x30ba0
addq $-0x10c1, %r13          # imm = 0xEF3F
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
movq %r13, %rax
mulq %rcx
movq $0x50, 0x50(%r14)
shrq $0x7, %rdx
movq %rdx, 0x58(%r14)
leaq 0xe696d(%rip), %rsi     # 0x1173fb
movq %rbx, %rdi
movl $0x50, %edx
xorl %eax, %eax
callq 0xe8617
movl 0x58(%r14), %edx
leaq 0xe690f(%rip), %rsi     # 0x1173b7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpq $0x0, 0x58(%r14)
jle 0x30ba0
movl $0x100, 0x2(%r14)       # imm = 0x100
movb $0x1, 0x4d(%r14)
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %ecx
callq 0x31aae
movq %rbx, %rdi
callq 0xf5358
movq %rax, 0x28(%r14)
movw $0x101, 0x10(%rax)      # imm = 0x101
movb $0x0, 0x12(%rax)
movq $0x100, 0x18(%rax)      # imm = 0x100
movq $0x10, 0x8(%r14)
movq $0x1000, 0x10(%r14)     # imm = 0x1000
movl $0xc1, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31bf0
cmpl $0x0, 0x20(%r14)
je 0x30b29
movb $0x1, 0x4(%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31d05
testl %eax, %eax
je 0x30ba0
addq $0x30, %r12
cmpl $0x2, 0x90(%rbx)
movq 0x28(%r14), %rax
jl 0x30b7a
testq %rax, %rax
je 0x30b78
movq %rbx, %rdi
callq 0xf62bc
cmpb $0x3, 0xd5(%rbx)
je 0x30b67
movq 0x28(%r14), %rax
movb $0x1, 0x13(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31e07
movq 0x28(%r14), %rax
jmp 0x30b7a
xorl %eax, %eax
movq %rax, 0x60(%r15)
movq 0x18(%rbx), %rax
movq %rax, 0x30(%r14)
movq $0x10c1, 0x38(%r14)     # imm = 0x10C1
movq %r13, 0x40(%r14)
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xf9c77
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf3fb8
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x31e6b