#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 */
typedef int /*<<< orphan*/ out;

/* Variables and functions */
int EDOM;
int PATH_MAX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ shell_escape(char const *, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char const *);
int test_shell_escape(void) {
  int i;
  char out[PATH_MAX];
  const char *inputs[] = {"foo",       "",  "it's like this", "it's like that",
                          "Hi\\There", NULL};
  const char *outputs[] = {
      "'foo'",         "''", "'it'\\''s like this'", "'it'\\''s like that'",
      "'Hi\\\\There'", NULL};
  for (i = 0; inputs[i]; ++i) {
    memset(out, 0, sizeof(out));
    if (shell_escape(inputs[i], out, sizeof(out))) {
      fprintf(stderr,
              "test_shell_escape: error on test "
              "pattern %d: shell_escape error\n",
              i);
      return -EDOM;
    }
    if (strcmp(out, outputs[i])) {
      fprintf(stderr,
              "test_shell_escape: error on test "
              "pattern %d: got %s, expected %s\n",
              i, out, outputs[i]);
      return -EDOM;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_shell_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x98(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x90(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
lea    0xe68(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xf05(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe56(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe5a(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe5e(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
lea    0xe4c(%rip),%rax
mov    %rax,-0x80(%rbp)
lea    0xe47(%rip),%rax
mov    %rax,-0x78(%rbp)
lea    0xe3f(%rip),%rax
mov    %rax,-0x70(%rbp)
lea    0xe48(%rip),%rax
mov    %rax,-0x68(%rbp)
lea    0xe51(%rip),%rax
mov    %rax,-0x60(%rbp)
movl   $0x0,-0x8(%rbp)
movslq -0x8(%rbp),%rax
cmpq   $0x0,-0x50(%rbp,%rax,8)
je     1315 <test_shell_escape+0x1d5>
mov    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x90(%rbp),%rsi
mov    -0x98(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    -0x50(%rbp,%rcx,8),%rdi
mov    %eax,%edx
call   1030 <shell_escape@plt>
cmp    $0x0,%rax
je     129c <test_shell_escape+0x15c>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2d58(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x84(%rbp)
jmp    1326 <test_shell_escape+0x1e6>
mov    -0x90(%rbp),%rdi
movslq -0x8(%rbp),%rax
mov    -0x80(%rbp,%rax,8),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1302 <test_shell_escape+0x1c2>
mov    -0x90(%rbp),%rcx
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
movslq -0x8(%rbp),%rax
mov    -0x80(%rbp,%rax,8),%r8
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2cf2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x84(%rbp)
jmp    1326 <test_shell_escape+0x1e6>
jmp    1307 <test_shell_escape+0x1c7>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1216 <test_shell_escape+0xd6>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x84(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_shell_escape>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x60,%rsp
mov    0x2e9a(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r15
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,-0x60(%rbp)
lea    0xe85(%rip),%r12
mov    %r12,-0x80(%rbp)
lea    0xf22(%rip),%rax
mov    %rax,-0x78(%rbp)
lea    0xe73(%rip),%rax
mov    %rax,-0x70(%rbp)
lea    0xe77(%rip),%rax
mov    %rax,-0x68(%rbp)
lea    0xe7b(%rip),%rax
mov    %rax,-0x60(%rbp)
movaps %xmm0,-0x30(%rbp)
lea    0xe75(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xe70(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe68(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe71(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x30(%rbp)
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1040 <memset@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <shell_escape@plt>
test   %rax,%rax
jne    1238 <test_shell_escape+0xf8>
mov    -0x50(%rbp,%rbx,8),%r12
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1256 <test_shell_escape+0x116>
mov    -0x78(%rbp,%rbx,8),%r12
add    $0x1,%rbx
test   %r12,%r12
jne    11f0 <test_shell_escape+0xb0>
xor    %r14d,%r14d
jmp    1282 <test_shell_escape+0x142>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
xor    %r14d,%r14d
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1278 <test_shell_escape+0x138>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
xor    %r14d,%r14d
mov    %ebx,%edx
mov    %r15,%rcx
mov    %r12,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d61(%rip),%rax
sub    (%rax),%r14d
mov    %r14d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [4 x i8] c"foo\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [15 x i8] c"it's like this\00"
@.str.3 = [15 x i8] c"it's like that\00"
@.str.4 = [9 x i8] c"Hi\\There\00"
@.str.5 = [6 x i8] c"'foo'\00"
@.str.6 = [3 x i8] c"''\00"
@.str.7 = [20 x i8] c"'it'\\''s like this'\00"
@.str.8 = [20 x i8] c"'it'\\''s like that'\00"
@.str.9 = [12 x i8] c"'Hi\\\\There'\00"
@stderr = global i32 0
@.str.10 = [65 x i8] c"test_shell_escape: error on test pattern %d: shell_escape error\0A\00"
@EDOM = global i32 0
@.str.11 = [66 x i8] c"test_shell_escape: error on test pattern %d: got %s, expected %s\0A\00"
define i32 @test_shell_escape() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [6 x i8*]
%6 = alloca [6 x i8*]
%7 = alloca i32
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %3
%11 = alloca i8, i64 %9
store i64 %9, i64* %4
%12 = bitcast [6 x i8*]* %5 to i8*
%13 = bitcast i8* %12 to [6 x i8*]*
%14 = getelementptr inbounds [6 x i8*], [6 x i8*]* %13, i32 0, i32 0
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8** %14
%15 = getelementptr inbounds [6 x i8*], [6 x i8*]* %13, i32 0, i32 1
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8** %15
%16 = getelementptr inbounds [6 x i8*], [6 x i8*]* %13, i32 0, i32 2
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i32 0, i32 0), i8** %16
%17 = getelementptr inbounds [6 x i8*], [6 x i8*]* %13, i32 0, i32 3
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i32 0, i32 0), i8** %17
%18 = getelementptr inbounds [6 x i8*], [6 x i8*]* %13, i32 0, i32 4
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i32 0, i32 0), i8** %18
%19 = bitcast [6 x i8*]* %6 to i8*
%20 = bitcast i8* %19 to [6 x i8*]*
%21 = getelementptr inbounds [6 x i8*], [6 x i8*]* %20, i32 0, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i32 0, i32 0), i8** %21
%22 = getelementptr inbounds [6 x i8*], [6 x i8*]* %20, i32 0, i32 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i32 0, i32 0), i8** %22
%23 = getelementptr inbounds [6 x i8*], [6 x i8*]* %20, i32 0, i32 2
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i32 0, i32 0), i8** %23
%24 = getelementptr inbounds [6 x i8*], [6 x i8*]* %20, i32 0, i32 3
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i32 0, i32 0), i8** %24
%25 = getelementptr inbounds [6 x i8*], [6 x i8*]* %20, i32 0, i32 4
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i32 0, i32 0), i8** %25
store i32 0, i32* %2
br label %26
26: ; preds = %66, %0
%27 = load i32, i32* %2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [6 x i8*], [6 x i8*]* %5, i64 0, i64 %28
%30 = load i8*, i8** %29
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %69
32: ; preds = %26
%33 = trunc i64 %9 to i32
%34 = call i32 @memset(i8* %11, i32 0, i32 %33)
%35 = load i32, i32* %2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [6 x i8*], [6 x i8*]* %5, i64 0, i64 %36
%38 = load i8*, i8** %37
%39 = trunc i64 %9 to i32
%40 = call i64 @shell_escape(i8* %38, i8* %11, i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %32
%43 = load i32, i32* @stderr
%44 = load i32, i32* %2
%45 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.10, i64 0, i64 0), i32 %44)
%46 = load i32, i32* @EDOM
%47 = sub nsw i32 0, %46
store i32 %47, i32* %1
store i32 1, i32* %7
br label %70
48: ; preds = %32
%49 = load i32, i32* %2
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [6 x i8*], [6 x i8*]* %6, i64 0, i64 %50
%52 = load i8*, i8** %51
%53 = call i64 @strcmp(i8* %11, i8* %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %65
55: ; preds = %48
%56 = load i32, i32* @stderr
%57 = load i32, i32* %2
%58 = load i32, i32* %2
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [6 x i8*], [6 x i8*]* %6, i64 0, i64 %59
%61 = load i8*, i8** %60
%62 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.11, i64 0, i64 0), i32 %57, i8* %11, i8* %61)
%63 = load i32, i32* @EDOM
%64 = sub nsw i32 0, %63
store i32 %64, i32* %1
store i32 1, i32* %7
br label %70
65: ; preds = %48
br label %66
66: ; preds = %65
%67 = load i32, i32* %2
%68 = add nsw i32 %67, 1
store i32 %68, i32* %2
br label %26
69: ; preds = %26
store i32 0, i32* %1
store i32 1, i32* %7
br label %70
70: ; preds = %69, %55, %42
%71 = load i8*, i8** %3
%72 = load i32, i32* %1
ret i32 %72
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [4 x i8] c"foo\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [15 x i8] c"it's like this\00"
@.str.3 = [15 x i8] c"it's like that\00"
@.str.4 = [9 x i8] c"Hi\\There\00"
@.str.5 = [6 x i8] c"'foo'\00"
@.str.6 = [3 x i8] c"''\00"
@.str.7 = [20 x i8] c"'it'\\''s like this'\00"
@.str.8 = [20 x i8] c"'it'\\''s like that'\00"
@.str.9 = [12 x i8] c"'Hi\\\\There'\00"
@stderr = global i32 0
@.str.10 = [65 x i8] c"test_shell_escape: error on test pattern %d: shell_escape error\0A\00"
@EDOM = global i32 0
@.str.11 = [66 x i8] c"test_shell_escape: error on test pattern %d: got %s, expected %s\0A\00"
define i32 @test_shell_escape() {
%1 = alloca [6 x i8*]
%2 = alloca [6 x i8*]
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast [6 x i8*]* %1 to i8*
%7 = getelementptr inbounds [6 x i8*], [6 x i8*]* %1, i64 0, i64 4
%8 = bitcast i8** %7 to i8*
%9 = getelementptr inbounds [6 x i8*], [6 x i8*]* %1, i64 0, i64 0
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [6 x i8*], [6 x i8*]* %1, i64 0, i64 1
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [6 x i8*], [6 x i8*]* %1, i64 0, i64 2
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [6 x i8*], [6 x i8*]* %1, i64 0, i64 3
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8** %12
%13 = getelementptr inbounds [6 x i8*], [6 x i8*]* %1, i64 0, i64 4
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8** %13
%14 = bitcast [6 x i8*]* %2 to i8*
%15 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 4
%16 = bitcast i8** %15 to i8*
%17 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8** %17
%18 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8** %18
%19 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 2
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i8** %19
%20 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 3
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i8** %20
%21 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 4
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8** %21
br label %22
22: ; preds = %0, %45
%23 = phi i64 [ 0, %0 ], [ %46, %45 ]
%24 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %0 ], [ %48, %45 ]
%25 = call i32 @memset(i8* %5, i32 0, i32 %3)
%26 = call i64 @shell_escape(i8* %24, i8* %5, i32 %3)
%27 = icmp eq i64 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %22
%29 = trunc i64 %23 to i32
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.10, i64 0, i64 0), i32 %29)
%32 = load i32, i32* @EDOM
%33 = sub nsw i32 0, %32
br label %50
34: ; preds = %22
%35 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 %23
%36 = load i8*, i8** %35
%37 = call i64 @strcmp(i8* %5, i8* %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %45, label %39
39: ; preds = %34
%40 = trunc i64 %23 to i32
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.11, i64 0, i64 0), i32 %40, i8* %5, i8* %36)
%43 = load i32, i32* @EDOM
%44 = sub nsw i32 0, %43
br label %50
45: ; preds = %34
%46 = add nuw i64 %23, 1
%47 = getelementptr inbounds [6 x i8*], [6 x i8*]* %1, i64 0, i64 %46
%48 = load i8*, i8** %47
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %22
50: ; preds = %45, %39, %28
%51 = phi i32 [ %33, %28 ], [ %44, %39 ], [ 0, %45 ]
ret i32 %51
}