// File: /workspace/github_c_makefile_star2_O1/mloar[P]charset/utf7.c
// Function: write_utf7
static int write_utf7(charset_spec const *charset, long int input_chr,
		      charset_state *state,
		      void (*emit)(void *ctx, long int output),
		      void *emitctx)
{
    unsigned long hws[2];
    int nhws;
    int i;

    /*
     * For writing: state->s0 contains accumulated base64 data with
     * a 1 in front, and state->s1 indicates how many bits of it we
     * have.
     */

    if ((input_chr >= 0xD800 && input_chr < 0xE000) ||
	input_chr >= 0x110000) {
	/*
	 * We can't output surrogates, or anything above 0x10FFFF.
	 */
	return FALSE;
    }

    /*
     * Look for characters which we output in ASCII mode. A special
     * case here is +, which can be encoded as the empty base64
     * escape sequence `+-': if we're _already_ in ASCII mode we do
     * that, but if we're in base64 mode at the point we see the +
     * then we simply stay in base64 mode and output it as a
     * halfword. (Switching back would cost three bytes, whereas
     * staying in base64 costs only 2 2/3.)
     */
    if (input_chr == -1 || SET_D(input_chr) ||
	(charset->charset == CS_UTF7 && SET_O(input_chr)) ||
	(!state->s0 && input_chr == '+')) {
	if (state->s0) {
	    /*
	     * These characters are output in ASCII mode, so flush any
	     * lingering base64 data.
	     */
	    state->s0 <<= 6 - state->s1;
	    emit(emitctx, base64_chars[state->s0 & 0x3F]);
	    /*
	     * I'm going to arbitrarily decide to always use the
	     * terminating minus sign. It's easier than figuring out
	     * whether to do so or not, and looks prettier besides.
	     */
	    emit(emitctx, '-');
	    state->s0 = state->s1 = 0;
	}

	/*
	 * Now output the character.
	 */
	if (input_chr != -1)	       /* special case: just reset state */
	    emit(emitctx, input_chr);
	if (input_chr == '+')
	    emit(emitctx, '-');	       /* +- encodes + */
	return TRUE;
    }

    /*
     * Now we know we have a character that needs to be output as
     * either one base64-encoded halfword or two. So first figure
     * out how many...
     */
    if (input_chr < 0x10000) {
	nhws = 1;
	hws[0] = input_chr;
    } else {
	input_chr -= 0x10000;
	if (input_chr >= 0x100000) {
	    /* Anything above 0x10FFFF is outside UTF-7 range. */
	    return FALSE;
	}

	nhws = 2;
	hws[0] = 0xD800 | ((input_chr >> 10) & 0x3FF);
	hws[1] = 0xDC00 | (input_chr & 0x3FF);
    }

    /*
     * ... switch into base64 mode if required ...
     */
    if (!state->s0) {
	emit(emitctx, '+');
	state->s0 = 1;
	state->s1 = 0;
    }

    /*
     * ... and do the base64 output.
     */
    for (i = 0; i < nhws; i++) {
	state->s0 = (state->s0 << 16) | hws[i];
	state->s1 += 16;

	while (state->s1 >= 6) {
	    /*
	     * The top set bit must be in position 16, 18 or 20.
	     */
	    unsigned long out, topbit;
	    
	    out = (state->s0 >> (state->s1 - 6)) & 0x3F;
	    state->s1 -= 6;
	    topbit = 1 << state->s1;
	    state->s0 = (state->s0 & (topbit-1)) | topbit;

	    emit(emitctx, base64_chars[out]);
	}
    }
    return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
movq %rsi, %rax
andq $-0x800, %rax           # imm = 0xF800
cmpq $0xd800, %rax           # imm = 0xD800
sete %al
cmpq $0x110000, %rsi         # imm = 0x110000
setge %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x9e0e
movq %r8, %r14
movq %rdx, %r15
movq %rsi, %r12
cmpq $-0x1, %rsi
je 0x9cc7
cmpq $0x7f, %r12
ja 0x9c9f
leaq 0x9a158(%rip), %rax     # 0xa3df0
testb $0x1, (%r12,%rax)
jne 0x9cc7
cmpq $0x7f, %r12
ja 0x9cb8
cmpl $0x3c, (%rdi)
jne 0x9cb8
leaq 0x9a13f(%rip), %rax     # 0xa3df0
testb $0x2, (%r12,%rax)
jne 0x9cc7
movq (%r15), %rax
movq %r12, %rcx
xorq $0x2b, %rcx
orq %rax, %rcx
jne 0x9d30
movq (%r15), %rax
testq %rax, %rax
je 0x9d00
movb $0x6, %cl
subb 0x8(%r15), %cl
shlq %cl, %rax
movq %rax, (%r15)
andl $0x3f, %eax
leaq 0x9a18b(%rip), %rcx     # 0xa3e70
movsbq (%rax,%rcx), %rsi
movq %r14, %rdi
callq *%rbx
movl $0x2d, %esi
movq %r14, %rdi
callq *%rbx
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movl $0x1, %ebp
cmpq $-0x1, %r12
je 0x9e0e
movq %r14, %rdi
movq %r12, %rsi
callq *%rbx
cmpq $0x2b, %r12
jne 0x9e0e
movl $0x2d, %esi
movq %r14, %rdi
callq *%rbx
jmp 0x9e0e
cmpq $0xffff, %r12           # imm = 0xFFFF
jg 0x9d45
movq %r12, (%rsp)
movl $0x1, %r12d
jmp 0x9d7a
leal 0xf0000(%r12), %ecx
shrl $0xa, %ecx
andl $0x3ff, %ecx            # imm = 0x3FF
orq $0xd800, %rcx           # imm = 0xD800
movq %rcx, (%rsp)
andl $0x3ff, %r12d           # imm = 0x3FF
orq $0xdc00, %r12           # imm = 0xDC00
movq %r12, 0x8(%rsp)
movl $0x2, %r12d
testq %rax, %rax
jne 0x9d98
movl $0x2b, %esi
movq %r14, %rdi
callq *%rbx
movq $0x1, (%r15)
movq $0x0, 0x8(%r15)
xorl %r13d, %r13d
leaq 0x9a0ce(%rip), %rbp     # 0xa3e70
movq (%r15), %rax
movq 0x8(%r15), %rcx
shlq $0x10, %rax
orq (%rsp,%r13,8), %rax
movq %rax, (%r15)
addq $0x10, %rcx
movq %rcx, 0x8(%r15)
cmpq $0x6, %rcx
jb 0x9e01
movq (%r15), %rax
addq $-0x6, %rcx
movq %rax, %rdx
shrq %cl, %rdx
movl $0x1, %esi
shll %cl, %esi
andl $0x3f, %edx
movq %rcx, 0x8(%r15)
movslq %esi, %rcx
leaq -0x1(%rcx), %rsi
andq %rax, %rsi
orq %rcx, %rsi
movq %rsi, (%r15)
movsbq (%rdx,%rbp), %rsi
movq %r14, %rdi
callq *%rbx
movq 0x8(%r15), %rcx
cmpq $0x5, %rcx
ja 0x9dc2
incq %r13
cmpq %r12, %r13
jne 0x9da2
movl $0x1, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop