#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*/ EXIT_FAILURE;
int /*<<< orphan*/ child(int, char *, char **, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int verifyampersand(char **, int);
int /*<<< orphan*/ wait(int *);
void executecommand(char *cmd, char **args, int argslen, char *filepath) {
  int ampersand = verifyampersand(args, argslen);
  if (ampersand == -1) {
    printf("ERROR: & Must be at the end of command\n");
  }
  int pid = fork();
  if (pid < 0) {
    perror("fork() failed!");
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    if (ampersand == 1)
      child(argslen - 1, cmd, args, filepath);
    else
      child(argslen, cmd, args, filepath);
  } else {
    if (ampersand != 1) {
      int status;
      wait(&status);
    } else {
      printf("[process running in background with pid %d]\n", pid);
    }
  }
}


x86 assembly (clang, -O0):
<executecommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <verifyampersand@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ae <executecommand+0x3e>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11dc <executecommand+0x6c>
lea    0xe5f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x28(%rbp)
jne    1225 <executecommand+0xb5>
cmpl   $0x1,-0x24(%rbp)
jne    120c <executecommand+0x9c>
mov    -0x14(%rbp),%edi
sub    $0x1,%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <child@plt>
jmp    1220 <executecommand+0xb0>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <child@plt>
jmp    1253 <executecommand+0xe3>
cmpl   $0x1,-0x24(%rbp)
je     123d <executecommand+0xcd>
lea    -0x2c(%rbp),%rdi
call   1080 <wait@plt>
jmp    124e <executecommand+0xde>
mov    -0x28(%rbp),%esi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1253 <executecommand+0xe3>
jmp    1258 <executecommand+0xe8>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<executecommand>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsi,%rdi
mov    %edx,%esi
call   1060 <verifyampersand@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11b4 <executecommand+0x34>
lea    0xe8d(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     121d <executecommand+0x9d>
je     11e2 <executecommand+0x62>
cmp    $0x1,%ebx
jne    1206 <executecommand+0x86>
lea    0xe42(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>
cmp    $0x1,%ebx
jne    11ea <executecommand+0x6a>
add    $0xffffffff,%ebp
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r14,%rcx
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <child@plt>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xddc(%rip),%rdi
call   1070 <perror@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"ERROR: & Must be at the end of command\0A\00"
@.str.1 = [15 x i8] c"fork() failed!\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [45 x i8] c"[process running in background with pid %d]\0A\00"
define void @executecommand(i8* %0, i8** %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%12 = load i8**, i8*** %6
%13 = load i32, i32* %7
%14 = call i32 @verifyampersand(i8** %12, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %4
%20 = call i32 (...) @fork()
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %19
%28 = load i32, i32* %10
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %47
30: ; preds = %27
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i32, i32* %7
%35 = sub nsw i32 %34, 1
%36 = load i8*, i8** %5
%37 = load i8**, i8*** %6
%38 = load i8*, i8** %8
%39 = call i32 @child(i32 %35, i8* %36, i8** %37, i8* %38)
br label %46
40: ; preds = %30
%41 = load i32, i32* %7
%42 = load i8*, i8** %5
%43 = load i8**, i8*** %6
%44 = load i8*, i8** %8
%45 = call i32 @child(i32 %41, i8* %42, i8** %43, i8* %44)
br label %46
46: ; preds = %40, %33
br label %56
47: ; preds = %27
%48 = load i32, i32* %9
%49 = icmp ne i32 %48, 1
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @wait(i32* %11)
br label %55
52: ; preds = %47
%53 = load i32, i32* %10
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %52, %50
br label %56
56: ; preds = %55, %46
br label %57
57: ; preds = %56
ret void
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"fork() failed!\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [45 x i8] c"[process running in background with pid %d]\0A\00"
@str = [39 x i8] c"ERROR: & Must be at the end of command\00"
define void @executecommand(i8* %0, i8** %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = tail call i32 @verifyampersand(i8** %1, i32 %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %8, %4
%11 = tail call i32 (...) @fork()
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
%18 = icmp eq i32 %11, 0
%19 = icmp eq i32 %6, 1
br i1 %18, label %20, label %26
20: ; preds = %17
br i1 %19, label %21, label %24
21: ; preds = %20
%22 = add nsw i32 %2, -1
%23 = tail call i32 @child(i32 %22, i8* %0, i8** %1, i8* %3)
br label %32
24: ; preds = %20
%25 = tail call i32 @child(i32 %2, i8* %0, i8** %1, i8* %3)
br label %32
26: ; preds = %17
br i1 %19, label %30, label %27
27: ; preds = %26
%28 = bitcast i32* %5 to i8*
%29 = call i32 @wait(i32* %5)
br label %32
30: ; preds = %26
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %32
32: ; preds = %24, %21, %30, %27
ret void
}