// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movl $0x70, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xd, %edx
callq 0x15e4a0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x48(%rax)
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0x16d7b0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
movq -0x8(%rbp), %rdi
leaq 0x15ecca(%rip), %rsi    # 0x19dc77
callq 0x15cb30
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x3efcc
movq -0x48(%rbp), %rdi
callq 0x15cca0
cltq
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
leaq -0x23(%rbp), %rdx
movl $0x1, %ecx
callq 0x173ca0
cmpb $0x0, -0x23(%rbp)
je 0x3f14a
movq -0x8(%rbp), %rdi
callq 0x173e20
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x30(%rbp), %rdx
callq 0x173db0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x20(%rcx), %rdx
movq %rdx, 0x80(%rax)
movq 0x28(%rcx), %rcx
movq %rcx, 0x88(%rax)
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movzbl 0x38(%rax), %eax
cmpl $0x5, %eax
jne 0x3f148
movl $0x1, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movzbl 0x39(%rax), %eax
cmpl $0x1, %eax
jne 0x3f0e4
movq -0x30(%rbp), %rax
movzwl 0x58(%rax), %eax
cmpl $0x0, %eax
jle 0x3f0e4
movq -0x30(%rbp), %rax
movzwl 0x58(%rax), %eax
movl %eax, %ecx
shlq %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x50(%rax)
jmp 0x3f0f9
movq -0x30(%rbp), %rax
movzbl 0x39(%rax), %eax
movl %eax, %ecx
shlq %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x30(%rbp), %rax
movzbl 0x3a(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x3f111
movq -0x18(%rbp), %rax
movb $0x1, 0x4d(%rax)
movq -0x30(%rbp), %rax
movzbl 0x3a(%rax), %eax
andl $0x18, %eax
sarl $0x3, %eax
cmpl $0x2, %eax
jne 0x3f12c
movq -0x20(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x30(%rbp), %rax
movzbl 0x3a(%rax), %eax
andl $0x6, %eax
sarl %eax
cmpl $0x2, %eax
jne 0x3f146
movq -0x20(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x3f148
jmp 0x3f14a
cmpl $0x0, -0x3c(%rbp)
jne 0x3f16c
movq -0x18(%rbp), %rax
movq $0xa0, 0x50(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x50(%rbp)
jle 0x3f17f
movq -0x50(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x18(%rbp), %rax
cmpq $0x1, 0x50(%rax)
jge 0x3f196
movq -0x18(%rbp), %rax
movq $0xa0, 0x50(%rax)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x50(%rcx), %rcx
shlq %rcx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x3f1c2
movq -0x8(%rbp), %rdi
leaq 0x163f41(%rip), %rsi    # 0x1a30fc
movb $0x0, %al
callq 0x15bc10
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x50(%rcx), %rcx
shlq %rcx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl %eax, %edx
leaq 0x163fb3(%rip), %rsi    # 0x1a31a9
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movl %eax, %edx
leaq 0x163f4b(%rip), %rsi    # 0x1a315d
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movb $0x1, 0x2(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x18(%rbp), %rax
movb $0x0, 0x4(%rax)
movq -0x18(%rbp), %rax
movb $0x0, 0x5(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0x4c(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x30, %rsi
movq -0x20(%rbp), %rdx
callq 0x176610
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x16d960
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x173e40
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x41280
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)