// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %r8, %r12
movq %rcx, 0x8(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq 0x18(%rdi), %r13
movq 0x8(%rdx), %rax
testl %eax, %eax
js 0x8584
movq 0x20(%r13), %rax
movq %rax, 0x8(%r14)
movq (%r14), %rcx
cmpq $0xffffff, %rcx         # imm = 0xFFFFFF
ja 0x85a9
cmpq $0x1b, %r15
ja 0x8772
movl $0x800c000, %edx        # imm = 0x800C000
btq %r15, %rdx
jae 0x8772
movl %ecx, %ebp
shrl $0x18, %ebp
andl $0x7, %ebp
movq %rcx, %rbx
shrq $0x1b, %rbx
testl $0xf000000, %eax        # imm = 0xF000000
je 0x85f2
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
je 0x85e6
andl $0x90ffffff, %eax       # imm = 0x90FFFFFF
movq %rax, 0x8(%r14)
movl $0xffff, %esi           # imm = 0xFFFF
movq %r12, %rdi
callq *0x8(%rsp)
movq %rbp, 0x18(%rsp)
movq %r12, 0x48(%rsp)
movq %r15, 0x30(%rsp)
movq %r14, 0x20(%rsp)
movslq 0x8(%r13), %rax
movq %rax, 0x50(%rsp)
movq %rbx, %rax
shlq $0x20, %rax
movq %rax, 0x28(%rsp)
sarq $0x1b, %rax
movq %rax, 0x38(%rsp)
movslq %ebx, %rcx
movl %ecx, %eax
shll $0x1b, %eax
movl %eax, 0x14(%rsp)
movq %rcx, %rax
shlq $0x5, %rax
movq 0x18(%rsp), %r14
movq %rcx, 0x40(%rsp)
cmpq 0x50(%rsp), %rcx
jge 0x871f
movq %r13, %r12
movq (%r13), %r15
movq %rax, %rbx
movq (%r15,%rax), %rbp
movq 0x38(%rsp), %rax
movq (%r15,%rax), %r13
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x6070
testl %eax, %eax
jne 0x86ad
movsbq (%rbp,%r14), %rax
cmpq 0x30(%rsp), %rax
jge 0x86ad
movq 0x40(%rsp), %rcx
incq %rcx
addl $0x8000000, 0x14(%rsp)  # imm = 0x8000000
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, 0x28(%rsp)
movq %rbx, %rax
addq $0x20, %rax
movq %r12, %r13
jmp 0x863c
movq 0x28(%rsp), %rax
sarq $0x1b, %rax
movq (%r15,%rax), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x6070
testl %eax, %eax
movq %r12, %r13
jne 0x871f
movq 0x18(%rsp), %rax
movsbq (%rbp,%rax), %rax
cmpq 0x30(%rsp), %rax
jne 0x871f
movq 0x18(%rsp), %rax
cmpb $0x0, 0x1(%rbp,%rax)
je 0x87e7
cmpl $0x7, %eax
je 0x890b
cmpq $0x20, 0x40(%rsp)
jge 0x890b
incq %rax
shll $0x18, %eax
movl 0x14(%rsp), %ecx
orl %eax, %ecx
movslq %ecx, %rax
movq 0x20(%rsp), %rcx
movq %rax, (%rcx)
jmp 0x8763
xorl %ebx, %ebx
movq 0x20(%rsp), %r15
movq 0x48(%rsp), %r12
movq 0x18(%rsp), %r14
movq 0x38(%rsp), %rbp
cmpq %rbx, %r14
je 0x8753
movq (%r13), %rax
movq (%rax,%rbp), %rax
movsbq (%rax,%rbx), %rsi
movq %r12, %rdi
callq *0x8(%rsp)
incq %rbx
jmp 0x8735
movq %r12, %rdi
movq 0x30(%rsp), %rsi
callq *0x8(%rsp)
andq $0x0, (%r15)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x21, %r15
jge 0x87c8
testl $0xf000000, %eax        # imm = 0xF000000
je 0x87ad
andq $-0xf000001, %rax       # imm = 0xF0FFFFFF
movq %rax, 0x8(%r14)
andq $0x0, (%r14)
movl $0xffff, %esi           # imm = 0xFFFF
movq %r12, %rdi
callq *0x8(%rsp)
movq 0x8(%r14), %rax
testl $0x60000000, %eax       # imm = 0x60000000
je 0x87ad
andl $0x9fffffff, %eax       # imm = 0x9FFFFFFF
movq %rax, 0x8(%r14)
movq %r12, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movl 0x30(%r13), %ecx
cmpq $0x7f, %r15
jb 0x8814
testl %ecx, %ecx
setne %cl
cmpq $0xa0, %r15
setae %dl
testb %cl, %dl
je 0x8778
jmp 0x8818
movq 0x28(%rsp), %rdx
sarq $0x20, %rdx
movq 0x20(%rsp), %rcx
andq $0x0, (%rcx)
shlq $0x5, %rdx
movq 0x8(%rcx), %rax
andq 0x8(%r15,%rdx), %rax
xorq 0x10(%r15,%rdx), %rax
movq %rax, 0x8(%rcx)
jmp 0x8763
testl %ecx, %ecx
je 0x887a
movl %eax, %ebx
shrl $0x1c, %ebx
andl $0x7, %ebx
testb %r15b, %r15b
js 0x882a
cmpl $0x2, %ebx
je 0x8839
testb %r15b, %r15b
jns 0x8867
movl %eax, %ecx
andl $0x50000000, %ecx       # imm = 0x50000000
je 0x8867
andq $-0xf000001, %rax       # imm = 0xF0FFFFFF
movq %rax, 0x8(%r14)
andq $0x0, (%r14)
movl $0xffff, %esi           # imm = 0xFFFF
movq %r12, %rdi
callq *0x8(%rsp)
movq 0x8(%r14), %rax
testl $0x60000000, %eax       # imm = 0x60000000
je 0x8867
andl $0x9fffffff, %eax       # imm = 0x9FFFFFFF
movq %rax, 0x8(%r14)
testb %r15b, %r15b
jns 0x887a
testl %ebx, %ebx
jne 0x887a
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
imull $0x6, %esi, %ecx
movq %rax, %rdi
shrq %cl, %rdi
movq 0x10(%r13), %rcx
movl %edi, %esi
andl $0x3f, %esi
movsbl (%rcx,%rsi), %ecx
cmpl %ecx, %edx
jl 0x88e9
andl $0x3f, %edi
movq %r15, %rsi
callq *0x38(%r13)
movq %r12, %rdi
movq %rax, %rsi
callq *0x8(%rsp)
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 0x8763
leaq 0x533d4(%rip), %rdi     # 0x5bce6
leaq 0x533dd(%rip), %rsi     # 0x5bcf6
leaq 0x533e1(%rip), %rcx     # 0x5bd01
movl $0xdd, %edx
callq 0x6060