// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/fmtutil.c
// Function: fmtutil_char_simple_run
void fmtutil_char_simple_run(deark *c, struct fmtutil_char_simplectx *csctx,
	struct de_char_context *charctx)
{
	i64 i, j;
	i64 inf_endpos;
	i64 fg_stride;
	i64 attr_offset;
	u8 ccode, acode;
	u8 fgcol, bgcol;
	struct de_char_screen *screen;
	de_encoding encoding;
	struct de_encconv_state es;

	inf_endpos = csctx->inf_pos + csctx->inf_len;
	if(csctx->use_default_pal) {
		de_copy_std_palette(DE_PALID_PC16, 0, 0, charctx->pal, 16, 0);
	}
	charctx->nscreens = 1;
	charctx->screens = de_mallocarray(c, charctx->nscreens, sizeof(struct de_char_screen*));
	charctx->screens[0] = de_malloc(c, sizeof(struct de_char_screen));
	screen = charctx->screens[0];
	screen->width = csctx->width_in_chars;
	screen->height = csctx->height_in_chars;
	screen->cell_rows = de_mallocarray(c, csctx->height_in_chars, sizeof(struct de_char_cell*));

	if(csctx->input_encoding==DE_ENCODING_UNKNOWN) {
		encoding = DE_ENCODING_CP437;
	}
	else {
		encoding = csctx->input_encoding;
	}
	de_encconv_init(&es, DE_EXTENC_MAKE(encoding, DE_ENCSUBTYPE_PRINTABLE));

	if(csctx->fg_stride) {
		fg_stride = csctx->fg_stride;
		attr_offset = csctx->attr_offset;
	}
	else {
		fg_stride = 2;
		attr_offset = 1;
	}

	for(j=0; j<csctx->height_in_chars; j++) {
		screen->cell_rows[j] = de_mallocarray(c, csctx->width_in_chars, sizeof(struct de_char_cell));

		for(i=0; i<csctx->width_in_chars; i++) {
			i64 pos;

			pos = csctx->inf_pos + j*csctx->width_in_chars*fg_stride + i*fg_stride;
			if(pos < inf_endpos)
				ccode = dbuf_getbyte(csctx->inf, pos);
			else
				ccode = 0;
			if(pos < inf_endpos)
				acode = dbuf_getbyte(csctx->inf, pos+attr_offset);
			else
				acode = 0;

			if((acode&0x80) && !csctx->nonblink) {
				screen->cell_rows[j][i].blink = 1;
				acode -= 0x80;
			}

			fgcol = (acode & 0x0f);
			bgcol = (acode & 0xf0) >> 4;

			screen->cell_rows[j][i].fgcol = (u32)fgcol;
			screen->cell_rows[j][i].bgcol = (u32)bgcol;
			screen->cell_rows[j][i].codepoint = (i32)ccode;
			screen->cell_rows[j][i].codepoint_unicode = de_char_to_unicode_ex((i32)ccode, &es);
		}
	}

	de_char_output_to_file(c, charctx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, 0x8(%rsp)
movq 0x10(%rsi), %rax
addq 0x8(%rsi), %rax
movq %rax, 0x30(%rsp)
cmpb $0x0, 0x1c(%rsi)
je 0xee328
leaq 0x20(%r14), %rcx
pushq $0x14
popq %rdi
pushq $0x10
popq %r8
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xe1b11
movq $0x1, 0x10(%r14)
pushq $0x1
popq %r13
pushq $0x8
popq %rbx
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0xdf66f
movq %rax, 0x18(%r14)
pushq $0x18
popq %rsi
movq %r12, %rdi
callq 0xdecaf
movq 0x18(%r14), %rcx
movq %rax, (%rcx)
movq 0x18(%r14), %rax
movq (%rax), %rbp
movq 0x20(%r15), %rax
movq %rax, (%rbp)
movq 0x28(%r15), %rsi
movq %rsi, 0x8(%rbp)
movq %r12, %rdi
movq %rbx, %rdx
callq 0xdf66f
movq %rbp, 0x10(%rsp)
movq %rax, 0x10(%rbp)
movl 0x18(%r15), %eax
movzbl %al, %ecx
orl $0x400, %ecx            # imm = 0x400
testl %eax, %eax
movl $0x40d, %esi            # imm = 0x40D
cmovnel %ecx, %esi
leaq 0x38(%rsp), %rdi
callq 0xe11fc
movq 0x30(%r15), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
movq %r14, 0x20(%rsp)
je 0xee3c5
movq 0x38(%r15), %r13
jmp 0xee3cd
pushq $0x2
popq %rax
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
movq %r13, 0x28(%rsp)
cmpq 0x28(%r15), %r14
jge 0xee4bc
movq 0x20(%r15), %rsi
movq 0x8(%rsp), %rdi
pushq $0x14
popq %rdx
callq 0xdf66f
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq %rax, (%rcx,%r14,8)
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x20(%r15), %r13
cmpq %r13, %r12
jge 0xee4b4
imulq %r14, %r13
addq %r12, %r13
imulq 0x18(%rsp), %r13
addq 0x8(%r15), %r13
cmpq 0x30(%rsp), %r13
jge 0xee465
movq (%r15), %rdi
movq %r13, %rsi
callq 0xe3eb4
movl %eax, %ebx
movq (%r15), %rdi
addq 0x28(%rsp), %r13
movq %r13, %rsi
callq 0xe3eb4
testb %al, %al
jns 0xee469
cmpb $0x0, 0x1d(%r15)
jne 0xee469
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx,%r14,8), %rcx
movb $0x1, 0x12(%rcx,%rbp)
andb $0x7f, %al
jmp 0xee469
xorl %ebx, %ebx
xorl %eax, %eax
movzbl %bl, %edi
movl %eax, %ecx
andb $0xf, %cl
shrb $0x4, %al
movzbl %cl, %ecx
movq 0x10(%rsp), %rbx
movq 0x10(%rbx), %rdx
movq (%rdx,%r14,8), %rdx
movl %ecx, 0x8(%rdx,%rbp)
movzbl %al, %eax
movl %eax, 0xc(%rdx,%rbp)
movl %edi, (%rdx,%rbp)
leaq 0x38(%rsp), %rsi
callq 0xe1679
movq 0x10(%rbx), %rcx
movq (%rcx,%r14,8), %rcx
movl %eax, 0x4(%rcx,%rbp)
incq %r12
addq $0x14, %rbp
jmp 0xee402
incq %r14
jmp 0xee3d5
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0xe8a63
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq