#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*/ BUFSIZ;
int /*<<< orphan*/ bzero(char *, int /*<<< orphan*/);
scalar_t__ malloc(int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
size_t strlen(char *);
void build_shell_command(char **args, int argc, char **argv) {
  args[0] = "/bin/sh";
  args[1] = "-c";
  args[2] = (char *)malloc(BUFSIZ);
  args[3] = NULL;
  bzero(args[2], BUFSIZ);
  for (int i = 1; i < argc; ++i) {
    args[2][strlen(args[2])] = ' ';
    strcat(args[2], argv[i]);
  }
  ++args[2];
}


x86 assembly (clang, -O0):
<build_shell_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
lea    0xea2(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
lea    0xe9c(%rip),%rcx
mov    %rcx,0x8(%rax)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x8(%rbp),%rax
movq   $0x0,0x18(%rax)
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1050 <bzero@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1209 <build_shell_command+0xc9>
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
movb   $0x20,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1060 <strcat@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b2 <build_shell_command+0x72>
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rcx
add    $0x1,%rcx
mov    %rcx,0x10(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_shell_command>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r12
lea    0xea9(%rip),%rax
mov    %rax,(%rdi)
lea    0xea7(%rip),%rax
mov    %rax,0x8(%rdi)
mov    0x2e74(%rip),%rbx
mov    (%rbx),%edi
call   1040 <malloc@plt>
mov    %rax,0x10(%r12)
movq   $0x0,0x18(%r12)
mov    (%rbx),%esi
mov    %rax,%rdi
call   1050 <bzero@plt>
cmp    $0x2,%ebp
jl     11c9 <build_shell_command+0x89>
mov    %ebp,%r15d
mov    $0x1,%ebx
nopl   0x0(%rax,%rax,1)
mov    0x10(%r12),%rbp
mov    %rbp,%rdi
call   1030 <strlen@plt>
movb   $0x20,0x0(%rbp,%rax,1)
mov    0x10(%r12),%rdi
mov    (%r14,%rbx,8),%rsi
call   1060 <strcat@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11a0 <build_shell_command+0x60>
addq   $0x1,0x10(%r12)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@BUFSIZ = global i32 0
define void @build_shell_command(i8** %0, i32 %1, i8** %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i8** %2, i8*** %6
%8 = load i8**, i8*** %4
%9 = getelementptr inbounds i8*, i8** %8, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = load i8**, i8*** %4
%11 = getelementptr inbounds i8*, i8** %10, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %11
%12 = load i32, i32* @BUFSIZ
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = load i8**, i8*** %4
%16 = getelementptr inbounds i8*, i8** %15, i64 2
store i8* %14, i8** %16
%17 = load i8**, i8*** %4
%18 = getelementptr inbounds i8*, i8** %17, i64 3
store i8* null, i8** %18
%19 = load i8**, i8*** %4
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = load i32, i32* @BUFSIZ
%23 = call i32 @bzero(i8* %21, i32 %22)
store i32 1, i32* %7
br label %24
24: ; preds = %46, %3
%25 = load i32, i32* %7
%26 = load i32, i32* %5
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %49
28: ; preds = %24
%29 = load i8**, i8*** %4
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = load i8**, i8*** %4
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i64 @strlen(i8* %34)
%36 = getelementptr inbounds i8, i8* %31, i64 %35
store i8 32, i8* %36
%37 = load i8**, i8*** %4
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = load i8**, i8*** %6
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %40, i64 %42
%44 = load i8*, i8** %43
%45 = call i32 @strcat(i8* %39, i8* %44)
br label %46
46: ; preds = %28
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
store i32 %48, i32* %7
br label %24
49: ; preds = %24
%50 = load i8**, i8*** %4
%51 = getelementptr inbounds i8*, i8** %50, i64 2
%52 = load i8*, i8** %51
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %51
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@BUFSIZ = global i32 0
define void @build_shell_command(i8** nocapture %0, i32 %1, i8** nocapture %2) {
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %0
%4 = getelementptr inbounds i8*, i8** %0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %4
%5 = load i32, i32* @BUFSIZ
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = getelementptr inbounds i8*, i8** %0, i64 2
store i8* %7, i8** %8
%9 = getelementptr inbounds i8*, i8** %0, i64 3
store i8* null, i8** %9
%10 = load i32, i32* @BUFSIZ
%11 = tail call i32 @bzero(i8* %7, i32 %10)
%12 = icmp sgt i32 %1, 1
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = zext i32 %1 to i64
br label %18
15: ; preds = %18, %3
%16 = load i8*, i8** %8
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8* %17, i8** %8
ret void
18: ; preds = %13, %18
%19 = phi i64 [ 1, %13 ], [ %27, %18 ]
%20 = load i8*, i8** %8
%21 = tail call i64 @strlen(i8* dereferenceable(1) %20)
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8 32, i8* %22
%23 = load i8*, i8** %8
%24 = getelementptr inbounds i8*, i8** %2, i64 %19
%25 = load i8*, i8** %24
%26 = tail call i32 @strcat(i8* %23, i8* %25)
%27 = add nuw nsw i64 %19, 1
%28 = icmp eq i64 %27, %14
br i1 %28, label %15, label %18
}