// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/bintext.c
// Function: de_run_xbin
static void de_run_xbin(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	struct de_char_context *charctx = NULL;
	struct de_SAUCE_detection_data sdd;
	struct de_SAUCE_info *si = NULL;
	i64 pos = 0;
	u8 flags;
	dbuf *unc_data = NULL;

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

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

	fmtutil_detect_SAUCE(c, c->infile, &sdd, 0x1);
	if(sdd.has_SAUCE) {
		si = fmtutil_create_SAUCE(c);

		de_dbg_indent(c, 1);
		fmtutil_handle_SAUCE(c, c->infile, si);
		de_dbg_indent(c, -1);

		charctx->title = si->title;
		charctx->artist = si->artist;
		charctx->organization = si->organization;
		charctx->creation_date = si->creation_date;
		charctx->comment = si->comment;
	}

	d->csctx.width_in_chars = de_getu16le(5);
	d->csctx.height_in_chars = de_getu16le(7);
	d->font_height = (i64)de_getbyte(9);

	flags = de_getbyte(10);
	de_dbg(c, "dimensions: %d"DE_CHAR_TIMES"%d characters", (int)d->csctx.width_in_chars,
		(int)d->csctx.height_in_chars);
	de_dbg(c, "font height: %d", (int)d->font_height);
	de_dbg(c, "flags: 0x%02x", (UI)flags);
	d->has_palette = (flags&0x01)?1:0;
	d->has_font = (flags&0x02)?1:0;
	d->compression = (flags&0x04)?1:0;
	d->csctx.nonblink = (flags&0x08)?1:0;
	d->has_512chars = (flags&0x10)?1:0;
	de_dbg_indent(c, 1);
	de_dbg(c, "has palette: %u", (UI)d->has_palette);
	de_dbg(c, "has font: %u", (UI)d->has_font);
	de_dbg(c, "compression: %u", (UI)d->compression);
	de_dbg(c, "non-blink mode: %u", (UI)d->csctx.nonblink);
	de_dbg(c, "512 character mode: %u", (UI)d->has_512chars);
	de_dbg_indent(c, -1);

	if(d->has_font && (d->font_height<1 || d->font_height>32)) {
		de_err(c, "Invalid font height: %d", (int)d->font_height);
		goto done;
	}
	pos = 11;

	if(d->has_palette) {
		do_read_palette(c, d, charctx, pos, 0);
		pos += 48;
	}
	else {
		de_dbg(c, "using default palette");
		d->csctx.use_default_pal = 1;
	}

	if(d->has_font) {
		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 = d->has_512chars ? 512 : 256;
		d->font_data_len = d->font->num_chars * d->font_height;
		if(d->font->num_chars!=256) {
			de_err(c, "%d-character mode is not supported", (int)d->font->num_chars);
			goto done;
		}

		do_read_font_data(c, d, pos);
		pos += d->font_data_len;

		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;
	}
	else {
		// Use default font

		if(d->has_512chars) {
			de_err(c, "This type of XBIN file is not supported.");
			goto done;
		}

		if(d->font_height==0) {
			// Not really legal, but we'll let it mean "default".
		}
		else if(d->font_height!=16) {
			if(charctx->outfmt==1) { // image output
				de_warn(c, "Incompatible font height (%d), using 16 instead.", (int)d->font_height);
			}
		}
		d->font_height = 16;
	}

	de_dbg(c, "image data at %"I64_FMT, pos);

	if(d->compression) {
		unc_data = dbuf_create_membuf(c, d->csctx.width_in_chars * d->csctx.height_in_chars * 2, 1);
		xbin_decompress_data(c, d, pos, unc_data);
	}
	else {
		unc_data = dbuf_open_input_subfile(c->infile, pos, c->infile->len-pos);
	}
	d->csctx.inf = unc_data;
	d->csctx.inf_pos = 0;
	d->csctx.inf_len = unc_data->len;
	fmtutil_char_simple_run(c, &d->csctx, charctx);

done:
	dbuf_close(unc_data);
	de_free_charctx_screens(c, charctx);
	de_destroy_charctx(c, charctx);
	fmtutil_free_SAUCE(c, si);
	free_lctx(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
pushq $0x70
popq %rsi
callq 0xdecaf
movq %rax, %r12
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe88f4
movq %rax, %r13
movb $0x1, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0xe89f9
movq 0x18(%rbx), %rsi
leaq 0x35(%rsp), %r15
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %r15, %rdx
callq 0xec67d
cmpb $0x0, (%r15)
je 0x2f179
movq %rbx, %rdi
callq 0xec762
movq %rax, %r14
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0xec71a
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movups 0x8(%r14), %xmm0
movups %xmm0, 0x68(%r13)
movq 0x18(%r14), %rax
movq %rax, 0x78(%r13)
movups 0x20(%r14), %xmm0
movups %xmm0, 0x80(%r13)
movq 0x60(%r14), %rax
movq %rax, 0x90(%r13)
movq 0x18(%rbx), %rdi
pushq $0x5
popq %rsi
callq 0xe3fa3
movq %rax, 0x50(%r12)
movq 0x18(%rbx), %rdi
pushq $0x7
popq %rsi
callq 0xe3fa3
movq %rax, 0x58(%r12)
movq 0x18(%rbx), %rdi
pushq $0x9
popq %rsi
callq 0xe3eb4
movzbl %al, %eax
movq %rax, 0x8(%r12)
movq 0x18(%rbx), %rdi
pushq $0xa
popq %rsi
callq 0xe3eb4
movl %eax, %ebp
movl 0x50(%r12), %edx
movl 0x58(%r12), %ecx
leaq 0xdaf01(%rip), %rsi     # 0x10a0cf
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movl 0x8(%r12), %edx
leaq 0xdaf58(%rip), %rsi     # 0x10a13c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movzbl %bpl, %ebp
leaq 0xf3bed(%rip), %rsi     # 0x122de6
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
movl %ebp, %eax
andb $0x1, %al
movb %al, 0x2(%r12)
movl %ebp, %eax
shrb %al
andb $0x1, %al
movb %al, 0x3(%r12)
movl %ebp, %eax
shrb $0x2, %al
andb $0x1, %al
movb %al, 0x4(%r12)
movl %ebp, %eax
shrb $0x3, %al
andb $0x1, %al
movb %al, 0x4d(%r12)
shrb $0x4, %bpl
andb $0x1, %bpl
movb %bpl, 0x5(%r12)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movzbl 0x2(%r12), %edx
leaq 0xdae97(%rip), %rsi     # 0x10a0ed
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movzbl 0x3(%r12), %edx
leaq 0xdae90(%rip), %rsi     # 0x10a0fd
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movzbl 0x4(%r12), %edx
leaq 0xd757a(%rip), %rsi     # 0x1067fe
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movzbl 0x4d(%r12), %edx
leaq 0xdae6f(%rip), %rsi     # 0x10a10a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movzbl 0x5(%r12), %edx
leaq 0xdae6b(%rip), %rsi     # 0x10a11d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpb $0x0, 0x3(%r12)
je 0x2f2f6
movq 0x8(%r12), %rdx
leaq -0x21(%rdx), %rax
cmpq $-0x21, %rax
ja 0x2f2f6
leaq 0xdae4f(%rip), %rsi     # 0x10a134
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x2f69c
cmpb $0x0, 0x2(%r12)
movq %r13, 0x8(%rsp)
je 0x2f319
pushq $0xb
popq %rdx
movq %rbx, %rdi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x30a01
pushq $0x3b
popq %r13
jmp 0x2f334
pushq $0xb
popq %r13
leaq 0xdae28(%rip), %rsi     # 0x10a14c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movb $0x1, 0x4c(%r12)
cmpb $0x0, 0x3(%r12)
je 0x2f3a2
movq %rbx, %rdi
callq 0xe9d18
movq %rax, 0x28(%r12)
movw $0x101, 0x10(%rax)      # imm = 0x101
movb $0x0, 0x12(%rax)
movb 0x5(%r12), %dl
xorl %esi, %esi
testb %dl, %dl
sete %cl
setne %sil
shll $0x8, %esi
addq $0x100, %rsi            # imm = 0x100
movq %rsi, 0x18(%rax)
movq 0x8(%r12), %rax
xorb $0x9, %cl
shlq %cl, %rax
movq %rax, 0x10(%r12)
testb %dl, %dl
je 0x2f3c6
leaq 0xdadd6(%rip), %rsi     # 0x10a162
xorl %ebp, %ebp
movq %rbx, %rdi
movl $0x200, %edx            # imm = 0x200
xorl %eax, %eax
callq 0xdf2a5
jmp 0x2f45d
cmpb $0x0, 0x5(%r12)
je 0x2f43d
leaq 0xdadd0(%rip), %rsi     # 0x10a185
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x2f45d
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x30b47
addq 0x10(%r12), %r13
cmpl $0x0, 0x20(%r12)
je 0x2f3ea
movq 0x8(%rsp), %rax
movb $0x1, 0x4(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x30c54
testl %eax, %eax
je 0x2f45b
cmpl $0x2, 0x90(%rbx)
movq 0x28(%r12), %rax
movq %r14, 0x20(%rsp)
jl 0x2f487
testq %rax, %rax
je 0x2f485
movq %rbx, %rdi
callq 0xeab2d
cmpb $0x3, 0xd5(%rbx)
je 0x2f42b
movq 0x28(%r12), %rax
movb $0x1, 0x13(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x30d49
movq 0x28(%r12), %rax
jmp 0x2f487
movq %r14, 0x20(%rsp)
movq 0x8(%r12), %rdx
testq $-0x11, %rdx
jne 0x2f467
movq $0x10, 0x8(%r12)
jmp 0x2f490
xorl %ebp, %ebp
movq 0x8(%rsp), %r13
jmp 0x2f69c
movq 0x8(%rsp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x2f450
leaq 0xdad35(%rip), %rsi     # 0x10a1ae
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
jmp 0x2f450
xorl %eax, %eax
movq 0x8(%rsp), %rcx
movq %rax, 0x60(%rcx)
movq 0x18(%rsp), %r14
addq $0x30, %r14
leaq 0xfe927(%rip), %rsi     # 0x12ddc7
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xde896
cmpb $0x0, 0x4(%r12)
je 0x2f65a
movq %r14, 0x18(%rsp)
movq 0x50(%r12), %rsi
imulq 0x58(%r12), %rsi
addq %rsi, %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0xe59cd
movq %rax, %rbp
leaq 0x10(%rsp), %r15
movq %r13, (%r15)
xorl %edx, %edx
xorl %eax, %eax
movq %r12, 0x38(%rsp)
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r13
jge 0x2f64e
xorl %ecx, %ecx
cmpq 0x50(%r12), %rax
movl $0x0, %r14d
cmovlq %rax, %r14
setge %cl
addq %rcx, %rdx
cmpq 0x58(%r12), %rdx
jge 0x2f64e
movq %rdx, 0x40(%rsp)
movq %r15, %rsi
callq 0xe3f2a
movl %eax, %ecx
shrb $0x6, %cl
andb $0x3f, %al
incb %al
movzbl %al, %r12d
movq 0x18(%rbx), %rdi
movzbl %cl, %eax
leaq 0xda9d5(%rip), %rcx     # 0x109f18
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %r12, 0x28(%rsp)
jmpq *%rax
movq 0x10(%rsp), %rsi
addl %r12d, %r12d
movq %r12, %rdx
movq %rbp, %rcx
callq 0xe45f4
addq %r12, 0x10(%rsp)
addq 0x28(%rsp), %r14
movq 0x10(%rsp), %r13
movq %r14, %rax
movq 0x38(%rsp), %r12
leaq 0x10(%rsp), %r15
movq 0x40(%rsp), %rdx
jmp 0x2f4eb
movq %r15, %rsi
callq 0xe3f2a
movzbl %al, %r13d
movq %r12, %r15
subq $0x1, %r15
jb 0x2f569
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe3f2a
movzbl %al, %esi
movq %rbp, %rdi
callq 0xe5bc5
movq %rbp, %rdi
movl %r13d, %esi
callq 0xe5bc5
jmp 0x2f599
movq %r15, %rsi
callq 0xe3f2a
movl %eax, %r12d
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3f2a
movzbl %r12b, %r13d
movzbl %al, %r12d
movq 0x28(%rsp), %r15
subq $0x1, %r15
jb 0x2f569
movq %rbp, %rdi
movl %r13d, %esi
callq 0xe5bc5
movq %rbp, %rdi
movl %r12d, %esi
callq 0xe5bc5
jmp 0x2f5e9
movq %r15, %rsi
callq 0xe3f2a
movzbl %al, %r13d
movq %r12, %r15
subq $0x1, %r15
jb 0x2f569
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe3f2a
movl %eax, %r12d
movq %rbp, %rdi
movl %r13d, %esi
callq 0xe5bc5
movzbl %r12b, %esi
movq %rbp, %rdi
callq 0xe5bc5
jmp 0x2f61a
movq 0x8(%rsp), %r13
movq 0x18(%rsp), %r14
jmp 0x2f675
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r13, %rdx
movq %r13, %rsi
callq 0xe6248
movq %rax, %rbp
movq 0x8(%rsp), %r13
movq %rbp, 0x30(%r12)
andq $0x0, 0x38(%r12)
movq 0x18(%rbp), %rax
movq %rax, 0x40(%r12)
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0xee2e5
movq 0x20(%rsp), %r14
movq %rbp, %rdi
callq 0xe5591
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe88fe
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe8998
movq %rbx, %rdi
movq %r14, %rsi
callq 0xec76a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x30db6
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq