// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %r8, %r13
movq %rcx, 0x10(%rsp)
movq %rsi, %r12
movq 0x18(%rdi), %r14
cmpq $0x0, 0x8(%rdx)
jne 0x7d47
movq $0x0, (%rdx)
movl $0x80000000, %eax       # imm = 0x80000000
movq %rax, 0x8(%rdx)
movb 0x4(%r14), %al
movb 0x7(%r14), %cl
leaq 0x9f5e9(%rip), %rbx     # 0xa7292
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
movb -0x2(%rbx), %r9b
cmpb %al, %r9b
jne 0x7cee
movb -0x1(%rbx), %r10b
cmpb 0x5(%r14), %r10b
jne 0x7cee
movb (%rbx), %r10b
cmpb 0x6(%r14), %r10b
jne 0x7cee
movq 0x8(%rdx), %r10
movl %r10d, %r11d
shrl $0x11, %r11d
andl $0x7f, %r11d
cmpq %r11, %r8
je 0x7cee
andq $-0xfe0001, %r10        # imm = 0xFF01FFFF
addq %rdi, %r10
movq %r10, 0x8(%rdx)
cmpb %cl, %r9b
jne 0x7d28
movb -0x1(%rbx), %r9b
cmpb 0x8(%r14), %r9b
jne 0x7d28
movb (%rbx), %r9b
cmpb 0x9(%r14), %r9b
jne 0x7d28
movq 0x8(%rdx), %r9
movl %r9d, %r10d
shrl $0x18, %r10d
andl $0x7f, %r10d
cmpq %r10, %r8
je 0x7d28
andq $-0x7f000001, %r9       # imm = 0x80FFFFFF
addq %rsi, %r9
movq %r9, 0x8(%rdx)
incq %r8
addq $0x28, %rbx
addq $0x20000, %rdi          # imm = 0x20000
addq $0x1000000, %rsi        # imm = 0x1000000
cmpq $0x26, %r8
jne 0x7cb0
cmpq $-0x1, %r12
je 0x7d77
cmpq $0x21, %r12
setl %al
leaq -0x7f(%r12), %rcx
cmpq $0x21, %rcx
setb %cl
orb %al, %cl
je 0x7e22
movq %r13, %rdi
movq %r12, %rsi
callq *0x10(%rsp)
jmp 0x7e0c
xorl %ebx, %ebx
movq %rdx, %r12
movq %rdx, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x8424
leaq 0x9f4f4(%rip), %r15     # 0xa7292
movb -0x2(%r15), %bpl
cmpb 0x4(%r14), %bpl
jne 0x7dcf
movb -0x1(%r15), %al
cmpb 0x5(%r14), %al
jne 0x7dcf
movb (%r15), %al
cmpb 0x6(%r14), %al
jne 0x7dcf
movq %r12, %rdi
movl %ebx, %esi
xorl %edx, %edx
movq 0x10(%rsp), %rcx
movq %r13, %r8
callq 0x8322
cmpb 0x7(%r14), %bpl
jne 0x7dff
movb -0x1(%r15), %al
cmpb 0x8(%r14), %al
jne 0x7dff
movb (%r15), %al
cmpb 0x9(%r14), %al
jne 0x7dff
movq %r12, %rdi
movl %ebx, %esi
movl $0x1, %edx
movq 0x10(%rsp), %rcx
movq %r13, %r8
callq 0x8322
incq %rbx
addq $0x28, %r15
cmpq $0x26, %rbx
jne 0x7d9e
movl $0x1, %ebx
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, 0x30(%rsp)
movq %r13, 0x20(%rsp)
movb $0x1, %r15b
leaq 0x9f45a(%rip), %rax     # 0xa7290
movq $0x0, 0x28(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x48(%rsp)
movq %r12, 0x38(%rsp)
movq %rax, %rbx
movl (%r14), %eax
movzbl 0x3(%rbx), %ecx
btl %ecx, %eax
jae 0x7f18
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x7e97
movq %r12, %rsi
callq 0x95d2
movslq 0x4(%rbx), %rcx
movq %rax, %rbp
subq %rcx, %rbp
negq %rcx
addq %rcx, %rax
addq $-0x20, %rax
cmpq $0x60, %rax
jae 0x7f18
jmp 0x7f3d
movq 0x18(%rbx), %r12
testq %r12, %r12
je 0x7edc
movl 0x20(%rbx), %eax
testl %eax, %eax
js 0x7ee3
cmpq %r12, 0x48(%rsp)
je 0x7f0d
movl %eax, 0x8(%rsp)
movq 0x38(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq *%r12
testl %eax, %eax
jne 0x7ed5
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movq %r12, 0x48(%rsp)
jmp 0x7f09
movq 0x38(%rsp), %r12
jmp 0x7f18
movl %eax, 0x8(%rsp)
movl %eax, 0xc(%rsp)
movq 0x38(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq *%r12
testl %eax, %eax
jne 0x7f09
movl $0x0, 0xc(%rsp)
movl 0x8(%rsp), %eax
cmpl %eax, 0xc(%rsp)
movq 0x38(%rsp), %r12
je 0x7f48
cmpq $0x25, %r13
leaq 0x1(%r13), %rcx
setb %r15b
leaq 0x28(%rbx), %rax
movq %rcx, %r13
cmpq $0x26, %rcx
jne 0x7e50
movl $0x26, %r13d
jmp 0x7f79
movq $0x0, 0x28(%rsp)
jmp 0x7f79
movl 0x1c(%rsp), %eax
movslq 0x4(%rbx), %rcx
subl %ecx, %eax
leal -0x20(%rax), %edx
cmpl $0x60, %edx
jae 0x8236
movslq 0x18(%rsp), %rdx
subq %rcx, %rdx
movq %rdx, 0x28(%rsp)
leal -0x20(%rdx), %ecx
cmpl $0x5f, %ecx
ja 0x8255
movl %eax, %ebp
testb $0x1, %r15b
je 0x7fce
movb (%rbx), %al
decb %al
cmpb $0x3, %al
jae 0x80a7
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
movq 0x20(%rsp), %r12
movq %r12, %rdx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x8424
movq %rbx, %rdi
movl %r13d, %esi
movl $0x1, %edx
movq %r15, %rcx
movq %r12, %r8
callq 0x8322
jmp 0x8105
leaq 0x56(%rsp), %r15
leaq 0x40(%rsp), %rax
movq %r15, (%rax)
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
leaq 0x9f8e2(%rip), %r13     # 0xa78c9
xorl %ebx, %ebx
movq 0x7(%r13), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq %r15, 0x40(%rsp)
movl (%r14), %eax
cmpq $0x3, %rbx
jne 0x802c
testb $0x20, %al
je 0x8057
xorl %edi, %edi
movq %r12, %rsi
xorl %edx, %edx
leaq 0x7b0(%rip), %rcx       # 0x87c5
leaq 0x40(%rsp), %r8
callq 0xa014
testl %eax, %eax
je 0x8057
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movb $0x1, %al
jmp 0x8059
movzbl (%r13), %ecx
btl %ecx, %eax
jae 0x8057
movq %r12, %rsi
leaq 0x60(%rsp), %rdx
leaq 0x780(%rip), %rcx       # 0x87c5
leaq 0x40(%rsp), %r8
callq *0x10(%rdi)
testl %eax, %eax
je 0x8057
movb $0x1, %al
movl %ebx, %ebp
jmp 0x8059
xorl %eax, %eax
testb %al, %al
jne 0x8072
leaq 0x1(%rbx), %rax
addq $0x18, %r13
cmpq $0x3, %rbx
movq %rax, %rbx
jne 0x7fe9
cmpl $-0x2, %ebp
je 0x809a
movl 0x40(%rsp), %r9d
leaq 0x56(%rsp), %r8
subl %r8d, %r9d
movq 0x30(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdx
movl %ebp, %ecx
callq 0x8424
xorl %ebx, %ebx
cmpl $-0x2, %ebp
setne %bl
jmp 0x7e11
cmpq $0x0, 0x8(%rbx)
movq 0x30(%rsp), %rdi
je 0x8149
movl 0x4(%rbx), %r14d
xorl %ebx, %ebx
cmpl $0x80, %r14d
sete %bl
movq 0x10(%rsp), %r15
movq %r15, %rsi
movq 0x20(%rsp), %r12
movq %r12, %rdx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x8424
movq 0x30(%rsp), %rdi
movl %r13d, %esi
movl %ebx, %edx
movq %r15, %rcx
movq %r12, %r8
callq 0x8322
cmpl $0x80, %r14d
jne 0x817f
leaq -0x20(%rbp), %rax
cmpq $0x60, %rax
jae 0x81ba
orq $0x80, %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq *%r15
movl $0x1, %ebx
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x7e11
cmpq $0x1f, %rsi
jle 0x81d9
orq $0x80, %rsi
jmp 0x81af
movq %rdi, %rbx
movq 0x10(%rsp), %r15
movq %r15, %rsi
movq 0x20(%rsp), %r12
movq %r12, %rdx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x8424
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
movq %r15, %rcx
movq %r12, %r8
callq 0x8322
leaq -0x21(%rbp), %rax
cmpq $0x5e, %rax
jae 0x81f8
movq %r12, %rdi
movq %rbp, %rsi
callq *%r15
movl $0x1, %ebx
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x7e11
leaq -0x21(%rsi), %rax
cmpq $0x5e, %rax
jae 0x8217
movq %r12, %rdi
callq *%r15
jmp 0x7e11
leaq 0x54988(%rip), %rdi     # 0x5cb49
leaq 0x54841(%rip), %rsi     # 0x5ca09
leaq 0x54993(%rip), %rcx     # 0x5cb62
movl $0x475, %edx            # imm = 0x475
callq 0x6060
leaq 0x549e1(%rip), %rdi     # 0x5cbc1
leaq 0x54822(%rip), %rsi     # 0x5ca09
leaq 0x54974(%rip), %rcx     # 0x5cb62
movl $0x478, %edx            # imm = 0x478
callq 0x6060
leaq 0x549db(%rip), %rdi     # 0x5cbda
leaq 0x54803(%rip), %rsi     # 0x5ca09
leaq 0x54955(%rip), %rcx     # 0x5cb62
movl $0x47c, %edx            # imm = 0x47C
callq 0x6060
leaq 0x549d3(%rip), %rdi     # 0x5cbf1
leaq 0x547e4(%rip), %rsi     # 0x5ca09
leaq 0x54936(%rip), %rcx     # 0x5cb62
movl $0x47f, %edx            # imm = 0x47F
callq 0x6060
leaq 0x5490c(%rip), %rdi     # 0x5cb49
leaq 0x547c5(%rip), %rsi     # 0x5ca09
leaq 0x54917(%rip), %rcx     # 0x5cb62
movl $0x44a, %edx            # imm = 0x44A
callq 0x6060
leaq 0x54965(%rip), %rdi     # 0x5cbc1
leaq 0x547a6(%rip), %rsi     # 0x5ca09
leaq 0x548f8(%rip), %rcx     # 0x5cb62
movl $0x44b, %edx            # imm = 0x44B
callq 0x6060