#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*/ ECHILD;
int /*<<< orphan*/ create_pipe(char **, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ redirect(char **, int, int, int);
int /*<<< orphan*/ stderr;
int wait(int /*<<< orphan*/ *);
void launchjob(char **arguments, char *line, char **fname, int inred,
               int outred, int append, int pipe_flag) {
  int pid, child_pid;
  if (pipe_flag != -1) {
    switch (pid = fork()) {
    case 0:
      create_pipe(arguments, pipe_flag);
      break;
    case -1:
      fprintf(stderr, "ERROR can't create child process!\n");
      break;
    default:
      while (child_pid = wait(NULL) != -1)
        ;
      if (errno != ECHILD) {
        printf("There has been a problem waiting for the children!\n");
        exit(107);
      }
    }
  } else {
    if (inred || outred || append)
      redirect(fname, inred, outred, append);
    switch (pid = fork()) {
    case 0:
      execvp(arguments[0], arguments);
      fprintf(stderr, "ERROR %s no such program!\n", line);
      exit(1);
    case -1:
      fprintf(stderr, "ERROR can't create child process!\n");
      break;
    default:
      while (child_pid = wait(NULL) != -1)
        ;
      if (errno != ECHILD) {
        printf("There has been a problem waiting for the children!\n");
        exit(107);
      }
    }
  }
}


x86 assembly (clang, -O0):
<launchjob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
cmpl   $0xffffffff,0x10(%rbp)
je     126f <launchjob+0xef>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x28(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x30(%rbp)
sub    $0xffffffff,%eax
je     11ea <launchjob+0x6a>
jmp    11c9 <launchjob+0x49>
mov    -0x30(%rbp),%eax
test   %eax,%eax
jne    1206 <launchjob+0x86>
jmp    11d9 <launchjob+0x59>
mov    -0x8(%rbp),%rdi
mov    0x10(%rbp),%esi
call   1050 <create_pipe@plt>
jmp    126a <launchjob+0xea>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    126a <launchjob+0xea>
jmp    120b <launchjob+0x8b>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x2c(%rbp)
test   $0x1,%al
jne    1232 <launchjob+0xb2>
jmp    1237 <launchjob+0xb7>
jmp    120b <launchjob+0x8b>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%eax
je     1265 <launchjob+0xe5>
lea    0xdcd(%rip),%rdi
call   1040 <printf@plt>
mov    $0x6b,%edi
call   1080 <exit@plt>
jmp    126a <launchjob+0xea>
jmp    1386 <launchjob+0x206>
cmpl   $0x0,-0x1c(%rbp)
jne    128d <launchjob+0x10d>
cmpl   $0x0,-0x20(%rbp)
jne    128d <launchjob+0x10d>
cmpl   $0x0,-0x24(%rbp)
je     129f <launchjob+0x11f>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
call   1030 <redirect@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x28(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
sub    $0xffffffff,%eax
je     1301 <launchjob+0x181>
jmp    12bc <launchjob+0x13c>
mov    -0x34(%rbp),%eax
test   %eax,%eax
jne    131d <launchjob+0x19d>
jmp    12cc <launchjob+0x14c>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <execvp@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd67(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
lea    0xcef(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1381 <launchjob+0x201>
jmp    1322 <launchjob+0x1a2>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x2c(%rbp)
test   $0x1,%al
jne    1349 <launchjob+0x1c9>
jmp    134e <launchjob+0x1ce>
jmp    1322 <launchjob+0x1a2>
mov    0x2c83(%rip),%rax
mov    (%rax),%eax
mov    0x2c72(%rip),%rcx
cmp    (%rcx),%eax
je     137c <launchjob+0x1fc>
lea    0xcb6(%rip),%rdi
call   1040 <printf@plt>
mov    $0x6b,%edi
call   1080 <exit@plt>
jmp    1381 <launchjob+0x201>
jmp    1386 <launchjob+0x206>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<launchjob>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x20(%rsp),%ebp
cmp    $0xffffffff,%ebp
je     11be <launchjob+0x3e>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     1215 <launchjob+0x95>
test   %eax,%eax
jne    11b0 <launchjob+0x30>
mov    %rbx,%rdi
mov    %ebp,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <create_pipe@plt>
xchg   %ax,%ax
xor    %edi,%edi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
jne    11b0 <launchjob+0x30>
jmp    11fc <launchjob+0x7c>
mov    %rsi,%r14
mov    %r8d,%eax
or     %ecx,%eax
or     %r9d,%eax
je     11db <launchjob+0x5b>
mov    %rdx,%rdi
mov    %ecx,%esi
mov    %r8d,%edx
mov    %r9d,%ecx
call   1040 <redirect@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     1215 <launchjob+0x95>
test   %eax,%eax
je     1246 <launchjob+0xc6>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
jne    11f0 <launchjob+0x70>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%eax
jne    1230 <launchjob+0xb0>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <fprintf@plt>
lea    0xe07(%rip),%rdi
call   1030 <puts@plt>
mov    $0x6b,%edi
call   1080 <exit@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1070 <execvp@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [35 x i8] c"ERROR can't create child process!\0A\00"
@errno = global i32 0
@ECHILD = global i32 0
@.str.1 = [52 x i8] c"There has been a problem waiting for the children!\0A\00"
@.str.2 = [27 x i8] c"ERROR %s no such program!\0A\00"
define void @launchjob(i8** %0, i8* %1, i8** %2, i32 %3, i32 %4, i32 %5, i32 %6) {
%8 = alloca i8**
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8** %0, i8*** %8
store i8* %1, i8** %9
store i8** %2, i8*** %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 %6, i32* %14
%17 = load i32, i32* %14
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %43
19: ; preds = %7
%20 = call i32 (...) @fork()
store i32 %20, i32* %15
switch i32 %20, label %28 [
i32 0, label %21
i32 -1, label %25
]
21: ; preds = %19
%22 = load i8**, i8*** %8
%23 = load i32, i32* %14
%24 = call i32 @create_pipe(i8** %22, i32 %23)
br label %42
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %42
28: ; preds = %19
br label %29
29: ; preds = %33, %28
%30 = call i32 @wait(i32* null)
%31 = icmp ne i32 %30, -1
%32 = zext i1 %31 to i32
store i32 %32, i32* %16
br i1 %31, label %33, label %34
33: ; preds = %29
br label %29
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = load i32, i32* @ECHILD
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @exit(i32 107)
unreachable
41: ; preds = %34
br label %42
42: ; preds = %41, %25, %21
br label %88
43: ; preds = %7
%44 = load i32, i32* %11
%45 = icmp ne i32 %44, 0
br i1 %45, label %52, label %46
46: ; preds = %43
%47 = load i32, i32* %12
%48 = icmp ne i32 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %46
%50 = load i32, i32* %13
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %49, %46, %43
%53 = load i8**, i8*** %10
%54 = load i32, i32* %11
%55 = load i32, i32* %12
%56 = load i32, i32* %13
%57 = call i32 @redirect(i8** %53, i32 %54, i32 %55, i32 %56)
br label %58
58: ; preds = %52, %49
%59 = call i32 (...) @fork()
store i32 %59, i32* %15
switch i32 %59, label %73 [
i32 0, label %60
i32 -1, label %70
]
60: ; preds = %58
%61 = load i8**, i8*** %8
%62 = getelementptr inbounds i8*, i8** %61, i64 0
%63 = load i8*, i8** %62
%64 = load i8**, i8*** %8
%65 = call i32 @execvp(i8* %63, i8** %64)
%66 = load i32, i32* @stderr
%67 = load i8*, i8** %9
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %67)
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %58
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %87
73: ; preds = %58
br label %74
74: ; preds = %78, %73
%75 = call i32 @wait(i32* null)
%76 = icmp ne i32 %75, -1
%77 = zext i1 %76 to i32
store i32 %77, i32* %16
br i1 %76, label %78, label %79
78: ; preds = %74
br label %74
79: ; preds = %74
%80 = load i32, i32* @errno
%81 = load i32, i32* @ECHILD
%82 = icmp ne i32 %80, %81
br i1 %82, label %83, label %86
83: ; preds = %79
%84 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%85 = call i32 @exit(i32 107)
unreachable
86: ; preds = %79
br label %87
87: ; preds = %86, %70
br label %88
88: ; preds = %87, %42
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [35 x i8] c"ERROR can't create child process!\0A\00"
@errno = global i32 0
@ECHILD = global i32 0
@.str.2 = [27 x i8] c"ERROR %s no such program!\0A\00"
@str.3 = [51 x i8] c"There has been a problem waiting for the children!\00"
define void @launchjob(i8** %0, i8* %1, i8** %2, i32 %3, i32 %4, i32 %5, i32 %6) {
%8 = icmp eq i32 %6, -1
br i1 %8, label %26, label %9
9: ; preds = %7
%10 = tail call i32 (...) @fork()
switch i32 %10, label %16 [
i32 0, label %11
i32 -1, label %13
]
11: ; preds = %9
%12 = tail call i32 @create_pipe(i8** %0, i32 %6)
br label %53
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %53
16: ; preds = %9, %16
%17 = tail call i32 @wait(i32* null)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %16
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = load i32, i32* @ECHILD
%22 = icmp eq i32 %20, %21
br i1 %22, label %53, label %23
23: ; preds = %19
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.3, i64 0, i64 0))
%25 = tail call i32 @exit(i32 107)
unreachable
26: ; preds = %7
%27 = or i32 %4, %3
%28 = or i32 %27, %5
%29 = icmp eq i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = tail call i32 @redirect(i8** %2, i32 %3, i32 %4, i32 %5)
br label %32
32: ; preds = %26, %30
%33 = tail call i32 (...) @fork()
switch i32 %33, label %43 [
i32 0, label %34
i32 -1, label %40
]
34: ; preds = %32
%35 = load i8*, i8** %0
%36 = tail call i32 @execvp(i8* %35, i8** %0)
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %32
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %53
43: ; preds = %32, %43
%44 = tail call i32 @wait(i32* null)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %43
46: ; preds = %43
%47 = load i32, i32* @errno
%48 = load i32, i32* @ECHILD
%49 = icmp eq i32 %47, %48
br i1 %49, label %53, label %50
50: ; preds = %46
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.3, i64 0, i64 0))
%52 = tail call i32 @exit(i32 107)
unreachable
53: ; preds = %40, %46, %11, %13, %19
ret void
}