#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;
  free(shell);
  shell = NULL;
  size_t n = strlen(src);
  if (strspn(src, "+,-./0123456789:@ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
                  "abcdefghijklmnopqrstuvwxyz") == n)
    return src;
  n += strcnt(src, '\'') * 3 + 3;
  shell = malloc(n);
  if (shell) {
    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    0x2ed5(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2ec5(%rip)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1060 <strspn@plt>
cmp    -0x18(%rbp),%rax
jne    11d7 <shell_escape+0x57>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12e1 <shell_escape+0x161>
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    -0x18(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,0x2e63(%rip)
cmpq   $0x0,0x2e5b(%rip)
je     12d6 <shell_escape+0x156>
mov    0x2e4e(%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    1275 <shell_escape+0xf5>
mov    -0x20(%rbp),%rdi
lea    0xdf4(%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    1230 <shell_escape+0xb0>
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    0x2d8b(%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
mov    0x2ed6(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2ec6(%rip)
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
lea    0xe4c(%rip),%rsi
mov    %r14,%rdi
call   1060 <strspn@plt>
cmp    %rbx,%rax
je     126e <shell_escape+0xee>
mov    %r14,%rdi
mov    $0x27,%esi
call   1070 <strcnt@plt>
lea    (%rax,%rax,2),%eax
cltq
lea    (%rax,%rbx,1),%rdi
add    $0x3,%rdi
call   1090 <malloc@plt>
mov    %rax,0x2e7d(%rip)
test   %rax,%rax
je     126b <shell_escape+0xeb>
mov    %rax,%r12
add    $0x1,%r12
movb   $0x27,(%rax)
mov    (%r14),%al
lea    0xe43(%rip),%r15
jmp    1252 <shell_escape+0xd2>
cs 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     125b <shell_escape+0xdb>
cmp    $0x27,%al
je     1210 <shell_escape+0x90>
mov    %r14,%rdi
jmp    1228 <shell_escape+0xa8>
movw   $0x27,(%r12)
mov    0x2dff(%rip),%r14
jmp    126e <shell_escape+0xee>
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** @shell_escape.shell
%8 = call i32 @free(i8* %7)
store i8* null, i8** @shell_escape.shell
%9 = load i8*, i8** %3
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %4
%11 = load i8*, i8** %3
%12 = call i64 @strspn(i8* %11, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0))
%13 = load i64, i64* %4
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = load i8*, i8** %3
store i8* %16, i8** %2
br label %70
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = call i32 @strcnt(i8* %18, i8 signext 39)
%20 = mul nsw i32 %19, 3
%21 = add nsw i32 %20, 3
%22 = sext i32 %21 to i64
%23 = load i64, i64* %4
%24 = add i64 %23, %22
store i64 %24, i64* %4
%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 = %17
%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, %17
%69 = load i8*, i8** @shell_escape.shell
store i8* %69, i8** %2
br label %70
70: ; preds = %68, %15
%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 = load i8*, i8** @shell_escape.shell
%3 = tail call i32 @free(i8* %2)
store i8* null, i8** @shell_escape.shell
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @strspn(i8* %0, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, %4
br i1 %6, label %41, label %7
7: ; preds = %1
%8 = tail call i32 @strcnt(i8* %0, i8 signext 39)
%9 = mul nsw i32 %8, 3
%10 = add nsw i32 %9, 3
%11 = sext i32 %10 to i64
%12 = add i64 %4, %11
%13 = tail call i8* @malloc(i64 %12)
store i8* %13, i8** @shell_escape.shell
%14 = icmp eq i8* %13, null
br i1 %14, label %41, label %15
15: ; preds = %7
%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 = %7, %38, %1
%42 = phi i8* [ %0, %1 ], [ %40, %38 ], [ null, %7 ]
ret i8* %42
}