#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(int);
int fork();
int /*<<< orphan*/ perror(char *, ...);
int /*<<< orphan*/ runPipe(char **, char **, int);
int waitpid(int, int *, int /*<<< orphan*/);
int cmd_pipe_init(char **tokens, int pipes, char **envp) {
  int pid, status;
  pid = fork();
  if (pid == -1) {
    perror("unable to fork child\n");
  } else if (pid == 0) {
    runPipe(tokens, envp, pipes);
  } else {
    if (pid == waitpid(pid, &status, 0)) {
      ;
    } else {
      perror("error in waitpid. Error no is %d\n", status);
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<cmd_pipe_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    118a <cmd_pipe_init+0x3a>
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1050 <perror@plt>
jmp    11f4 <cmd_pipe_init+0xa4>
cmpl   $0x0,-0x1c(%rbp)
jne    11a9 <cmd_pipe_init+0x59>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <runPipe@plt>
jmp    11ef <cmd_pipe_init+0x9f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jne    11cf <cmd_pipe_init+0x7f>
jmp    11ea <cmd_pipe_init+0x9a>
mov    -0x20(%rbp),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11ef <cmd_pipe_init+0x9f>
jmp    11f4 <cmd_pipe_init+0xa4>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_pipe_init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbp
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     11a0 <cmd_pipe_init+0x50>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11bb <cmd_pipe_init+0x6b>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    %eax,%ebx
je     11ae <cmd_pipe_init+0x5e>
mov    0x4(%rsp),%esi
lea    0xe87(%rip),%rdi
xor    %eax,%eax
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <runPipe@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3e(%rip),%rdi
xor    %eax,%eax
call   1050 <perror@plt>
jmp    11ae <cmd_pipe_init+0x5e>

LLVM IR (-O0):
@.str = [22 x i8] c"unable to fork child\0A\00"
@.str.1 = [34 x i8] c"error in waitpid. Error no is %d\0A\00"
define i32 @cmd_pipe_init(i8** %0, i32 %1, i8** %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i8** %2, i8*** %6
%9 = call i32 (...) @fork()
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %34
14: ; preds = %3
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i8**, i8*** %4
%19 = load i8**, i8*** %6
%20 = load i32, i32* %5
%21 = call i32 @runPipe(i8** %18, i8** %19, i32 %20)
br label %33
22: ; preds = %14
%23 = load i32, i32* %7
%24 = load i32, i32* %7
%25 = call i32 @waitpid(i32 %24, i32* %8, i32 0)
%26 = icmp eq i32 %23, %25
br i1 %26, label %27, label %28
27: ; preds = %22
br label %32
28: ; preds = %22
%29 = load i32, i32* %8
%30 = call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %27
br label %33
33: ; preds = %32, %17
br label %34
34: ; preds = %33, %12
ret i32 0
}

LLVM IR (-O2):
@.str = [22 x i8] c"unable to fork child\0A\00"
@.str.1 = [34 x i8] c"error in waitpid. Error no is %d\0A\00"
define i32 @cmd_pipe_init(i8** %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 (...) @fork()
switch i32 %6, label %11 [
i32 -1, label %7
i32 0, label %9
]
7: ; preds = %3
%8 = tail call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %18
9: ; preds = %3
%10 = tail call i32 @runPipe(i8** %0, i8** %2, i32 %1)
br label %18
11: ; preds = %3
%12 = call i32 @waitpid(i32 %6, i32* %4, i32 0)
%13 = icmp eq i32 %6, %12
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = load i32, i32* %4
%16 = call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %9, %11, %7
ret i32 0
}