// File: /workspace/github_c_makefile_star2_O1/excess-c64[P]lib1541img/src/lib/1541img/petscii.c
// Function: petscii_fromUtf8
SOEXPORT size_t petscii_fromUtf8(
	char *buf, size_t bufsz, const char *str, size_t len,
	PetsciiCase casemode, int onlyPrintable, char unknown)
{
    size_t strpos = 0;
    size_t bufpos = 0;
    size_t result = 0;

    while (strpos < len)
    {
	if ((unsigned char)str[strpos] < 0x20)
	{
	    if (!onlyPrintable)
	    {
		unsigned char npp = nppetscii(str[strpos]);
		if (npp) result += appendPetscii(buf, bufsz, &bufpos, npp);
	    }
	    ++strpos;
	}
	else
	{
	    char screencode = screencodeFromUtf8(str, len, &strpos, &casemode);
	    if ((unsigned char)screencode < 0x80)
	    {
		result += appendPetscii(
			buf, bufsz, &bufpos, toPetsciiChar(screencode));
	    }
	    else if (unknown)
	    {
		result += appendPetscii(buf, bufsz, &bufpos, unknown);
	    }
	}
    }
    return result + 1;
}

// Assembly:
testq %rcx, %rcx
je 0xc852
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movb 0x38(%rsp), %r10b
xorl %eax, %eax
xorl %r15d, %r15d
xorl %r11d, %r11d
movzbl (%rdx,%r11), %r12d
cmpl $0x1f, %r12d
ja 0xbce4
testl %r9d, %r9d
jne 0xbf05
cmpl $0x9, %r12d
jg 0xbdb5
cmpl $0x3, %r12d
je 0xbee4
cmpl $0x8, %r12d
jne 0xbed9
movb $0x14, %r12b
jmp 0xbee4
leaq 0x1(%r11), %r13
testb %r12b, %r12b
js 0xbd02
movl %r12d, %r11d
andb $0x60, %r11b
cmpb $0x20, %r11b
jne 0xbd79
movl %r12d, %r14d
jmp 0xc3df
movb $-0x1, %r14b
cmpq %rcx, %r13
jae 0xc3df
cmpl $0xe1, %r12d
jg 0xbdfa
cmpl $0xc2, %r12d
je 0xbf10
cmpl $0xcf, %r12d
jne 0xc1c4
addq $0x2, %r11
cmpq %rcx, %r11
jae 0xbfc4
xorl %ebx, %ebx
cmpb (%rdx,%r13), %bl
jno 0xc3ba
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x5e, %r14b
jmp 0xbfc4
leal -0x61(%r12), %r11d
cmpb $0x19, %r11b
ja 0xbdd1
leal -0x60(%r12), %r11d
movl %r8d, %ebp
andl $-0x6, %ebp
incl %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
andl $0x3, %ebp
cmpl $0x2, %ebp
movzbl %r11b, %r14d
movl $0xff, %r11d
cmovel %r11d, %r14d
jmp 0xc3df
cmpl $0xa, %r12d
je 0xbee1
cmpl $0xd, %r12d
jne 0xbed9
movb $-0x73, %r12b
jmp 0xbee4
leal -0x41(%r12), %r11d
cmpb $0x19, %r11b
ja 0xbeac
leal -0x40(%r12), %r11d
testb $0x1, %r8b
movzbl %r12b, %ebp
movzbl %r11b, %r14d
cmovnel %ebp, %r14d
jmp 0xc3df
cmpl $0xe2, %r12d
je 0xbf3a
cmpl $0xf0, %r12d
jne 0xc1c4
leaq 0x2(%r11), %rbp
cmpq %rcx, %rbp
jae 0xbfb9
cmpb $-0x61, (%rdx,%r13)
jne 0xbfcc
leaq 0x3(%r11), %r12
cmpq %rcx, %r12
jae 0xc3dc
movzbl (%rdx,%rbp), %ebp
cmpl $0xae, %ebp
je 0xc1cc
cmpl $0xad, %ebp
jne 0xc3bd
movzbl 0x3(%rdx,%r11), %r12d
addq $0x4, %r11
addl $0xffffff50, %r12d      # imm = 0xFFFFFF50
cmpl $0xf, %r12d
ja 0xc3ba
leaq 0x2a45(%rip), %rbx      # 0xe8bc
movslq (%rbx,%r12,4), %r12
addq %rbx, %r12
jmpq *%r12
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x54, %r14b
jmp 0xbfc4
movb $-0x1, %r14b
movl %r12d, %r11d
addl $-0x40, %r11d
cmpl $0x1d, %r11d
ja 0xc3df
movl $0x28000001, %ebx       # imm = 0x28000001
btl %r11d, %ebx
jae 0xc3df
leal -0x40(%r12), %r14d
jmp 0xc3df
movb $0x1, %r14b
xorl %r12d, %r12d
jmp 0xbee7
movb $0xd, %r12b
xorl %r14d, %r14d
testb %r14b, %r14b
jne 0xbf05
leaq 0x1(%r15), %r14
cmpq %rsi, %r14
jae 0xbf02
movb %r12b, (%rdi,%r15)
movb $0x0, 0x1(%rdi,%r15)
movq %r14, %r15
incq %rax
incq %r11
movq %r11, %r13
jmp 0xc437
movzbl 0x1(%rdx,%r11), %ebp
addq $0x2, %r11
cmpl $0xa3, %ebp
je 0xbfc1
cmpl $0xa0, %ebp
jne 0xc3ba
movb $0x60, %r14b
jmp 0xbfc4
leaq 0x2(%r11), %r12
cmpq %rcx, %r12
jae 0xc3dc
movzbl (%rdx,%r13), %r13d
leal -0x94(%r13), %ebp
cmpl $0x8, %ebp
ja 0xbfd4
leaq 0x299d(%rip), %rbx      # 0xe8fc
movslq (%rbx,%rbp,4), %r13
addq %rbx, %r13
jmpq *%r13
movzbl 0x2(%rdx,%r11), %r12d
addq $0x3, %r11
cmpl $0x97, %r12d
jle 0xc20d
cmpl $0xab, %r12d
jg 0xc251
cmpl $0x98, %r12d
je 0xc575
cmpl $0x9c, %r12d
je 0xc55d
cmpl $0xa4, %r12d
jne 0xc3ba
movb $0x73, %r14b
jmp 0xbfc4
movq %rbp, %r13
jmp 0xc3df
movb $0x1c, %r14b
movq %r11, %r13
jmp 0xc3df
movq %rbp, %r12
jmp 0xc3bd
cmpl $0x80, %r13d
je 0xc189
cmpl $0x86, %r13d
jne 0xc3bd
movzbl 0x2(%rdx,%r11), %ebp
addq $0x3, %r11
cmpl $0x91, %ebp
je 0xc249
cmpl $0x90, %ebp
jne 0xc3ba
movb $0x1f, %r14b
jmp 0xbfc4
movzbl 0x2(%rdx,%r11), %ebp
addq $0x3, %r11
cmpl $0xa5, %ebp
je 0xc2f4
cmpl $0xa4, %ebp
je 0xc2c9
cmpl $0x8b, %ebp
jne 0xc3ba
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x57, %r14b
jmp 0xbfc4
movzbl 0x2(%rdx,%r11), %r12d
addq $0x3, %r11
addl $0xffffff53, %r12d      # imm = 0xFFFFFF53
cmpl $0x6, %r12d
ja 0xc3ba
leaq 0x2904(%rip), %rbx      # 0xe994
movslq (%rbx,%r12,4), %r14
addq %rbx, %r14
jmpq *%r14
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x55, %r14b
jmp 0xbfc4
movzbl 0x2(%rdx,%r11), %r12d
addq $0x3, %r11
addl $0xffffff60, %r12d      # imm = 0xFFFFFF60
cmpl $0x6, %r12d
ja 0xc3ba
leaq 0x28c9(%rip), %rbx      # 0xe9b0
movslq (%rbx,%r12,4), %r12
addq %rbx, %r12
jmpq *%r12
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x41, %r14b
jmp 0xbfc4
movzbl 0x2(%rdx,%r11), %r12d
addq $0x3, %r11
addl $0xffffff7f, %r12d      # imm = 0xFFFFFF7F
cmpl $0x1c, %r12d
ja 0xc3ba
leaq 0x27e2(%rip), %rbx      # 0xe920
movslq (%rbx,%r12,4), %r12
addq %rbx, %r12
jmpq *%r12
movb $0x64, %r14b
jmp 0xbfc4
leaq 0x3(%r11), %r13
cmpb $-0x6d, 0x2(%rdx,%r11)
jne 0xc1c4
movl %r8d, %r11d
andl $-0x6, %r11d
incl %r11d
testb $0x4, %r8b
cmovnel %r11d, %r8d
movl %r8d, %r11d
andl $0x3, %r11d
cmpl $0x2, %r11d
sete %r14b
negb %r14b
orb $0x7a, %r14b
jmp 0xc3df
leaq 0x3(%r11), %r13
cmpb $-0x5e, 0x2(%rdx,%r11)
jne 0xc1c4
movl %r8d, %r11d
andl $-0x6, %r11d
testb $0x4, %r8b
cmovnel %r11d, %r8d
movl %r8d, %r11d
notl %r11d
andl $0x3, %r11d
cmpl $0x1, %r11d
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x51, %r14b
jmp 0xc3df
movq %r13, %r12
jmp 0xc3bd
movzbl 0x3(%rdx,%r11), %r12d
addq $0x4, %r11
cmpl $0x94, %r12d
jg 0xc280
addl $0xffffff7e, %r12d      # imm = 0xFFFFFF7E
cmpl $0xd, %r12d
ja 0xc3ba
leaq 0x2689(%rip), %rbx      # 0xe884
movslq (%rbx,%r12,4), %r12
addq %rbx, %r12
jmpq *%r12
movb $0x77, %r14b
jmp 0xbfc4
cmpl $0x8b, %r12d
jle 0xc39c
cmpl $0x8c, %r12d
je 0xc56d
cmpl $0x90, %r12d
je 0xc555
cmpl $0x94, %r12d
jne 0xc3ba
movb $0x6d, %r14b
jmp 0xbfc4
movb $0x1e, %r14b
jmp 0xbfc4
cmpl $0xac, %r12d
je 0xc57d
cmpl $0xb4, %r12d
je 0xc565
cmpl $0xbc, %r12d
jne 0xc3ba
movb $0x5b, %r14b
jmp 0xbfc4
cmpl $0x95, %r12d
je 0xc5ad
cmpl $0x98, %r12d
je 0xc5d2
cmpl $0x99, %r12d
jne 0xc3ba
movl %r8d, %ebp
andl $-0x6, %ebp
incl %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
andl $0x3, %ebp
cmpl $0x2, %ebp
sete %r14b
negb %r14b
jmp 0xc2eb
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x69, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
jmp 0xc5f2
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x53, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x58, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x5a, %r14b
jmp 0xbfc4
cmpl $0x80, %r12d
je 0xc54a
cmpl $0x82, %r12d
jne 0xc3ba
movb $0x5d, %r14b
jmp 0xbfc4
movq %r11, %r12
cmpq %rcx, %r12
jae 0xc3dc
movb (%rdx,%r12), %r11b
andb $-0x40, %r11b
negb %r11b
jno 0xc3dc
incq %r12
cmpq %r12, %rcx
jne 0xc3c2
movq %rcx, %r13
jmp 0xc3df
movq %r12, %r13
testb %r14b, %r14b
js 0xc419
leaq 0x1(%r15), %r11
cmpq %rsi, %r11
jae 0xc434
leal -0x60(%r14), %ebp
leal 0x40(%r14), %r12d
leal 0x20(%r14), %ebx
cmpb $0x40, %r14b
movzbl %r14b, %r14d
movzbl %bl, %ebx
cmovbl %r14d, %ebx
cmpb $-0x40, %bpl
movzbl %r12b, %ebp
cmovael %ebx, %ebp
movb %bpl, (%rdi,%r15)
jmp 0xc42b
testb %r10b, %r10b
je 0xc437
leaq 0x1(%r15), %r11
cmpq %rsi, %r11
jae 0xc434
movb %r10b, (%rdi,%r15)
movb $0x0, 0x1(%rdi,%r15)
movq %r11, %r15
incq %rax
movq %r13, %r11
cmpq %rcx, %r13
jb 0xbcaa
jmp 0xc844
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x4e, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x4b, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x4a, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x49, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x4d, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x56, %r14b
jmp 0xbfc4
movq %r11, %r13
movb $0x40, %r14b
jmp 0xc3df
movb $0x6e, %r14b
jmp 0xbfc4
movb $0x6b, %r14b
jmp 0xbfc4
movb $0x71, %r14b
jmp 0xbfc4
movb $0x70, %r14b
jmp 0xbfc4
movb $0x7d, %r14b
jmp 0xbfc4
movb $0x72, %r14b
jmp 0xbfc4
movb $0x5c, %r14b
jmp 0xbfc4
movb $0x6a, %r14b
jmp 0xbfc4
movb $0x76, %r14b
jmp 0xbfc4
movb $0x78, %r14b
jmp 0xbfc4
movb $0x68, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
incl %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
andl $0x3, %ebp
cmpl $0x2, %ebp
sete %r14b
negb %r14b
jmp 0xbd70
movl %r8d, %ebp
andl $-0x6, %ebp
incl %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
andl $0x3, %ebp
cmpl $0x2, %ebp
sete %r14b
negb %r14b
orb $0x5f, %r14b
jmp 0xbfc4
movb $0x7f, %r14b
jmp 0xbfc4
movb $0x7b, %r14b
jmp 0xbfc4
movb $0x7e, %r14b
jmp 0xbfc4
movb $0x79, %r14b
jmp 0xbfc4
movb $0x75, %r14b
jmp 0xbfc4
movb $0x6c, %r14b
jmp 0xbfc4
movb $0x6f, %r14b
jmp 0xbfc4
movb $0x65, %r14b
jmp 0xbfc4
movb $0x66, %r14b
jmp 0xbfc4
movb $0x7c, %r14b
jmp 0xbfc4
movb $0x62, %r14b
jmp 0xbfc4
movb $0x61, %r14b
jmp 0xbfc4
movb $0x67, %r14b
jmp 0xbfc4
movb $0x63, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x50, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x48, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x44, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x7a, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x47, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x46, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x52, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x59, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x45, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x4f, %r14b
jmp 0xbfc4
movl %r8d, %ebp
andl $-0x6, %ebp
testb $0x4, %r8b
cmovnel %ebp, %r8d
movl %r8d, %ebp
notl %ebp
andl $0x3, %ebp
cmpl $0x1, %ebp
movl $0x0, %r14d
sbbl %r14d, %r14d
orb $0x4c, %r14b
jmp 0xbfc4
incq %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %eax
retq