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


x86 assembly (clang, -O0):
<openDevNull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     1184 <openDevNull+0x54>
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     1184 <openDevNull+0x54>
mov    -0x8(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0xffffffff,%edx
cmovne %ecx,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openDevNull>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
call   1040 <close@plt>
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
lea    0xeb6(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     116a <openDevNull+0x3a>
cmp    %ebx,%ebp
je     116a <openDevNull+0x3a>
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <dup2@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
cmp    $0xffffffff,%ebp
sete   %al
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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