// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/exectext.c
// Function: gtxt_convert_to_text
static void gtxt_convert_to_text(deark *c, lctx *d, dbuf *outf)
{
	struct ecnv_ctx *ecnv = NULL;
	u8 esc_mode = 0;
	u8 cur_mask;
	i64 endpos = d->tpos + d->tlen;
	i64 pos;

	if(d->proctype==ET_PROCTYPE_FMTCONV_AND_ENCCONV) {
		ecnv = ecnv_create(c, d->proctype, DE_EXTENC_MAKE(d->input_encoding,
			DE_ENCSUBTYPE_PRINTABLE), outf);
		if(c->write_bom) {
			dbuf_write_uchar_as_utf8(outf, 0xfeff);
		}
	}
	if(!ecnv) {
		ecnv = ecnv_create(c, d->proctype, DE_ENCODING_UNKNOWN, outf);
	}

	pos = d->tpos;
	cur_mask = 0x7f;

	while(pos < endpos) {
		u8 x_raw, x_mod;
		u8 was_escaped = 0;
		de_rune u;

		x_raw = de_getbyte_p(&pos);
		if(x_raw==0x00) goto done;

		x_mod = x_raw & cur_mask;
		cur_mask = 0x7f; // We used the mask, now reset it to the default

		was_escaped = esc_mode;
		esc_mode = 0; // Reset to default

		// GTXT applies the mask *before* special characters are checked for.
		if(!was_escaped && (x_mod=='%')) {
			esc_mode = 1;
			cur_mask = 0xff;
		}
		else if(!was_escaped && (x_mod=='^')) {
			esc_mode = 1;
			cur_mask = 0x3f;
		}
		else if(!was_escaped && (x_mod=='~')) {
			if(d->proctype==ET_PROCTYPE_FMTCONV_AND_ENCCONV) {
				u = 0x240c; // Page break -> SYMBOL FOR FORM FEED, I guess.
				ecnv_add_rune(ecnv, 0x0d);
				ecnv_add_rune(ecnv, 0x0a);
				ecnv_add_rune(ecnv, u);
			}
			else {
				ecnv_add_byte(ecnv, 0x0d);
				ecnv_add_byte(ecnv, 0x0a);
				ecnv_add_byte(ecnv, 0x0c); // Page break -> form feed, I guess.
			}
		}
		else {
			// Process literal byte x_mod
			if(d->proctype==ET_PROCTYPE_FMTCONV_AND_ENCCONV) {
				if(d->chartypes[(UI)x_mod]==ETCT_CONTROL) {
					u = (de_rune)x_mod;
					ecnv_add_rune(ecnv, u);
				}
				else {
					ecnv_add_byte(ecnv, x_mod);
				}
			}
			else {
				ecnv_add_byte(ecnv, x_mod);
			}
		}
	}

done:
	if(ecnv) {
		ecnv_hard_flush(c, ecnv);
		ecnv_destroy(c, ecnv);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movb $0x0, -0x21(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x30(%rcx), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x3, 0x18(%rax)
jne 0x8f766
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %esi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edx
andl $0xff, %edx
orl $0x400, %edx            # imm = 0x400
movq -0x18(%rbp), %rcx
callq 0x8e260
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
cmpb $0x0, 0xcc(%rax)
je 0x8f764
movq -0x18(%rbp), %rdi
movl $0xfeff, %esi           # imm = 0xFEFF
callq 0x160cc0
jmp 0x8f766
cmpq $0x0, -0x20(%rbp)
jne 0x8f787
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %esi
movq -0x18(%rbp), %rcx
xorl %edx, %edx
callq 0x8e260
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
movb $0x7f, -0x22(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x8f8ee
movb $0x0, -0x3b(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165170
movb %al, -0x39(%rbp)
movzbl -0x39(%rbp), %eax
cmpl $0x0, %eax
jne 0x8f7cb
jmp 0x8f8f0
movzbl -0x39(%rbp), %eax
movzbl -0x22(%rbp), %ecx
andl %ecx, %eax
movb %al, -0x3a(%rbp)
movb $0x7f, -0x22(%rbp)
movb -0x21(%rbp), %al
movb %al, -0x3b(%rbp)
movb $0x0, -0x21(%rbp)
cmpb $0x0, -0x3b(%rbp)
jne 0x8f802
movzbl -0x3a(%rbp), %eax
cmpl $0x25, %eax
jne 0x8f802
movb $0x1, -0x21(%rbp)
movb $-0x1, -0x22(%rbp)
jmp 0x8f8e9
cmpb $0x0, -0x3b(%rbp)
jne 0x8f81e
movzbl -0x3a(%rbp), %eax
cmpl $0x5e, %eax
jne 0x8f81e
movb $0x1, -0x21(%rbp)
movb $0x3f, -0x22(%rbp)
jmp 0x8f8e7
cmpb $0x0, -0x3b(%rbp)
jne 0x8f894
movzbl -0x3a(%rbp), %eax
cmpl $0x7e, %eax
jne 0x8f894
movq -0x10(%rbp), %rax
cmpl $0x3, 0x18(%rax)
jne 0x8f868
movl $0x240c, -0x40(%rbp)    # imm = 0x240C
movq -0x20(%rbp), %rdi
movl $0xd, %esi
callq 0x8e560
movq -0x20(%rbp), %rdi
movl $0xa, %esi
callq 0x8e560
movq -0x20(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x8e560
jmp 0x8f892
movq -0x20(%rbp), %rdi
movl $0xd, %esi
callq 0x8e5b0
movq -0x20(%rbp), %rdi
movl $0xa, %esi
callq 0x8e5b0
movq -0x20(%rbp), %rdi
movl $0xc, %esi
callq 0x8e5b0
jmp 0x8f8e5
movq -0x10(%rbp), %rax
cmpl $0x3, 0x18(%rax)
jne 0x8f8d6
movq -0x10(%rbp), %rax
movzbl -0x3a(%rbp), %ecx
movl %ecx, %ecx
movzbl 0x38(%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x8f8c7
movzbl -0x3a(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x8e560
jmp 0x8f8d4
movq -0x20(%rbp), %rdi
movzbl -0x3a(%rbp), %esi
callq 0x8e5b0
jmp 0x8f8e3
movq -0x20(%rbp), %rdi
movzbl -0x3a(%rbp), %esi
callq 0x8e5b0
jmp 0x8f8e5
jmp 0x8f8e7
jmp 0x8f8e9
jmp 0x8f797
jmp 0x8f8f0
cmpq $0x0, -0x20(%rbp)
je 0x8f911
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x8e4e0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x8e520
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)