// File: /workspace/github_c_makefile_star2_O3/mloar[P]charset/iso2022s.c
// Function: read_iso2022s
static void read_iso2022s(charset_spec const *charset, long int input_chr,
			  charset_state *state,
			  void (*emit)(void *ctx, long int output),
			  void *emitctx)
{
    struct iso2022 const *iso = (struct iso2022 *)charset->data;

    /*
     * For reading ISO-2022 subsets, we divide up our state
     * variables as follows:
     * 
     * 	- The top byte of s0 (bits 31:24) indicates, if nonzero,
     * 	  that we are part-way through a recognised ISO-2022 escape
     * 	  sequence. Five of those bits (31:27) give the index of
     * 	  the first member of the escapes list matching what we
     * 	  have so far; the remaining three (26:24) give the number
     * 	  of characters we have seen so far.
     * 
     * 	- The top bit of s1 (bit 31) is non-zero at all times, to
     * 	  indicate that we have performed any necessary
     * 	  initialisation. When we start, we detect a zero s1 and
     * 	  respond to it by initialising the default container
     * 	  contents.
     * 
     * 	- The next three bits of s1 (bits 30:28) indicate which
     * 	  _container_ is currently selected. This isn't quite as
     * 	  simple as it sounds, since we have to preserve memory of
     * 	  which of the SI/SO containers we came from when we're
     * 	  temporarily in SS2/SS3. Hence, what happens is:
     *     + bit 28 indicates SI/SO.
     * 	   + if we're in an SS2/SS3 container, that's indicated by
     * 	     the two bits above that being nonzero and holding
     * 	     either 2 or 3.
     * 	   + Hence: 0 is SI, 1 is SO, 4 is SS2-from-SI, 5 is
     * 	     SS2-from-SO, 6 is SS3-from-SI, 7 is SS3-from-SO.
     * 	   + For added fun: in an _8-bit_ ISO 2022 subset, we have
     * 	     the further special value 2, which means that we're
     * 	     theoretically in SI but the current character being
     * 	     accumulated is composed of 8-bit characters and will
     * 	     therefore be interpreted as if in SO.
     * 
     * 	- The next nibble of s1 (27:24) indicates how many bytes
     * 	  have been accumulated in the current character.
     * 
     * 	- The remaining three bytes of s1 are divided into four
     * 	  six-bit sections, and each section gives the current
     * 	  sub-charset selected in one of the possible containers.
     * 	  (Those containers are SI, SO, SS2 and SS3, respectively
     * 	  and in order from the bottom of s0 to the top.)
     * 
     * 	- The bottom 24 bits of s0 give the accumulated character
     * 	  data so far.
     * 
     * (Note that this means s1 contains all the parts of the state
     * which might need to be operated on by escape sequences.
     * Cunning, eh?)
     */

    if (!(state->s1 & 0x80000000)) {
	state->s1 = iso->s1;
    }

    /*
     * So. Firstly, we process escape sequences, if we're in the
     * middle of one or if we see a possible introducer (SI, SO,
     * ESC).
     */
    if ((state->s0 >> 24) ||
	(input_chr == SO || input_chr == SI || input_chr == ESC)) {
	int n = (state->s0 >> 24) & 7, i = (state->s0 >> 27), oi = i, j;

	/*
	 * If this is the start of an escape sequence, we might be
	 * in mid-character. If so, clear the character state and
	 * emit an error token for the incomplete character.
	 */
	if (state->s1 & 0x0F000000) {
	    state->s1 &= ~0x0F000000;
	    state->s0 &= 0xFF000000;
	    /*
	     * If we were in the SS2 or SS3 container, we
	     * automatically exit it.
	     */
	    if (state->s1 & 0x60000000)
		state->s1 &= 0x9FFFFFFF;
	    emit(emitctx, ERROR);
	}

	j = i;
	while (j < iso->nescapes &&
	       !memcmp(iso->escapes[j].sequence,
		       iso->escapes[oi].sequence, n)) {
	    if (iso->escapes[j].sequence[n] < input_chr)
		i = ++j;
	    else
		break;
	}
	if (i >= iso->nescapes ||
	    memcmp(iso->escapes[i].sequence,
		   iso->escapes[oi].sequence, n) ||
	    iso->escapes[i].sequence[n] != input_chr) {
	    /*
	     * This character does not appear in any valid escape
	     * sequence. Therefore, we must emit all the characters
	     * we had previously swallowed, plus this one, and
	     * return to non-escape-sequence state.
	     */
	    for (j = 0; j < n; j++)
		emit(emitctx, iso->escapes[oi].sequence[j]);
	    emit(emitctx, input_chr);
	    state->s0 = 0;
	    return;
	}

	/*
	 * Otherwise, we have found an additional character in our
	 * escape sequence. See if we have reached the _end_ of our
	 * sequence (and therefore must process the sequence).
	 */
	n++;
	if (!iso->escapes[i].sequence[n]) {
	    state->s0 = 0;
	    state->s1 &= iso->escapes[i].andbits;
	    state->s1 ^= iso->escapes[i].xorbits;
	    return;
	}

	/*
	 * Failing _that_, we simply update our escape-sequence-
	 * tracking state.
	 */
	assert(i < 32 && n < 8);
	state->s0 = (i << 27) | (n << 24);
	return;
    }

    /*
     * If this isn't an escape sequence, it must be part of a
     * character. One possibility is that it's a control character
     * (00-20 or 7F-9F; also in non-8-bit ISO 2022 subsets I'm
     * going to treat all top-half characters as controls), in
     * which case we output it verbatim.
     */
    if (input_chr < 0x21 ||
	(input_chr > 0x7E && (!iso->eightbit || input_chr < 0xA0))) {
	/*
	 * We might be in mid-multibyte-character. If so, clear the
	 * character state and emit an error token for the
	 * incomplete character.
	 */
	if (state->s1 & 0x0F000000) {
	    state->s1 &= ~0x0F000000;
	    state->s0 &= 0xFF000000;
	    emit(emitctx, ERROR);
	    /*
	     * If we were in the SS2 or SS3 container, we
	     * automatically exit it.
	     */
	    if (state->s1 & 0x60000000)
		state->s1 &= 0x9FFFFFFF;
	}

	emit(emitctx, input_chr);
	return;
    }

    /*
     * Otherwise, accumulate character data.
     */
    {
	unsigned long chr;
	int chrlen, cont, subcharset, bytes;

	/*
	 * Verify that we've seen the right kind of character for
	 * what we're currently doing. This only matters in 8-bit
	 * subsets.
	 */
	if (iso->eightbit) {
	    cont = (state->s1 >> 28) & 7;
	    /*
	     * If cont==0, we're entitled to see either GL or GR
	     * characters. If cont==2, we expect only GR; otherwise
	     * we expect only GL.
	     * 
	     * If we see a GR character while cont==0, we set
	     * cont=2 immediately.
	     */
	    if ((cont == 2 && !(input_chr & 0x80)) ||
		(cont != 0 && cont != 2 && (input_chr & 0x80))) {
		/*
		 * Clear the previous character; it was prematurely
		 * terminated by this error.
		 */
		state->s1 &= ~0x0F000000;
		state->s0 &= 0xFF000000;
		emit(emitctx, ERROR);
		/*
		 * If we were in the SS2 or SS3 container, we
		 * automatically exit it.
		 */
		if (state->s1 & 0x60000000)
		    state->s1 &= 0x9FFFFFFF;
	    }

	    if (cont == 0 && (input_chr & 0x80)) {
		state->s1 |= 0x20000000;
	    }
	}

	/* The current character and its length. */
	chr = ((state->s0 & 0x00FFFFFF) << 8) | (input_chr & 0x7F);
	chrlen = ((state->s1 >> 24) & 0xF) + 1;
	/* The current sub-charset. */
	cont = (state->s1 >> 28) & 7;
	if (cont > 1) cont >>= 1;
	subcharset = (state->s1 >> (6*cont)) & 0x3F;
	/* The number of bytes-per-character in that sub-charset. */
	bytes = iso->nbytes[subcharset];

	/*
	 * If this character is now complete, we convert and emit
	 * it. Otherwise, we simply update the state and return.
	 */
	if (chrlen >= bytes) {
	    emit(emitctx, iso->to_ucs(subcharset, chr));
	    chr = chrlen = 0;
	    /*
	     * If we were in the SS2 or SS3 container, we
	     * automatically exit it.
	     */
	    if (state->s1 & 0x60000000)
		state->s1 &= 0x9FFFFFFF;
	}
	state->s0 = (state->s0 & 0xFF000000) | chr;
	state->s1 = (state->s1 & 0xF0FFFFFF) | (chrlen << 24);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %r13
movq %rcx, %r8
movq %rdx, %r14
movq %rsi, %r15
movq 0x18(%rdi), %rbp
movq 0x8(%rdx), %rax
testl %eax, %eax
js 0x86de
movq 0x20(%rbp), %rax
movq %rax, 0x8(%r14)
movq (%r14), %rcx
cmpq $0xffffff, %rcx         # imm = 0xFFFFFF
ja 0x8703
cmpq $0x1b, %r15
ja 0x88d3
movl $0x800c000, %edx        # imm = 0x800C000
btq %r15, %rdx
jae 0x88d3
movq %r8, 0x28(%rsp)
movl %ecx, %r12d
shrl $0x18, %r12d
movq %rcx, %rbx
shrq $0x1b, %rbx
testl $0xf000000, %eax        # imm = 0xF000000
je 0x8753
movq %rax, %rdx
andq $-0xf000001, %rdx       # imm = 0xF0FFFFFF
movq %rdx, 0x8(%r14)
andl $0xff000000, %ecx       # imm = 0xFF000000
movq %rcx, (%r14)
testl $0x60000000, %eax       # imm = 0x60000000
movq 0x28(%rsp), %rcx
je 0x8749
andl $0x90ffffff, %eax       # imm = 0x90FFFFFF
movq %rax, 0x8(%r14)
movl $0xffff, %esi           # imm = 0xFFFF
movq %r13, %rdi
callq *%rcx
movq %r13, 0x38(%rsp)
movq %r15, 0x8(%rsp)
movq %r14, (%rsp)
andl $0x7, %r12d
movq %rbp, 0x20(%rsp)
movslq 0x8(%rbp), %rax
movq %rbx, 0x18(%rsp)
movq %rax, 0x40(%rsp)
cmpl %ebx, %eax
jle 0x886d
movq 0x20(%rsp), %rax
movq (%rax), %rdx
movq 0x18(%rsp), %rcx
movq %rcx, %r14
shlq $0x20, %r14
movq %r14, %rax
sarq $0x1b, %rax
movq (%rdx,%rax), %rax
movq %rax, 0x30(%rsp)
movslq %ecx, %r13
movl %ecx, %r15d
shll $0x1b, %r15d
movq %r13, %rbx
shlq $0x5, %rbx
movq %rdx, 0x10(%rsp)
addq %rdx, %rbx
movq (%rbx), %rbp
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
movq %r12, %rdx
callq 0x6070
testl %eax, %eax
jne 0x8805
movsbq (%rbp,%r12), %rax
cmpq 0x8(%rsp), %rax
jge 0x8805
incq %r13
addl $0x8000000, %r15d       # imm = 0x8000000
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r14
addq $0x20, %rbx
cmpq %r13, 0x40(%rsp)
jne 0x87bd
jmp 0x886d
movq %r14, %rax
sarq $0x1b, %rax
movq 0x10(%rsp), %rcx
movq (%rcx,%rax), %rbp
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
movq %r12, %rdx
callq 0x6070
testl %eax, %eax
jne 0x886d
movsbq (%rbp,%r12), %rax
cmpq 0x8(%rsp), %rax
jne 0x886d
cmpb $0x0, 0x1(%rbp,%r12)
je 0x8a4c
cmpl $0x7, %r12d
je 0x8a7b
cmpq $0x20, %r13
jge 0x8a7b
leaq 0x1(%r12), %rax
shll $0x18, %eax
orl %eax, %r15d
movslq %r15d, %rax
movq (%rsp), %rcx
movq %rax, (%rcx)
jmp 0x88c4
testl %r12d, %r12d
movq 0x28(%rsp), %r13
movq 0x38(%rsp), %rbp
movq %r12, %rax
movq 0x20(%rsp), %r12
je 0x88ae
movslq 0x18(%rsp), %rbx
shlq $0x5, %rbx
movl %eax, %r14d
xorl %r15d, %r15d
movq (%r12), %rax
movq (%rax,%rbx), %rax
movsbq (%rax,%r15), %rsi
movq %rbp, %rdi
callq *%r13
incq %r15
cmpq %r15, %r14
jne 0x8893
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq *%r13
movq (%rsp), %rax
movq $0x0, (%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x21, %r15
jge 0x892d
testl $0xf000000, %eax        # imm = 0xF000000
je 0x8916
andq $-0xf000001, %rax       # imm = 0xF0FFFFFF
movq %rax, 0x8(%r14)
movq $0x0, (%r14)
movl $0xffff, %esi           # imm = 0xFFFF
movq %r13, %rdi
movq %r8, %rbx
callq *%r8
movq %rbx, %r8
movq 0x8(%r14), %rax
testl $0x60000000, %eax       # imm = 0x60000000
je 0x8916
andl $0x9fffffff, %eax       # imm = 0x9FFFFFFF
movq %rax, 0x8(%r14)
movq %r13, %rdi
movq %r15, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%r8
movl 0x30(%rbp), %ecx
cmpq $0x7f, %r15
jb 0x894b
testl %ecx, %ecx
setne %cl
cmpq $0xa0, %r15
setae %dl
testb %cl, %dl
je 0x88d9
jmp 0x894f
testl %ecx, %ecx
je 0x89b9
movl %eax, %ebx
shrl $0x1c, %ebx
andl $0x7, %ebx
testb %r15b, %r15b
js 0x8961
cmpl $0x2, %ebx
je 0x8970
testb %r15b, %r15b
jns 0x89a6
movl %eax, %ecx
andl $0x50000000, %ecx       # imm = 0x50000000
je 0x89a6
andq $-0xf000001, %rax       # imm = 0xF0FFFFFF
movq %rax, 0x8(%r14)
movq $0x0, (%r14)
movl $0xffff, %esi           # imm = 0xFFFF
movq %r13, %rdi
movq %r8, %r12
callq *%r8
movq 0x8(%r14), %rax
testl $0x60000000, %eax       # imm = 0x60000000
je 0x89a3
andl $0x9fffffff, %eax       # imm = 0x9FFFFFFF
movq %rax, 0x8(%r14)
movq %r12, %r8
testb %r15b, %r15b
jns 0x89b9
testl %ebx, %ebx
jne 0x89b9
orq $0x20000000, %rax       # imm = 0x20000000
movq %rax, 0x8(%r14)
movl (%r14), %ecx
shll $0x8, %ecx
andl $0x7f, %r15d
orq %rcx, %r15
movl %eax, %edx
shrl $0x18, %edx
andl $0xf, %edx
incl %edx
movl %eax, %esi
shrl $0x1c, %esi
andl $0x7, %esi
cmpl $0x2, %esi
setae %cl
shrl %cl, %esi
addl %esi, %esi
leal (%rsi,%rsi,2), %ecx
movq %rax, %rdi
shrq %cl, %rdi
movq 0x10(%rbp), %rcx
movl %edi, %esi
andl $0x3f, %esi
movsbl (%rcx,%rsi), %ecx
cmpl %ecx, %edx
jl 0x8a2a
andl $0x3f, %edi
movq %r15, %rsi
movq %r8, %rbx
callq *0x38(%rbp)
movq %r13, %rdi
movq %rax, %rsi
callq *%rbx
movq 0x8(%r14), %rcx
movl %ecx, %eax
andl $0x9fffffff, %eax       # imm = 0x9FFFFFFF
testl $0x60000000, %ecx       # imm = 0x60000000
cmoveq %rcx, %rax
xorl %r15d, %r15d
xorl %edx, %edx
movzbl 0x3(%r14), %ecx
shll $0x18, %ecx
orq %r15, %rcx
movq %rcx, (%r14)
andl $0xf0ffffff, %eax       # imm = 0xF0FFFFFF
shll $0x18, %edx
orq %rax, %rdx
movq %rdx, 0x8(%r14)
jmp 0x88c4
sarq $0x20, %r14
movq (%rsp), %rcx
movq $0x0, (%rcx)
shlq $0x5, %r14
movq 0x8(%rcx), %rax
movq 0x10(%rsp), %rdx
andq 0x8(%rdx,%r14), %rax
xorq 0x10(%rdx,%r14), %rax
movq %rax, 0x8(%rcx)
jmp 0x88c4
leaq 0x54264(%rip), %rdi     # 0x5cce6
leaq 0x5426d(%rip), %rsi     # 0x5ccf6
leaq 0x54271(%rip), %rcx     # 0x5cd01
movl $0xdd, %edx
callq 0x6060