#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 scalar_t__ pid_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int /*<<< orphan*/ dup2(int, int);
int execvp(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int open(char const *, int, int);
int /*<<< orphan*/ throwing_fork_errors(scalar_t__);
scalar_t__ wait(int *);
void fork_exec_with_inp_redir(char **for_exe, int flag, const char *file) {
  pid_t pid;
  pid = fork();
  if (pid == 0) {
    if (flag) {
      int fd = open(file, O_RDWR | O_TRUNC | O_CREAT,
                    S_IWUSR | S_IROTH | S_IWGRP | S_IRUSR);
      dup2(fd, 1);
    }
    if (execvp(for_exe[0], &for_exe[1]) == -1) {
      exit(-2);
    }
  } else if (pid < 0) {
    throwing_fork_errors(pid);
  } else {
    while (wait((int *)NULL) != pid)
      ;
  }
}


x86 assembly (clang, -O0):
<fork_exec_with_inp_redir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1227 <fork_exec_with_inp_redir+0xb7>
cmpl   $0x0,-0xc(%rbp)
je     11fb <fork_exec_with_inp_redir+0x8b>
mov    -0x18(%rbp),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
mov    0x2de6(%rip),%rax
or     (%rax),%edx
mov    0x2df5(%rip),%rax
or     (%rax),%edx
mov    0x2dcc(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
add    $0x8,%rsi
call   1060 <execvp@plt>
cmp    $0xffffffff,%eax
jne    1222 <fork_exec_with_inp_redir+0xb2>
mov    $0xfffffffe,%edi
call   1070 <exit@plt>
jmp    1267 <fork_exec_with_inp_redir+0xf7>
cmpq   $0x0,-0x20(%rbp)
jge    1240 <fork_exec_with_inp_redir+0xd0>
mov    -0x20(%rbp),%rdi
call   1040 <throwing_fork_errors@plt>
jmp    1262 <fork_exec_with_inp_redir+0xf2>
jmp    1245 <fork_exec_with_inp_redir+0xd5>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
cmp    -0x20(%rbp),%rax
je     125d <fork_exec_with_inp_redir+0xed>
jmp    1245 <fork_exec_with_inp_redir+0xd5>
jmp    1262 <fork_exec_with_inp_redir+0xf2>
jmp    1267 <fork_exec_with_inp_redir+0xf7>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_exec_with_inp_redir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11ae <fork_exec_with_inp_redir+0x3e>
mov    %rax,%rbx
js     1224 <fork_exec_with_inp_redir+0xb4>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
call   1080 <wait@plt>
cmp    %rbx,%rax
jne    11a0 <fork_exec_with_inp_redir+0x30>
jmp    1219 <fork_exec_with_inp_redir+0xa9>
test   %ebp,%ebp
je     1205 <fork_exec_with_inp_redir+0x95>
mov    0x2e1f(%rip),%rax
mov    0x2e30(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
mov    0x2df4(%rip),%rax
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dea(%rip),%rax
or     (%rax),%edx
mov    0x2dc1(%rip),%rax
or     (%rax),%edx
mov    %r14,%rdi
call   1050 <open@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    (%r15),%rdi
add    $0x8,%r15
mov    %r15,%rsi
call   1060 <execvp@plt>
cmp    $0xffffffff,%eax
je     1236 <fork_exec_with_inp_redir+0xc6>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <throwing_fork_errors@plt>
mov    $0xfffffffe,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IROTH = global i32 0
@S_IWGRP = global i32 0
@S_IRUSR = global i32 0
define void @fork_exec_with_inp_redir(i8** %0, i32 %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = call i64 (...) @fork()
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %43
12: ; preds = %3
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %12
%16 = load i8*, i8** %6
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IWUSR
%23 = load i32, i32* @S_IROTH
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IWGRP
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IRUSR
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %16, i32 %21, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = call i32 @dup2(i32 %30, i32 1)
br label %32
32: ; preds = %15, %12
%33 = load i8**, i8*** %4
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = load i8**, i8*** %4
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = call i32 @execvp(i8* %35, i8** %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @exit(i32 -2)
unreachable
42: ; preds = %32
br label %57
43: ; preds = %3
%44 = load i64, i64* %7
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i64, i64* %7
%48 = call i32 @throwing_fork_errors(i64 %47)
br label %56
49: ; preds = %43
br label %50
50: ; preds = %54, %49
%51 = call i64 @wait(i32* null)
%52 = load i64, i64* %7
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50
br label %50
55: ; preds = %50
br label %56
56: ; preds = %55, %46
br label %57
57: ; preds = %56, %42
ret void
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IROTH = global i32 0
@S_IWGRP = global i32 0
@S_IRUSR = global i32 0
define void @fork_exec_with_inp_redir(i8** %0, i32 %1, i8* nocapture %2) {
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %30
6: ; preds = %3
%7 = icmp eq i32 %1, 0
br i1 %7, label %23, label %8
8: ; preds = %6
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %10, %9
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IWUSR
%15 = load i32, i32* @S_IROTH
%16 = or i32 %15, %14
%17 = load i32, i32* @S_IWGRP
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = or i32 %18, %19
%21 = tail call i32 @open(i8* %2, i32 %13, i32 %20)
%22 = tail call i32 @dup2(i32 %21, i32 1)
br label %23
23: ; preds = %8, %6
%24 = load i8*, i8** %0
%25 = getelementptr inbounds i8*, i8** %0, i64 1
%26 = tail call i32 @execvp(i8* %24, i8** %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %37
28: ; preds = %23
%29 = tail call i32 @exit(i32 -2)
unreachable
30: ; preds = %3
%31 = icmp slt i64 %4, 0
br i1 %31, label %32, label %34
32: ; preds = %30
%33 = tail call i32 @throwing_fork_errors(i64 %4)
br label %37
34: ; preds = %30, %34
%35 = tail call i64 @wait(i32* null)
%36 = icmp eq i64 %35, %4
br i1 %36, label %37, label %34
37: ; preds = %34, %32, %23
ret void
}