#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_DUPFD;
int /*<<< orphan*/ F_DUPFD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ O_RDWR;
scalar_t__ close(int);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int util_safeio_get_fd_min(int fd, int min_fd, int force_new) {
  int flags, new_fd;
  if (fd < 0 || (fd >= min_fd && !force_new))
    return fd;
  flags = fcntl(fd, F_GETFD);
  if (flags < 0)
    return -1;
  if (flags & FD_CLOEXEC)
    new_fd = fcntl(fd, F_DUPFD_CLOEXEC, min_fd);
  else
    new_fd = fcntl(fd, F_DUPFD, min_fd);
  if (new_fd < 0)
    return -1;
  if (close(fd) < 0)
    return -1;
  open("/dev/null", O_RDWR);
  return new_fd;
}


x86 assembly (clang, -O0):
<util_safeio_get_fd_min>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1161 <util_safeio_get_fd_min+0x31>
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     116c <util_safeio_get_fd_min+0x3c>
cmpl   $0x0,-0x10(%rbp)
jne    116c <util_safeio_get_fd_min+0x3c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1233 <util_safeio_get_fd_min+0x103>
mov    -0x8(%rbp),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1198 <util_safeio_get_fd_min+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <util_safeio_get_fd_min+0x103>
mov    -0x14(%rbp),%eax
mov    0x2e26(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11cb <util_safeio_get_fd_min+0x9b>
mov    -0x8(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
jmp    11e4 <util_safeio_get_fd_min+0xb4>
mov    -0x8(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11fa <util_safeio_get_fd_min+0xca>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <util_safeio_get_fd_min+0x103>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1218 <util_safeio_get_fd_min+0xe8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <util_safeio_get_fd_min+0x103>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdi
call   1050 <open@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_safeio_get_fd_min>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
test   %edi,%edi
js     1145 <util_safeio_get_fd_min+0x15>
mov    %esi,%r14d
cmp    %esi,%ebx
jl     114e <util_safeio_get_fd_min+0x1e>
test   %edx,%edx
jne    114e <util_safeio_get_fd_min+0x1e>
mov    %ebx,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1147 <util_safeio_get_fd_min+0x17>
mov    0x2e58(%rip),%rcx
and    (%rcx),%eax
jne    117d <util_safeio_get_fd_min+0x4d>
mov    0x2e65(%rip),%rax
jmp    1184 <util_safeio_get_fd_min+0x54>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1147 <util_safeio_get_fd_min+0x17>
mov    %eax,%r14d
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
js     1147 <util_safeio_get_fd_min+0x17>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1050 <open@plt>
mov    %r14d,%ebp
jmp    1147 <util_safeio_get_fd_min+0x17>

LLVM IR (-O0):
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_DUPFD_CLOEXEC = global i32 0
@F_DUPFD = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @util_safeio_get_fd_min(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %3
%13 = load i32, i32* %5
%14 = load i32, i32* %6
%15 = icmp sge i32 %13, %14
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16, %3
%20 = load i32, i32* %5
store i32 %20, i32* %4
br label %56
21: ; preds = %16, %12
%22 = load i32, i32* %5
%23 = load i32, i32* @F_GETFD
%24 = call i32 (i32, i32, ...) @fcntl(i32 %22, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 -1, i32* %4
br label %56
28: ; preds = %21
%29 = load i32, i32* %8
%30 = load i32, i32* @FD_CLOEXEC
%31 = and i32 %29, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = load i32, i32* %5
%35 = load i32, i32* @F_DUPFD_CLOEXEC
%36 = load i32, i32* %6
%37 = call i32 (i32, i32, ...) @fcntl(i32 %34, i32 %35, i32 %36)
store i32 %37, i32* %9
br label %43
38: ; preds = %28
%39 = load i32, i32* %5
%40 = load i32, i32* @F_DUPFD
%41 = load i32, i32* %6
%42 = call i32 (i32, i32, ...) @fcntl(i32 %39, i32 %40, i32 %41)
store i32 %42, i32* %9
br label %43
43: ; preds = %38, %33
%44 = load i32, i32* %9
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 -1, i32* %4
br label %56
47: ; preds = %43
%48 = load i32, i32* %5
%49 = call i64 @close(i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %47
store i32 -1, i32* %4
br label %56
52: ; preds = %47
%53 = load i32, i32* @O_RDWR
%54 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %53)
%55 = load i32, i32* %9
store i32 %55, i32* %4
br label %56
56: ; preds = %52, %51, %46, %27, %19
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_DUPFD_CLOEXEC = global i32 0
@F_DUPFD = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @util_safeio_get_fd_min(i32 %0, i32 %1, i32 %2) {
%4 = icmp slt i32 %0, 0
br i1 %4, label %28, label %5
5: ; preds = %3
%6 = icmp slt i32 %0, %1
%7 = icmp ne i32 %2, 0
%8 = or i1 %6, %7
br i1 %8, label %9, label %28
9: ; preds = %5
%10 = load i32, i32* @F_GETFD
%11 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %28, label %13
13: ; preds = %9
%14 = load i32, i32* @FD_CLOEXEC
%15 = and i32 %14, %11
%16 = icmp eq i32 %15, 0
%17 = load i32, i32* @F_DUPFD
%18 = load i32, i32* @F_DUPFD_CLOEXEC
%19 = select i1 %16, i32 %17, i32 %18
%20 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %19, i32 %1)
%21 = icmp slt i32 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %13
%23 = tail call i64 @close(i32 %0)
%24 = icmp slt i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* @O_RDWR
%27 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %22, %13, %9, %3, %5, %25
%29 = phi i32 [ %20, %25 ], [ %0, %5 ], [ %0, %3 ], [ -1, %9 ], [ -1, %13 ], [ -1, %22 ]
ret i32 %29
}