#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 */
scalar_t__ isalpha(char const);
char *malloc(int);
int strlen(char const *);
char *escape(const char *s) {
  static const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                             '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  char *buf, *bufp;
  const char *p = s;
  buf = malloc((strlen(s) * 3) + 1);
  if (!buf)
    return NULL;
  bufp = buf;
  while (*p) {
    if (isalpha(*p)) {
      *bufp++ = *p++;
    } else {
      *bufp++ = '%';
      *bufp++ = hex[*p >> 4];
      *bufp++ = hex[*p++ & 0x0F];
    }
  }
  *bufp = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
imul   $0x3,%eax,%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1174 <escape+0x44>
movq   $0x0,-0x8(%rbp)
jmp    1247 <escape+0x117>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     1238 <escape+0x108>
mov    -0x28(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isalpha@plt>
cmp    $0x0,%rax
je     11c6 <escape+0x96>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    1233 <escape+0x103>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x25,(%rax)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
sar    $0x4,%eax
movslq %eax,%rcx
lea    0xe14(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movsbl (%rax),%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xde1(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    117c <escape+0x4c>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <strlen@plt>
lea    (%rax,%rax,2),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11c9 <escape+0x99>
mov    (%r15),%al
mov    %r14,%rbx
test   %al,%al
je     11c6 <escape+0x96>
lea    0xe9c(%rip),%r12
mov    %r14,%rbx
jmp    1185 <escape+0x55>
nopl   0x0(%rax)
movzbl (%rcx),%eax
add    %rsi,%rbx
mov    %al,(%rdx)
movzbl 0x1(%r15),%eax
add    $0x1,%r15
test   %al,%al
je     11c6 <escape+0x96>
movsbl %al,%edi
call   1050 <isalpha@plt>
mov    $0x1,%esi
mov    %r15,%rcx
mov    %rbx,%rdx
test   %rax,%rax
jne    1170 <escape+0x40>
movb   $0x25,(%rbx)
movzbl (%r15),%ecx
mov    %ecx,%eax
sar    $0x4,%al
movsbq %al,%rax
movzbl (%rax,%r12,1),%eax
lea    0x2(%rbx),%rdx
mov    %al,0x1(%rbx)
and    $0xf,%ecx
add    %r12,%rcx
mov    $0x3,%esi
jmp    1170 <escape+0x40>
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@escape.hex = internal constant [16 x i8] c"0123456789abcdef"
define i8* @escape(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
store i8* %7, i8** %6
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = mul nsw i32 %9, 3
%11 = add nsw i32 %10, 1
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %1
store i8* null, i8** %2
br label %59
16: ; preds = %1
%17 = load i8*, i8** %4
store i8* %17, i8** %5
br label %18
18: ; preds = %55, %16
%19 = load i8*, i8** %6
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %56
22: ; preds = %18
%23 = load i8*, i8** %6
%24 = load i8, i8* %23
%25 = call i64 @isalpha(i8 signext %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %6
%30 = load i8, i8* %28
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %5
store i8 %30, i8* %31
br label %55
33: ; preds = %22
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %5
store i8 37, i8* %34
%36 = load i8*, i8** %6
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = ashr i32 %38, 4
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [16 x i8], [16 x i8]* @escape.hex, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %5
store i8 %42, i8* %43
%45 = load i8*, i8** %6
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %6
%47 = load i8, i8* %45
%48 = sext i8 %47 to i32
%49 = and i32 %48, 15
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [16 x i8], [16 x i8]* @escape.hex, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = load i8*, i8** %5
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %5
store i8 %52, i8* %53
br label %55
55: ; preds = %33, %27
br label %18
56: ; preds = %18
%57 = load i8*, i8** %5
store i8 0, i8* %57
%58 = load i8*, i8** %4
store i8* %58, i8** %2
br label %59
59: ; preds = %56, %15
%60 = load i8*, i8** %2
ret i8* %60
}

LLVM IR (-O2):
@escape.hex = internal unnamed_addr constant [16 x i8] c"0123456789abcdef"
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 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %38, label %7
7: ; preds = %1
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %36, label %10
10: ; preds = %7, %27
%11 = phi i8 [ %34, %27 ], [ %8, %7 ]
%12 = phi i8* [ %33, %27 ], [ %0, %7 ]
%13 = phi i8* [ %32, %27 ], [ %5, %7 ]
%14 = tail call i64 @isalpha(i8 signext %11)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %10
%17 = getelementptr inbounds i8, i8* %13, i64 1
store i8 37, i8* %13
%18 = load i8, i8* %12
%19 = ashr i8 %18, 4
%20 = sext i8 %19 to i64
%21 = getelementptr inbounds [16 x i8], [16 x i8]* @escape.hex, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = getelementptr inbounds i8, i8* %13, i64 2
store i8 %22, i8* %17
%24 = and i8 %18, 15
%25 = zext i8 %24 to i64
%26 = getelementptr inbounds [16 x i8], [16 x i8]* @escape.hex, i64 0, i64 %25
br label %27
27: ; preds = %10, %16
%28 = phi i8* [ %26, %16 ], [ %12, %10 ]
%29 = phi i64 [ 3, %16 ], [ 1, %10 ]
%30 = phi i8* [ %23, %16 ], [ %13, %10 ]
%31 = load i8, i8* %28
%32 = getelementptr inbounds i8, i8* %13, i64 %29
store i8 %31, i8* %30
%33 = getelementptr inbounds i8, i8* %12, i64 1
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 0
br i1 %35, label %36, label %10
36: ; preds = %27, %7
%37 = phi i8* [ %5, %7 ], [ %32, %27 ]
store i8 0, i8* %37
br label %38
38: ; preds = %1, %36
ret i8* %5
}