// File: /workspace/github_c_makefile_star2_O3/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 0xc6c0
movq %rcx, %r14
movq %rdx, %r15
movq %rdi, %rbx
movq 0x58(%rsp), %rbp
xorl %r12d, %r12d
xorl %r13d, %r13d
testq %rbp, %rbp
setne %dl
movb (%r15,%r13), %al
cmpb $-0x60, %al
sete %sil
movq %rbp, %rcx
testb %sil, %dl
jne 0xc69a
movzbl %al, %edx
testb $0x60, %al
je 0xc591
andl $-0x20, %edx
cmpl $0x20, %edx
je 0xc58b
cmpl $0x40, %edx
jne 0xc5ad
addb $-0x40, %al
testb %al, %al
jns 0xc5e2
jmp 0xc597
cmpl $0x0, (%rsp)
je 0xc5b5
movq 0x50(%rsp), %rax
movq %rax, %rcx
testq %rax, %rax
jne 0xc69a
jmp 0xc6af
cmpb $0x60, %al
jl 0xc5db
addb $-0x20, %al
jmp 0xc5e2
cmpl $0x13, %edx
jg 0xc629
leaq 0x340b(%rip), %rcx      # 0xf9cc
cmpl $0x3, %edx
je 0xc69a
cmpl $0xd, %edx
jne 0xc597
leaq 0x33f8(%rip), %rcx      # 0xf9ce
jmp 0xc69a
cmpb $-0x60, %dl
jne 0xc65f
addb $-0x40, %al
movzbl %al, %edx
cmpl $0x0, 0x4(%rsp)
je 0xc678
cmpl $0x68, %edx
jg 0xc616
leaq 0x33d8(%rip), %rcx      # 0xf9d4
cmpl $0x5e, %edx
je 0xc69a
cmpl $0x5f, %edx
jne 0xc643
leaq 0x33c8(%rip), %rcx      # 0xf9d9
jmp 0xc69a
cmpl $0x69, %edx
je 0xc656
cmpl $0x7a, %edx
jne 0xc643
leaq 0x33bc(%rip), %rcx      # 0xf9e3
jmp 0xc69a
cmpl $0x14, %edx
je 0xc666
cmpl $0x8d, %edx
jne 0xc597
leaq 0x3391(%rip), %rcx      # 0xf9d2
jmp 0xc69a
leal -0x1(%rax), %ecx
cmpb $0x19, %cl
ja 0xc66f
decl %edx
leaq 0x76dc(%rip), %rax      # 0x13d30
jmp 0xc67f
leaq 0x3381(%rip), %rcx      # 0xf9de
jmp 0xc69a
addb $-0x80, %al
jmp 0xc58b
leaq 0x3363(%rip), %rcx      # 0xf9d0
jmp 0xc69a
addb $-0x41, %al
cmpb $0x19, %al
ja 0xc678
addl $-0x40, %edx
leaq 0x72b1(%rip), %rax      # 0x13930
leaq (%rax,%rdx,8), %rax
movq (%rax), %rax
movq %rax, %rcx
testq %rax, %rax
jne 0xc693
movq 0x50(%rsp), %rcx
orq 0x50(%rsp), %rax
je 0xc6af
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0xc6d8
addq %rax, %r12
incq %r13
cmpq %r13, %r14
jne 0xc559
incq %r12
jmp 0xc6c6
movl $0x1, %r12d
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq