#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 EINTR;
int close(int);
int dup2(int, int);
int errno;
int open(char *, int);
int fd_null(int fd, int oflag) {
  int rc;
  int fdn;
start:
  fdn = open("/dev/null", oflag);
  if (fdn == -1) {
    if (errno == EINTR)
      goto start;
    else
      return -1;
  }
  if (dup2(fdn, fd) == -1) {
    rc = errno;
    goto out;
  }
  rc = 0;
out:
  while (close(fdn) == -1 && errno == EINTR)
    ;
  return rc;
}


x86 assembly (clang, -O0):
<fd_null>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xeb8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1183 <fd_null+0x53>
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%eax
jne    1177 <fd_null+0x47>
jmp    113e <fd_null+0xe>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <fd_null+0xcf>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11a8 <fd_null+0x78>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11af <fd_null+0x7f>
movl   $0x0,-0x10(%rbp)
jmp    11b4 <fd_null+0x84>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x15(%rbp)
jne    11e4 <fd_null+0xb4>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11f4 <fd_null+0xc4>
jmp    11f9 <fd_null+0xc9>
jmp    11b4 <fd_null+0x84>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_null>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%r14d
lea    0xeb9(%rip),%r15
mov    0x2e92(%rip),%r12
mov    0x2e83(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    1180 <fd_null+0x50>
mov    (%r12),%eax
cmp    0x0(%r13),%eax
je     1160 <fd_null+0x30>
mov    $0xffffffff,%eax
jmp    11bf <fd_null+0x8f>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14d,%esi
call   1030 <dup2@plt>
mov    %eax,%ebp
mov    (%r12),%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11b6 <fd_null+0x86>
mov    0x0(%r13),%eax
cmp    %eax,(%r12)
je     11a0 <fd_null+0x70>
xor    %eax,%eax
cmp    $0xffffffff,%ebp
cmove  %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@errno = global i32 0
@EINTR = global i32 0
define i32 @fd_null(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %8
8: ; preds = %17, %2
%9 = load i32, i32* %5
%10 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %8
%14 = load i32, i32* @errno
%15 = load i32, i32* @EINTR
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %13
br label %8
18: ; preds = %13
store i32 -1, i32* %3
br label %41
19: ; preds = %8
%20 = load i32, i32* %7
%21 = load i32, i32* %4
%22 = call i32 @dup2(i32 %20, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = load i32, i32* @errno
store i32 %25, i32* %6
br label %27
26: ; preds = %19
store i32 0, i32* %6
br label %27
27: ; preds = %26, %24
br label %28
28: ; preds = %38, %27
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i32, i32* @errno
%34 = load i32, i32* @EINTR
%35 = icmp eq i32 %33, %34
br label %36
36: ; preds = %32, %28
%37 = phi i1 [ false, %28 ], [ %35, %32 ]
br i1 %37, label %38, label %39
38: ; preds = %36
br label %28
39: ; preds = %36
%40 = load i32, i32* %6
store i32 %40, i32* %3
br label %41
41: ; preds = %39, %18
%42 = load i32, i32* %3
ret i32 %42
}

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