#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 */
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *mempcpy(char *, char const *, int);
char *strchrnul(char const *, char);
int strcnt(char const *, char);
size_t strlen(char const *);
size_t strspn(char const *, char *);
const char *shell_escape(const char *src) {
  static char *shell = NULL;
  size_t n = strlen(src);
  if (strspn(src, "+,-./0123456789:@ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
                  "abcdefghijklmnopqrstuvwxyz") == n)
    return src;
  n += strcnt(src, '\'') * 3 + 3;
  free(shell);
  shell = malloc(n);
  if (shell != NULL) {
    char *dst = shell;
    *dst++ = '\'';
    do {
      const char *c = src;
      if (*src == '\'') {
        memcpy(dst, "'\\'", 3);
        dst += 3;
        c += 3;
      }
      c = strchrnul(c, '\'');
      dst = mempcpy(dst, src, c - src);
      src = c;
    } while (*src != '\0');
    *dst++ = '\'';
    *dst = '\0';
  }
  return shell;
}


x86 assembly (clang, -O0):
<shell_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1060 <strspn@plt>
cmp    -0x18(%rbp),%rax
jne    11c0 <shell_escape+0x40>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d6 <shell_escape+0x156>
mov    -0x10(%rbp),%rdi
mov    $0x27,%esi
call   1070 <strcnt@plt>
imul   $0x3,%eax,%eax
add    $0x3,%eax
cltq
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e83(%rip),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,0x2e6e(%rip)
cmpq   $0x0,0x2e66(%rip)
je     12cb <shell_escape+0x14b>
mov    0x2e59(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x27,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x27,%eax
jne    126a <shell_escape+0xea>
mov    -0x20(%rbp),%rdi
lea    0xdff(%rip),%rsi
mov    $0x3,%edx
call   1080 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x27,%esi
call   1040 <strchrnul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   10a0 <mempcpy@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1225 <shell_escape+0xa5>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x27,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2d96(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shell_escape>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <strlen@plt>
mov    %rax,%rbx
lea    0xe66(%rip),%rsi
mov    %r14,%rdi
call   1060 <strspn@plt>
cmp    %rbx,%rax
je     125e <shell_escape+0xde>
mov    %r14,%rdi
mov    $0x27,%esi
call   1070 <strcnt@plt>
lea    (%rax,%rax,2),%eax
cltq
add    %rax,%rbx
add    $0x3,%rbx
mov    0x2e9d(%rip),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1090 <malloc@plt>
mov    %rax,0x2e89(%rip)
test   %rax,%rax
je     125b <shell_escape+0xdb>
mov    %rax,%r12
add    $0x1,%r12
movb   $0x27,(%rax)
mov    (%r14),%al
lea    0xe4f(%rip),%r15
jmp    1242 <shell_escape+0xc2>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r15,%rsi
mov    $0x3,%edx
call   1080 <memcpy@plt>
add    $0x3,%r12
lea    0x3(%r14),%rdi
mov    $0x27,%esi
call   1040 <strchrnul@plt>
mov    %rax,%rbx
mov    %ebx,%edx
sub    %r14d,%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   10a0 <mempcpy@plt>
mov    %rax,%r12
movzbl (%rbx),%eax
mov    %rbx,%r14
test   %al,%al
je     124b <shell_escape+0xcb>
cmp    $0x27,%al
je     1200 <shell_escape+0x80>
mov    %r14,%rdi
jmp    1218 <shell_escape+0x98>
movw   $0x27,(%r12)
mov    0x2e0f(%rip),%r14
jmp    125e <shell_escape+0xde>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@shell_escape.shell = internal global i8* null
@.str = [71 x i8] c"+,-./0123456789:@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz\00"
@.str.1 = [4 x i8] c"'\\'\00"
define i8* @shell_escape(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %4
%9 = load i8*, i8** %3
%10 = call i64 @strspn(i8* %9, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0))
%11 = load i64, i64* %4
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i8*, i8** %3
store i8* %14, i8** %2
br label %70
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 @strcnt(i8* %16, i8 signext 39)
%18 = mul nsw i32 %17, 3
%19 = add nsw i32 %18, 3
%20 = sext i32 %19 to i64
%21 = load i64, i64* %4
%22 = add i64 %21, %20
store i64 %22, i64* %4
%23 = load i8*, i8** @shell_escape.shell
%24 = call i32 @free(i8* %23)
%25 = load i64, i64* %4
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** @shell_escape.shell
%27 = load i8*, i8** @shell_escape.shell
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %68
29: ; preds = %15
%30 = load i8*, i8** @shell_escape.shell
store i8* %30, i8** %5
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %5
store i8 39, i8* %31
br label %33
33: ; preds = %59, %29
%34 = load i8*, i8** %3
store i8* %34, i8** %6
%35 = load i8*, i8** %3
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 39
br i1 %38, label %39, label %46
39: ; preds = %33
%40 = load i8*, i8** %5
%41 = call i32 @memcpy(i8* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%42 = load i8*, i8** %5
%43 = getelementptr inbounds i8, i8* %42, i64 3
store i8* %43, i8** %5
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i64 3
store i8* %45, i8** %6
br label %46
46: ; preds = %39, %33
%47 = load i8*, i8** %6
%48 = call i8* @strchrnul(i8* %47, i8 signext 39)
store i8* %48, i8** %6
%49 = load i8*, i8** %5
%50 = load i8*, i8** %3
%51 = load i8*, i8** %6
%52 = load i8*, i8** %3
%53 = ptrtoint i8* %51 to i64
%54 = ptrtoint i8* %52 to i64
%55 = sub i64 %53, %54
%56 = trunc i64 %55 to i32
%57 = call i8* @mempcpy(i8* %49, i8* %50, i32 %56)
store i8* %57, i8** %5
%58 = load i8*, i8** %6
store i8* %58, i8** %3
br label %59
59: ; preds = %46
%60 = load i8*, i8** %3
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 0
br i1 %63, label %33, label %64
64: ; preds = %59
%65 = load i8*, i8** %5
%66 = getelementptr inbounds i8, i8* %65, i32 1
store i8* %66, i8** %5
store i8 39, i8* %65
%67 = load i8*, i8** %5
store i8 0, i8* %67
br label %68
68: ; preds = %64, %15
%69 = load i8*, i8** @shell_escape.shell
store i8* %69, i8** %2
br label %70
70: ; preds = %68, %13
%71 = load i8*, i8** %2
ret i8* %71
}

LLVM IR (-O2):
@shell_escape.shell = internal unnamed_addr global i8* null
@.str = [71 x i8] c"+,-./0123456789:@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz\00"
@.str.1 = [4 x i8] c"'\\'\00"
define i8* @shell_escape(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = tail call i64 @strspn(i8* %0, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, %2
br i1 %4, label %41, label %5
5: ; preds = %1
%6 = tail call i32 @strcnt(i8* %0, i8 signext 39)
%7 = mul nsw i32 %6, 3
%8 = add nsw i32 %7, 3
%9 = sext i32 %8 to i64
%10 = add i64 %2, %9
%11 = load i8*, i8** @shell_escape.shell
%12 = tail call i32 @free(i8* %11)
%13 = tail call i8* @malloc(i64 %10)
store i8* %13, i8** @shell_escape.shell
%14 = icmp eq i8* %13, null
br i1 %14, label %41, label %15
15: ; preds = %5
%16 = getelementptr inbounds i8, i8* %13, i64 1
store i8 39, i8* %13
%17 = load i8, i8* %0
br label %18
18: ; preds = %27, %15
%19 = phi i8 [ %36, %27 ], [ %17, %15 ]
%20 = phi i8* [ %30, %27 ], [ %0, %15 ]
%21 = phi i8* [ %35, %27 ], [ %16, %15 ]
%22 = icmp eq i8 %19, 39
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = tail call i32 @memcpy(i8* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%25 = getelementptr inbounds i8, i8* %21, i64 3
%26 = getelementptr inbounds i8, i8* %20, i64 3
br label %27
27: ; preds = %23, %18
%28 = phi i8* [ %25, %23 ], [ %21, %18 ]
%29 = phi i8* [ %26, %23 ], [ %20, %18 ]
%30 = tail call i8* @strchrnul(i8* %29, i8 signext 39)
%31 = ptrtoint i8* %30 to i64
%32 = ptrtoint i8* %20 to i64
%33 = sub i64 %31, %32
%34 = trunc i64 %33 to i32
%35 = tail call i8* @mempcpy(i8* %28, i8* %20, i32 %34)
%36 = load i8, i8* %30
%37 = icmp eq i8 %36, 0
br i1 %37, label %38, label %18
38: ; preds = %27
%39 = getelementptr inbounds i8, i8* %35, i64 1
store i8 39, i8* %35
store i8 0, i8* %39
%40 = load i8*, i8** @shell_escape.shell
br label %41
41: ; preds = %5, %38, %1
%42 = phi i8* [ %0, %1 ], [ %40, %38 ], [ null, %5 ]
ret i8* %42
}