void ps_convert(const unsigned char *string, unsigned char *ps_string) {
  const unsigned char *s;
  unsigned char *p = ps_string;
  for (s = string; *s; s++) {
    switch (*s) {
    case '(':
    case ')':
    case '\\':
      *p++ = '\\';
      *p++ = *s;
      break;
    case 0xC2:
      *p++ = *++s;
      break;
    case 0xC3:
      *p++ = *++s + 64;
      break;
    default:
      if (*s < 0x80) {
        *p++ = *s;
      }
      break;
    }
  }
  *p = '\0';
}


x86 assembly (clang, -O0):
<ps_convert>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1243 <ps_convert+0x143>
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,-0x24(%rbp)
add    $0xffffffd8,%eax
sub    $0x2,%eax
jb     117b <ps_convert+0x7b>
jmp    1144 <ps_convert+0x44>
mov    -0x24(%rbp),%eax
sub    $0x5c,%eax
je     117b <ps_convert+0x7b>
jmp    1155 <ps_convert+0x55>
mov    -0x24(%rbp),%eax
sub    $0xc2,%eax
je     11a9 <ps_convert+0xa9>
jmp    1168 <ps_convert+0x68>
mov    -0x24(%rbp),%eax
sub    $0xc3,%eax
je     11d1 <ps_convert+0xd1>
jmp    11ff <ps_convert+0xff>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    122d <ps_convert+0x12d>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    0x1(%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    122d <ps_convert+0x12d>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movzbl 0x1(%rax),%eax
add    $0x40,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    122d <ps_convert+0x12d>
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x80,%eax
jge    1228 <ps_convert+0x128>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    122d <ps_convert+0x12d>
jmp    1232 <ps_convert+0x132>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    111c <ps_convert+0x1c>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<ps_convert>:
movabs $0x10000000000003,%rax
jmp    111e <ps_convert+0x1e>
movzbl 0x1(%rdi),%ecx
add    $0x1,%rdi
mov    %cl,(%rsi)
add    $0x1,%rsi
add    $0x1,%rdi
movzbl (%rdi),%ecx
cmp    $0x27,%cl
jle    1150 <ps_convert+0x50>
lea    -0x28(%rcx),%edx
cmp    $0x34,%dl
ja     116b <ps_convert+0x6b>
movzbl %dl,%edx
bt     %rdx,%rax
jae    116b <ps_convert+0x6b>
movb   $0x5c,(%rsi)
movzbl (%rdi),%ecx
mov    %cl,0x1(%rsi)
add    $0x2,%rsi
add    $0x1,%rdi
jmp    111e <ps_convert+0x1e>
nopw   0x0(%rax,%rax,1)
cmp    $0xc2,%cl
je     110c <ps_convert+0xc>
cmp    $0xc3,%cl
jne    1167 <ps_convert+0x67>
movzbl 0x1(%rdi),%ecx
add    $0x1,%rdi
add    $0x40,%cl
jmp    1114 <ps_convert+0x14>
test   %cl,%cl
je     117b <ps_convert+0x7b>
test   %cl,%cl
js     1144 <ps_convert+0x44>
mov    %cl,(%rsi)
add    $0x1,%rsi
add    $0x1,%rdi
jmp    111e <ps_convert+0x1e>
movb   $0x0,(%rsi)
ret

LLVM IR (-O0):
define void @ps_convert(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
store i8* %7, i8** %6
%8 = load i8*, i8** %3
store i8* %8, i8** %5
br label %9
9: ; preds = %51, %2
%10 = load i8*, i8** %5
%11 = load i8, i8* %10
%12 = icmp ne i8 %11, 0
br i1 %12, label %13, label %54
13: ; preds = %9
%14 = load i8*, i8** %5
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
switch i32 %16, label %39 [
i32 40, label %17
i32 41, label %17
i32 92, label %17
i32 194, label %24
i32 195, label %30
]
17: ; preds = %13, %13, %13
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %6
store i8 92, i8* %18
%20 = load i8*, i8** %5
%21 = load i8, i8* %20
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %6
store i8 %21, i8* %22
br label %50
24: ; preds = %13
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %5
%27 = load i8, i8* %26
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %6
store i8 %27, i8* %28
br label %50
30: ; preds = %13
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %5
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = add nsw i32 %34, 64
%36 = trunc i32 %35 to i8
%37 = load i8*, i8** %6
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %6
store i8 %36, i8* %37
br label %50
39: ; preds = %13
%40 = load i8*, i8** %5
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = icmp slt i32 %42, 128
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = load i8*, i8** %5
%46 = load i8, i8* %45
%47 = load i8*, i8** %6
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %6
store i8 %46, i8* %47
br label %49
49: ; preds = %44, %39
br label %50
50: ; preds = %49, %30, %24, %17
br label %51
51: ; preds = %50
%52 = load i8*, i8** %5
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %5
br label %9
54: ; preds = %9
%55 = load i8*, i8** %6
store i8 0, i8* %55
ret void
}

LLVM IR (-O2):
define void @ps_convert(i8* nocapture %0, i8* nocapture writeonly %1) {
br label %3
3: ; preds = %24, %2
%4 = phi i8* [ %0, %2 ], [ %27, %24 ]
%5 = phi i8* [ %1, %2 ], [ %26, %24 ]
%6 = load i8, i8* %4
switch i8 %6, label %20 [
i8 0, label %28
i8 40, label %7
i8 41, label %7
i8 92, label %7
i8 -62, label %11
i8 -61, label %15
]
7: ; preds = %3, %3, %3
%8 = getelementptr inbounds i8, i8* %5, i64 1
store i8 92, i8* %5
%9 = load i8, i8* %4
%10 = getelementptr inbounds i8, i8* %5, i64 2
store i8 %9, i8* %8
br label %24
11: ; preds = %3
%12 = getelementptr inbounds i8, i8* %4, i64 1
%13 = load i8, i8* %12
%14 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %13, i8* %5
br label %24
15: ; preds = %3
%16 = getelementptr inbounds i8, i8* %4, i64 1
%17 = load i8, i8* %16
%18 = add i8 %17, 64
%19 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %18, i8* %5
br label %24
20: ; preds = %3
%21 = icmp sgt i8 %6, -1
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %6, i8* %5
br label %24
24: ; preds = %7, %11, %15, %22, %20
%25 = phi i8* [ %4, %22 ], [ %4, %20 ], [ %16, %15 ], [ %12, %11 ], [ %4, %7 ]
%26 = phi i8* [ %23, %22 ], [ %5, %20 ], [ %19, %15 ], [ %14, %11 ], [ %10, %7 ]
%27 = getelementptr inbounds i8, i8* %25, i64 1
br label %3
28: ; preds = %3
store i8 0, i8* %5
ret void
}