// File: /workspace/github_c_makefile_star2_O2/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, 0x8(%rsp)
movq %rcx, %r13
movq %rsi, (%rsp)
movq 0x18(%rdi), %r14
movq %rdx, 0x10(%rsp)
cmpq $0x0, 0x8(%rdx)
je 0x7afc
movq (%rsp), %rsi
cmpq $-0x1, %rsi
je 0x7b7f
cmpq $0x21, %rsi
setl %al
leaq -0x7f(%rsi), %rcx
cmpq $0x21, %rcx
setb %cl
orb %al, %cl
je 0x7c15
movq 0x8(%rsp), %rdi
callq *%r13
pushq $0x1
popq %rbp
jmp 0x7f11
movq 0x10(%rsp), %rcx
andq $0x0, (%rcx)
movl $0x80000000, %eax       # imm = 0x80000000
movq %rax, 0x8(%rcx)
leaq 0x9e77d(%rip), %r15     # 0xa6292
xorl %ebx, %ebx
pushq $0x1
popq %rbp
cmpq $0x26, %rbx
je 0x7ac4
movb -0x2(%r15), %r12b
cmpb 0x4(%r14), %r12b
jne 0x7b4d
cmpb $0x0, 0x5(%r14)
jne 0x7b4d
movb (%r15), %al
cmpb 0x6(%r14), %al
jne 0x7b4d
movq 0x10(%rsp), %rdi
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x80f0
cmpb 0x7(%r14), %r12b
jne 0x7b76
cmpb $0x0, 0x8(%r14)
jne 0x7b76
movb (%r15), %al
cmpb 0x9(%r14), %al
jne 0x7b76
movq 0x10(%rsp), %rdi
movl %ebx, %esi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x80f0
incq %rbx
addq $0x28, %r15
jmp 0x7b1a
leaq 0x9e70c(%rip), %r15     # 0xa6292
xorl %ebx, %ebx
pushq $-0x2
popq %rcx
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x81ce
pushq $0x1
popq %rbp
cmpq $0x26, %rbx
je 0x7f11
movb -0x2(%r15), %r12b
cmpb 0x4(%r14), %r12b
jne 0x7be0
cmpb $0x0, 0x5(%r14)
jne 0x7be0
movb (%r15), %al
cmpb 0x6(%r14), %al
jne 0x7be0
movq 0x10(%rsp), %rdi
movl %ebx, %esi
xorl %edx, %edx
movq %r13, %rcx
movq 0x8(%rsp), %r8
callq 0x80f0
cmpb 0x7(%r14), %r12b
jne 0x7c0c
cmpb $0x0, 0x8(%r14)
jne 0x7c0c
movb (%r15), %al
cmpb 0x9(%r14), %al
jne 0x7c0c
movq 0x10(%rsp), %rdi
movl %ebx, %esi
movl %ebp, %edx
movq %r13, %rcx
movq 0x8(%rsp), %r8
callq 0x80f0
incq %rbx
addq $0x28, %r15
jmp 0x7ba6
movq %r13, 0x28(%rsp)
leaq 0x9e66f(%rip), %r15     # 0xa6290
xorl %r13d, %r13d
movq $0x0, 0x30(%rsp)
cmpq $0x26, %r13
je 0x7ce8
movl (%r14), %eax
movzbl 0x3(%r15), %ecx
btl %ecx, %eax
jae 0x7cdc
movq 0x8(%r15), %rbx
testq %rbx, %rbx
je 0x7c76
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x9236
movq %rax, %rbp
movslq 0x4(%r15), %r12
subq %r12, %rbp
leaq -0x20(%rbp), %rax
cmpq $0x60, %rax
jae 0x7cdc
jmp 0x7d77
movq 0x18(%r15), %r12
testq %r12, %r12
je 0x7cdc
movl 0x20(%r15), %ebp
testl %ebp, %ebp
js 0x7cb4
cmpq %r12, 0x30(%rsp)
je 0x7cd2
movq (%rsp), %rdi
leaq 0x1c(%rsp), %rsi
leaq 0x24(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq *%r12
testl %eax, %eax
jne 0x7cad
orl $-0x1, 0x1c(%rsp)
movq %r12, 0x30(%rsp)
jmp 0x7cd2
movl %ebp, 0x1c(%rsp)
movq (%rsp), %rdi
leaq 0x24(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq *%r12
testl %eax, %eax
jne 0x7cd2
andl $0x0, 0x1c(%rsp)
cmpl %ebp, 0x1c(%rsp)
je 0x7e6c
incq %r13
addq $0x28, %r15
jmp 0x7c2d
leaq 0x9ebe1(%rip), %r12     # 0xa68d0
xorl %r15d, %r15d
xorps %xmm0, %xmm0
leaq 0x46(%rsp), %rbx
leaq 0x843(%rip), %r13       # 0x8544
leaq 0x38(%rsp), %rbp
cmpq $0x4, %r15
je 0x7d70
movq (%r12), %rdi
movaps %xmm0, 0x50(%rsp)
movq %rbx, 0x38(%rsp)
movl (%r14), %eax
cmpq $0x3, %r15
jne 0x7d46
testb $0x20, %al
je 0x7d67
xorl %edi, %edi
movq (%rsp), %rsi
xorl %edx, %edx
movq %r13, %rcx
movq %rbp, %r8
callq 0x9bbf
xorps %xmm0, %xmm0
testl %eax, %eax
je 0x7d67
jmp 0x7e43
testb $0x10, %al
je 0x7d67
movq (%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq %r13, %rcx
movq %rbp, %r8
callq *0x10(%rdi)
xorps %xmm0, %xmm0
testl %eax, %eax
jne 0x7e3e
incq %r15
addq $0x18, %r12
jmp 0x7d06
xorl %ebp, %ebp
jmp 0x7f11
movb (%r15), %al
decb %al
cmpb $0x2, %al
ja 0x7d8d
xorl %r12d, %r12d
movq 0x8(%rsp), %rbx
jmp 0x7eae
movq $0x0, (%rsp)
xorl %ebx, %ebx
cmpl $0x80, %r12d
sete %bl
pushq $-0x2
popq %rcx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
movq 0x8(%rsp), %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x81ce
movq %r14, %rdi
movl %r13d, %esi
movl %ebx, %edx
movq 0x8(%rsp), %rbx
movq %r15, %rcx
movq %rbx, %r8
callq 0x80f0
cmpl $0x80, %r12d
movq (%rsp), %r12
je 0x7ee3
leaq -0x21(%rbp), %rax
cmpq $0x5e, %rax
jae 0x7f8c
movq %rbx, %rdi
movq %rbp, %rsi
callq *%r15
pushq $0x1
popq %rbp
testq %r12, %r12
je 0x7f11
leaq -0x21(%r12), %rax
cmpq $0x5e, %rax
jb 0x7f08
leaq 0x53daf(%rip), %rdi     # 0x5bbd5
leaq 0x53bc0(%rip), %rsi     # 0x5b9ed
leaq 0x53d12(%rip), %rcx     # 0x5bb46
movl $0x47f, %edx            # imm = 0x47F
callq 0x6060
movl %r15d, %ecx
jmp 0x7e46
pushq $-0x1
popq %rcx
movl 0x38(%rsp), %r9d
leaq 0x46(%rsp), %r8
subl %r8d, %r9d
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x81ce
jmp 0x7af4
movl 0x24(%rsp), %eax
movl 0x4(%r15), %r12d
subl %r12d, %eax
leal -0x20(%rax), %ecx
cmpl $0x60, %ecx
jae 0x7fab
movl 0x20(%rsp), %ecx
subl %r12d, %ecx
movq %rcx, (%rsp)
addl $-0x20, %ecx
cmpl $0x60, %ecx
jae 0x7fca
movl %eax, %ebp
movb (%r15), %al
decb %al
cmpb $0x2, %al
ja 0x7f22
movq 0x8(%rsp), %rbx
movq (%rsp), %r12
pushq $-0x2
popq %rcx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
movq %rbx, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x81ce
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %r13d, %esi
movq %r15, %rcx
movq %rbx, %r8
callq 0x80f0
orq $0x80, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq *%r15
pushq $0x1
popq %rbp
testq %r12, %r12
je 0x7f11
cmpl $0x1f, %r12d
jbe 0x7f6d
orq $0x80, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq *%r15
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rbx, %rbx
jne 0x7d95
pushq $-0x2
popq %rcx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x81ce
movq %r14, %rdi
movl %r13d, %esi
xorl %edx, %edx
movq %r15, %rcx
movq %rbx, %r8
callq 0x80f0
movq (%rsp), %r12
jmp 0x7ded
leaq 0x53c31(%rip), %rdi     # 0x5bba5
leaq 0x53a72(%rip), %rsi     # 0x5b9ed
leaq 0x53bc4(%rip), %rcx     # 0x5bb46
movl $0x478, %edx            # imm = 0x478
callq 0x6060
leaq 0x53c2b(%rip), %rdi     # 0x5bbbe
leaq 0x53a53(%rip), %rsi     # 0x5b9ed
leaq 0x53ba5(%rip), %rcx     # 0x5bb46
movl $0x47c, %edx            # imm = 0x47C
callq 0x6060
leaq 0x53b7b(%rip), %rdi     # 0x5bb2d
leaq 0x53a34(%rip), %rsi     # 0x5b9ed
leaq 0x53b86(%rip), %rcx     # 0x5bb46
movl $0x44a, %edx            # imm = 0x44A
callq 0x6060
leaq 0x53bd4(%rip), %rdi     # 0x5bba5
leaq 0x53a15(%rip), %rsi     # 0x5b9ed
leaq 0x53b67(%rip), %rcx     # 0x5bb46
movl $0x44b, %edx            # imm = 0x44B
callq 0x6060