// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/bintext.c
// Function: de_run_bintext
static void de_run_bintext(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 effective_file_size = 0;
	int valid_sauce = 0;
	const char *s;
	i64 width_req = 0;

	d = de_malloc(c, sizeof(lctx));
	d->csctx.input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437);

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

	s=de_get_ext_option(c, "char:width");
	if(s) {
		width_req = de_atoi(s);
	}

	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;

		effective_file_size = si->original_file_size;

		if(si->data_type==5) {
			valid_sauce = 1;

			if(si->file_type==1 && si->tinfo1>0) {
				// Some files created by ACiDDraw do this.
				d->csctx.width_in_chars = 2*(i64)si->tinfo1;
			}
			else {
				// For BinText, the FileType field is inexplicably used for the width (usually).
				d->csctx.width_in_chars = 2*(i64)si->file_type;
			}

			if(si->tflags & 0x01) {
				d->csctx.nonblink = 1;
			}
			if((si->tflags & 0x18)>>3 == 0x02) {
				// Square pixels requested
				charctx->no_density = 1;
			}
			if((si->tflags & 0x06)>>1 == 0x02) {
				charctx->prefer_9col_mode = 1;
			}
		}
	}

	if(!valid_sauce) {
		d->csctx.width_in_chars = 160;
		effective_file_size = c->infile->len;
	}

	if(width_req>0) d->csctx.width_in_chars = width_req;

	if(d->csctx.width_in_chars<1) d->csctx.width_in_chars=160;
	if(effective_file_size%(d->csctx.width_in_chars*2)) {
		de_warn(c, "File does not contain a whole number of rows. The width may "
			"be wrong. Try \"-opt char:width=...\".");
	}
	d->csctx.height_in_chars = effective_file_size / (d->csctx.width_in_chars*2);

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

	d->csctx.use_default_pal = 1;
	d->csctx.inf = c->infile;
	d->csctx.inf_pos = 0;
	d->csctx.inf_len = effective_file_size;
	fmtutil_char_simple_run(c, &d->csctx, charctx);

	de_free_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 $0x18, %rsp
movq %rdi, %rbx
movl $0x70, %esi
callq 0xe969b
movq %rax, %r14
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb7d1
movl %eax, 0x48(%r14)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf43cc
movq %rax, %r15
movb $0x0, (%rax)
leaq 0xe12c8(%rip), %rsi     # 0x111ba7
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0x308f7
movq %rax, %rdi
callq 0xea781
movslq %eax, %r12
movq %r12, 0x10(%rsp)
movq 0x18(%rbx), %rsi
leaq 0xd(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rdx
movl $0x1, %ecx
callq 0xf840d
movb $0x1, %bpl
cmpb $0x0, (%r12)
je 0x309e5
movq %rbx, %rdi
callq 0xf84f4
movq %rax, %r12
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0xf84ad
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x8(%r12), %rax
movq %rax, 0x68(%r15)
movq 0x10(%r12), %rax
movq %rax, 0x70(%r15)
movq 0x18(%r12), %rax
movq %rax, 0x78(%r15)
movups 0x20(%r12), %xmm0
movups %xmm0, 0x80(%r15)
movq 0x60(%r12), %rax
movq %rax, 0x90(%r15)
movq 0x30(%r12), %r13
cmpb $0x5, 0x38(%r12)
jne 0x309eb
movzbl 0x39(%r12), %eax
cmpq $0x1, %rax
jne 0x309b0
movzwl 0x58(%r12), %ecx
testq %rcx, %rcx
jne 0x309b3
movq %rax, %rcx
addl %ecx, %ecx
movq %rcx, 0x50(%r14)
movb 0x3a(%r12), %al
testb $0x1, %al
je 0x309c7
movb $0x1, 0x4d(%r14)
movl %eax, %ecx
andb $0x18, %cl
cmpb $0x10, %cl
jne 0x309d6
movb $0x1, 0x3(%r15)
andb $0x6, %al
cmpb $0x4, %al
jne 0x309e1
movb $0x1, 0x1(%r15)
xorl %ebp, %ebp
jmp 0x309eb
xorl %r12d, %r12d
xorl %r13d, %r13d
testb %bpl, %bpl
je 0x30a00
movq $0xa0, 0x50(%r14)
movq 0x18(%rbx), %rax
movq 0x18(%rax), %r13
movq 0x10(%rsp), %rax
testq %rax, %rax
jle 0x30a0e
movq %rax, 0x50(%r14)
cmpq $0x0, 0x50(%r14)
jg 0x30a1d
movq $0xa0, 0x50(%r14)
movq 0x50(%r14), %rcx
addq %rcx, %rcx
movq %r13, %rax
cqto
idivq %rcx
testq %rdx, %rdx
je 0x30a42
leaq 0xe687e(%rip), %rsi     # 0x1172b6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movq %r14, %rbp
addq $0x30, %rbp
movq 0x50(%r14), %rcx
leaq (%rcx,%rcx), %rsi
movq %r13, %rax
cqto
idivq %rsi
movq %rax, 0x58(%r14)
leaq 0xe68ff(%rip), %rsi     # 0x117363
movq %rbx, %rdi
movl %ecx, %edx
xorl %eax, %eax
callq 0xe9226
movl 0x58(%r14), %edx
leaq 0xe689c(%rip), %rsi     # 0x117317
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movl $0x101, 0x2(%r14)       # imm = 0x101
movb $0x1, 0x4c(%r14)
movq 0x18(%rbx), %rax
movq %rax, 0x30(%r14)
movq $0x0, 0x38(%r14)
movq %r13, 0x40(%r14)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xfa176
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf44c8
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf84fe
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31fe3
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq