// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %r8d, %r11d
movq %rcx, %r15
movb 0x38(%rsp), %r10b
movl $0x28000001, %ebx       # imm = 0x28000001
leaq 0x28a4(%rip), %r14      # 0xe8fc
xorl %ecx, %ecx
xorl %r12d, %r12d
xorl %eax, %eax
addq $0x2, %rcx
leaq -0x2(%rcx), %r8
cmpq %r15, %r8
jae 0xc9c3
movzbl -0x2(%rdx,%rcx), %ebp
cmpl $0x1f, %ebp
ja 0xc0bf
testl %r9d, %r9d
jne 0xc0ba
cmpl $0x3, %ebp
je 0xc0a0
cmpl $0xd, %ebp
je 0xc098
cmpl $0xa, %ebp
je 0xc09d
cmpl $0x8, %ebp
jne 0xc0ba
movb $0x14, %bpl
jmp 0xc0a0
movb $-0x73, %bpl
jmp 0xc0a0
movb $0xd, %bpl
leaq 0x1(%r12), %r8
cmpq %rsi, %r8
jae 0xc0b7
movb %bpl, (%rdi,%r12)
movb $0x0, 0x1(%rdi,%r12)
movq %r8, %r12
incq %rax
incq %rcx
jmp 0xc063
leaq -0x1(%rcx), %r13
testb %bpl, %bpl
js 0xc0f4
movl %ebp, %ecx
andb $0x60, %cl
cmpb $0x20, %cl
jne 0xc19e
movl %r11d, %r8d
leal -0x60(%rbp), %ecx
cmpb $-0x41, %cl
ja 0xc1e2
movq %r13, %rcx
movl %r8d, %r11d
addb $0x40, %bpl
jmp 0xc1fa
cmpq %r15, %r13
jae 0xc1c6
cmpl $0xc2, %ebp
je 0xc366
cmpl $0xcf, %ebp
je 0xc25f
cmpl $0xe2, %ebp
je 0xc29d
cmpl $0xf0, %ebp
jne 0xc5e1
cmpq %r15, %rcx
jae 0xc1c9
cmpb $-0x61, -0x1(%rdx,%rcx)
jne 0xc5de
leaq 0x1(%rcx), %r13
cmpq %r15, %r13
jae 0xc1c6
movzbl (%rdx,%rcx), %r8d
cmpl $0xad, %r8d
je 0xc54c
cmpl $0xae, %r8d
jne 0xc5e1
movzbl 0x1(%rdx,%rcx), %r8d
addq $0x2, %rcx
leal -0x82(%r8), %r13d
cmpl $0xd, %r13d
ja 0xc59a
leaq 0x26f9(%rip), %rbp      # 0xe884
movslq (%rbp,%r13,4), %r8
addq %rbp, %r8
jmpq *%r8
movb $0x77, %bpl
jmp 0xc0eb
leal -0x61(%rbp), %ecx
cmpb $0x19, %cl
ja 0xc219
movl %r11d, %r8d
andl $-0x6, %r8d
incl %r8d
testb $0x4, %r11b
cmovel %r11d, %r8d
movl %r8d, %ecx
andl $0x3, %ecx
cmpl $0x2, %ecx
jne 0xc238
movl %r8d, %r11d
movq %r13, %rcx
testb %r10b, %r10b
je 0xc05f
leaq 0x1(%r12), %r8
cmpq %rsi, %r8
jae 0xc211
movb %r10b, (%rdi,%r12)
jmp 0xc208
leal 0x20(%rbp), %ecx
cmpb $0x40, %bpl
movzbl %bpl, %r11d
movzbl %cl, %ebp
cmovbl %r11d, %ebp
movq %r13, %rcx
movl %r8d, %r11d
leaq 0x1(%r12), %r8
cmpq %rsi, %r8
jae 0xc211
movb %bpl, (%rdi,%r12)
movb $0x0, 0x1(%rdi,%r12)
movq %r8, %r12
incq %rax
jmp 0xc05f
leal -0x41(%rbp), %ecx
cmpb $0x19, %cl
ja 0xc240
leal -0x40(%rbp), %ecx
testb $0x1, %r11b
movzbl %bpl, %r8d
movzbl %cl, %ebp
cmovnel %r8d, %ebp
jmp 0xc0d6
addl $-0x60, %ebp
jmp 0xc0d9
movl %ebp, %ecx
addl $-0x40, %ecx
cmpl $0x1d, %ecx
ja 0xc1c6
btl %ecx, %ebx
jae 0xc1c6
addl $-0x40, %ebp
jmp 0xc0d6
cmpq %r15, %rcx
jae 0xc1c9
xorl %r8d, %r8d
cmpb -0x1(%rdx,%rcx), %r8b
jno 0xc5de
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x7e, %bpl
testb $0x3, %r8b
je 0xc1c9
jmp 0xc1fa
cmpq %r15, %rcx
jae 0xc1c9
movzbl -0x1(%rdx,%rcx), %r8d
leal -0x94(%r8), %r13d
cmpl $0x8, %r13d
ja 0xc392
movslq (%r14,%r13,4), %r8
addq %r14, %r8
jmpq *%r8
movzbl (%rdx,%rcx), %r8d
incq %rcx
cmpl $0x80, %r8d
je 0xc77b
cmpl $0x82, %r8d
je 0xc753
cmpl $0x8c, %r8d
je 0xc79b
cmpl $0x90, %r8d
je 0xc773
cmpl $0x94, %r8d
je 0xc76b
cmpl $0x98, %r8d
je 0xc793
cmpl $0x9c, %r8d
je 0xc75b
cmpl $0xa4, %r8d
je 0xc763
cmpl $0xac, %r8d
je 0xc783
cmpl $0xb4, %r8d
je 0xc78b
cmpl $0xbc, %r8d
jne 0xc5de
movb $0x7b, %bpl
jmp 0xc1fa
movzbl -0x1(%rdx,%rcx), %r8d
cmpl $0xa0, %r8d
je 0xc38a
cmpl $0xa3, %r8d
jne 0xc5de
movb $0x1c, %bpl
jmp 0xc0eb
movb $0x60, %bpl
jmp 0xc0eb
cmpl $0x80, %r8d
je 0xc51f
cmpl $0x86, %r8d
jne 0xc5de
movzbl (%rdx,%rcx), %r8d
incq %rcx
cmpl $0x90, %r8d
je 0xc592
cmpl $0x91, %r8d
jne 0xc5de
movb $0x1e, %bpl
jmp 0xc0eb
movzbl (%rdx,%rcx), %r8d
incq %rcx
cmpl $0x8b, %r8d
je 0xc631
cmpl $0xa4, %r8d
je 0xc60a
cmpl $0xa5, %r8d
jne 0xc5de
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x7f, %bpl
jmp 0xc28e
movzbl (%rdx,%rcx), %r8d
incq %rcx
addl $0xffffff53, %r8d       # imm = 0xFFFFFF53
cmpl $0x6, %r8d
ja 0xc5de
leaq 0x2552(%rip), %r13      # 0xe994
movslq (%r13,%r8,4), %r8
addq %r13, %r8
jmpq *%r8
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x75, %bpl
jmp 0xc28e
movzbl (%rdx,%rcx), %r8d
incq %rcx
addl $0xffffff60, %r8d       # imm = 0xFFFFFF60
cmpl $0x6, %r8d
ja 0xc5de
leaq 0x2526(%rip), %r13      # 0xe9b0
movslq (%r13,%r8,4), %r8
addq %r13, %r8
jmpq *%r8
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x61, %bpl
jmp 0xc28e
movzbl (%rdx,%rcx), %r8d
incq %rcx
addl $0xffffff7f, %r8d       # imm = 0xFFFFFF7F
cmpl $0x1c, %r8d
ja 0xc5de
leaq 0x244e(%rip), %r13      # 0xe920
movslq (%r13,%r8,4), %r8
addq %r13, %r8
jmpq *%r8
movb $0x64, %bpl
jmp 0xc0eb
leaq 0x1(%rcx), %r13
cmpb $-0x6d, (%rdx,%rcx)
jne 0xc5e1
movl %r11d, %ecx
andl $-0x6, %ecx
incl %ecx
testb $0x4, %r11b
cmovnel %ecx, %r11d
movl %r11d, %r8d
andl $0x3, %r8d
movb $0x7a, %bpl
movq %r13, %rcx
cmpl $0x2, %r8d
jne 0xc0eb
jmp 0xc1c9
leaq 0x1(%rcx), %r13
cmpb $-0x5e, (%rdx,%rcx)
jne 0xc5e1
movl %r11d, %ecx
andl $-0x6, %ecx
testb $0x4, %r11b
cmovnel %ecx, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x71, %bpl
movq %r13, %rcx
jmp 0xc28e
movzbl 0x1(%rdx,%rcx), %r8d
addq $0x2, %rcx
addl $0xffffff50, %r8d       # imm = 0xFFFFFF50
cmpl $0xf, %r8d
ja 0xc5de
leaq 0x2352(%rip), %r13      # 0xe8bc
movslq (%r13,%r8,4), %r8
addq %r13, %r8
jmpq *%r8
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x74, %bpl
jmp 0xc28e
movb $0x1f, %bpl
jmp 0xc0eb
cmpl $0x95, %r8d
je 0xc7e9
cmpl $0x98, %r8d
je 0xc7cb
cmpl $0x99, %r8d
jne 0xc5de
movl %r11d, %r8d
andl $-0x6, %r8d
incl %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
andl $0x3, %r8d
movb $0x69, %bpl
jmp 0xc510
movq %rcx, %r13
cmpq %r15, %r13
movq %r15, %rcx
cmovaq %r13, %rcx
cmpq %r15, %r13
jae 0xc1c9
movb (%rdx,%r13), %r8b
andb $-0x40, %r8b
negb %r8b
jno 0xc1c6
incq %r13
jmp 0xc5eb
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x69, %bpl
testb $0x3, %r8b
jne 0xc0eb
jmp 0xc1c9
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x77, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x73, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x78, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x7a, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x6e, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x6b, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x6a, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x69, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x6d, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x76, %bpl
jmp 0xc28e
movb $0x7d, %bpl
jmp 0xc1fa
movb $0x6b, %bpl
jmp 0xc0eb
movb $0x73, %bpl
jmp 0xc0eb
movb $0x6d, %bpl
jmp 0xc0eb
movb $0x6e, %bpl
jmp 0xc0eb
movb $0x60, %bpl
jmp 0xc1fa
movb $0x72, %bpl
jmp 0xc0eb
movb $0x71, %bpl
jmp 0xc0eb
movb $0x7d, %bpl
jmp 0xc0eb
movb $0x70, %bpl
jmp 0xc0eb
movb $0x7c, %bpl
jmp 0xc1fa
movb $0x76, %bpl
jmp 0xc0eb
movb $0x6a, %bpl
jmp 0xc0eb
movb $0x78, %bpl
jmp 0xc0eb
movb $0x68, %bpl
jmp 0xc0eb
movl %r11d, %r8d
andl $-0x6, %r8d
incl %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
andl $0x3, %r8d
movb $0x7f, %bpl
jmp 0xc805
movl %r11d, %r8d
andl $-0x6, %r8d
incl %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
andl $0x3, %r8d
movb $0x7e, %bpl
cmpl $0x2, %r8d
je 0xc1c9
jmp 0xc1fa
movb $0x7f, %bpl
jmp 0xc0eb
movb $0x7b, %bpl
jmp 0xc0eb
movb $0x7e, %bpl
jmp 0xc0eb
movb $0x79, %bpl
jmp 0xc0eb
movb $0x75, %bpl
jmp 0xc0eb
movb $0x6c, %bpl
jmp 0xc0eb
movb $0x6f, %bpl
jmp 0xc0eb
movb $0x65, %bpl
jmp 0xc0eb
movb $0x66, %bpl
jmp 0xc0eb
movb $0x7c, %bpl
jmp 0xc0eb
movb $0x62, %bpl
jmp 0xc0eb
movb $0x61, %bpl
jmp 0xc0eb
movb $0x67, %bpl
jmp 0xc0eb
movb $0x63, %bpl
jmp 0xc0eb
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x70, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x68, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x64, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x7a, %bpl
jmp 0xc622
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x67, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x66, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x72, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x79, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x65, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x6f, %bpl
jmp 0xc28e
movl %r11d, %r8d
andl $-0x6, %r8d
testb $0x4, %r11b
cmovnel %r8d, %r11d
movl %r11d, %r8d
notl %r8d
movb $0x6c, %bpl
jmp 0xc28e
incq %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq