// File: /workspace/github_cpp_makefile_star2_O3/Fohlen[P]cubescript-standalone/stream.cpp
// Function: encodeutf8(unsigned char*, unsigned long, unsigned char const*, unsigned long, unsigned long*)
size_t encodeutf8(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(src < srcend && dst < dstend) do
    {
        int uni = cube2uni(*src);
        if(uni <= 0x7F)
        {
            if(dst >= dstend) goto done;
            const uchar *end = min(srcend, &src[dstend-dst]);
            do 
            { 
                if(uni == '\f')
                {
                    if(++src >= srcend) goto done;
                    goto uni1;
                }
                *dst++ = uni; 
                if(++src >= end) goto done; 
                uni = cube2uni(*src); 
            } 
            while(uni <= 0x7F);
        }
        if(uni <= 0x7FF) { if(dst + 2 > dstend) goto done; *dst++ = 0xC0 | (uni>>6); goto uni2; }
        else if(uni <= 0xFFFF) { if(dst + 3 > dstend) goto done; *dst++ = 0xE0 | (uni>>12); goto uni3; }
        else if(uni <= 0x1FFFFF) { if(dst + 4 > dstend) goto done; *dst++ = 0xF0 | (uni>>18); goto uni4; }
        else if(uni <= 0x3FFFFFF) { if(dst + 5 > dstend) goto done; *dst++ = 0xF8 | (uni>>24); goto uni5; }
        else if(uni <= 0x7FFFFFFF) { if(dst + 6 > dstend) goto done; *dst++ = 0xFC | (uni>>30); goto uni6; }
        else goto uni1;
    uni6: *dst++ = 0x80 | ((uni>>24)&0x3F);
    uni5: *dst++ = 0x80 | ((uni>>18)&0x3F);
    uni4: *dst++ = 0x80 | ((uni>>12)&0x3F);
    uni3: *dst++ = 0x80 | ((uni>>6)&0x3F);
    uni2: *dst++ = 0x80 | (uni&0x3F);
    uni1:;
    } 
    while(++src < srcend);

done:
    if(carry) *carry += src - srcbuf;
    return dst - dstbuf;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
testq %rcx, %rcx
setle %al
testq %rsi, %rsi
setle %r9b
orb %al, %r9b
movq %rdx, %r9
movq %rdi, %rax
jne 0x13a08
addq %rdi, %rsi
addq %rdx, %rcx
leaq 0x4ad8(%rip), %r10      # 0x18370
movq %rdx, %r9
movq %rdi, %rax
movzbl (%r9), %r11d
movl (%r10,%r11,4), %r11d
cmpl $0x7f, %r11d
jg 0x138f4
cmpq %rsi, %rax
jae 0x13a08
movq %rsi, %rbx
subq %rax, %rbx
addq %r9, %rbx
cmpq %rbx, %rcx
cmovbq %rcx, %rbx
incq %r9
cmpl $0xc, %r11d
je 0x13941
movb %r11b, (%rax)
incq %rax
cmpq %rbx, %r9
jae 0x13a08
movzbl (%r9), %r11d
movl (%r10,%r11,4), %r11d
incq %r9
cmpl $0x80, %r11d
jl 0x138c8
decq %r9
cmpl $0x7ff, %r11d           # imm = 0x7FF
ja 0x13918
leaq 0x2(%rax), %rbx
cmpq %rsi, %rbx
ja 0x13a08
movl %r11d, %ebx
shrl $0x6, %ebx
orb $-0x40, %bl
jmp 0x139ea
cmpl $0xffff, %r11d          # imm = 0xFFFF
ja 0x1394f
leaq 0x3(%rax), %rbx
cmpq %rsi, %rbx
ja 0x13a08
movl %r11d, %ebx
shrl $0xc, %ebx
orb $-0x20, %bl
movb %bl, (%rax)
incq %rax
jmp 0x139de
cmpq %rcx, %r9
jb 0x139fc
jmp 0x13a08
cmpl $0x1fffff, %r11d        # imm = 0x1FFFFF
ja 0x13970
leaq 0x4(%rax), %rbx
cmpq %rsi, %rbx
ja 0x13a08
movl %r11d, %ebx
shrl $0x12, %ebx
orb $-0x10, %bl
jmp 0x139c9
cmpl $0x3ffffff, %r11d       # imm = 0x3FFFFFF
ja 0x13996
leaq 0x5(%rax), %rbx
cmpq %rsi, %rbx
ja 0x13a08
movl %r11d, %ebx
shrl $0x18, %ebx
orb $-0x8, %bl
movb %bl, (%rax)
incq %rax
jmp 0x139bd
leaq 0x6(%rax), %rbx
cmpq %rsi, %rbx
ja 0x13a08
movl %r11d, %ebx
shrl $0x1e, %ebx
orb $-0x4, %bl
movb %bl, (%rax)
movl %r11d, %ebx
shrl $0x18, %ebx
andb $0x3f, %bl
orb $-0x80, %bl
movb %bl, 0x1(%rax)
addq $0x2, %rax
movl %r11d, %ebx
shrl $0x12, %ebx
andb $0x3f, %bl
orb $-0x80, %bl
movb %bl, (%rax)
movl %r11d, %ebx
shrl $0xc, %ebx
andb $0x3f, %bl
orb $-0x80, %bl
movb %bl, 0x1(%rax)
addq $0x2, %rax
movl %r11d, %ebx
shrl $0x6, %ebx
andb $0x3f, %bl
orb $-0x80, %bl
movb %bl, (%rax)
andb $0x3f, %r11b
orb $-0x80, %r11b
movb %r11b, 0x1(%rax)
addq $0x2, %rax
incq %r9
cmpq %rcx, %r9
jb 0x1389e
testq %r8, %r8
je 0x13a13
subq %rdx, %r9
addq %r9, (%r8)
subq %rdi, %rax
popq %rbx
popq %rbp
retq