// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/exectext.c
// Function: exectext_convert_and_write_slice
static void exectext_convert_and_write_slice(deark *c, lctx *d,
	i64 pos1, i64 len, struct ecnv_ctx *ecnv)
{
	i64 pos = pos1;
	i64 endpos = pos1 + len;

	while(pos < endpos) {
		u8 x;
		u8 tmpbyte;
		u8 next_byte_is_same;
		int k;

		x = dbuf_getbyte_p(d->inf, &pos);
		switch(d->chartypes[x]) {

		case ETCT_CONTROL:
			if(ecnv->proctype==ET_PROCTYPE_FMTCONV_AND_ENCCONV) {
				ecnv_add_rune(ecnv, (de_rune)x);
			}
			else {
				ecnv_add_byte(ecnv, x);
			}
			break;
		case ETCT_SPECIAL:
			if(ecnv->proctype==ET_PROCTYPE_FMTCONV_AND_ENCCONV) {
				ecnv_add_rune(ecnv, 0xfffd);
			}
			break;
		case ETCT_8SPACES:
			for(k=0; k<8; k++) {
				ecnv_add_byte(ecnv, 0x20);
			}
			break;
		case ETCT_CRLF:
			if(d->proctype==ET_PROCTYPE_FMTCONV_AND_ENCCONV) {
				ecnv_add_rune(ecnv, 0x0d);
				ecnv_add_rune(ecnv, 0x0a);
			}
			else {
				ecnv_add_byte(ecnv, 0x0d);
				ecnv_add_byte(ecnv, 0x0a);
			}
			break;
		case ETCT_DOC2COMSPECIAL:
			next_byte_is_same = 0;
			// Escaping is done by doubling the special character.
			// Peek ahead at the next byte:
			if(pos < endpos-1) {
				tmpbyte = dbuf_getbyte(d->inf, pos);
				if(tmpbyte==x) {
					next_byte_is_same = 1;
				}
			}
			if(next_byte_is_same) {
				ecnv_add_byte(ecnv, x);
				pos++; // Skip the extra byte we read
			}
			else {
				if(ecnv->proctype==ET_PROCTYPE_FMTCONV_AND_ENCCONV) {
					ecnv_add_rune(ecnv, 0xfffd);
				}
			}
			break;
		default:
			ecnv_add_byte(ecnv, x);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x8e4ce
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x39(%rbp)
movq -0x10(%rbp), %rax
movzbl -0x39(%rbp), %ecx
movzbl 0x38(%rax,%rcx), %eax
movl %eax, -0x44(%rbp)
subl $0x1, %eax
je 0x8e388
jmp 0x8e359
movl -0x44(%rbp), %eax
subl $0x2, %eax
je 0x8e3b3
jmp 0x8e363
movl -0x44(%rbp), %eax
subl $0x64, %eax
je 0x8e3d0
jmp 0x8e36d
movl -0x44(%rbp), %eax
subl $0x65, %eax
je 0x8e444
jmp 0x8e37b
movl -0x44(%rbp), %eax
subl $0x66, %eax
je 0x8e3fb
jmp 0x8e4bc
movq -0x28(%rbp), %rax
cmpl $0x3, 0x18(%rax)
jne 0x8e3a1
movq -0x28(%rbp), %rdi
movzbl -0x39(%rbp), %esi
callq 0x8e560
jmp 0x8e3ae
movq -0x28(%rbp), %rdi
movzbl -0x39(%rbp), %esi
callq 0x8e5b0
jmp 0x8e4c9
movq -0x28(%rbp), %rax
cmpl $0x3, 0x18(%rax)
jne 0x8e3cb
movq -0x28(%rbp), %rdi
movl $0xfffd, %esi           # imm = 0xFFFD
callq 0x8e560
jmp 0x8e4c9
movl $0x0, -0x40(%rbp)
cmpl $0x8, -0x40(%rbp)
jge 0x8e3f6
movq -0x28(%rbp), %rdi
movl $0x20, %esi
callq 0x8e5b0
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x8e3d7
jmp 0x8e4c9
movq -0x10(%rbp), %rax
cmpl $0x3, 0x18(%rax)
jne 0x8e423
movq -0x28(%rbp), %rdi
movl $0xd, %esi
callq 0x8e560
movq -0x28(%rbp), %rdi
movl $0xa, %esi
callq 0x8e560
jmp 0x8e43f
movq -0x28(%rbp), %rdi
movl $0xd, %esi
callq 0x8e5b0
movq -0x28(%rbp), %rdi
movl $0xa, %esi
callq 0x8e5b0
jmp 0x8e4c9
movb $0x0, -0x3b(%rbp)
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jge 0x8e47f
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165060
movb %al, -0x3a(%rbp)
movzbl -0x3a(%rbp), %eax
movzbl -0x39(%rbp), %ecx
cmpl %ecx, %eax
jne 0x8e47d
movb $0x1, -0x3b(%rbp)
jmp 0x8e47f
cmpb $0x0, -0x3b(%rbp)
je 0x8e4a0
movq -0x28(%rbp), %rdi
movzbl -0x39(%rbp), %esi
callq 0x8e5b0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x8e4ba
movq -0x28(%rbp), %rax
cmpl $0x3, 0x18(%rax)
jne 0x8e4b8
movq -0x28(%rbp), %rdi
movl $0xfffd, %esi           # imm = 0xFFFD
callq 0x8e560
jmp 0x8e4ba
jmp 0x8e4c9
movq -0x28(%rbp), %rdi
movzbl -0x39(%rbp), %esi
callq 0x8e5b0
jmp 0x8e320
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)