#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
char *strdup(char *);
int strlen(char const *);
char *escape(const char *in) {
  const char hexnumbers[] = "0123456789ABCDEF";
  int const n = strlen(in);
  int const nn = n * 3 + 1;
  char outs[nn];
  char *out = outs;
  for (int i = 0; i < n; ++i) {
    char c = *in++;
    if (c >= 0x61 && c <= 0x7A || c >= 0x41 && c <= 0x5A ||
        c >= 0x30 && c <= 0x39 || c == 0x2D || c == 0x2E || c == 0x5F ||
        c == 0x7E)
      *out++ = c;
    else {
      *out++ = '%';
      *out++ = hexnumbers[((c & 0xf0) >> 4) & 0xf];
      *out++ = hexnumbers[(c & 0xf) & 0xf];
    }
  }
  *out = '\0';
  return strdup(outs);
}


x86 assembly (clang, -O0):
<escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movaps 0xecd(%rip),%xmm0
movaps %xmm0,-0x20(%rbp)
movb   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
mov    %ecx,%eax
lea    0x1(%rax,%rax,2),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x50(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x38(%rbp)
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12ad <escape+0x18d>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x45(%rbp)
movsbl -0x45(%rbp),%eax
cmp    $0x61,%eax
jl     11c0 <escape+0xa0>
movsbl -0x45(%rbp),%eax
cmp    $0x7a,%eax
jle    1228 <escape+0x108>
movsbl -0x45(%rbp),%eax
cmp    $0x41,%eax
jl     11da <escape+0xba>
movsbl -0x45(%rbp),%eax
cmp    $0x5a,%eax
jle    1228 <escape+0x108>
movsbl -0x45(%rbp),%eax
cmp    $0x30,%eax
jl     11f4 <escape+0xd4>
movsbl -0x45(%rbp),%eax
cmp    $0x39,%eax
jle    1228 <escape+0x108>
movsbl -0x45(%rbp),%eax
cmp    $0x2d,%eax
je     1228 <escape+0x108>
movsbl -0x45(%rbp),%eax
cmp    $0x2e,%eax
je     1228 <escape+0x108>
movsbl -0x45(%rbp),%eax
cmp    $0x5f,%eax
je     1228 <escape+0x108>
movsbl -0x45(%rbp),%eax
cmp    $0x7e,%eax
jne    1241 <escape+0x121>
mov    -0x45(%rbp),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
jmp    129a <escape+0x17a>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x40(%rbp)
movb   $0x25,(%rax)
movsbl -0x45(%rbp),%eax
and    $0xf0,%eax
sar    $0x4,%eax
and    $0xf,%eax
cltq
mov    -0x20(%rbp,%rax,1),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
movsbl -0x45(%rbp),%eax
and    $0xf,%eax
and    $0xf,%eax
cltq
mov    -0x20(%rbp,%rax,1),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
jmp    129f <escape+0x17f>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1186 <escape+0x66>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
call   1040 <strdup@plt>
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
lea    (%rax,%rax,2),%ecx
add    $0x1,%ecx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,%rsp
mov    %rdi,%rdx
test   %eax,%eax
jle    11c9 <escape+0xa9>
mov    %eax,%eax
xor    %ecx,%ecx
lea    0xea8(%rip),%r8
mov    %rdi,%rdx
nopl   0x0(%rax,%rax,1)
movzbl (%r14,%rcx,1),%esi
mov    %esi,%ebx
and    $0xdf,%bl
add    $0xbf,%bl
cmp    $0x1a,%bl
jb     1190 <escape+0x70>
lea    -0x30(%rsi),%ebx
cmp    $0xa,%bl
jb     1190 <escape+0x70>
lea    -0x2d(%rsi),%ebx
cmp    $0x2,%bl
jb     1190 <escape+0x70>
cmp    $0x7e,%sil
je     1190 <escape+0x70>
cmp    $0x5f,%sil
jne    11a1 <escape+0x81>
xchg   %ax,%ax
mov    %sil,(%rdx)
add    $0x1,%rdx
add    $0x1,%rcx
cmp    %ecx,%eax
jne    1160 <escape+0x40>
jmp    11c9 <escape+0xa9>
movzbl %sil,%r9d
movb   $0x25,(%rdx)
mov    %r9,%rsi
shr    $0x4,%rsi
movzbl (%rsi,%r8,1),%ebx
mov    %bl,0x1(%rdx)
and    $0xf,%r9d
movzbl (%r9,%r8,1),%ebx
mov    %bl,0x2(%rdx)
add    $0x3,%rdx
jmp    1197 <escape+0x77>
movb   $0x0,(%rdx)
call   1040 <strdup@plt>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.escape.hexnumbers = [17 x i8] c"0123456789ABCDEF\00"
define i8* @escape(i8* %0) {
%2 = alloca i8*
%3 = alloca [17 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8
store i8* %0, i8** %2
%11 = bitcast [17 x i8]* %3 to i8*
%12 = load i8*, i8** %2
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = mul nsw i32 %14, 3
%16 = add nsw i32 %15, 1
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %6
%20 = alloca i8, i64 %18
store i64 %18, i64* %7
store i8* %20, i8** %8
store i32 0, i32* %9
br label %21
21: ; preds = %95, %1
%22 = load i32, i32* %9
%23 = load i32, i32* %4
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %98
25: ; preds = %21
%26 = load i8*, i8** %2
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %2
%28 = load i8, i8* %26
store i8 %28, i8* %10
%29 = load i8, i8* %10
%30 = sext i8 %29 to i32
%31 = icmp sge i32 %30, 97
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = load i8, i8* %10
%34 = sext i8 %33 to i32
%35 = icmp sle i32 %34, 122
br i1 %35, label %68, label %36
36: ; preds = %32, %25
%37 = load i8, i8* %10
%38 = sext i8 %37 to i32
%39 = icmp sge i32 %38, 65
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i8, i8* %10
%42 = sext i8 %41 to i32
%43 = icmp sle i32 %42, 90
br i1 %43, label %68, label %44
44: ; preds = %40, %36
%45 = load i8, i8* %10
%46 = sext i8 %45 to i32
%47 = icmp sge i32 %46, 48
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = load i8, i8* %10
%50 = sext i8 %49 to i32
%51 = icmp sle i32 %50, 57
br i1 %51, label %68, label %52
52: ; preds = %48, %44
%53 = load i8, i8* %10
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 45
br i1 %55, label %68, label %56
56: ; preds = %52
%57 = load i8, i8* %10
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 46
br i1 %59, label %68, label %60
60: ; preds = %56
%61 = load i8, i8* %10
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 95
br i1 %63, label %68, label %64
64: ; preds = %60
%65 = load i8, i8* %10
%66 = sext i8 %65 to i32
%67 = icmp eq i32 %66, 126
br i1 %67, label %68, label %72
68: ; preds = %64, %60, %56, %52, %48, %40, %32
%69 = load i8, i8* %10
%70 = load i8*, i8** %8
%71 = getelementptr inbounds i8, i8* %70, i32 1
store i8* %71, i8** %8
store i8 %69, i8* %70
br label %94
72: ; preds = %64
%73 = load i8*, i8** %8
%74 = getelementptr inbounds i8, i8* %73, i32 1
store i8* %74, i8** %8
store i8 37, i8* %73
%75 = load i8, i8* %10
%76 = sext i8 %75 to i32
%77 = and i32 %76, 240
%78 = ashr i32 %77, 4
%79 = and i32 %78, 15
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 %80
%82 = load i8, i8* %81
%83 = load i8*, i8** %8
%84 = getelementptr inbounds i8, i8* %83, i32 1
store i8* %84, i8** %8
store i8 %82, i8* %83
%85 = load i8, i8* %10
%86 = sext i8 %85 to i32
%87 = and i32 %86, 15
%88 = and i32 %87, 15
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 %89
%91 = load i8, i8* %90
%92 = load i8*, i8** %8
%93 = getelementptr inbounds i8, i8* %92, i32 1
store i8* %93, i8** %8
store i8 %91, i8* %92
br label %94
94: ; preds = %72, %68
br label %95
95: ; preds = %94
%96 = load i32, i32* %9
%97 = add nsw i32 %96, 1
store i32 %97, i32* %9
br label %21
98: ; preds = %21
%99 = load i8*, i8** %8
store i8 0, i8* %99
%100 = call i8* @strdup(i8* %20)
%101 = load i8*, i8** %6
ret i8* %100
}

LLVM IR (-O2):
@__const.escape.hexnumbers = [17 x i8] c"0123456789ABCDEF\00"
define noalias i8* @escape(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = mul nsw i32 %2, 3
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = icmp sgt i32 %2, 0
br i1 %7, label %11, label %8
8: ; preds = %40, %1
%9 = phi i8* [ %6, %1 ], [ %41, %40 ]
store i8 0, i8* %9
%10 = call i8* @strdup(i8* %6)
ret i8* %10
11: ; preds = %1, %40
%12 = phi i8* [ %15, %40 ], [ %0, %1 ]
%13 = phi i32 [ %42, %40 ], [ 0, %1 ]
%14 = phi i8* [ %41, %40 ], [ %6, %1 ]
%15 = getelementptr inbounds i8, i8* %12, i64 1
%16 = load i8, i8* %12
%17 = zext i8 %16 to i32
%18 = and i8 %16, -33
%19 = add i8 %18, -65
%20 = icmp ult i8 %19, 26
br i1 %20, label %26, label %21
21: ; preds = %11
%22 = freeze i8 %16
%23 = add i8 %22, -48
%24 = icmp ult i8 %23, 10
br i1 %24, label %26, label %25
25: ; preds = %21
switch i8 %22, label %28 [
i8 126, label %26
i8 95, label %26
i8 46, label %26
i8 45, label %26
]
26: ; preds = %25, %25, %25, %25, %21, %11
%27 = getelementptr inbounds i8, i8* %14, i64 1
store i8 %16, i8* %14
br label %40
28: ; preds = %25
%29 = getelementptr inbounds i8, i8* %14, i64 1
store i8 37, i8* %14
%30 = lshr i32 %17, 4
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.escape.hexnumbers, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = getelementptr inbounds i8, i8* %14, i64 2
store i8 %33, i8* %29
%35 = and i32 %17, 15
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.escape.hexnumbers, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = getelementptr inbounds i8, i8* %14, i64 3
store i8 %38, i8* %34
br label %40
40: ; preds = %28, %26
%41 = phi i8* [ %27, %26 ], [ %39, %28 ]
%42 = add nuw nsw i32 %13, 1
%43 = icmp eq i32 %42, %2
br i1 %43, label %8, label %11
}