#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 STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execvp(char const *, char *const *);
scalar_t__ fflush(int /*<<< orphan*/);
scalar_t__ popen_noshell_close_and_dup(int *, int, int);
scalar_t__ popen_noshell_reopen_fd_to_dev_null(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ warn(char *, ...);
void popen_noshell_child_process(int pipefd_0, int pipefd_1, int read_pipe,
                                 int ignore_stderr, const char *file,
                                 const char *const *argv) {
  int closed_child_fd;
  int closed_pipe_fd;
  int dupped_child_fd;
  int pipefd[2] = {pipefd_0, pipefd_1};
  if (ignore_stderr) {
    if (popen_noshell_reopen_fd_to_dev_null(STDERR_FILENO) != 0) {
      warn("popen_noshell_reopen_fd_to_dev_null(%d)", STDERR_FILENO);
      _exit(255);
    };
  }
  if (read_pipe) {
    closed_child_fd = STDIN_FILENO;
    closed_pipe_fd = 0;
    dupped_child_fd = STDOUT_FILENO;
  } else {
    closed_child_fd = STDOUT_FILENO;
    closed_pipe_fd = 1;
    dupped_child_fd = STDIN_FILENO;
  }
  if (popen_noshell_reopen_fd_to_dev_null(closed_child_fd) != 0) {
    {
      warn("popen_noshell_reopen_fd_to_dev_null(%d)", closed_child_fd);
      _exit(255);
    };
  }
  if (popen_noshell_close_and_dup(pipefd, closed_pipe_fd, dupped_child_fd) !=
      0) {
    {
      warn("popen_noshell_close_and_dup(%d ,%d)", closed_pipe_fd,
           dupped_child_fd);
      _exit(255);
    };
  }
  execvp(file, (char *const *)argv);
  warn("exec(\"%s\") inside the child", file);
  if (fflush(stdout) != 0) {
    warn("fflush(stdout)");
    _exit(255);
  };
  if (fflush(stderr) != 0) {
    warn("fflush(stderr)");
    _exit(255);
  };
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  _exit(255);
}


x86 assembly (clang, -O0):
<popen_noshell_child_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8,-0x18(%rbp)
mov    %r9,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11e0 <popen_noshell_child_process+0x70>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1070 <popen_noshell_reopen_fd_to_dev_null@plt>
cmp    $0x0,%rax
je     11db <popen_noshell_child_process+0x6b>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <warn@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>
jmp    11e0 <popen_noshell_child_process+0x70>
cmpl   $0x0,-0xc(%rbp)
je     120e <popen_noshell_child_process+0x9e>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    122d <popen_noshell_child_process+0xbd>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <popen_noshell_reopen_fd_to_dev_null@plt>
cmp    $0x0,%rax
je     125a <popen_noshell_child_process+0xea>
mov    -0x24(%rbp),%esi
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <warn@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>
lea    -0x34(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1060 <popen_noshell_close_and_dup@plt>
cmp    $0x0,%rax
je     1291 <popen_noshell_child_process+0x121>
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1040 <warn@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <execvp@plt>
mov    -0x18(%rbp),%rsi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1040 <warn@plt>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
cmp    $0x0,%rax
je     12e0 <popen_noshell_child_process+0x170>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1040 <warn@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
cmp    $0x0,%rax
je     1310 <popen_noshell_child_process+0x1a0>
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1040 <warn@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>

x86 assembly (clang, -O2):
<popen_noshell_child_process>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r15
mov    %r8,%rbp
mov    %edx,%ebx
mov    %edi,(%rsp)
mov    %esi,0x4(%rsp)
test   %ecx,%ecx
je     11a9 <popen_noshell_child_process+0x39>
mov    0x2e38(%rip),%r14
mov    (%r14),%edi
call   1070 <popen_noshell_reopen_fd_to_dev_null@plt>
test   %rax,%rax
jne    127d <popen_noshell_child_process+0x10d>
mov    %r15,0x8(%rsp)
mov    %rbp,0x10(%rsp)
xor    %r12d,%r12d
test   %ebx,%ebx
sete   %r13b
mov    0x2dfd(%rip),%r15
mov    (%r15),%ebp
mov    0x2e23(%rip),%r14
mov    (%r14),%eax
mov    %eax,%ebx
cmove  %ebp,%ebx
cmove  %eax,%ebp
mov    %ebp,%edi
call   1070 <popen_noshell_reopen_fd_to_dev_null@plt>
test   %rax,%rax
jne    1272 <popen_noshell_child_process+0x102>
mov    %r13b,%r12b
mov    %rsp,%rdi
mov    %r12d,%esi
mov    %ebx,%edx
call   1060 <popen_noshell_close_and_dup@plt>
test   %rax,%rax
jne    1298 <popen_noshell_child_process+0x128>
mov    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
call   1090 <execvp@plt>
lea    0xe32(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <warn@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
test   %rax,%rax
jne    12b5 <popen_noshell_child_process+0x145>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
test   %rax,%rax
jne    12be <popen_noshell_child_process+0x14e>
mov    (%r15),%edi
call   1050 <close@plt>
mov    (%r14),%edi
call   1050 <close@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>
lea    0xd87(%rip),%rdi
mov    %ebp,%esi
jmp    1287 <popen_noshell_child_process+0x117>
mov    (%r14),%esi
lea    0xd79(%rip),%rdi
xor    %eax,%eax
call   1040 <warn@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>
lea    0xd89(%rip),%rdi
mov    %r12d,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <warn@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>
lea    0xdac(%rip),%rdi
jmp    12c5 <popen_noshell_child_process+0x155>
lea    0xdb2(%rip),%rdi
xor    %eax,%eax
call   1040 <warn@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@STDERR_FILENO = global i32 0
@.str = [40 x i8] c"popen_noshell_reopen_fd_to_dev_null(%d)\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [36 x i8] c"popen_noshell_close_and_dup(%d ,%d)\00"
@.str.2 = [28 x i8] c"exec(\22%s\22) inside the child\00"
@stdout = global i32 0
@.str.3 = [15 x i8] c"fflush(stdout)\00"
@stderr = global i32 0
@.str.4 = [15 x i8] c"fflush(stderr)\00"
define void @popen_noshell_child_process(i32 %0, i32 %1, i32 %2, i32 %3, i8* %4, i8** %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8**
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca [2 x i32]
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store i8** %5, i8*** %12
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 0
%18 = load i32, i32* %7
store i32 %18, i32* %17
%19 = getelementptr inbounds i32, i32* %17, i64 1
%20 = load i32, i32* %8
store i32 %20, i32* %19
%21 = load i32, i32* %10
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %6
%24 = load i32, i32* @STDERR_FILENO
%25 = call i64 @popen_noshell_reopen_fd_to_dev_null(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @STDERR_FILENO
%29 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = call i32 @_exit(i32 255)
unreachable
31: ; preds = %23
br label %32
32: ; preds = %31, %6
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32, i32* @STDIN_FILENO
store i32 %36, i32* %13
store i32 0, i32* %14
%37 = load i32, i32* @STDOUT_FILENO
store i32 %37, i32* %15
br label %41
38: ; preds = %32
%39 = load i32, i32* @STDOUT_FILENO
store i32 %39, i32* %13
store i32 1, i32* %14
%40 = load i32, i32* @STDIN_FILENO
store i32 %40, i32* %15
br label %41
41: ; preds = %38, %35
%42 = load i32, i32* %13
%43 = call i64 @popen_noshell_reopen_fd_to_dev_null(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32, i32* %13
%47 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %46)
%48 = call i32 @_exit(i32 255)
unreachable
49: ; preds = %41
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %16, i64 0, i64 0
%51 = load i32, i32* %14
%52 = load i32, i32* %15
%53 = call i64 @popen_noshell_close_and_dup(i32* %50, i32 %51, i32 %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %49
%56 = load i32, i32* %14
%57 = load i32, i32* %15
%58 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %56, i32 %57)
%59 = call i32 @_exit(i32 255)
unreachable
60: ; preds = %49
%61 = load i8*, i8** %11
%62 = load i8**, i8*** %12
%63 = call i32 @execvp(i8* %61, i8** %62)
%64 = load i8*, i8** %11
%65 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %64)
%66 = load i32, i32* @stdout
%67 = call i64 @fflush(i32 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %60
%70 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%71 = call i32 @_exit(i32 255)
unreachable
72: ; preds = %60
%73 = load i32, i32* @stderr
%74 = call i64 @fflush(i32 %73)
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %72
%77 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%78 = call i32 @_exit(i32 255)
unreachable
79: ; preds = %72
%80 = load i32, i32* @STDIN_FILENO
%81 = call i32 @close(i32 %80)
%82 = load i32, i32* @STDOUT_FILENO
%83 = call i32 @close(i32 %82)
%84 = load i32, i32* @STDERR_FILENO
%85 = call i32 @close(i32 %84)
%86 = call i32 @_exit(i32 255)
unreachable
}

LLVM IR (-O2):
@STDERR_FILENO = global i32 0
@.str = [40 x i8] c"popen_noshell_reopen_fd_to_dev_null(%d)\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [36 x i8] c"popen_noshell_close_and_dup(%d ,%d)\00"
@.str.2 = [28 x i8] c"exec(\22%s\22) inside the child\00"
@stdout = global i32 0
@.str.3 = [15 x i8] c"fflush(stdout)\00"
@stderr = global i32 0
@.str.4 = [15 x i8] c"fflush(stderr)\00"
define void @popen_noshell_child_process(i32 %0, i32 %1, i32 %2, i32 %3, i8* %4, i8** %5) {
%7 = alloca [2 x i32]
%8 = bitcast [2 x i32]* %7 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
store i32 %0, i32* %9
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
store i32 %1, i32* %10
%11 = icmp eq i32 %3, 0
br i1 %11, label %20, label %12
12: ; preds = %6
%13 = load i32, i32* @STDERR_FILENO
%14 = tail call i64 @popen_noshell_reopen_fd_to_dev_null(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = load i32, i32* @STDERR_FILENO
%18 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = tail call i32 @_exit(i32 255)
unreachable
20: ; preds = %12, %6
%21 = icmp eq i32 %2, 0
%22 = zext i1 %21 to i32
%23 = load i32, i32* @STDIN_FILENO
%24 = load i32, i32* @STDOUT_FILENO
%25 = select i1 %21, i32 %23, i32 %24
%26 = select i1 %21, i32 %24, i32 %23
%27 = tail call i64 @popen_noshell_reopen_fd_to_dev_null(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %20
%30 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %26)
%31 = tail call i32 @_exit(i32 255)
unreachable
32: ; preds = %20
%33 = call i64 @popen_noshell_close_and_dup(i32* %9, i32 %22, i32 %25)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %25)
%37 = call i32 @_exit(i32 255)
unreachable
38: ; preds = %32
%39 = call i32 @execvp(i8* %4, i8** %5)
%40 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%41 = load i32, i32* @stdout
%42 = call i64 @fflush(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %38
%45 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @_exit(i32 255)
unreachable
47: ; preds = %38
%48 = load i32, i32* @stderr
%49 = call i64 @fflush(i32 %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %47
%52 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%53 = call i32 @_exit(i32 255)
unreachable
54: ; preds = %47
%55 = load i32, i32* @STDIN_FILENO
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* @STDOUT_FILENO
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* @STDERR_FILENO
%60 = call i32 @close(i32 %59)
%61 = call i32 @_exit(i32 255)
unreachable
}