#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 */
scalar_t__ EINVAL;
int /*<<< orphan*/ F_DUPFD;
int /*<<< orphan*/ F_DUPFD_CLOEXEC;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, long);
int set_cloexec_or_close(int);
int wl_os_dupfd_cloexec(int fd, long minfd) {
  int newfd;
  newfd = fcntl(fd, F_DUPFD_CLOEXEC, minfd);
  if (newfd >= 0)
    return newfd;
  if (errno != EINVAL)
    return -1;
  newfd = fcntl(fd, F_DUPFD, minfd);
  return set_cloexec_or_close(newfd);
}


x86 assembly (clang, -O0):
<wl_os_dupfd_cloexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     115c <wl_os_dupfd_cloexec+0x3c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a5 <wl_os_dupfd_cloexec+0x85>
mov    0x2e6d(%rip),%rax
mov    (%rax),%rax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
je     1182 <wl_os_dupfd_cloexec+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a5 <wl_os_dupfd_cloexec+0x85>
mov    -0x8(%rbp),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <set_cloexec_or_close@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wl_os_dupfd_cloexec>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb1(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
jns    1178 <wl_os_dupfd_cloexec+0x58>
mov    0x2e8c(%rip),%rax
mov    (%rax),%rcx
mov    0x2e7a(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    1178 <wl_os_dupfd_cloexec+0x58>
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <fcntl@plt>
mov    %eax,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <set_cloexec_or_close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_DUPFD_CLOEXEC = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@F_DUPFD = global i32 0
define i32 @wl_os_dupfd_cloexec(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
%7 = load i32, i32* %4
%8 = load i32, i32* @F_DUPFD_CLOEXEC
%9 = load i64, i64* %5
%10 = call i32 @fcntl(i32 %7, i32 %8, i64 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32, i32* %6
store i32 %14, i32* %3
br label %27
15: ; preds = %2
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINVAL
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %3
br label %27
20: ; preds = %15
%21 = load i32, i32* %4
%22 = load i32, i32* @F_DUPFD
%23 = load i64, i64* %5
%24 = call i32 @fcntl(i32 %21, i32 %22, i64 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = call i32 @set_cloexec_or_close(i32 %25)
store i32 %26, i32* %3
br label %27
27: ; preds = %20, %19, %13
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@F_DUPFD_CLOEXEC = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@F_DUPFD = global i32 0
define i32 @wl_os_dupfd_cloexec(i32 %0, i64 %1) {
%3 = load i32, i32* @F_DUPFD_CLOEXEC
%4 = tail call i32 @fcntl(i32 %0, i32 %3, i64 %1)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINVAL
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = load i32, i32* @F_DUPFD
%12 = tail call i32 @fcntl(i32 %0, i32 %11, i64 %1)
%13 = tail call i32 @set_cloexec_or_close(i32 %12)
br label %14
14: ; preds = %6, %2, %10
%15 = phi i32 [ %13, %10 ], [ %4, %2 ], [ -1, %6 ]
ret i32 %15
}