#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ die(char *);
int dup(int);
int fcntl(int, int /*<<< orphan*/, ...);
int dup_cloexec(int oldfd) {
  int fd = dup(oldfd);
  if (fd < 0)
    die("dup");
  int flags = fcntl(fd, F_GETFD);
  if (flags < 0)
    die("fcntl");
  if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
    die("fcntl");
  return fd;
}


x86 assembly (clang, -O0):
<dup_cloexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    115c <dup_cloexec+0x2c>
lea    0xea9(%rip),%rdi
call   1050 <die@plt>
mov    -0x8(%rbp),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1188 <dup_cloexec+0x58>
lea    0xe81(%rip),%rdi
call   1050 <die@plt>
mov    -0x8(%rbp),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2e3a(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11bc <dup_cloexec+0x8c>
lea    0xe4d(%rip),%rdi
call   1050 <die@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dup_cloexec>:
push   %rbp
push   %rbx
push   %rax
call   1040 <dup@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    114a <dup_cloexec+0x1a>
lea    0xebb(%rip),%rdi
call   1050 <die@plt>
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    116e <dup_cloexec+0x3e>
lea    0xe9b(%rip),%rdi
call   1050 <die@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
or     %ebp,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
jns    119b <dup_cloexec+0x6b>
lea    0xe6e(%rip),%rdi
call   1050 <die@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"dup\00"
@F_GETFD = global i32 0
@.str.1 = [6 x i8] c"fcntl\00"
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @dup_cloexec(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @dup(i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @die(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = load i32, i32* %3
%13 = load i32, i32* @F_GETFD
%14 = call i32 (i32, i32, ...) @fcntl(i32 %12, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = load i32, i32* %3
%21 = load i32, i32* @F_SETFD
%22 = load i32, i32* %4
%23 = load i32, i32* @FD_CLOEXEC
%24 = or i32 %22, %23
%25 = call i32 (i32, i32, ...) @fcntl(i32 %20, i32 %21, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %19
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [4 x i8] c"dup\00"
@F_GETFD = global i32 0
@.str.1 = [6 x i8] c"fcntl\00"
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @dup_cloexec(i32 %0) {
%2 = tail call i32 @dup(i32 %0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @die(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = load i32, i32* @F_GETFD
%8 = tail call i32 (i32, i32, ...) @fcntl(i32 %2, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = load i32, i32* @F_SETFD
%14 = load i32, i32* @FD_CLOEXEC
%15 = or i32 %14, %8
%16 = tail call i32 (i32, i32, ...) @fcntl(i32 %2, i32 %13, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
ret i32 %2
}