#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 */
struct cleanup {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert_cloexec(int);
struct cleanup *cleanup_allocate();
int /*<<< orphan*/ cleanup_commit_close_fd(struct cleanup *, int);
int /*<<< orphan*/ die_errno(char *);
int fcntl(int, int, int);
int xdup(int fd) {
  struct cleanup *cl = cleanup_allocate();
  int newfd = fcntl(fd, 1030, fd);
  if (newfd == -1)
    die_errno("F_DUPFD_CLOEXEC");
  assert_cloexec(newfd);
  cleanup_commit_close_fd(cl, newfd);
  return newfd;
}


x86 assembly (clang, -O0):
<xdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1040 <cleanup_allocate@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%edx
mov    $0x406,%esi
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    118f <xdup+0x3f>
lea    0xe76(%rip),%rdi
call   1060 <die_errno@plt>
mov    -0x14(%rbp),%edi
call   1070 <assert_cloexec@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <cleanup_commit_close_fd@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xdup>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <cleanup_allocate@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    $0x406,%esi
mov    %ebp,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1180 <xdup+0x30>
lea    0xe85(%rip),%rdi
call   1060 <die_errno@plt>
mov    %ebp,%edi
call   1070 <assert_cloexec@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <cleanup_commit_close_fd@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.cleanup = type { i32 }
@.str = [16 x i8] c"F_DUPFD_CLOEXEC\00"
define i32 @xdup(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.cleanup*
%4 = alloca i32
store i32 %0, i32* %2
%5 = call %struct.cleanup* (...) @cleanup_allocate()
store %struct.cleanup* %5, %struct.cleanup** %3
%6 = load i32, i32* %2
%7 = load i32, i32* %2
%8 = call i32 @fcntl(i32 %6, i32 1030, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @die_errno(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
%14 = load i32, i32* %4
%15 = call i32 @assert_cloexec(i32 %14)
%16 = load %struct.cleanup*, %struct.cleanup** %3
%17 = load i32, i32* %4
%18 = call i32 @cleanup_commit_close_fd(%struct.cleanup* %16, i32 %17)
%19 = load i32, i32* %4
ret i32 %19
}

LLVM IR (-O2):
%struct.cleanup = type { i32 }
@.str = [16 x i8] c"F_DUPFD_CLOEXEC\00"
define i32 @xdup(i32 %0) {
%2 = tail call %struct.cleanup* (...) @cleanup_allocate()
%3 = tail call i32 @fcntl(i32 %0, i32 1030, i32 %0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @die_errno(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %1
%8 = tail call i32 @assert_cloexec(i32 %3)
%9 = tail call i32 @cleanup_commit_close_fd(%struct.cleanup* %2, i32 %3)
ret i32 %3
}