// File: /workspace/github_c_makefile_star2_O2/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 %rcx, %r14
movq %rdx, %r15
movq %rsi, 0x8(%rsp)
movq %rdi, %r13
movq 0x58(%rsp), %rbx
leaq 0x10(%rsp), %rax
andq $0x0, (%rax)
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq %r12, %r14
je 0xbfcf
testq %rbx, %rbx
setne %dl
movb (%r15,%r12), %al
cmpb $-0x60, %al
sete %sil
movq %rbx, %rcx
testb %sil, %dl
jne 0xbfb2
movzbl %al, %edx
testb $0x60, %al
je 0xbebf
andl $-0x20, %edx
cmpl $0x20, %edx
je 0xbeb9
cmpl $0x40, %edx
jne 0xbedb
addb $-0x40, %al
testb %al, %al
jns 0xbf18
jmp 0xbec5
cmpl $0x0, (%rsp)
je 0xbee3
movq 0x50(%rsp), %rax
movq %rax, %rcx
testq %rax, %rax
jne 0xbfb2
jmp 0xbfc7
cmpb $0x60, %al
jl 0xbf11
addb $-0x20, %al
jmp 0xbf18
leaq 0x2ae2(%rip), %rcx      # 0xe9cc
cmpl $0x3, %edx
je 0xbfb2
cmpl $0x8d, %edx
je 0xbf75
cmpl $0x14, %edx
je 0xbf7e
cmpl $0xd, %edx
jne 0xbec5
leaq 0x2ac2(%rip), %rcx      # 0xe9ce
jmp 0xbfb2
cmpb $-0x60, %dl
jne 0xbf6e
addb $-0x40, %al
movzbl %al, %edx
cmpl $0x0, 0x4(%rsp)
je 0xbf90
leaq 0x2aab(%rip), %rcx      # 0xe9d4
cmpb $0x5e, %al
je 0xbfb2
cmpl $0x7a, %edx
je 0xbf49
cmpl $0x69, %edx
je 0xbf52
cmpl $0x5f, %edx
jne 0xbf5b
leaq 0x2a92(%rip), %rcx      # 0xe9d9
jmp 0xbfb2
leaq 0x2a93(%rip), %rcx      # 0xe9e3
jmp 0xbfb2
leaq 0x2a85(%rip), %rcx      # 0xe9de
jmp 0xbfb2
leal -0x1(%rax), %ecx
cmpb $0x19, %cl
ja 0xbf87
decl %edx
leaq 0x6dc4(%rip), %rax      # 0x12d30
jmp 0xbf97
addb $-0x80, %al
jmp 0xbeb9
leaq 0x2a56(%rip), %rcx      # 0xe9d2
jmp 0xbfb2
leaq 0x2a4b(%rip), %rcx      # 0xe9d0
jmp 0xbfb2
addb $-0x41, %al
cmpb $0x19, %al
ja 0xbf90
addl $-0x40, %edx
leaq 0x6999(%rip), %rax      # 0x12930
leaq (%rax,%rdx,8), %rax
movq (%rax), %rax
movq %rax, %rcx
testq %rax, %rax
jne 0xbfab
movq 0x50(%rsp), %rcx
orq 0x50(%rsp), %rax
je 0xbfc7
movq %r13, %rdi
movq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0xbfe4
addq %rax, %rbp
incq %r12
jmp 0xbe7e
incq %rbp
movq %rbp, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq