// File: /workspace/github_c_makefile_star2_O1/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, %rbp
movq %rcx, %r8
movq %rdx, %r15
movq %rsi, %r12
movq 0x18(%rdi), %r14
testb $-0x80, 0xb(%rdx)
jne 0x8800
movq 0x20(%r14), %rax
movq %rax, 0x8(%r15)
movq (%r15), %rax
cmpq $0xffffff, %rax         # imm = 0xFFFFFF
ja 0x8824
cmpq $0x1b, %r12
ja 0x8a37
movl $0x800c000, %ecx        # imm = 0x800C000
btq %r12, %rcx
jae 0x8a37
movq %r8, 0x28(%rsp)
movl %eax, %edx
shrl $0x18, %edx
movq %rax, %r13
shrq $0x1b, %r13
movq 0x8(%r15), %rcx
testl $0xf000000, %ecx        # imm = 0xF000000
je 0x887e
movq %rdx, %rbx
movq %rcx, %rdx
andq $-0xf000001, %rdx       # imm = 0xF0FFFFFF
movq %rdx, 0x8(%r15)
andl $0xff000000, %eax       # imm = 0xFF000000
movq %rax, (%r15)
testl $0x60000000, %ecx       # imm = 0x60000000
movq 0x28(%rsp), %rax
je 0x8871
andl $0x90ffffff, %ecx       # imm = 0x90FFFFFF
movq %rcx, 0x8(%r15)
movl $0xffff, %esi           # imm = 0xFFFF
movq %rbp, %rdi
callq *%rax
movq %rbx, %rdx
movq %rbp, 0x38(%rsp)
movq %r12, 0x18(%rsp)
movq %r15, 0x10(%rsp)
andl $0x7, %edx
movslq 0x8(%r14), %rax
movq %r13, 0x8(%rsp)
movq %rax, 0x30(%rsp)
cmpl %r13d, %eax
setg %r15b
jle 0x89da
movq (%r14), %r12
movslq 0x8(%rsp), %rbx
movq %rbx, %r13
shlq $0x5, %r13
movq (%r12,%r13), %rbp
movq %rbp, %rdi
movq %rbp, %rsi
movq %rdx, (%rsp)
callq 0x6080
testl %eax, %eax
je 0x896a
movq 0x8(%rsp), %rax
movl %eax, %r13d
movq (%rsp), %rdx
testb $0x1, %r15b
je 0x89da
movq (%r14), %r15
movslq %ebx, %r12
shlq $0x5, %r12
movq (%r15,%r12), %rbp
movslq 0x8(%rsp), %rax
shlq $0x5, %rax
movq (%r15,%rax), %rsi
movq %rbp, %rdi
movq (%rsp), %rdx
callq 0x6080
movq (%rsp), %rdx
testl %eax, %eax
jne 0x89da
movsbq (%rbp,%rdx), %rax
cmpq 0x18(%rsp), %rax
jne 0x89da
cmpb $0x0, 0x1(%rbp,%rdx)
je 0x8bb7
cmpl $0x7, %edx
je 0x8bde
cmpq $0x20, %rbx
jge 0x8bde
leaq 0x1(%rdx), %rax
shll $0x1b, %r13d
shll $0x18, %eax
orl %r13d, %eax
cltq
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
jmp 0x8a28
addq %r13, %r12
addq $0x20, %r12
movq 0x30(%rsp), %rax
decl %eax
movl %eax, 0x24(%rsp)
movq 0x8(%rsp), %rax
movl %eax, %r13d
movq %rbp, 0x40(%rsp)
movq (%rsp), %rdx
movsbq (%rbp,%rdx), %rax
cmpq 0x18(%rsp), %rax
jge 0x88e1
cmpl %r13d, 0x24(%rsp)
je 0x89da
incq %rbx
cmpq 0x30(%rsp), %rbx
setl %r15b
movq (%r12), %rbp
movq %rbp, %rdi
movq 0x40(%rsp), %rsi
movq (%rsp), %rdx
callq 0x6080
movq (%rsp), %rdx
incl %r13d
addq $0x20, %r12
testl %eax, %eax
je 0x898d
jmp 0x88e1
testl %edx, %edx
movq 0x28(%rsp), %r13
movq 0x38(%rsp), %rbp
je 0x8a11
movslq 0x8(%rsp), %rbx
shlq $0x5, %rbx
movl %edx, %r15d
xorl %r12d, %r12d
movq (%r14), %rax
movq (%rax,%rbx), %rax
movsbq (%rax,%r12), %rsi
movq %rbp, %rdi
callq *%r13
incq %r12
cmpq %r12, %r15
jne 0x89f7
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
callq *%r13
movq 0x10(%rsp), %rax
movq $0x0, (%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x21, %r12
jge 0x8a95
movq 0x8(%r15), %rax
testl $0xf000000, %eax        # imm = 0xF000000
je 0x8a7e
andq $-0xf000001, %rax       # imm = 0xF0FFFFFF
movq %rax, 0x8(%r15)
movq $0x0, (%r15)
movl $0xffff, %esi           # imm = 0xFFFF
movq %rbp, %rdi
movq %r8, %rbx
callq *%r8
movq %rbx, %r8
movq 0x8(%r15), %rax
testl $0x60000000, %eax       # imm = 0x60000000
je 0x8a7e
andl $0x9fffffff, %eax       # imm = 0x9FFFFFFF
movq %rax, 0x8(%r15)
movq %rbp, %rdi
movq %r12, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%r8
cmpq $0x7f, %r12
jl 0x8aab
cmpq $0xa0, %r12
jl 0x8a3d
cmpl $0x0, 0x30(%r14)
je 0x8a3d
cmpl $0x0, 0x30(%r14)
je 0x8b1e
movq 0x8(%r15), %rax
movl %eax, %ebx
shrl $0x1c, %ebx
andl $0x7, %ebx
testb %r12b, %r12b
js 0x8ac8
cmpl $0x2, %ebx
je 0x8ada
testb %r12b, %r12b
setns %cl
testl $0x50000000, %eax       # imm = 0x50000000
sete %dl
orb %cl, %dl
jne 0x8b10
andq $-0xf000001, %rax       # imm = 0xF0FFFFFF
movq %rax, 0x8(%r15)
movq $0x0, (%r15)
movl $0xffff, %esi           # imm = 0xFFFF
movq %rbp, %rdi
movq %r8, %r13
callq *%r8
movq %r13, %r8
movq 0x8(%r15), %rax
testl $0x60000000, %eax       # imm = 0x60000000
je 0x8b10
andl $0x9fffffff, %eax       # imm = 0x9FFFFFFF
movq %rax, 0x8(%r15)
testb %r12b, %r12b
jns 0x8b1e
testl %ebx, %ebx
jne 0x8b1e
orb $0x20, 0xb(%r15)
movl (%r15), %eax
shll $0x8, %eax
andl $0x7f, %r12d
orq %rax, %r12
movq 0x8(%r15), %rdi
movl %edi, %eax
shrl $0x18, %eax
andl $0xf, %eax
incl %eax
movl %edi, %edx
shrl $0x1c, %edx
andl $0x7, %edx
cmpl $0x2, %edx
setae %cl
shrl %cl, %edx
addl %edx, %edx
leal (%rdx,%rdx,2), %ecx
shrq %cl, %rdi
movq 0x10(%r14), %rcx
movl %edi, %edx
andl $0x3f, %edx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jl 0x8b90
andl $0x3f, %edi
movq %r12, %rsi
movq %r8, %rbx
callq *0x38(%r14)
movq %rbp, %rdi
movq %rax, %rsi
callq *%rbx
movq 0x8(%r15), %rax
testl $0x60000000, %eax       # imm = 0x60000000
je 0x8b8b
andl $0x9fffffff, %eax       # imm = 0x9FFFFFFF
movq %rax, 0x8(%r15)
xorl %r12d, %r12d
xorl %eax, %eax
movzbl 0x3(%r15), %ecx
shll $0x18, %ecx
orq %r12, %rcx
movq %rcx, (%r15)
movq 0x8(%r15), %rcx
andl $0xf0ffffff, %ecx       # imm = 0xF0FFFFFF
shll $0x18, %eax
orq %rcx, %rax
movq %rax, 0x8(%r15)
jmp 0x8a28
movq 0x10(%rsp), %rcx
movq $0x0, (%rcx)
movq 0x8(%rcx), %rax
andq 0x8(%r15,%r12), %rax
movq %rax, 0x8(%rcx)
xorq 0x10(%r15,%r12), %rax
movq %rax, 0x8(%rcx)
jmp 0x8a28
leaq 0x5411d(%rip), %rdi     # 0x5cd02
leaq 0x54126(%rip), %rsi     # 0x5cd12
leaq 0x5412a(%rip), %rcx     # 0x5cd1d
movl $0xdd, %edx
callq 0x6060