// File: /workspace/github_c_makefile_star2_O3/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 0xd108
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, %r11
movb 0x38(%rsp), %r10b
movl $0x28000001, %ebx       # imm = 0x28000001
leaq 0x31aa(%rip), %r14      # 0xf8fc
xorl %eax, %eax
xorl %ebp, %ebp
xorl %r12d, %r12d
movzbl (%rdx,%r12), %r13d
cmpl $0x1f, %r13d
ja 0xc793
testl %r9d, %r9d
jne 0xc9cc
cmpl $0x9, %r13d
jg 0xc8a0
cmpl $0x3, %r13d
je 0xc9b4
cmpl $0x8, %r13d
jne 0xc9cc
movb $0x14, %r13b
jmp 0xc9b4
leaq 0x1(%r12), %rcx
testb %r13b, %r13b
js 0xc7ce
movl %r13d, %r15d
andb $0x60, %r15b
cmpb $0x20, %r15b
jne 0xc838
movl %r8d, %r15d
leal -0x60(%r13), %r8d
cmpb $-0x41, %r8b
ja 0xc870
movq %rcx, %r12
movl %r15d, %r8d
addb $0x40, %r13b
jmp 0xc88a
cmpq %r11, %rcx
jae 0xc868
cmpl $0xe1, %r13d
jg 0xc8e3
cmpl $0xc2, %r13d
je 0xc9dc
cmpl $0xcf, %r13d
jne 0xc9d4
addq $0x2, %r12
cmpq %r11, %r12
jae 0xce02
xorl %r15d, %r15d
cmpb (%rdx,%rcx), %r15b
jno 0xcddf
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x7e, %r13b
testb $0x3, %cl
jne 0xc88a
jmp 0xce02
leal -0x61(%r13), %r15d
cmpb $0x19, %r15b
ja 0xc8bc
movl %r8d, %r15d
andl $-0x6, %r15d
incl %r15d
testb $0x4, %r8b
cmovel %r8d, %r15d
movl %r15d, %r8d
andl $0x3, %r8d
cmpl $0x2, %r8d
jne 0xc984
movl %r15d, %r8d
movq %rcx, %r12
jmp 0xce02
leal 0x20(%r13), %r8d
cmpb $0x40, %r13b
movzbl %r13b, %r12d
movzbl %r8b, %r13d
cmovbl %r12d, %r13d
movq %rcx, %r12
movl %r15d, %r8d
leaq 0x1(%rbp), %rcx
cmpq %rsi, %rcx
jae 0xce1c
movb %r13b, (%rdi,%rbp)
jmp 0xce14
cmpl $0xa, %r13d
je 0xc9b1
cmpl $0xd, %r13d
jne 0xc9cc
movb $-0x73, %r13b
jmp 0xc9b4
leal -0x41(%r13), %r15d
cmpb $0x19, %r15b
ja 0xc98d
leal -0x40(%r13), %r15d
testb $0x1, %r8b
movzbl %r13b, %r12d
movzbl %r15b, %r13d
cmovnel %r12d, %r13d
jmp 0xc7ae
cmpl $0xe2, %r13d
je 0xca06
cmpl $0xf0, %r13d
jne 0xc9d4
leaq 0x2(%r12), %r13
cmpq %r11, %r13
jae 0xca7f
cmpb $-0x61, (%rdx,%rcx)
jne 0xca8f
leaq 0x3(%r12), %r15
cmpq %r11, %r15
jae 0xcdff
movzbl (%rdx,%r13), %ecx
cmpl $0xae, %ecx
je 0xcc45
cmpl $0xad, %ecx
jne 0xcde2
movzbl 0x3(%rdx,%r12), %ecx
addq $0x4, %r12
addl $0xffffff50, %ecx       # imm = 0xFFFFFF50
cmpl $0xf, %ecx
ja 0xcddf
leaq 0x2f5c(%rip), %r15      # 0xf8bc
movslq (%r15,%rcx,4), %rcx
addq %r15, %rcx
jmpq *%rcx
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x74, %r13b
jmp 0xc82e
addl $-0x60, %r13d
jmp 0xc7b1
movl %r13d, %r15d
addl $-0x40, %r15d
cmpl $0x1d, %r15d
ja 0xc868
btl %r15d, %ebx
jae 0xc868
addl $-0x40, %r13d
jmp 0xc7ae
movb $0xd, %r13b
leaq 0x1(%rbp), %rcx
cmpq %rsi, %rcx
jae 0xc9c9
movb %r13b, (%rdi,%rbp)
movb $0x0, 0x1(%rdi,%rbp)
movq %rcx, %rbp
incq %rax
incq %r12
jmp 0xce1f
movq %rcx, %r15
jmp 0xcde2
movzbl 0x1(%rdx,%r12), %ecx
addq $0x2, %r12
cmpl $0xa3, %ecx
je 0xca87
cmpl $0xa0, %ecx
jne 0xcddf
movb $0x60, %r13b
jmp 0xc7c5
leaq 0x2(%r12), %r15
cmpq %r11, %r15
jae 0xcdff
movzbl (%rdx,%rcx), %ecx
leal -0x94(%rcx), %r13d
cmpl $0x8, %r13d
ja 0xca97
movslq (%r14,%r13,4), %rcx
addq %r14, %rcx
jmpq *%rcx
movzbl 0x2(%rdx,%r12), %ecx
addq $0x3, %r12
cmpl $0x97, %ecx
jle 0xcc82
cmpl $0xab, %ecx
jg 0xccc2
cmpl $0x98, %ecx
je 0xcee7
movb $0x6b, %r13b
cmpl $0x9c, %ecx
je 0xc7c5
cmpl $0xa4, %ecx
jne 0xcddf
movb $0x73, %r13b
jmp 0xc7c5
movq %r13, %r12
jmp 0xce02
movb $0x1c, %r13b
jmp 0xc7c5
movq %r13, %r15
jmp 0xcde2
cmpl $0x80, %ecx
je 0xcc16
cmpl $0x86, %ecx
jne 0xcde2
movzbl 0x2(%rdx,%r12), %ecx
addq $0x3, %r12
cmpl $0x91, %ecx
je 0xccba
cmpl $0x90, %ecx
jne 0xcddf
movb $0x1f, %r13b
jmp 0xc7c5
movzbl 0x2(%rdx,%r12), %ecx
addq $0x3, %r12
cmpl $0xa5, %ecx
je 0xcd54
cmpl $0xa4, %ecx
je 0xcd30
cmpl $0x8b, %ecx
jne 0xcddf
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x77, %r13b
jmp 0xc82e
movzbl 0x2(%rdx,%r12), %ecx
addq $0x3, %r12
addl $0xffffff53, %ecx       # imm = 0xFFFFFF53
cmpl $0x6, %ecx
ja 0xcddf
leaq 0x2e52(%rip), %r15      # 0xf994
movslq (%r15,%rcx,4), %rcx
addq %r15, %rcx
jmpq *%rcx
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x75, %r13b
jmp 0xc82e
movzbl 0x2(%rdx,%r12), %ecx
addq $0x3, %r12
addl $0xffffff60, %ecx       # imm = 0xFFFFFF60
cmpl $0x6, %ecx
ja 0xcddf
leaq 0x2e2a(%rip), %r15      # 0xf9b0
movslq (%r15,%rcx,4), %rcx
addq %r15, %rcx
jmpq *%rcx
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x61, %r13b
jmp 0xc82e
movzbl 0x2(%rdx,%r12), %ecx
addq $0x3, %r12
addl $0xffffff7f, %ecx       # imm = 0xFFFFFF7F
cmpl $0x1c, %ecx
ja 0xcddf
leaq 0x2d56(%rip), %r15      # 0xf920
movslq (%r15,%rcx,4), %rcx
addq %r15, %rcx
jmpq *%rcx
movb $0x64, %r13b
jmp 0xc7c5
leaq 0x3(%r12), %r15
cmpb $-0x6d, 0x2(%rdx,%r12)
jne 0xcde2
movl %r8d, %ecx
andl $-0x6, %ecx
incl %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
andl $0x3, %ecx
movb $0x7a, %r13b
movq %r15, %r12
cmpl $0x2, %ecx
jne 0xc7c5
jmp 0xce02
leaq 0x3(%r12), %r15
cmpb $-0x5e, 0x2(%rdx,%r12)
jne 0xcde2
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x71, %r13b
movq %r15, %r12
jmp 0xc82e
movzbl 0x3(%rdx,%r12), %ecx
addq $0x4, %r12
cmpl $0x94, %ecx
jg 0xccee
addl $0xffffff7e, %ecx       # imm = 0xFFFFFF7E
cmpl $0xd, %ecx
ja 0xcddf
leaq 0x2c13(%rip), %r15      # 0xf884
movslq (%r15,%rcx,4), %rcx
addq %r15, %rcx
jmpq *%rcx
movb $0x77, %r13b
jmp 0xc7c5
cmpl $0x8b, %ecx
jle 0xcdc0
cmpl $0x8c, %ecx
je 0xcedf
cmpl $0x90, %ecx
je 0xcecf
cmpl $0x94, %ecx
jne 0xcddf
movb $0x6d, %r13b
jmp 0xc7c5
movb $0x1e, %r13b
jmp 0xc7c5
cmpl $0xac, %ecx
je 0xceef
cmpl $0xb4, %ecx
je 0xced7
cmpl $0xbc, %ecx
jne 0xcddf
movb $0x7b, %r13b
jmp 0xc88a
cmpl $0x95, %ecx
je 0xcf1f
cmpl $0x98, %ecx
je 0xcf3a
cmpl $0x99, %ecx
jne 0xcddf
movl %r8d, %ecx
andl $-0x6, %ecx
incl %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
andl $0x3, %ecx
movb $0x69, %r13b
jmp 0xcc08
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x69, %r13b
testb $0x3, %cl
jne 0xc7c5
jmp 0xce02
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x7f, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x73, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x78, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x7a, %r13b
jmp 0xc82e
movb $0x60, %r13b
cmpl $0x80, %ecx
je 0xc88a
cmpl $0x82, %ecx
jne 0xcddf
movb $0x7d, %r13b
jmp 0xc88a
movq %r12, %r15
cmpq %r11, %r15
jae 0xcdff
movb (%rdx,%r15), %cl
andb $-0x40, %cl
negb %cl
jno 0xcdff
incq %r15
cmpq %r15, %r11
jne 0xcde7
movq %r11, %r12
jmp 0xce02
movq %r15, %r12
testb %r10b, %r10b
je 0xce1f
leaq 0x1(%rbp), %rcx
cmpq %rsi, %rcx
jae 0xce1c
movb %r10b, (%rdi,%rbp)
movb $0x0, 0x1(%rdi,%rbp)
movq %rcx, %rbp
incq %rax
cmpq %r11, %r12
jb 0xc759
jmp 0xd0fa
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x6e, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x6b, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x6a, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x69, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x6d, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x76, %r13b
jmp 0xc82e
movb $0x6e, %r13b
jmp 0xc7c5
movb $0x71, %r13b
jmp 0xc7c5
movb $0x70, %r13b
jmp 0xc7c5
movb $0x7d, %r13b
jmp 0xc7c5
movb $0x72, %r13b
jmp 0xc7c5
movb $0x7c, %r13b
jmp 0xc88a
movb $0x6a, %r13b
jmp 0xc7c5
movb $0x76, %r13b
jmp 0xc7c5
movb $0x78, %r13b
jmp 0xc7c5
movb $0x68, %r13b
jmp 0xc7c5
movl %r8d, %ecx
andl $-0x6, %ecx
incl %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
andl $0x3, %ecx
movb $0x7e, %r13b
jmp 0xcf53
movl %r8d, %ecx
andl $-0x6, %ecx
incl %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
andl $0x3, %ecx
movb $0x7f, %r13b
cmpl $0x2, %ecx
jne 0xc88a
jmp 0xce02
movb $0x7f, %r13b
jmp 0xc7c5
movb $0x7b, %r13b
jmp 0xc7c5
movb $0x7e, %r13b
jmp 0xc7c5
movb $0x79, %r13b
jmp 0xc7c5
movb $0x75, %r13b
jmp 0xc7c5
movb $0x6c, %r13b
jmp 0xc7c5
movb $0x6f, %r13b
jmp 0xc7c5
movb $0x65, %r13b
jmp 0xc7c5
movb $0x66, %r13b
jmp 0xc7c5
movb $0x7c, %r13b
jmp 0xc7c5
movb $0x62, %r13b
jmp 0xc7c5
movb $0x61, %r13b
jmp 0xc7c5
movb $0x67, %r13b
jmp 0xc7c5
movb $0x63, %r13b
jmp 0xc7c5
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x70, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x68, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x64, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x7a, %r13b
jmp 0xcd46
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x67, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x66, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x72, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x79, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x65, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x6f, %r13b
jmp 0xc82e
movl %r8d, %ecx
andl $-0x6, %ecx
testb $0x4, %r8b
cmovnel %ecx, %r8d
movl %r8d, %ecx
notl %ecx
movb $0x6c, %r13b
jmp 0xc82e
incq %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %eax
retq