#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
scalar_t__ fork();
int open(char const *, int, int);
int system(char *);
int /*<<< orphan*/ throwing_fork_errors(scalar_t__);
scalar_t__ wait(int *);
void fork_exec_with_inp_redir2(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);
    }
    int df = system(for_exe);
    fflush(NULL);
    exit(df);
  } else if (pid < 0) {
    throwing_fork_errors(pid);
  } else {
    while (wait((int *)NULL) != pid)
      ;
  }
}


x86 assembly (clang, -O0):
<fork_exec_with_inp_redir2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1228 <fork_exec_with_inp_redir2+0xa8>
cmpl   $0x0,-0xc(%rbp)
je     120b <fork_exec_with_inp_redir2+0x8b>
mov    -0x18(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
mov    0x2dd6(%rip),%rax
or     (%rax),%edx
mov    0x2de5(%rip),%rax
or     (%rax),%edx
mov    0x2dbc(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%rdi
call   1030 <system@plt>
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x28(%rbp),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jge    1241 <fork_exec_with_inp_redir2+0xc1>
mov    -0x20(%rbp),%rdi
call   1050 <throwing_fork_errors@plt>
jmp    1263 <fork_exec_with_inp_redir2+0xe3>
jmp    1246 <fork_exec_with_inp_redir2+0xc6>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
cmp    -0x20(%rbp),%rax
je     125e <fork_exec_with_inp_redir2+0xde>
jmp    1246 <fork_exec_with_inp_redir2+0xc6>
jmp    1263 <fork_exec_with_inp_redir2+0xe3>
jmp    1268 <fork_exec_with_inp_redir2+0xe8>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_exec_with_inp_redir2>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11c9 <fork_exec_with_inp_redir2+0x49>
mov    %rax,%rbx
js     11b7 <fork_exec_with_inp_redir2+0x37>
xor    %edi,%edi
call   1090 <wait@plt>
cmp    %rbx,%rax
jne    11a0 <fork_exec_with_inp_redir2+0x20>
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    1050 <throwing_fork_errors@plt>
test   %ebp,%ebp
je     1220 <fork_exec_with_inp_redir2+0xa0>
mov    0x2e04(%rip),%rax
mov    0x2e15(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dcf(%rip),%rax
or     (%rax),%edx
mov    0x2da6(%rip),%rax
or     (%rax),%edx
mov    %r15,%rdi
call   1070 <open@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %r14,%rdi
call   1030 <system@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebx,%edi
call   1080 <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_redir2(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = call i64 (...) @fork()
store i64 %10, i64* %7
%11 = load i64, i64* %7
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %39
13: ; preds = %3
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %13
%17 = load i8*, i8** %6
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IWUSR
%24 = load i32, i32* @S_IROTH
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IWGRP
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRUSR
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %17, i32 %22, i32 %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = call i32 @dup2(i32 %31, i32 1)
br label %33
33: ; preds = %16, %13
%34 = load i8*, i8** %4
%35 = call i32 @system(i8* %34)
store i32 %35, i32* %9
%36 = call i32 @fflush(i32* null)
%37 = load i32, i32* %9
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %3
%40 = load i64, i64* %7
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i64, i64* %7
%44 = call i32 @throwing_fork_errors(i64 %43)
br label %52
45: ; preds = %39
br label %46
46: ; preds = %50, %45
%47 = call i64 @wait(i32* null)
%48 = load i64, i64* %7
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
br label %46
51: ; preds = %46
br label %52
52: ; preds = %51, %42
br label %53
53: ; preds = %52
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_redir2(i8* nocapture %0, i32 %1, i8* nocapture %2) {
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %27
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 = tail call i32 @system(i8* %0)
%25 = tail call i32 @fflush(i32* null)
%26 = tail call i32 @exit(i32 %24)
unreachable
27: ; preds = %3
%28 = icmp slt i64 %4, 0
br i1 %28, label %29, label %31
29: ; preds = %27
%30 = tail call i32 @throwing_fork_errors(i64 %4)
br label %34
31: ; preds = %27, %31
%32 = tail call i64 @wait(i32* null)
%33 = icmp eq i64 %32, %4
br i1 %33, label %34, label %31
34: ; preds = %31, %29
ret void
}