#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(scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup(char const *);
char *string_escape_shell(char const *);
scalar_t__ strlen(char const *);
char *strstr(char const *, char *);
char *string_wrap_command(const char *command, const char *wrapper_command) {
  if (!wrapper_command)
    return strdup(command);
  char *braces = strstr(wrapper_command, "{}");
  char *square = strstr(wrapper_command, "[]");
  char *new_command;
  if (braces) {
    new_command = strdup(command);
  } else {
    new_command = string_escape_shell(command);
  }
  char *result = malloc(strlen(new_command) + strlen(wrapper_command) + 16);
  if (braces) {
    strcpy(result, wrapper_command);
    result[braces - wrapper_command] = 0;
    strcat(result, new_command);
    strcat(result, braces + 2);
  } else if (square) {
    strcpy(result, wrapper_command);
    result[square - wrapper_command] = 0;
    strcat(result, new_command);
    strcat(result, square + 2);
  } else {
    strcpy(result, wrapper_command);
    strcat(result, " /bin/sh -c ");
    strcat(result, new_command);
  }
  free(new_command);
  return result;
}


x86 assembly (clang, -O0):
<string_wrap_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ad <string_wrap_command+0x2d>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1305 <string_wrap_command+0x185>
mov    -0x18(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f2 <string_wrap_command+0x72>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x30(%rbp)
jmp    11ff <string_wrap_command+0x7f>
mov    -0x10(%rbp),%rdi
call   1070 <string_escape_shell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x10,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1277 <string_wrap_command+0xf7>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x2,%rsi
call   1080 <strcat@plt>
jmp    12f4 <string_wrap_command+0x174>
cmpq   $0x0,-0x28(%rbp)
je     12c5 <string_wrap_command+0x145>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x2,%rsi
call   1080 <strcat@plt>
jmp    12ef <string_wrap_command+0x16f>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x38(%rbp),%rdi
lea    0xd29(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <strcat@plt>
jmp    12f4 <string_wrap_command+0x174>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_wrap_command>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     11c7 <string_wrap_command+0x47>
mov    %rsi,%rbx
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strstr@plt>
mov    %rax,%r12
lea    0xe56(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strstr@plt>
mov    %rax,%r13
mov    %r14,%rdi
test   %r12,%r12
je     11d8 <string_wrap_command+0x58>
call   1090 <strdup@plt>
jmp    11dd <string_wrap_command+0x5d>
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1090 <strdup@plt>
call   1070 <string_escape_shell@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    (%r15,%rax,1),%rdi
add    $0x10,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
test   %r12,%r12
je     1235 <string_wrap_command+0xb5>
call   1040 <strcpy@plt>
mov    %r12,%rax
sub    %rbx,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
add    $0x2,%r12
mov    %r15,%rdi
mov    %r12,%rsi
jmp    1276 <string_wrap_command+0xf6>
call   1040 <strcpy@plt>
test   %r13,%r13
je     1261 <string_wrap_command+0xe1>
mov    %r13,%rax
sub    %rbx,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
add    $0x2,%r13
mov    %r15,%rdi
mov    %r13,%rsi
jmp    1276 <string_wrap_command+0xf6>
lea    0xd9e(%rip),%rsi
mov    %r15,%rdi
call   1080 <strcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"{}\00"
@.str.1 = [3 x i8] c"[]\00"
@.str.2 = [13 x i8] c" /bin/sh -c \00"
define i8* @string_wrap_command(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %3
br label %91
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = call i8* @strstr(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %6
%18 = load i8*, i8** %5
%19 = call i8* @strstr(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* %19, i8** %7
%20 = load i8*, i8** %6
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i8*, i8** %4
%24 = call i8* @strdup(i8* %23)
store i8* %24, i8** %8
br label %28
25: ; preds = %15
%26 = load i8*, i8** %4
%27 = call i8* @string_escape_shell(i8* %26)
store i8* %27, i8** %8
br label %28
28: ; preds = %25, %22
%29 = load i8*, i8** %8
%30 = call i64 @strlen(i8* %29)
%31 = load i8*, i8** %5
%32 = call i64 @strlen(i8* %31)
%33 = add nsw i64 %30, %32
%34 = add nsw i64 %33, 16
%35 = call i8* @malloc(i64 %34)
store i8* %35, i8** %9
%36 = load i8*, i8** %6
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %56
38: ; preds = %28
%39 = load i8*, i8** %9
%40 = load i8*, i8** %5
%41 = call i32 @strcpy(i8* %39, i8* %40)
%42 = load i8*, i8** %9
%43 = load i8*, i8** %6
%44 = load i8*, i8** %5
%45 = ptrtoint i8* %43 to i64
%46 = ptrtoint i8* %44 to i64
%47 = sub i64 %45, %46
%48 = getelementptr inbounds i8, i8* %42, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %9
%50 = load i8*, i8** %8
%51 = call i32 @strcat(i8* %49, i8* %50)
%52 = load i8*, i8** %9
%53 = load i8*, i8** %6
%54 = getelementptr inbounds i8, i8* %53, i64 2
%55 = call i32 @strcat(i8* %52, i8* %54)
br label %87
56: ; preds = %28
%57 = load i8*, i8** %7
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %77
59: ; preds = %56
%60 = load i8*, i8** %9
%61 = load i8*, i8** %5
%62 = call i32 @strcpy(i8* %60, i8* %61)
%63 = load i8*, i8** %9
%64 = load i8*, i8** %7
%65 = load i8*, i8** %5
%66 = ptrtoint i8* %64 to i64
%67 = ptrtoint i8* %65 to i64
%68 = sub i64 %66, %67
%69 = getelementptr inbounds i8, i8* %63, i64 %68
store i8 0, i8* %69
%70 = load i8*, i8** %9
%71 = load i8*, i8** %8
%72 = call i32 @strcat(i8* %70, i8* %71)
%73 = load i8*, i8** %9
%74 = load i8*, i8** %7
%75 = getelementptr inbounds i8, i8* %74, i64 2
%76 = call i32 @strcat(i8* %73, i8* %75)
br label %86
77: ; preds = %56
%78 = load i8*, i8** %9
%79 = load i8*, i8** %5
%80 = call i32 @strcpy(i8* %78, i8* %79)
%81 = load i8*, i8** %9
%82 = call i32 @strcat(i8* %81, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%83 = load i8*, i8** %9
%84 = load i8*, i8** %8
%85 = call i32 @strcat(i8* %83, i8* %84)
br label %86
86: ; preds = %77, %59
br label %87
87: ; preds = %86, %38
%88 = load i8*, i8** %8
%89 = call i32 @free(i8* %88)
%90 = load i8*, i8** %9
store i8* %90, i8** %3
br label %91
91: ; preds = %87, %12
%92 = load i8*, i8** %3
ret i8* %92
}

LLVM IR (-O2):
@.str = [3 x i8] c"{}\00"
@.str.1 = [3 x i8] c"[]\00"
@.str.2 = [13 x i8] c" /bin/sh -c \00"
define i8* @string_wrap_command(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @strdup(i8* %0)
br label %46
6: ; preds = %2
%7 = tail call i8* @strstr(i8* %1, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = tail call i8* @strstr(i8* %1, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i8* %7, null
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i8* @strdup(i8* %0)
br label %14
12: ; preds = %6
%13 = tail call i8* @string_escape_shell(i8* %0)
br label %14
14: ; preds = %12, %10
%15 = phi i8* [ %11, %10 ], [ %13, %12 ]
%16 = tail call i64 @strlen(i8* dereferenceable(1) %15)
%17 = tail call i64 @strlen(i8* %1)
%18 = add i64 %16, 16
%19 = add i64 %18, %17
%20 = tail call i8* @malloc(i64 %19)
br i1 %9, label %30, label %21
21: ; preds = %14
%22 = tail call i32 @strcpy(i8* %20, i8* %1)
%23 = ptrtoint i8* %7 to i64
%24 = ptrtoint i8* %1 to i64
%25 = sub i64 %23, %24
%26 = getelementptr inbounds i8, i8* %20, i64 %25
store i8 0, i8* %26
%27 = tail call i32 @strcat(i8* %20, i8* %15)
%28 = getelementptr inbounds i8, i8* %7, i64 2
%29 = tail call i32 @strcat(i8* %20, i8* %28)
br label %44
30: ; preds = %14
%31 = icmp eq i8* %8, null
%32 = tail call i32 @strcpy(i8* %20, i8* %1)
br i1 %31, label %41, label %33
33: ; preds = %30
%34 = ptrtoint i8* %8 to i64
%35 = ptrtoint i8* %1 to i64
%36 = sub i64 %34, %35
%37 = getelementptr inbounds i8, i8* %20, i64 %36
store i8 0, i8* %37
%38 = tail call i32 @strcat(i8* %20, i8* %15)
%39 = getelementptr inbounds i8, i8* %8, i64 2
%40 = tail call i32 @strcat(i8* %20, i8* %39)
br label %44
41: ; preds = %30
%42 = tail call i32 @strcat(i8* %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%43 = tail call i32 @strcat(i8* %20, i8* %15)
br label %44
44: ; preds = %33, %41, %21
%45 = tail call i32 @free(i8* %15)
br label %46
46: ; preds = %44, %4
%47 = phi i8* [ %20, %44 ], [ %5, %4 ]
ret i8* %47
}