// File: 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
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
addq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x181c5
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x181c5
jmp 0x17f22
movq -0x40(%rbp), %rax
movzbl (%rax), %edi
callq 0x18200
movl %eax, -0x4c(%rbp)
cmpl $0x7f, -0x4c(%rbp)
jg 0x17fcf
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jb 0x17f4a
jmp 0x181c7
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
addq %rax, %rsi
callq 0x18220
movq %rax, -0x58(%rbp)
cmpl $0xc, -0x4c(%rbp)
jne 0x17f8b
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
cmpq -0x48(%rbp), %rax
jb 0x17f86
jmp 0x181c7
jmp 0x181ab
movl -0x4c(%rbp), %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
cmpq -0x58(%rbp), %rax
jb 0x17fb8
jmp 0x181c7
movq -0x40(%rbp), %rax
movzbl (%rax), %edi
callq 0x18200
movl %eax, -0x4c(%rbp)
cmpl $0x7f, -0x4c(%rbp)
jle 0x17f69
jmp 0x17fcf
cmpl $0x7ff, -0x4c(%rbp)     # imm = 0x7FF
jg 0x1800e
movq -0x30(%rbp), %rax
addq $0x2, %rax
cmpq -0x38(%rbp), %rax
jbe 0x17feb
jmp 0x181c7
movl -0x4c(%rbp), %eax
sarl $0x6, %eax
orl $0xc0, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
jmp 0x1818d
cmpl $0xffff, -0x4c(%rbp)    # imm = 0xFFFF
jg 0x1804d
movq -0x30(%rbp), %rax
addq $0x3, %rax
cmpq -0x38(%rbp), %rax
jbe 0x1802a
jmp 0x181c7
movl -0x4c(%rbp), %eax
sarl $0xc, %eax
orl $0xe0, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
jmp 0x1816c
cmpl $0x1fffff, -0x4c(%rbp)  # imm = 0x1FFFFF
jg 0x1808c
movq -0x30(%rbp), %rax
addq $0x4, %rax
cmpq -0x38(%rbp), %rax
jbe 0x18069
jmp 0x181c7
movl -0x4c(%rbp), %eax
sarl $0x12, %eax
orl $0xf0, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
jmp 0x1814b
cmpl $0x3ffffff, -0x4c(%rbp) # imm = 0x3FFFFFF
jg 0x180c8
movq -0x30(%rbp), %rax
addq $0x5, %rax
cmpq -0x38(%rbp), %rax
jbe 0x180a8
jmp 0x181c7
movl -0x4c(%rbp), %eax
sarl $0x18, %eax
orl $0xf8, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
jmp 0x1812a
cmpl $0x7fffffff, -0x4c(%rbp) # imm = 0x7FFFFFFF
jg 0x18104
movq -0x30(%rbp), %rax
addq $0x6, %rax
cmpq -0x38(%rbp), %rax
jbe 0x180e4
jmp 0x181c7
movl -0x4c(%rbp), %eax
sarl $0x1e, %eax
orl $0xfc, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
jmp 0x18109
jmp 0x181ab
movl -0x4c(%rbp), %eax
sarl $0x18, %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movl -0x4c(%rbp), %eax
sarl $0x12, %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movl -0x4c(%rbp), %eax
sarl $0xc, %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movl -0x4c(%rbp), %eax
sarl $0x6, %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movl -0x4c(%rbp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
jmp 0x181ad
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
cmpq -0x48(%rbp), %rax
jb 0x17f22
jmp 0x181c5
jmp 0x181c7
cmpq $0x0, -0x28(%rbp)
je 0x181e3
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
subq %rax, %rcx
movq -0x28(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
subq %rcx, %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)