#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*/ O_RDWR;
scalar_t__ close(int);
int dup2(int, int);
int open(char *, int /*<<< orphan*/);
int popen_noshell_reopen_fd_to_dev_null(int fd) {
  int dev_null_fd;
  dev_null_fd = open("/dev/null", O_RDWR);
  if (dev_null_fd < 0)
    return -1;
  if (close(fd) != 0) {
    return -1;
  }
  if (dup2(dev_null_fd, fd) == -1) {
    return -1;
  }
  if (close(dev_null_fd) != 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<popen_noshell_reopen_fd_to_dev_null>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
lea    0xeb5(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1169 <popen_noshell_reopen_fd_to_dev_null+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <popen_noshell_reopen_fd_to_dev_null+0x9c>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1187 <popen_noshell_reopen_fd_to_dev_null+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <popen_noshell_reopen_fd_to_dev_null+0x9c>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11a7 <popen_noshell_reopen_fd_to_dev_null+0x77>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <popen_noshell_reopen_fd_to_dev_null+0x9c>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     11c5 <popen_noshell_reopen_fd_to_dev_null+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <popen_noshell_reopen_fd_to_dev_null+0x9c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<popen_noshell_reopen_fd_to_dev_null>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
lea    0xeb9(%rip),%rdi
call   1050 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     117f <popen_noshell_reopen_fd_to_dev_null+0x4f>
mov    %eax,%ebp
mov    %r14d,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    117f <popen_noshell_reopen_fd_to_dev_null+0x4f>
mov    %ebp,%edi
mov    %r14d,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     117f <popen_noshell_reopen_fd_to_dev_null+0x4f>
mov    %ebp,%edi
call   1040 <close@plt>
neg    %rax
sbb    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @popen_noshell_reopen_fd_to_dev_null(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %27
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i64 @close(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 -1, i32* %2
br label %27
15: ; preds = %10
%16 = load i32, i32* %4
%17 = load i32, i32* %3
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 -1, i32* %2
br label %27
21: ; preds = %15
%22 = load i32, i32* %4
%23 = call i64 @close(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %2
br label %27
26: ; preds = %21
store i32 0, i32* %2
br label %27
27: ; preds = %26, %25, %20, %14, %9
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @popen_noshell_reopen_fd_to_dev_null(i32 %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %15, label %5
5: ; preds = %1
%6 = tail call i64 @close(i32 %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %5
%9 = tail call i32 @dup2(i32 %3, i32 %0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = tail call i64 @close(i32 %3)
%13 = icmp ne i64 %12, 0
%14 = sext i1 %13 to i32
br label %15
15: ; preds = %11, %8, %5, %1
%16 = phi i32 [ -1, %1 ], [ -1, %5 ], [ -1, %8 ], [ %14, %11 ]
ret i32 %16
}