// File: /workspace/github_cpp_makefile_star2_O1/Fohlen[P]cubescript-standalone/stream.cpp
// Function: decodeutf8(unsigned char*, unsigned long, unsigned char const*, unsigned long, unsigned long*)
size_t decodeutf8(uchar *dstbuf, size_t dstlen, const uchar *srcbuf, size_t srclen, size_t *carry)
{
    uchar *dst = dstbuf, *dstend = &dstbuf[dstlen];
    const uchar *src = srcbuf, *srcend = &srcbuf[srclen];
    if(dstbuf == srcbuf)
    {
        int len = min(dstlen, srclen);
        for(const uchar *end4 = &srcbuf[len&~3]; src < end4; src += 4) if(*(const int *)src & 0x80808080) goto decode;
        for(const uchar *end = &srcbuf[len]; src < end; src++) if(*src & 0x80) goto decode;
        if(carry) *carry += len;
        return len;
    }

decode:
    dst += src - srcbuf;
    while(src < srcend && dst < dstend)
    {
        int c = *src++;
        if(c < 0x80) *dst++ = c;
        else if(c >= 0xC0)
        {
            int uni;
            if(c >= 0xE0)
            {
                if(c >= 0xF0)
                {
                    if(c >= 0xF8)
                    {
                        if(c >= 0xFC)
                        {
                            if(c >= 0xFE) continue;
                            uni = c&1; if(srcend - src < 5) break;
                            c = *src; if((c&0xC0) != 0x80) continue; src++; uni = (uni<<6) | (c&0x3F);
                        }
                        else { uni = c&3; if(srcend - src < 4) break; }
                        c = *src; if((c&0xC0) != 0x80) continue; src++; uni = (uni<<6) | (c&0x3F);
                    }
                    else { uni = c&7; if(srcend - src < 3) break; }
                    c = *src; if((c&0xC0) != 0x80) continue; src++; uni = (uni<<6) | (c&0x3F);
                }
                else { uni = c&0xF; if(srcend - src < 2) break; }
                c = *src; if((c&0xC0) != 0x80) continue; src++; uni = (uni<<6) | (c&0x3F);
            }
            else { uni = c&0x1F; if(srcend - src < 1) break; }
            c = *src; if((c&0xC0) != 0x80) continue; src++; uni = (uni<<6) | (c&0x3F);
            c = uni2cube(uni);
            if(!c) continue;
            *dst++ = c;
        }
    }
    if(carry) *carry += src - srcbuf;
    return dst - dstbuf;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdx, %rbx
cmpq %rdx, %rdi
jne 0x13155
cmpq %rcx, %rsi
movq %rcx, %rax
cmovbq %rsi, %rax
cltq
movq %rax, %r11
movl $0x2, %r9d
andq $-0x4, %r11
setg %r10b
jle 0x130f3
testl $0x80808080, (%rdx)     # imm = 0x80808080
je 0x130f8
movq %rdx, %rbx
jmp 0x13117
movq %rdx, %rbx
jmp 0x1311d
addq %rdx, %r11
leaq 0x4(%rdx), %r14
movq %r14, %rbx
cmpq %r11, %r14
setb %r10b
jae 0x1311d
leaq 0x4(%rbx), %r14
testl $0x80808080, (%rbx)     # imm = 0x80808080
je 0x130ff
movl $0x5, %r9d
testb %r10b, %r10b
jne 0x1314b
leaq (%rdx,%rax), %r9
cmpq %r9, %rbx
jae 0x13135
cmpb $0x0, (%rbx)
js 0x13145
incq %rbx
jmp 0x13126
movl $0x1, %r9d
testq %r8, %r8
je 0x1314b
addq %rax, (%r8)
jmp 0x1314b
movl $0x5, %r9d
cmpl $0x5, %r9d
jne 0x13357
addq %rdi, %rsi
addq %rdx, %rcx
movq %rbx, %rax
subq %rdx, %rax
addq %rdi, %rax
leaq 0x44a5(%rip), %r9       # 0x17610
leaq 0x44be(%rip), %r10      # 0x17630
cmpq %rcx, %rbx
jae 0x13346
cmpq %rsi, %rax
jae 0x13346
leaq 0x1(%rbx), %r11
movb (%rbx), %r15b
testb %r15b, %r15b
js 0x1319b
movb %r15b, (%rax)
incq %rax
jmp 0x13335
cmpb $-0x40, %r15b
jb 0x13335
movzbl %r15b, %r14d
cmpb $-0x20, %r15b
jb 0x13216
cmpb $-0x10, %r15b
jb 0x1322a
cmpb $-0x8, %r15b
jb 0x13251
cmpb $-0x4, %r15b
jb 0x1326a
cmpb $-0x3, %r15b
ja 0x13335
movq %rcx, %r12
subq %r11, %r12
movb $0x1, %r15b
cmpq $0x5, %r12
jl 0x13338
movzbl (%r11), %r15d
movl %r15d, %r12d
andl $-0x40, %r12d
cmpl $0x80, %r12d
jne 0x13335
addq $0x2, %rbx
shll $0x6, %r14d
andl $0x40, %r14d
andl $0x3f, %r15d
orl %r14d, %r15d
movq %rbx, %r11
jmp 0x13284
movq %rcx, %rbx
subq %r11, %rbx
testq %rbx, %rbx
jle 0x13249
andl $0x1f, %r14d
jmp 0x132ea
movq %rcx, %rbx
subq %r11, %rbx
movb $0x1, %r15b
cmpq $0x2, %rbx
jl 0x13338
andl $0xf, %r14d
movl %r14d, %ebx
jmp 0x132c9
movb $0x1, %r15b
jmp 0x13338
movq %rcx, %rbx
subq %r11, %rbx
movb $0x1, %r15b
cmpq $0x3, %rbx
jl 0x13338
andl $0x7, %r14d
jmp 0x132a8
movq %rcx, %rbx
subq %r11, %rbx
movb $0x1, %r15b
cmpq $0x4, %rbx
jl 0x13338
andl $0x3, %r14d
movl %r14d, %r15d
movzbl (%r11), %r14d
movl %r14d, %ebx
andl $-0x40, %ebx
cmpl $0x80, %ebx
jne 0x13335
incq %r11
shll $0x6, %r15d
andl $0x3f, %r14d
orl %r15d, %r14d
movzbl (%r11), %ebx
movl %ebx, %r15d
andl $-0x40, %r15d
cmpl $0x80, %r15d
jne 0x13335
incq %r11
shll $0x6, %r14d
andl $0x3f, %ebx
orl %r14d, %ebx
movzbl (%r11), %r14d
movl %r14d, %r15d
andl $-0x40, %r15d
cmpl $0x80, %r15d
jne 0x13335
incq %r11
shll $0x6, %ebx
andl $0x3f, %r14d
orl %ebx, %r14d
movzbl (%r11), %ebx
movl %ebx, %r15d
andl $-0x40, %r15d
cmpl $0x80, %r15d
jne 0x13335
cmpl $0x1f, %r14d
ja 0x13326
movl %r14d, %r15d
shll $0x6, %r15d
andl $0x3f, %ebx
andl $-0x4, %r14d
movslq (%r14,%r9), %r14
movzbl %r15b, %r15d
orq %rbx, %r15
addq %r14, %r15
movb (%r10,%r15), %r15b
jmp 0x13329
xorl %r15d, %r15d
incq %r11
testb %r15b, %r15b
jne 0x13190
xorl %r15d, %r15d
movq %r11, %rbx
testb %r15b, %r15b
je 0x13172
jmp 0x13349
movq %rbx, %r11
testq %r8, %r8
je 0x13354
subq %rdx, %r11
addq %r11, (%r8)
subq %rdi, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq