#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*/ close(int);
int dup2(int, int);
int exec_path(char **, char **, char **);
int fork();
int /*<<< orphan*/ my_putstr(char *);
int pipe(int *);
int setsid();
int wait_father(int *, char **, char **, char **);
int execution_pipe(char **s1, char **s2, char **env, char **path) {
  int p[2];
  int pid;
  if ((pipe(p)) == -1)
    return (-1);
  if ((pid = fork()) == -1) {
    my_putstr("Fork is dead\n");
    return (1);
  }
  if (pid == 0) {
    if ((setsid() == -1))
      return (-1);
    close(p[0]);
    if ((dup2(p[1], 1)) == -1)
      return (-1);
    if ((exec_path(env, s1, path)) == -1)
      return (-1);
  } else if ((wait_father(p, env, s2, path)) == -1)
    return (-1);
  return (0);
}


x86 assembly (clang, -O0):
<execution_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11b6 <execution_pipe+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <execution_pipe+0x112>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    11e1 <execution_pipe+0x61>
lea    0xe30(%rip),%rdi
call   1070 <my_putstr@plt>
movl   $0x1,-0x4(%rbp)
jmp    1292 <execution_pipe+0x112>
cmpl   $0x0,-0x34(%rbp)
jne    125c <execution_pipe+0xdc>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1207 <execution_pipe+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <execution_pipe+0x112>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1231 <execution_pipe+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <execution_pipe+0x112>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1080 <exec_path@plt>
cmp    $0xffffffff,%eax
jne    1257 <execution_pipe+0xd7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <execution_pipe+0x112>
jmp    128b <execution_pipe+0x10b>
lea    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1090 <wait_father@plt>
cmp    $0xffffffff,%eax
jne    1286 <execution_pipe+0x106>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <execution_pipe+0x112>
jmp    128b <execution_pipe+0x10b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execution_pipe>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     1226 <execution_pipe+0xa6>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11cf <execution_pipe+0x4f>
cmp    $0xffffffff,%eax
jne    120c <execution_pipe+0x8c>
lea    0xe3d(%rip),%rdi
call   1070 <my_putstr@plt>
mov    $0x1,%ebp
jmp    1226 <execution_pipe+0xa6>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1226 <execution_pipe+0xa6>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1226 <execution_pipe+0xa6>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1080 <exec_path@plt>
cmp    $0xffffffff,%eax
jne    1224 <execution_pipe+0xa4>
jmp    1226 <execution_pipe+0xa6>
lea    0x8(%rsp),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1090 <wait_father@plt>
cmp    $0xffffffff,%eax
je     1226 <execution_pipe+0xa6>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Fork is dead\0A\00"
define i32 @execution_pipe(i8** %0, i8** %1, i8** %2, i8** %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i8**
%9 = alloca i8**
%10 = alloca [2 x i32]
%11 = alloca i32
store i8** %0, i8*** %6
store i8** %1, i8*** %7
store i8** %2, i8*** %8
store i8** %3, i8*** %9
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 -1, i32* %5
br label %55
16: ; preds = %4
%17 = call i32 (...) @fork()
store i32 %17, i32* %11
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @my_putstr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %55
21: ; preds = %16
%22 = load i32, i32* %11
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %45
24: ; preds = %21
%25 = call i32 (...) @setsid()
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 -1, i32* %5
br label %55
28: ; preds = %24
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @dup2(i32 %33, i32 1)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %28
store i32 -1, i32* %5
br label %55
37: ; preds = %28
%38 = load i8**, i8*** %8
%39 = load i8**, i8*** %6
%40 = load i8**, i8*** %9
%41 = call i32 @exec_path(i8** %38, i8** %39, i8** %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %37
store i32 -1, i32* %5
br label %55
44: ; preds = %37
br label %54
45: ; preds = %21
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%47 = load i8**, i8*** %8
%48 = load i8**, i8*** %7
%49 = load i8**, i8*** %9
%50 = call i32 @wait_father(i32* %46, i8** %47, i8** %48, i8** %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %45
store i32 -1, i32* %5
br label %55
53: ; preds = %45
br label %54
54: ; preds = %53, %44
store i32 0, i32* %5
br label %55
55: ; preds = %54, %52, %43, %36, %27, %19, %15
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
@.str = [14 x i8] c"Fork is dead\0A\00"
define i32 @execution_pipe(i8** %0, i8** %1, i8** %2, i8** %3) {
%5 = alloca [2 x i32]
%6 = bitcast [2 x i32]* %5 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %31, label %10
10: ; preds = %4
%11 = call i32 (...) @fork()
switch i32 %11, label %27 [
i32 -1, label %12
i32 0, label %14
]
12: ; preds = %10
%13 = call i32 @my_putstr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %31
14: ; preds = %10
%15 = call i32 (...) @setsid()
%16 = icmp eq i32 %15, -1
br i1 %16, label %31, label %17
17: ; preds = %14
%18 = load i32, i32* %7
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @dup2(i32 %21, i32 1)
%23 = icmp eq i32 %22, -1
br i1 %23, label %31, label %24
24: ; preds = %17
%25 = call i32 @exec_path(i8** %2, i8** %0, i8** %3)
%26 = icmp eq i32 %25, -1
br i1 %26, label %31, label %30
27: ; preds = %10
%28 = call i32 @wait_father(i32* %7, i8** %2, i8** %1, i8** %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %31, label %30
30: ; preds = %27, %24
br label %31
31: ; preds = %27, %24, %17, %14, %4, %30, %12
%32 = phi i32 [ 1, %12 ], [ 0, %30 ], [ -1, %4 ], [ -1, %14 ], [ -1, %17 ], [ -1, %24 ], [ -1, %27 ]
ret i32 %32
}