// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %rbx
testq %rcx, %rcx
setle %al
testq %rsi, %rsi
setle %r9b
orb %al, %r9b
movq %rdx, %r9
movq %rdi, %rax
jne 0x13552
addq %rdi, %rsi
addq %rdx, %rcx
leaq 0x3e7e(%rip), %r10      # 0x17210
movq %rdx, %r9
movq %rdi, %rax
movzbl (%r9), %r11d
movl (%r10,%r11,4), %r11d
cmpl $0x7f, %r11d
jg 0x13431
movl $0x4, %ebx
cmpq %rsi, %rax
jae 0x13542
movq %rsi, %rbx
subq %rax, %rbx
addq %r9, %rbx
cmpq %rbx, %rcx
cmovbq %rcx, %rbx
incq %r9
movq %r9, %r15
movq %rax, %r14
movq %r15, %r9
cmpl $0xc, %r11d
je 0x133ff
movb %r11b, (%r14)
cmpq %rbx, %r9
jae 0x13415
leaq 0x1(%r14), %rax
movzbl (%r9), %r11d
movl (%r10,%r11,4), %r11d
leaq 0x1(%r9), %r15
cmpl $0x80, %r11d
jl 0x133ce
xorl %ebx, %ebx
jmp 0x1341a
xorl %eax, %eax
cmpq %rcx, %r9
setb %al
leal (%rax,%rax,2), %ebx
addl $0x4, %ebx
movl $0xc, %r11d
jmp 0x1341d
movl $0x4, %ebx
incq %r14
movq %r14, %rax
cmpl $0x7, %ebx
je 0x13540
testl %ebx, %ebx
jne 0x13542
cmpl $0x7ff, %r11d           # imm = 0x7FF
jg 0x1345a
leaq 0x2(%rax), %r14
movl $0x4, %ebx
cmpq %rsi, %r14
ja 0x13542
movl %r11d, %ebx
shrl $0x6, %ebx
orb $-0x40, %bl
jmp 0x1352e
cmpl $0xffff, %r11d          # imm = 0xFFFF
jg 0x13488
leaq 0x3(%rax), %r14
movl $0x4, %ebx
cmpq %rsi, %r14
ja 0x13542
movl %r11d, %ebx
shrl $0xc, %ebx
orb $-0x20, %bl
movb %bl, (%rax)
incq %rax
jmp 0x13522
cmpl $0x1fffff, %r11d        # imm = 0x1FFFFF
jg 0x134ae
leaq 0x4(%rax), %r14
movl $0x4, %ebx
cmpq %rsi, %r14
ja 0x13542
movl %r11d, %ebx
shrl $0x12, %ebx
orb $-0x10, %bl
jmp 0x1350d
cmpl $0x3ffffff, %r11d       # imm = 0x3FFFFFF
jg 0x134d5
leaq 0x5(%rax), %r14
movl $0x4, %ebx
cmpq %rsi, %r14
ja 0x13542
movl %r11d, %ebx
shrl $0x18, %ebx
orb $-0x8, %bl
movb %bl, (%rax)
incq %rax
jmp 0x13501
leaq 0x6(%rax), %r14
movl $0x4, %ebx
cmpq %rsi, %r14
ja 0x13542
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
xorl %ebx, %ebx
testl %ebx, %ebx
jne 0x13552
incq %r9
cmpq %rcx, %r9
jb 0x13398
testq %r8, %r8
je 0x1355d
subq %rdx, %r9
addq %r9, (%r8)
subq %rdi, %rax
popq %rbx
popq %r14
popq %r15
popq %rbp
retq