#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*/ execv(char *, char **);
int /*<<< orphan*/ free(char **);
char **malloc(int);
int /*<<< orphan*/ perror(char *);
int exec_busybox(const char *app, int argc, char **argv) {
  char **newargv = malloc((argc + 2) * sizeof(char *));
  int i;
  newargv[0] = "/bin/busybox";
  newargv[1] = (char *)app;
  for (i = 1; i < argc; i++)
    newargv[i + 1] = argv[i];
  newargv[argc + 1] = NULL;
  execv(newargv[0], newargv);
  perror(newargv[0]);
  free(newargv);
  return 1;
}


x86 assembly (clang, -O0):
<exec_busybox>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
lea    0xe8b(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11c2 <exec_busybox+0x82>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    118b <exec_busybox+0x4b>
mov    -0x20(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <execv@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_busybox>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%r14d
mov    %rdi,%r12
lea    0x10(,%r14,8),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0xe88(%rip),%rax
mov    %rax,(%rbx)
mov    %r12,0x8(%rbx)
cmp    $0x2,%r14d
jl     11a0 <exec_busybox+0x50>
mov    %rbx,%rdi
add    $0x10,%rdi
add    $0x8,%r15
lea    -0x1(%r14),%edx
shl    $0x3,%rdx
mov    %r15,%rsi
call   1040 <memcpy@plt>
movslq %r14d,%rax
movq   $0x0,0x8(%rbx,%rax,8)
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1070 <execv@plt>
mov    (%rbx),%rdi
call   1060 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"/bin/busybox\00"
define i32 @exec_busybox(i8* %0, i32 %1, i8** nocapture %2) {
%4 = shl i32 %1, 3
%5 = add i32 %4, 16
%6 = tail call i8** @malloc(i32 %5)
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds i8*, i8** %6, i64 1
store i8* %0, i8** %7
%8 = icmp sgt i32 %1, 1
br i1 %8, label %9, label %17
9: ; preds = %3
%10 = getelementptr i8*, i8** %6, i64 2
%11 = bitcast i8** %10 to i8*
%12 = getelementptr i8*, i8** %2, i64 1
%13 = bitcast i8** %12 to i8*
%14 = add nsw i32 %1, -1
%15 = zext i32 %14 to i64
%16 = shl nuw nsw i64 %15, 3
br label %17
17: ; preds = %9, %3
%18 = add nsw i32 %1, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8*, i8** %6, i64 %19
store i8* null, i8** %20
%21 = load i8*, i8** %6
%22 = tail call i32 @execv(i8* %21, i8** %6)
%23 = load i8*, i8** %6
%24 = tail call i32 @perror(i8* %23)
%25 = tail call i32 @free(i8** %6)
ret i32 1
}