#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 ind(char *, char);
int strlen(char *);
void escape_shell_cmd(char *cmd) {
  register int x, y, l;
  l = strlen(cmd);
  for (x = 0; cmd[x]; x++) {
    if (ind("&;`'\"|*?~<>^()[]{}$\\\x0A", cmd[x]) != -1) {
      for (y = l + 1; y > x; y--)
        cmd[y] = cmd[y - 1];
      l++;
      cmd[x] = '\\';
      x++;
    }
  }
}


x86 assembly (clang, -O0):
<escape_shell_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11e1 <escape_shell_cmd+0xc1>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
lea    0xea0(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1040 <ind@plt>
cmp    $0xffffffff,%eax
je     11ce <escape_shell_cmd+0xae>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    11b0 <escape_shell_cmd+0x90>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    117b <escape_shell_cmd+0x5b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x5c,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11d3 <escape_shell_cmd+0xb3>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113f <escape_shell_cmd+0x1f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_shell_cmd>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    (%rbx),%al
test   %al,%al
je     11b6 <escape_shell_cmd+0x96>
xor    %r12d,%r12d
lea    0xebc(%rip),%r14
mov    %rbx,%r13
jmp    1173 <escape_shell_cmd+0x53>
nopl   0x0(%rax)
movb   $0x5c,0x0(%r13)
add    $0x1,%r12d
mov    %esi,%r15d
movslq %r12d,%rax
lea    0x1(%rax),%r12
lea    (%rbx,%rax,1),%r13
add    $0x1,%r13
mov    0x1(%rbx,%rax,1),%al
test   %al,%al
je     11b6 <escape_shell_cmd+0x96>
movsbl %al,%esi
mov    %r14,%rdi
call   1040 <ind@plt>
cmp    $0xffffffff,%eax
je     115c <escape_shell_cmd+0x3c>
lea    0x1(%r15),%esi
cmp    %r12d,%r15d
jl     1150 <escape_shell_cmd+0x30>
movslq %esi,%rdx
movslq %r12d,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl -0x1(%rbx,%rdx,1),%eax
mov    %al,(%rbx,%rdx,1)
lea    -0x1(%rdx),%rax
mov    %rax,%rdx
cmp    %rcx,%rax
jg     11a0 <escape_shell_cmd+0x80>
jmp    1150 <escape_shell_cmd+0x30>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [22 x i8] c"&;`'\22|*?~<>^()[]{}$\\\0A\00"
define void @escape_shell_cmd(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %5
store i32 0, i32* %3
br label %8
8: ; preds = %54, %1
%9 = load i8*, i8** %2
%10 = load i32, i32* %3
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %57
15: ; preds = %8
%16 = load i8*, i8** %2
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = call i32 @ind(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8 signext %20)
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %53
23: ; preds = %15
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %26
26: ; preds = %41, %23
%27 = load i32, i32* %4
%28 = load i32, i32* %3
%29 = icmp sgt i32 %27, %28
br i1 %29, label %30, label %44
30: ; preds = %26
%31 = load i8*, i8** %2
%32 = load i32, i32* %4
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = load i8, i8* %35
%37 = load i8*, i8** %2
%38 = load i32, i32* %4
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 %36, i8* %40
br label %41
41: ; preds = %30
%42 = load i32, i32* %4
%43 = add nsw i32 %42, -1
store i32 %43, i32* %4
br label %26
44: ; preds = %26
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
%47 = load i8*, i8** %2
%48 = load i32, i32* %3
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 92, i8* %50
%51 = load i32, i32* %3
%52 = add nsw i32 %51, 1
store i32 %52, i32* %3
br label %53
53: ; preds = %44, %15
br label %54
54: ; preds = %53
%55 = load i32, i32* %3
%56 = add nsw i32 %55, 1
store i32 %56, i32* %3
br label %8
57: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"&;`'\22|*?~<>^()[]{}$\\\0A\00"
define void @escape_shell_cmd(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %35, label %5
5: ; preds = %1, %27
%6 = phi i8 [ %33, %27 ], [ %3, %1 ]
%7 = phi i8* [ %32, %27 ], [ %0, %1 ]
%8 = phi i32 [ %29, %27 ], [ %2, %1 ]
%9 = phi i32 [ %30, %27 ], [ 0, %1 ]
%10 = tail call i32 @ind(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8 signext %6)
%11 = icmp eq i32 %10, -1
br i1 %11, label %27, label %12
12: ; preds = %5
%13 = add i32 %8, 1
%14 = icmp slt i32 %8, %9
br i1 %14, label %25, label %15
15: ; preds = %12
%16 = sext i32 %13 to i64
%17 = sext i32 %9 to i64
br label %18
18: ; preds = %15, %18
%19 = phi i64 [ %16, %15 ], [ %20, %18 ]
%20 = add nsw i64 %19, -1
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = load i8, i8* %21
%23 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 %22, i8* %23
%24 = icmp sgt i64 %20, %17
br i1 %24, label %18, label %25
25: ; preds = %18, %12
store i8 92, i8* %7
%26 = add nsw i32 %9, 1
br label %27
27: ; preds = %5, %25
%28 = phi i32 [ %26, %25 ], [ %9, %5 ]
%29 = phi i32 [ %13, %25 ], [ %8, %5 ]
%30 = add nsw i32 %28, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %0, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 0
br i1 %34, label %35, label %5
35: ; preds = %27, %1
ret void
}