// File: /workspace/github_c_makefile_star2_O3/mloar[P]charset/iso2022.c
// Function: write_iso2022
static int write_iso2022(charset_spec const *charset, long int input_chr,
			 charset_state *state,
			 void (*emit)(void *ctx, long int output),
			 void *emitctx)
{
    int i;
    struct iso2022_subcharset const *subcs;
    struct iso2022_mode const *mode = (struct iso2022_mode *)charset->data;
    to_dbcs_planar_t last_planar_dbcs = NULL;
    int last_p, last_r, last_c;
    long int c1, c2;

    /*
     * For output, I allocate the state variables as follows:
     * 
     *  s1[31] == 1 if output state has been initialised
     *  s1[30:24] == G1 charset (always in GR)
     *  s1[23:17] == G0 charset (always in GL)
     *  s1[16:14] == DOCS index plus 2 (because -1 and -2 are special)
     *  s1[13:11] == number of DOCS accumulated characters (up to five)
     *  s1[7:0] + s0[31:0] == DOCS collected characters
     */

    if (!state->s1) {
	state->s0 = 0x00000000UL;
	state->s1 = 0x80000000UL;
	/*
	 * Start with US-ASCII in GL and also in GR.
	 */
	for (i = 0; (unsigned)i < lenof(iso2022_subcharsets); i++) {
	    subcs = &iso2022_subcharsets[i];
	    if (subcs->type == mode->ltype &&
		subcs->i == mode->li &&
		subcs->f == mode->lf)
		oselect(state, i, FALSE, NULL, NULL);
	    if (subcs->type == mode->rtype &&
		subcs->i == mode->ri &&
		subcs->f == mode->rf)
		oselect(state, i, TRUE, NULL, NULL);
	}
    }

    if (input_chr == -1) {
	/*
	 * Special case: reset encoding state.
	 */
	docs_char(state, emit, emitctx, -2, NULL, 0);   /* leave DOCS */

	for (i = 0; (unsigned)i < lenof(iso2022_subcharsets); i++) {
	    subcs = &iso2022_subcharsets[i];
	    if (subcs->type == mode->ltype &&
		subcs->i == mode->li &&
		subcs->f == mode->lf)
		oselect(state, i, FALSE, emit, emitctx);
	    if (subcs->type == mode->rtype &&
		subcs->i == mode->ri &&
		subcs->f == mode->rf)
		oselect(state, i, TRUE, emit, emitctx);
	}
	return TRUE;
    }

    /*
     * Special-case characters: Space, Delete, and anything in C0
     * or C1 are output unchanged.
     */
    if (input_chr <= 0x20 || (input_chr >= 0x7F && input_chr < 0xA0)) {
	emit(emitctx, input_chr);
	return TRUE;
    }

    /*
     * Analyse the input character and work out which subcharset it
     * belongs to.
     */
    for (i = 0; (unsigned)i < lenof(iso2022_subcharsets); i++) {
	subcs = &iso2022_subcharsets[i];
	if (!(mode->enable_mask & (1 << subcs->enable)))
	    continue;		       /* this charset is disabled */
	if (subcs->sbcs_base) {
	    c1 = sbcs_from_unicode(subcs->sbcs_base, input_chr);
	    c1 -= subcs->offset;
	    if (c1 >= 0x20 && c1 <= 0x7f) {
		c2 = 0;
		break;
	    }
	} else if (subcs->to_dbcs) {
	    if (subcs->to_dbcs_plane >= 0) {
		/*
		 * Since multiplanar DBCSes almost by definition
		 * involve several entries in iso2022_subcharsets
		 * with the same to_dbcs function and different
		 * plane values, we remember the last such function
		 * we called and what its result was, so that we
		 * don't (for example) have to call
		 * unicode_to_cns11643 seven times.
		 */
		if (last_planar_dbcs != REPLANARISE(subcs->to_dbcs)) {
		    last_planar_dbcs = REPLANARISE(subcs->to_dbcs);
		    if (!last_planar_dbcs(input_chr,
					  &last_p, &last_r, &last_c))
			last_p = -1;
		}
	    } else {
		last_p = subcs->to_dbcs_plane;
		if (!subcs->to_dbcs(input_chr, &last_r, &last_c))
		    last_p = 0;	       /* cannot match since to_dbcs_plane<0 */
	    }

	    if (last_p == subcs->to_dbcs_plane) {
		c1 = last_r - subcs->offset;
		c2 = last_c - subcs->offset;
		assert(c1 >= 0x20 && c1 <= 0x7f);
		assert(c2 >= 0x20 && c2 <= 0x7f);
		break;
	    }
	}
    }

    if ((unsigned)i < lenof(iso2022_subcharsets)) {
	int right;

	/*
	 * Our character is represented by c1 (and possibly also
	 * c2) in subcharset `subcs'. So now we must decide whether
	 * to designate that character set into G0/GL or G1/GR.
	 * 
	 * Any S6 or M6 subcharset has to go in GR because it won't
	 * fit in GL. In addition, the compound text rules state
	 * that any single-byte subcharset defined as the
	 * right-hand half of some SBCS must go in GR.
	 * 
	 * M4 subcharsets can go in either half according to the
	 * rules. I choose to put them in GR always because it's a
	 * simple policy with reasonable behaviour (facilitates
	 * switching between them and ASCII).
	 */
	right = (subcs->type == S6 || subcs->type == M6 || subcs->type == M4 ||
		 (subcs->sbcs_base && subcs->offset == 0x80));

	/*
	 * If we're in a DOCS mode, leave it.
	 */
	docs_char(state, emit, emitctx, -2, NULL, 0);

	/*
	 * If this subcharset is not already selected in that
	 * container, select it.
	 */
	oselect(state, i, right, emit, emitctx);

	/*
	 * Now emit the actual characters.
	 */
	if (right) {
	    assert(c1 >= 0x20 && c1 <= 0x7f);
	    emit(emitctx, c1 | 0x80);
	    if (c2) {
		assert(c2 >= 0x20 && c2 <= 0x7f);
		emit(emitctx, c2 | 0x80);
	    }
	} else {
	    assert(c1 > 0x20 && c1 < 0x7f);
	    emit(emitctx, c1);
	    if (c2) {
		assert(c2 > 0x20 && c2 < 0x7f);
		emit(emitctx, c2);
	    }
	}

	return TRUE;
    }

    /*
     * Fall back to DOCS.
     */
    {
	char data[10];
	char *p = data;
	int i, cs;

	cs = -2;		       /* means failure */

	for (i = 0; (unsigned)i <= lenof(ctext_encodings); i++) {
	    charset_state substate;
	    charset_spec const *subcs = ctext_encodings[i].subcs;

	    /*
	     * We assume that all character sets dealt with by DOCS
	     * are stateless for output purposes.
	     */
	    substate.s1 = substate.s0 = 0;
	    p = data;

	    if ((unsigned)i < lenof(ctext_encodings)) {
		if ((mode->enable_mask & (1 << ctext_encodings[i].enable)) &&
		    subcs->write(subcs, input_chr, &substate,
				 write_to_pointer, &p)) {
		    cs = i;
		    break;
		}
	    } else {
		if ((mode->enable_mask & (1 << CDU)) &&
		    write_utf8(NULL, input_chr, NULL, write_to_pointer, &p)) {
		    cs = -1;
		    break;
		}
	    }
	}

	if (cs != -2) {
	    docs_char(state, emit, emitctx, cs, data, p - data);
	    return TRUE;
	}
    }

    return FALSE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r13
movq %rcx, %r12
movq 0x18(%rdi), %r15
movq %rdx, (%rsp)
cmpq $0x0, 0x8(%rdx)
jne 0x7cb4
movl $0x80000000, %eax       # imm = 0x80000000
movq (%rsp), %rcx
movq $0x0, (%rcx)
movq %rax, 0x8(%rcx)
movb 0x4(%r15), %cl
movb 0x7(%r15), %dl
leaq 0x9f675(%rip), %rbx     # 0xa7292
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movb -0x2(%rbx), %r10b
cmpb %cl, %r10b
jne 0x7c5f
cmpb $0x0, 0x5(%r15)
jne 0x7c5f
movb (%rbx), %r11b
cmpb 0x6(%r15), %r11b
jne 0x7c5f
movl %eax, %r11d
shrl $0x11, %r11d
andl $0x7f, %r11d
cmpq %r11, %r9
je 0x7c5f
andq $-0xfe0001, %rax        # imm = 0xFF01FFFF
addq %r8, %rax
movq (%rsp), %r11
movq %rax, 0x8(%r11)
cmpb %dl, %r10b
jne 0x7c95
cmpb $0x0, 0x8(%r15)
jne 0x7c95
movb (%rbx), %r10b
cmpb 0x9(%r15), %r10b
jne 0x7c95
movl %eax, %r10d
shrl $0x18, %r10d
andl $0x7f, %r10d
cmpq %r10, %r9
je 0x7c95
andq $-0x7f000001, %rax      # imm = 0x80FFFFFF
addq %rdi, %rax
movq (%rsp), %r10
movq %rax, 0x8(%r10)
incq %r9
addq $0x28, %rbx
addq $0x20000, %r8           # imm = 0x20000
addq $0x1000000, %rdi        # imm = 0x1000000
cmpq $0x26, %r9
jne 0x7c25
cmpq $-0x1, %rsi
je 0x7cdf
cmpq $0x21, %rsi
setl %al
leaq -0x7f(%rsi), %rcx
cmpq $0x21, %rcx
setb %cl
orb %al, %cl
je 0x7d6d
movq %r13, %rdi
callq *%r12
jmp 0x7f06
xorl %ebx, %ebx
movq (%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x8316
leaq 0x9f590(%rip), %r14     # 0xa7292
movb -0x2(%r14), %bpl
cmpb 0x4(%r15), %bpl
jne 0x7d2f
cmpb $0x0, 0x5(%r15)
jne 0x7d2f
movb (%r14), %al
cmpb 0x6(%r15), %al
jne 0x7d2f
movq (%rsp), %rdi
movl %ebx, %esi
xorl %edx, %edx
movq %r12, %rcx
movq %r13, %r8
callq 0x8227
cmpb 0x7(%r15), %bpl
jne 0x7d5b
cmpb $0x0, 0x8(%r15)
jne 0x7d5b
movb (%r14), %al
cmpb 0x9(%r15), %al
jne 0x7d5b
movq (%rsp), %rdi
movl %ebx, %esi
movl $0x1, %edx
movq %r12, %rcx
movq %r13, %r8
callq 0x8227
incq %rbx
addq $0x28, %r14
cmpq $0x26, %rbx
jne 0x7d02
jmp 0x7f06
movq %rsi, 0x8(%rsp)
movq %r13, 0x28(%rsp)
movq %r12, 0x18(%rsp)
leaq 0x9f50d(%rip), %r14     # 0xa7290
xorl %r13d, %r13d
movq $0x0, 0x30(%rsp)
movl (%r15), %eax
movzbl 0x3(%r14), %ecx
btl %ecx, %eax
jae 0x7e3d
movq 0x8(%r14), %rbx
testq %rbx, %rbx
je 0x7dcf
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x948a
movq %rax, %rbp
movslq 0x4(%r14), %r12
subq %r12, %rbp
leaq -0x20(%rbp), %rax
cmpq $0x60, %rax
jae 0x7e3d
jmp 0x7f20
movq 0x18(%r14), %r12
testq %r12, %r12
je 0x7e3d
movl 0x20(%r14), %ebp
testl %ebp, %ebp
js 0x7e11
cmpq %r12, 0x30(%rsp)
je 0x7e33
movq 0x8(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0x24(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq *%r12
testl %eax, %eax
jne 0x7e0a
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movq %r12, 0x30(%rsp)
jmp 0x7e33
movl %ebp, 0x14(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x24(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq *%r12
testl %eax, %eax
jne 0x7e33
movl $0x0, 0x14(%rsp)
cmpl %ebp, 0x14(%rsp)
je 0x7fe7
incq %r13
addq $0x28, %r14
cmpq $0x26, %r13
jne 0x7d8f
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rax
movaps %xmm0, (%rax)
leaq 0x46(%rsp), %r12
leaq 0x38(%rsp), %rbp
movq %r12, (%rbp)
movl (%r15), %eax
leaq 0x9f2b7(%rip), %rdi     # 0xa7128
leaq 0x9fa70(%rip), %r13     # 0xa78e8
xorl %ebx, %ebx
leaq 0x81e(%rip), %r14       # 0x869f
testb $0x10, %al
je 0x7e9f
movq 0x8(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq %r14, %rcx
movq %rbp, %r8
callq *0x10(%rdi)
xorps %xmm0, %xmm0
testl %eax, %eax
jne 0x7ee4
incq %rbx
movq (%r13), %rdi
movaps %xmm0, 0x50(%rsp)
movq %r12, 0x38(%rsp)
movl (%r15), %eax
addq $0x18, %r13
cmpq $0x3, %rbx
jne 0x7e81
testb $0x20, %al
je 0x7f1c
leaq 0x7d7(%rip), %rcx       # 0x869f
leaq 0x38(%rsp), %r8
xorl %edi, %edi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x9e90
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x7f1c
movl 0x38(%rsp), %r9d
leaq 0x46(%rsp), %r8
subl %r8d, %r9d
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %rdx
movl %ebx, %ecx
callq 0x8316
movl $0x1, %ebx
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x7f0b
movb (%r14), %al
decb %al
cmpb $0x2, %al
ja 0x7f31
xorl %r12d, %r12d
jmp 0x8026
movq $0x0, 0x8(%rsp)
xorl %ebx, %ebx
cmpl $0x80, %r12d
sete %bl
movq (%rsp), %r14
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %r15
movq %r15, %rdx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x8316
movq %r14, %rdi
movl %r13d, %esi
movq 0x18(%rsp), %r14
movl %ebx, %edx
movq %r14, %rcx
movq %r15, %r8
callq 0x8227
cmpl $0x80, %r12d
movq 0x8(%rsp), %r12
je 0x8066
leaq -0x21(%rbp), %rax
cmpq $0x5e, %rax
jae 0x810e
movq %r15, %rdi
movq %rbp, %rsi
callq *%r14
movl $0x1, %ebx
testq %r12, %r12
je 0x7f0b
leaq -0x21(%r12), %rax
cmpq $0x5e, %rax
jb 0x8091
leaq 0x54c06(%rip), %rdi     # 0x5cbd5
leaq 0x54a17(%rip), %rsi     # 0x5c9ed
leaq 0x54b69(%rip), %rcx     # 0x5cb46
movl $0x47f, %edx            # imm = 0x47F
callq 0x6060
movl 0x24(%rsp), %eax
movl 0x4(%r14), %r12d
subl %r12d, %eax
leal -0x20(%rax), %ecx
cmpl $0x60, %ecx
jae 0x812d
movl 0x20(%rsp), %ecx
subl %r12d, %ecx
movq %rcx, 0x8(%rsp)
addl $-0x20, %ecx
cmpl $0x60, %ecx
jae 0x814c
movl %eax, %ebp
movb (%r14), %al
decb %al
cmpb $0x2, %al
ja 0x809f
movq 0x8(%rsp), %r12
movq (%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %r14
movq %r14, %rsi
movq 0x28(%rsp), %rbx
movq %rbx, %rdx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x8316
movq %r15, %rdi
movl %r13d, %esi
movq %rbx, %r15
movl $0x1, %edx
movq %r14, %rcx
movq %rbx, %r8
callq 0x8227
orq $0x80, %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq *%r14
movl $0x1, %ebx
testq %r12, %r12
je 0x7f0b
cmpl $0x1f, %r12d
jbe 0x80ef
orq $0x80, %r12
movq %r15, %rdi
movq %r12, %rsi
callq *%r14
jmp 0x7f0b
testq %rbx, %rbx
jne 0x7f3a
movq (%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %r14
movq %r14, %rsi
movq 0x28(%rsp), %rbx
movq %rbx, %rdx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x8316
movq %r15, %rdi
movl %r13d, %esi
movq %rbx, %r15
xorl %edx, %edx
movq %r14, %rcx
movq %rbx, %r8
callq 0x8227
movq 0x8(%rsp), %r12
jmp 0x7f94
leaq 0x54aaf(%rip), %rdi     # 0x5cba5
leaq 0x548f0(%rip), %rsi     # 0x5c9ed
leaq 0x54a42(%rip), %rcx     # 0x5cb46
movl $0x478, %edx            # imm = 0x478
callq 0x6060
leaq 0x54aa9(%rip), %rdi     # 0x5cbbe
leaq 0x548d1(%rip), %rsi     # 0x5c9ed
leaq 0x54a23(%rip), %rcx     # 0x5cb46
movl $0x47c, %edx            # imm = 0x47C
callq 0x6060
leaq 0x549f9(%rip), %rdi     # 0x5cb2d
leaq 0x548b2(%rip), %rsi     # 0x5c9ed
leaq 0x54a04(%rip), %rcx     # 0x5cb46
movl $0x44a, %edx            # imm = 0x44A
callq 0x6060
leaq 0x54a52(%rip), %rdi     # 0x5cba5
leaq 0x54893(%rip), %rsi     # 0x5c9ed
leaq 0x549e5(%rip), %rcx     # 0x5cb46
movl $0x44b, %edx            # imm = 0x44B
callq 0x6060