#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 */
typedef int pid_t;

/* Variables and functions */
int call_exec(char **, char **, char **);
int dup2(int, int);
int fork();
int /*<<< orphan*/ my_putstr(char *);
int setsid();
int wait_exec_red(int, int, int);
int my_exec_red(char **arg, char **env, char **tab, int fd) {
  pid_t pid;
  int statue;
  statue = 0;
  if ((pid = fork()) == -1) {
    my_putstr("Fork is dead\n");
    return (-1);
  }
  if (pid == 0) {
    if ((setsid() == -1))
      return (-1);
    if ((dup2(fd, 1)) == -1)
      return (-1);
    if ((call_exec(arg, env, tab)) == -1)
      return (-1);
  } else if (wait_exec_red(pid, statue, fd) == -1)
    return (-1);
  return (0);
}


x86 assembly (clang, -O0):
<my_exec_red>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    11a9 <my_exec_red+0x49>
lea    0xe68(%rip),%rdi
call   1050 <my_putstr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <my_exec_red+0xeb>
cmpl   $0x0,-0x28(%rbp)
jne    121c <my_exec_red+0xbc>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11cf <my_exec_red+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <my_exec_red+0xeb>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11f1 <my_exec_red+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <my_exec_red+0xeb>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <call_exec@plt>
cmp    $0xffffffff,%eax
jne    1217 <my_exec_red+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <my_exec_red+0xeb>
jmp    1244 <my_exec_red+0xe4>
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x24(%rbp),%edx
call   1070 <wait_exec_red@plt>
cmp    $0xffffffff,%eax
jne    123f <my_exec_red+0xdf>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <my_exec_red+0xeb>
jmp    1244 <my_exec_red+0xe4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_exec_red>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1191 <my_exec_red+0x31>
cmp    $0xffffffff,%eax
jne    11c8 <my_exec_red+0x68>
lea    0xe76(%rip),%rdi
call   1050 <my_putstr@plt>
jmp    11dc <my_exec_red+0x7c>
xor    %eax,%eax
call   1040 <setsid@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11e1 <my_exec_red+0x81>
mov    %ebp,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     11e1 <my_exec_red+0x81>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <call_exec@plt>
cmp    $0xffffffff,%eax
jne    11d8 <my_exec_red+0x78>
jmp    11e1 <my_exec_red+0x81>
mov    %eax,%edi
xor    %esi,%esi
mov    %ebp,%edx
call   1070 <wait_exec_red@plt>
cmp    $0xffffffff,%eax
je     11dc <my_exec_red+0x7c>
xor    %ebx,%ebx
jmp    11e1 <my_exec_red+0x81>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
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 @my_exec_red(i8** %0, i8** %1, i8** %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i8**
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8** %0, i8*** %6
store i8** %1, i8*** %7
store i8** %2, i8*** %8
store i32 %3, i32* %9
store i32 0, i32* %11
%12 = call i32 (...) @fork()
store i32 %12, i32* %10
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = 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 %45
16: ; preds = %4
%17 = load i32, i32* %10
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %36
19: ; preds = %16
%20 = call i32 (...) @setsid()
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 -1, i32* %5
br label %45
23: ; preds = %19
%24 = load i32, i32* %9
%25 = call i32 @dup2(i32 %24, i32 1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 -1, i32* %5
br label %45
28: ; preds = %23
%29 = load i8**, i8*** %6
%30 = load i8**, i8*** %7
%31 = load i8**, i8*** %8
%32 = call i32 @call_exec(i8** %29, i8** %30, i8** %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %28
store i32 -1, i32* %5
br label %45
35: ; preds = %28
br label %44
36: ; preds = %16
%37 = load i32, i32* %10
%38 = load i32, i32* %11
%39 = load i32, i32* %9
%40 = call i32 @wait_exec_red(i32 %37, i32 %38, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 -1, i32* %5
br label %45
43: ; preds = %36
br label %44
44: ; preds = %43, %35
store i32 0, i32* %5
br label %45
45: ; preds = %44, %42, %34, %27, %22, %14
%46 = load i32, i32* %5
ret i32 %46
}

LLVM IR (-O2):
@.str = [14 x i8] c"Fork is dead\0A\00"
define i32 @my_exec_red(i8** %0, i8** %1, i8** %2, i32 %3) {
%5 = tail call i32 (...) @fork()
switch i32 %5, label %17 [
i32 -1, label %6
i32 0, label %8
]
6: ; preds = %4
%7 = tail call i32 @my_putstr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %21
8: ; preds = %4
%9 = tail call i32 (...) @setsid()
%10 = icmp eq i32 %9, -1
br i1 %10, label %21, label %11
11: ; preds = %8
%12 = tail call i32 @dup2(i32 %3, i32 1)
%13 = icmp eq i32 %12, -1
br i1 %13, label %21, label %14
14: ; preds = %11
%15 = tail call i32 @call_exec(i8** %0, i8** %1, i8** %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %21, label %20
17: ; preds = %4
%18 = tail call i32 @wait_exec_red(i32 %5, i32 0, i32 %3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %21, label %20
20: ; preds = %17, %14
br label %21
21: ; preds = %17, %14, %11, %8, %20, %6
%22 = phi i32 [ -1, %6 ], [ 0, %20 ], [ -1, %8 ], [ -1, %11 ], [ -1, %14 ], [ -1, %17 ]
ret i32 %22
}