// File: /workspace/github_c_makefile_star2_O1/excess-c64[P]lib1541img/src/lib/1541img/petscii.c
// Function: petscii_toUtf8
SOEXPORT size_t petscii_toUtf8(
        char *buf, size_t bufsz, const char *str, size_t len, int lowercase,
        int onlyPrintable, const char *unknown, const char *shiftspace)
{
    size_t bufpos = 0;
    size_t result = 0;
    for (size_t i = 0; i < len; ++i)
    {
        unsigned char petsciiChar = (unsigned char)str[i];
        if (shiftspace && petsciiChar == 0xa0)
        {
            result += appendUtf8(buf, bufsz, &bufpos, shiftspace);
            continue;
        }
        char screencode = toScreencode(petsciiChar);
        const char *utf8Char = 0;
        if (screencode < 0)
        {
            if (!onlyPrintable) utf8Char = nonprintable(petsciiChar);
        }
        else
        {
            utf8Char = screencodeToUtf8(screencode, lowercase);
        }
        if (utf8Char)
        {
            result += appendUtf8(buf, bufsz, &bufpos, utf8Char);
        }
        else if (unknown)
        {
            result += appendUtf8(buf, bufsz, &bufpos, unknown);
        }
    }
    return result + 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, (%rsp)
movl %r8d, 0x4(%rsp)
movq %rsi, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
testq %rcx, %rcx
je 0xbc25
movq %rcx, %r14
movq %rdx, %r15
movq %rdi, %rbx
movq 0x58(%rsp), %rbp
xorl %r12d, %r12d
xorl %r13d, %r13d
testq %rbp, %rbp
setne %al
movb (%r15,%r13), %dl
cmpb $-0x60, %dl
sete %sil
movq %rbp, %rcx
testb %sil, %al
jne 0xbbf4
movzbl %dl, %eax
testb $0x60, %dl
je 0xbaf2
movl %eax, %ecx
andl $-0x20, %ecx
cmpl $0x20, %ecx
je 0xbaf6
cmpl $0x40, %ecx
jne 0xbafa
leal -0x40(%rdx), %ecx
jmp 0xbb0c
movb $-0x1, %cl
jmp 0xbb0c
movl %edx, %ecx
jmp 0xbb0c
cmpb $0x60, %dl
jl 0xbb04
leal -0x20(%rdx), %ecx
jmp 0xbb0c
cmpb $-0x60, %cl
je 0xbaed
leal -0x80(%rdx), %ecx
testb %cl, %cl
js 0xbb44
movzbl %cl, %eax
cmpl $0x0, 0x4(%rsp)
je 0xbbd5
cmpl $0x68, %eax
jg 0xbb77
leaq 0x2f12(%rip), %rsi      # 0xea3c
cmpl $0x5e, %eax
je 0xbbe0
cmpl $0x5f, %eax
jne 0xbba7
leaq 0x2f02(%rip), %rsi      # 0xea41
jmp 0xbbe0
cmpl $0x0, (%rsp)
jne 0xbba3
movl $0x0, %esi
andb $0x60, %dl
jne 0xbbe0
cmpl $0x13, %eax
jg 0xbb8a
leaq 0x2ed0(%rip), %rsi      # 0xea34
cmpl $0x3, %eax
je 0xbbe0
cmpl $0xd, %eax
jne 0xbba3
leaq 0x2ec1(%rip), %rsi      # 0xea36
jmp 0xbbe0
cmpl $0x69, %eax
je 0xbbc1
cmpl $0x7a, %eax
jne 0xbba7
leaq 0x2ec3(%rip), %rsi      # 0xea4b
jmp 0xbbe0
cmpl $0x14, %eax
je 0xbc17
cmpl $0x8d, %eax
jne 0xbba3
leaq 0x2e99(%rip), %rsi      # 0xea3a
jmp 0xbbe0
xorl %esi, %esi
jmp 0xbbe0
leal -0x1(%rcx), %edx
cmpb $0x19, %dl
ja 0xbbca
decl %eax
leaq 0x2e14(%rip), %rcx      # 0xe9cc
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
jmp 0xbbe0
leaq 0x2e7e(%rip), %rsi      # 0xea46
jmp 0xbbe0
addb $-0x41, %cl
cmpb $0x19, %cl
ja 0xbbd5
addl $-0x40, %eax
leaq 0x6e24(%rip), %rcx      # 0x12a00
movq (%rcx,%rax,8), %rsi
movq %rsi, %rcx
testq %rsi, %rsi
jne 0xbbed
movq 0x50(%rsp), %rcx
orq 0x50(%rsp), %rsi
je 0xbc09
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0xbc3d
addq %rax, %r12
incq %r13
cmpq %r13, %r14
jne 0xbab9
jmp 0xbc20
leaq 0x2e1a(%rip), %rsi      # 0xea38
jmp 0xbbe0
incq %r12
jmp 0xbc2b
movl $0x1, %r12d
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq