// File: /workspace/github_c_makefile_star2_O3/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, %rbx
movq %rsi, %r15
movq %rdi, 0x8(%rsp)
movq 0x10(%rsi), %rax
addq 0x8(%rsi), %rax
movq %rax, 0x30(%rsp)
cmpb $0x0, 0x1c(%rsi)
je 0xf9cbe
leaq 0x20(%rbx), %rcx
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xec55a
movq $0x1, 0x10(%rbx)
movl $0x1, %esi
movl $0x8, %edx
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0xe96f7
movq %rax, 0x18(%rbx)
movl $0x18, %esi
movq %r14, %rdi
callq 0xe8b2b
movq 0x18(%rbx), %rcx
movq %rax, (%rcx)
movq 0x18(%rbx), %rax
movq (%rax), %r12
movq 0x20(%r15), %rax
movq %rax, (%r12)
movq 0x28(%r15), %rsi
movq %rsi, 0x8(%r12)
movl $0x8, %edx
movq %r14, %rdi
callq 0xe96f7
movq %r12, 0x10(%rsp)
movq %rax, 0x10(%r12)
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 0xebbf7
movq 0x30(%r15), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
movq %rbx, 0x28(%rsp)
je 0xf9d63
movq 0x38(%r15), %rax
movq %rax, 0x18(%rsp)
jmp 0xf9d77
movl $0x1, %eax
movq %rax, 0x18(%rsp)
movl $0x2, %eax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x28(%r15)
jle 0xf9e7a
movq 0x20(%r15), %r13
xorl %ebx, %ebx
movl $0x14, %edx
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0xe96f7
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq %rax, (%rcx,%rbx,8)
movq 0x20(%r15), %r13
testq %r13, %r13
jle 0xf9e6d
xorl %r12d, %r12d
xorl %r14d, %r14d
imulq %rbx, %r13
addq %r14, %r13
imulq 0x20(%rsp), %r13
addq 0x8(%r15), %r13
cmpq 0x30(%rsp), %r13
jge 0xf9e11
movq (%r15), %rdi
movq %r13, %rsi
callq 0xeead3
movl %eax, %ebp
movq (%r15), %rdi
addq 0x18(%rsp), %r13
movq %r13, %rsi
callq 0xeead3
testb %al, %al
jns 0xf9e15
cmpb $0x0, 0x1d(%r15)
jne 0xf9e15
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx,%rbx,8), %rcx
movb $0x1, 0x12(%rcx,%r12)
andb $0x7f, %al
jmp 0xf9e15
xorl %ebp, %ebp
xorl %eax, %eax
movzbl %bpl, %edi
movl %eax, %ecx
andb $0xf, %cl
shrb $0x4, %al
movzbl %cl, %ecx
movq 0x10(%rsp), %r13
movq 0x10(%r13), %rdx
movq (%rdx,%rbx,8), %rdx
movl %ecx, 0x8(%rdx,%r12)
movzbl %al, %eax
movl %eax, 0xc(%rdx,%r12)
movl %edi, (%rdx,%r12)
leaq 0x38(%rsp), %rsi
callq 0xec0ad
movq 0x10(%r13), %rcx
movq (%rcx,%rbx,8), %rcx
movl %eax, 0x4(%rcx,%r12)
incq %r14
movq 0x20(%r15), %r13
addq $0x14, %r12
cmpq %r13, %r14
jl 0xf9dba
incq %rbx
cmpq 0x28(%r15), %rbx
jl 0xf9d88
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0xf404b
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq