#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 */
struct timeval {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
scalar_t__ EXIT_FAILURE;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ _exit(scalar_t__);
int /*<<< orphan*/ c_init(int);
int c_wait(int, struct timeval *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int execvp(char *const, char *const *);
int /*<<< orphan*/ exit(scalar_t__);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int **);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
void *malloc(int);
int pipe(int *);
int /*<<< orphan*/ pipe_to_stdin(int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ stdout_to_pipe(int *);
int fork_exec_wait(char *const *args, int cmd, int no_cmds, int background) {
  static int **pipe_fds, n;
  struct timeval t0;
  int i, return_value, status = 0;
  pid_t c_pid;
  if ((no_cmds > 1) && (cmd == 1)) {
    pipe_fds = malloc((no_cmds - 1) * sizeof(int *));
    for (i = 0; i < (no_cmds - 1); i++) {
      pipe_fds[i] = malloc(2 * sizeof(int));
      if (pipe(pipe_fds[i]) == -1) {
        fprintf(stderr, "fork_exec_wait: Could not pipe\n");
        exit(EXIT_FAILURE);
      }
    }
    n = 0;
  }
  if ((no_cmds > 1) && (cmd > 1 && cmd < no_cmds))
    n++;
  gettimeofday(&t0, NULL);
  if ((c_pid = fork()) == -1) {
    fprintf(stderr, "fork_exec_wait: Could not fork\n");
    exit(EXIT_FAILURE);
  } else if (c_pid == 0) {
    c_init(!background);
    if ((no_cmds > 1)) {
      if ((cmd == 1)) {
        stdout_to_pipe(pipe_fds[n]);
      }
      if ((cmd > 1 && cmd < no_cmds)) {
        pipe_to_stdin(pipe_fds[n - 1]);
        stdout_to_pipe(pipe_fds[n]);
      }
      if ((cmd == no_cmds)) {
        pipe_to_stdin(pipe_fds[n]);
      }
    }
    if (execvp(args[0], args) == -1) {
      _exit(EXIT_FAILURE);
    }
  } else if (c_pid > 0) {
    if ((no_cmds > 1))
      close(pipe_fds[n][(1)]);
    if (!background && (cmd == no_cmds)) {
      fprintf(stdout, "[%d] Spawned in foreground\n", c_pid);
      status = c_wait(c_pid, &t0, 0);
    } else {
      fprintf(stdout, "[%d] Spawned in background\n", c_pid);
    }
  }
  if (!background && WEXITSTATUS(status) == EXIT_FAILURE) {
    return_value = -1;
  } else {
    return_value = 0;
  }
  if ((no_cmds > 1) && ((cmd == no_cmds) || (return_value == -1)))
    free(pipe_fds);
  return return_value;
}


x86 assembly (clang, -O0):
<fork_exec_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x1,-0x10(%rbp)
jle    12ce <fork_exec_wait+0xde>
cmpl   $0x1,-0xc(%rbp)
jne    12ce <fork_exec_wait+0xde>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,0x2e66(%rip)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12c4 <fork_exec_wait+0xd4>
mov    $0x8,%edi
call   10b0 <malloc@plt>
mov    %rax,%rdx
mov    0x2e3a(%rip),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    0x2e2b(%rip),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1090 <pipe@plt>
cmp    $0xffffffff,%eax
jne    12b1 <fork_exec_wait+0xc1>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xd65(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%rdi
call   1100 <exit@plt>
jmp    12b6 <fork_exec_wait+0xc6>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1241 <fork_exec_wait+0x51>
movl   $0x0,0x2dda(%rip)
cmpl   $0x1,-0x10(%rbp)
jle    12fd <fork_exec_wait+0x10d>
cmpl   $0x1,-0xc(%rbp)
jle    12fd <fork_exec_wait+0x10d>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12fd <fork_exec_wait+0x10d>
mov    0x2db4(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2dab(%rip)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    1343 <fork_exec_wait+0x153>
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
lea    0xcf3(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%rdi
call   1100 <exit@plt>
cmpl   $0x0,-0x28(%rbp)
jne    1429 <fork_exec_wait+0x239>
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   10e0 <c_init@plt>
cmpl   $0x1,-0x10(%rbp)
jle    13fc <fork_exec_wait+0x20c>
cmpl   $0x1,-0xc(%rbp)
jne    138b <fork_exec_wait+0x19b>
mov    0x2d25(%rip),%rax
movslq 0x2d26(%rip),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <stdout_to_pipe@plt>
cmpl   $0x1,-0xc(%rbp)
jle    13d4 <fork_exec_wait+0x1e4>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    13d4 <fork_exec_wait+0x1e4>
mov    0x2cf8(%rip),%rax
mov    0x2cfa(%rip),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1070 <pipe_to_stdin@plt>
mov    0x2cdc(%rip),%rax
movslq 0x2cdd(%rip),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <stdout_to_pipe@plt>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jne    13f7 <fork_exec_wait+0x207>
mov    0x2cb9(%rip),%rax
movslq 0x2cba(%rip),%rcx
mov    (%rax,%rcx,8),%rdi
call   1070 <pipe_to_stdin@plt>
jmp    13fc <fork_exec_wait+0x20c>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   10f0 <execvp@plt>
cmp    $0xffffffff,%eax
jne    1424 <fork_exec_wait+0x234>
mov    0x2bbc(%rip),%rax
mov    (%rax),%rdi
call   1050 <_exit@plt>
jmp    14c1 <fork_exec_wait+0x2d1>
cmpl   $0x0,-0x28(%rbp)
jle    14bc <fork_exec_wait+0x2cc>
cmpl   $0x1,-0x10(%rbp)
jle    1457 <fork_exec_wait+0x267>
mov    0x2c5c(%rip),%rax
movslq 0x2c5d(%rip),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x4(%rax),%edi
call   1080 <close@plt>
cmpl   $0x0,-0x14(%rbp)
jne    149d <fork_exec_wait+0x2ad>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jne    149d <fork_exec_wait+0x2ad>
mov    0x2b5c(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xbc0(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x28(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   10d0 <c_wait@plt>
mov    %eax,-0x24(%rbp)
jmp    14b7 <fork_exec_wait+0x2c7>
mov    0x2b2c(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xbac(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    14bc <fork_exec_wait+0x2cc>
jmp    14c1 <fork_exec_wait+0x2d1>
jmp    14c6 <fork_exec_wait+0x2d6>
cmpl   $0x0,-0x14(%rbp)
jne    14f4 <fork_exec_wait+0x304>
mov    -0x24(%rbp),%edi
call   10c0 <WEXITSTATUS@plt>
mov    0x2af9(%rip),%rcx
cmp    (%rcx),%rax
jne    14f4 <fork_exec_wait+0x304>
movl   $0xffffffff,-0x20(%rbp)
jmp    14fb <fork_exec_wait+0x30b>
movl   $0x0,-0x20(%rbp)
cmpl   $0x1,-0x10(%rbp)
jle    1527 <fork_exec_wait+0x337>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     151b <fork_exec_wait+0x32b>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1527 <fork_exec_wait+0x337>
mov    0x2b7e(%rip),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_exec_wait>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %edx,%r13d
mov    %esi,%r12d
mov    %rdi,%rbx
cmp    $0x1,%esi
jne    12c0 <fork_exec_wait+0xd0>
cmp    $0x2,%r13d
jl     12c0 <fork_exec_wait+0xd0>
mov    %rbx,0x10(%rsp)
lea    -0x8(,%r13,8),%edi
ff
call   10b0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,0x2e67(%rip)
mov    $0x8,%edi
call   10b0 <malloc@plt>
mov    %rax,0x0(%rbp)
mov    %rax,%rdi
call   1090 <pipe@plt>
cmp    $0xffffffff,%eax
je     128b <fork_exec_wait+0x9b>
lea    -0x1(%r13),%ebp
mov    $0x1,%ebx
nopl   (%rax)
cmp    %rbx,%rbp
je     12b1 <fork_exec_wait+0xc1>
mov    0x2e34(%rip),%r14
mov    $0x8,%edi
call   10b0 <malloc@plt>
mov    %rax,(%r14,%rbx,8)
mov    %rax,%rdi
call   1090 <pipe@plt>
add    $0x1,%rbx
cmp    $0xffffffff,%eax
jne    1260 <fork_exec_wait+0x70>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xd65(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%rdi
call   1100 <exit@plt>
movl   $0x0,0x2ded(%rip)
mov    0x10(%rsp),%rbx
cmp    %r13d,%r12d
jge    12d8 <fork_exec_wait+0xe8>
cmp    $0x2,%r12d
jl     12d8 <fork_exec_wait+0xe8>
cmp    $0x2,%r13d
jl     12d8 <fork_exec_wait+0xe8>
addl   $0x1,0x2dd0(%rip)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
je     1362 <fork_exec_wait+0x172>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1460 <fork_exec_wait+0x270>
test   %ebp,%ebp
jle    13f9 <fork_exec_wait+0x209>
cmp    $0x2,%r13d
jl     1322 <fork_exec_wait+0x132>
mov    0x2d91(%rip),%rax
movslq 0x2d92(%rip),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x4(%rax),%edi
call   1080 <close@plt>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
cmp    %r13d,%r12d
jne    141a <fork_exec_wait+0x22a>
test   %r15d,%r15d
jne    141a <fork_exec_wait+0x22a>
lea    0xcfc(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   10a0 <fprintf@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10d0 <c_wait@plt>
mov    %eax,%edi
jmp    1400 <fork_exec_wait+0x210>
xor    %edi,%edi
test   %r15d,%r15d
sete   %dil
call   10e0 <c_init@plt>
cmp    $0x2,%r13d
jl     13e9 <fork_exec_wait+0x1f9>
cmp    $0x1,%r12d
jne    1393 <fork_exec_wait+0x1a3>
mov    0x2d1d(%rip),%rax
movslq 0x2d1e(%rip),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <stdout_to_pipe@plt>
cmp    $0x2,%r12d
jl     13cd <fork_exec_wait+0x1dd>
cmp    %r13d,%r12d
jge    13cd <fork_exec_wait+0x1dd>
mov    0x2cfb(%rip),%rax
movslq 0x2cfc(%rip),%rcx
mov    -0x8(%rax,%rcx,8),%rdi
call   1070 <pipe_to_stdin@plt>
mov    0x2ce3(%rip),%rax
movslq 0x2ce4(%rip),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <stdout_to_pipe@plt>
cmp    %r13d,%r12d
jne    13e9 <fork_exec_wait+0x1f9>
mov    0x2cc7(%rip),%rax
movslq 0x2cc8(%rip),%rcx
mov    (%rax,%rcx,8),%rdi
call   1070 <pipe_to_stdin@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   10f0 <execvp@plt>
cmp    $0xffffffff,%eax
je     1475 <fork_exec_wait+0x285>
test   %r15d,%r15d
jne    142f <fork_exec_wait+0x23f>
xor    %edi,%edi
call   10c0 <WEXITSTATUS@plt>
mov    0x2bcc(%rip),%rcx
cmp    (%rcx),%rax
jne    142f <fork_exec_wait+0x23f>
mov    $0xffffffff,%ebp
mov    $0x1,%al
jmp    1433 <fork_exec_wait+0x243>
lea    0xc3b(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   10a0 <fprintf@plt>
test   %r15d,%r15d
je     13fe <fork_exec_wait+0x20e>
xor    %eax,%eax
xor    %ebp,%ebp
cmp    %r13d,%r12d
sete   %cl
cmp    $0x2,%r13d
jl     144f <fork_exec_wait+0x25f>
or     %al,%cl
je     144f <fork_exec_wait+0x25f>
mov    0x2c56(%rip),%rdi
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2b79(%rip),%rax
mov    (%rax),%edi
lea    0xbb0(%rip),%rsi
jmp    129b <fork_exec_wait+0xab>
mov    0x2b5c(%rip),%rax
mov    (%rax),%rdi
call   1050 <_exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32 }
@fork_exec_wait.pipe_fds = internal global i32** null
@fork_exec_wait.n = internal global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"fork_exec_wait: Could not pipe\0A\00"
@EXIT_FAILURE = global i64 0
@.str.1 = [32 x i8] c"fork_exec_wait: Could not fork\0A\00"
@stdout = global i32 0
@.str.2 = [28 x i8] c"[%d] Spawned in foreground\0A\00"
@.str.3 = [28 x i8] c"[%d] Spawned in background\0A\00"
define i32 @fork_exec_wait(i8** %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %12
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %56
16: ; preds = %4
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %56
19: ; preds = %16
%20 = load i32, i32* %7
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 8
%24 = trunc i64 %23 to i32
%25 = call i8* @malloc(i32 %24)
%26 = bitcast i8* %25 to i32**
store i32** %26, i32*** @fork_exec_wait.pipe_fds
store i32 0, i32* %10
br label %27
27: ; preds = %52, %19
%28 = load i32, i32* %10
%29 = load i32, i32* %7
%30 = sub nsw i32 %29, 1
%31 = icmp slt i32 %28, %30
br i1 %31, label %32, label %55
32: ; preds = %27
%33 = call i8* @malloc(i32 8)
%34 = bitcast i8* %33 to i32*
%35 = load i32**, i32*** @fork_exec_wait.pipe_fds
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32*, i32** %35, i64 %37
store i32* %34, i32** %38
%39 = load i32**, i32*** @fork_exec_wait.pipe_fds
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32*, i32** %39, i64 %41
%43 = load i32*, i32** %42
%44 = call i32 @pipe(i32* %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %51
46: ; preds = %32
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%49 = load i64, i64* @EXIT_FAILURE
%50 = call i32 @exit(i64 %49)
unreachable
51: ; preds = %32
br label %52
52: ; preds = %51
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %27
55: ; preds = %27
store i32 0, i32* @fork_exec_wait.n
br label %56
56: ; preds = %55, %16, %4
%57 = load i32, i32* %7
%58 = icmp sgt i32 %57, 1
br i1 %58, label %59, label %69
59: ; preds = %56
%60 = load i32, i32* %6
%61 = icmp sgt i32 %60, 1
br i1 %61, label %62, label %69
62: ; preds = %59
%63 = load i32, i32* %6
%64 = load i32, i32* %7
%65 = icmp slt i32 %63, %64
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = load i32, i32* @fork_exec_wait.n
%68 = add nsw i32 %67, 1
store i32 %68, i32* @fork_exec_wait.n
br label %69
69: ; preds = %66, %62, %59, %56
%70 = call i32 @gettimeofday(%struct.timeval* %9, i32* null)
%71 = call i32 (...) @fork()
store i32 %71, i32* %13
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %78
73: ; preds = %69
%74 = load i32, i32* @stderr
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%76 = load i64, i64* @EXIT_FAILURE
%77 = call i32 @exit(i64 %76)
unreachable
78: ; preds = %69
%79 = load i32, i32* %13
%80 = icmp eq i32 %79, 0
br i1 %80, label %81, label %143
81: ; preds = %78
%82 = load i32, i32* %8
%83 = icmp ne i32 %82, 0
%84 = xor i1 %83, true
%85 = zext i1 %84 to i32
%86 = call i32 @c_init(i32 %85)
%87 = load i32, i32* %7
%88 = icmp sgt i32 %87, 1
br i1 %88, label %89, label %132
89: ; preds = %81
%90 = load i32, i32* %6
%91 = icmp eq i32 %90, 1
br i1 %91, label %92, label %99
92: ; preds = %89
%93 = load i32**, i32*** @fork_exec_wait.pipe_fds
%94 = load i32, i32* @fork_exec_wait.n
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i32*, i32** %93, i64 %95
%97 = load i32*, i32** %96
%98 = call i32 @stdout_to_pipe(i32* %97)
br label %99
99: ; preds = %92, %89
%100 = load i32, i32* %6
%101 = icmp sgt i32 %100, 1
br i1 %101, label %102, label %120
102: ; preds = %99
%103 = load i32, i32* %6
%104 = load i32, i32* %7
%105 = icmp slt i32 %103, %104
br i1 %105, label %106, label %120
106: ; preds = %102
%107 = load i32**, i32*** @fork_exec_wait.pipe_fds
%108 = load i32, i32* @fork_exec_wait.n
%109 = sub nsw i32 %108, 1
%110 = sext i32 %109 to i64
%111 = getelementptr inbounds i32*, i32** %107, i64 %110
%112 = load i32*, i32** %111
%113 = call i32 @pipe_to_stdin(i32* %112)
%114 = load i32**, i32*** @fork_exec_wait.pipe_fds
%115 = load i32, i32* @fork_exec_wait.n
%116 = sext i32 %115 to i64
%117 = getelementptr inbounds i32*, i32** %114, i64 %116
%118 = load i32*, i32** %117
%119 = call i32 @stdout_to_pipe(i32* %118)
br label %120
120: ; preds = %106, %102, %99
%121 = load i32, i32* %6
%122 = load i32, i32* %7
%123 = icmp eq i32 %121, %122
br i1 %123, label %124, label %131
124: ; preds = %120
%125 = load i32**, i32*** @fork_exec_wait.pipe_fds
%126 = load i32, i32* @fork_exec_wait.n
%127 = sext i32 %126 to i64
%128 = getelementptr inbounds i32*, i32** %125, i64 %127
%129 = load i32*, i32** %128
%130 = call i32 @pipe_to_stdin(i32* %129)
br label %131
131: ; preds = %124, %120
br label %132
132: ; preds = %131, %81
%133 = load i8**, i8*** %5
%134 = getelementptr inbounds i8*, i8** %133, i64 0
%135 = load i8*, i8** %134
%136 = load i8**, i8*** %5
%137 = call i32 @execvp(i8* %135, i8** %136)
%138 = icmp eq i32 %137, -1
br i1 %138, label %139, label %142
139: ; preds = %132
%140 = load i64, i64* @EXIT_FAILURE
%141 = call i32 @_exit(i64 %140)
unreachable
142: ; preds = %132
br label %177
143: ; preds = %78
%144 = load i32, i32* %13
%145 = icmp sgt i32 %144, 0
br i1 %145, label %146, label %176
146: ; preds = %143
%147 = load i32, i32* %7
%148 = icmp sgt i32 %147, 1
br i1 %148, label %149, label %158
149: ; preds = %146
%150 = load i32**, i32*** @fork_exec_wait.pipe_fds
%151 = load i32, i32* @fork_exec_wait.n
%152 = sext i32 %151 to i64
%153 = getelementptr inbounds i32*, i32** %150, i64 %152
%154 = load i32*, i32** %153
%155 = getelementptr inbounds i32, i32* %154, i64 1
%156 = load i32, i32* %155
%157 = call i32 @close(i32 %156)
br label %158
158: ; preds = %149, %146
%159 = load i32, i32* %8
%160 = icmp ne i32 %159, 0
br i1 %160, label %171, label %161
161: ; preds = %158
%162 = load i32, i32* %6
%163 = load i32, i32* %7
%164 = icmp eq i32 %162, %163
br i1 %164, label %165, label %171
165: ; preds = %161
%166 = load i32, i32* @stdout
%167 = load i32, i32* %13
%168 = call i32 (i32, i8*, ...) @fprintf(i32 %166, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %167)
%169 = load i32, i32* %13
%170 = call i32 @c_wait(i32 %169, %struct.timeval* %9, i32 0)
store i32 %170, i32* %12
br label %175
171: ; preds = %161, %158
%172 = load i32, i32* @stdout
%173 = load i32, i32* %13
%174 = call i32 (i32, i8*, ...) @fprintf(i32 %172, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %173)
br label %175
175: ; preds = %171, %165
br label %176
176: ; preds = %175, %143
br label %177
177: ; preds = %176, %142
br label %178
178: ; preds = %177
%179 = load i32, i32* %8
%180 = icmp ne i32 %179, 0
br i1 %180, label %187, label %181
181: ; preds = %178
%182 = load i32, i32* %12
%183 = call i64 @WEXITSTATUS(i32 %182)
%184 = load i64, i64* @EXIT_FAILURE
%185 = icmp eq i64 %183, %184
br i1 %185, label %186, label %187
186: ; preds = %181
store i32 -1, i32* %11
br label %188
187: ; preds = %181, %178
store i32 0, i32* %11
br label %188
188: ; preds = %187, %186
%189 = load i32, i32* %7
%190 = icmp sgt i32 %189, 1
br i1 %190, label %191, label %201
191: ; preds = %188
%192 = load i32, i32* %6
%193 = load i32, i32* %7
%194 = icmp eq i32 %192, %193
br i1 %194, label %198, label %195
195: ; preds = %191
%196 = load i32, i32* %11
%197 = icmp eq i32 %196, -1
br i1 %197, label %198, label %201
198: ; preds = %195, %191
%199 = load i32**, i32*** @fork_exec_wait.pipe_fds
%200 = call i32 @free(i32** %199)
br label %201
201: ; preds = %198, %195, %188
%202 = load i32, i32* %11
ret i32 %202
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@fork_exec_wait.pipe_fds = internal unnamed_addr global i32** null
@fork_exec_wait.n = internal unnamed_addr global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"fork_exec_wait: Could not pipe\0A\00"
@EXIT_FAILURE = global i64 0
@.str.1 = [32 x i8] c"fork_exec_wait: Could not fork\0A\00"
@stdout = global i32 0
@.str.2 = [28 x i8] c"[%d] Spawned in foreground\0A\00"
@.str.3 = [28 x i8] c"[%d] Spawned in background\0A\00"
define i32 @fork_exec_wait(i8** %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.timeval
%6 = bitcast %struct.timeval* %5 to i8*
%7 = icmp sgt i32 %2, 1
%8 = icmp eq i32 %1, 1
%9 = and i1 %8, %7
br i1 %9, label %10, label %38
10: ; preds = %4
%11 = add nsw i32 %2, -1
%12 = shl i32 %11, 3
%13 = tail call i8* @malloc(i32 %12)
store i8* %13, i8** bitcast (i32*** @fork_exec_wait.pipe_fds to i8**)
%14 = zext i32 %11 to i64
%15 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%16 = bitcast i8* %13 to i8**
store i8* %15, i8** %16
%17 = bitcast i8* %15 to i32*
%18 = tail call i32 @pipe(i32* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %32, label %20
20: ; preds = %10, %23
%21 = phi i64 [ %31, %23 ], [ 1, %10 ]
%22 = icmp eq i64 %21, %14
br i1 %22, label %37, label %23
23: ; preds = %20
%24 = load i32**, i32*** @fork_exec_wait.pipe_fds
%25 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%26 = getelementptr inbounds i32*, i32** %24, i64 %21
%27 = bitcast i32** %26 to i8**
store i8* %25, i8** %27
%28 = bitcast i8* %25 to i32*
%29 = tail call i32 @pipe(i32* %28)
%30 = icmp eq i32 %29, -1
%31 = add nuw nsw i64 %21, 1
br i1 %30, label %32, label %20
32: ; preds = %23, %10
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%35 = load i64, i64* @EXIT_FAILURE
%36 = tail call i32 @exit(i64 %35)
unreachable
37: ; preds = %20
store i32 0, i32* @fork_exec_wait.n
br label %38
38: ; preds = %37, %4
%39 = icmp sgt i32 %1, 1
%40 = and i1 %39, %7
%41 = icmp slt i32 %1, %2
%42 = and i1 %41, %40
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i32, i32* @fork_exec_wait.n
%45 = add nsw i32 %44, 1
store i32 %45, i32* @fork_exec_wait.n
br label %46
46: ; preds = %43, %38
%47 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%48 = tail call i32 (...) @fork()
switch i32 %48, label %98 [
i32 -1, label %49
i32 0, label %54
]
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = tail call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%52 = load i64, i64* @EXIT_FAILURE
%53 = tail call i32 @exit(i64 %52)
unreachable
54: ; preds = %46
%55 = icmp eq i32 %3, 0
%56 = zext i1 %55 to i32
%57 = tail call i32 @c_init(i32 %56)
br i1 %7, label %58, label %91
58: ; preds = %54
br i1 %8, label %59, label %66
59: ; preds = %58
%60 = load i32**, i32*** @fork_exec_wait.pipe_fds
%61 = load i32, i32* @fork_exec_wait.n
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32*, i32** %60, i64 %62
%64 = load i32*, i32** %63
%65 = tail call i32 @stdout_to_pipe(i32* %64)
br label %66
66: ; preds = %59, %58
%67 = and i1 %39, %41
br i1 %67, label %68, label %82
68: ; preds = %66
%69 = load i32**, i32*** @fork_exec_wait.pipe_fds
%70 = load i32, i32* @fork_exec_wait.n
%71 = add nsw i32 %70, -1
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i32*, i32** %69, i64 %72
%74 = load i32*, i32** %73
%75 = tail call i32 @pipe_to_stdin(i32* %74)
%76 = load i32**, i32*** @fork_exec_wait.pipe_fds
%77 = load i32, i32* @fork_exec_wait.n
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i32*, i32** %76, i64 %78
%80 = load i32*, i32** %79
%81 = tail call i32 @stdout_to_pipe(i32* %80)
br label %82
82: ; preds = %68, %66
%83 = icmp eq i32 %1, %2
br i1 %83, label %84, label %91
84: ; preds = %82
%85 = load i32**, i32*** @fork_exec_wait.pipe_fds
%86 = load i32, i32* @fork_exec_wait.n
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i32*, i32** %85, i64 %87
%89 = load i32*, i32** %88
%90 = tail call i32 @pipe_to_stdin(i32* %89)
br label %91
91: ; preds = %82, %84, %54
%92 = load i8*, i8** %0
%93 = tail call i32 @execvp(i8* %92, i8** %0)
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %120
95: ; preds = %91
%96 = load i64, i64* @EXIT_FAILURE
%97 = tail call i32 @_exit(i64 %96)
unreachable
98: ; preds = %46
%99 = icmp sgt i32 %48, 0
br i1 %99, label %100, label %120
100: ; preds = %98
br i1 %7, label %101, label %110
101: ; preds = %100
%102 = load i32**, i32*** @fork_exec_wait.pipe_fds
%103 = load i32, i32* @fork_exec_wait.n
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds i32*, i32** %102, i64 %104
%106 = load i32*, i32** %105
%107 = getelementptr inbounds i32, i32* %106, i64 1
%108 = load i32, i32* %107
%109 = tail call i32 @close(i32 %108)
br label %110
110: ; preds = %101, %100
%111 = icmp eq i32 %3, 0
%112 = icmp eq i32 %1, %2
%113 = and i1 %112, %111
%114 = load i32, i32* @stdout
br i1 %113, label %115, label %118
115: ; preds = %110
%116 = tail call i32 (i32, i8*, ...) @fprintf(i32 %114, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %48)
%117 = call i32 @c_wait(i32 %48, %struct.timeval* %5, i32 0)
br label %122
118: ; preds = %110
%119 = tail call i32 (i32, i8*, ...) @fprintf(i32 %114, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %48)
br label %120
120: ; preds = %91, %118, %98
%121 = icmp eq i32 %3, 0
br i1 %121, label %122, label %127
122: ; preds = %115, %120
%123 = phi i32 [ %117, %115 ], [ 0, %120 ]
%124 = call i64 @WEXITSTATUS(i32 %123)
%125 = load i64, i64* @EXIT_FAILURE
%126 = icmp eq i64 %124, %125
br i1 %126, label %128, label %127
127: ; preds = %122, %120
br label %128
128: ; preds = %122, %127
%129 = phi i1 [ false, %127 ], [ true, %122 ]
%130 = phi i32 [ 0, %127 ], [ -1, %122 ]
%131 = icmp eq i32 %1, %2
%132 = or i1 %131, %129
%133 = and i1 %7, %132
br i1 %133, label %134, label %137
134: ; preds = %128
%135 = load i32**, i32*** @fork_exec_wait.pipe_fds
%136 = call i32 @free(i32** %135)
br label %137
137: ; preds = %134, %128
ret i32 %130
}