#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 /*<<< orphan*/ ENFILE;
int /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ EX_SOFTWARE;
int /*<<< orphan*/ F_GETFL;
int INT_MAX;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup2(int, int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ errc(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int fcntl(int, int /*<<< orphan*/, int *);
int unreservefd(int fd) {
  int nfd;
  int fl;
  if (fd < 0 || !(((fd) == STDIN_FILENO) || ((fd) == STDOUT_FILENO) ||
                  ((fd) == STDERR_FILENO)))
    return (fd);
  for (nfd = 0; (((nfd) == STDIN_FILENO) || ((nfd) == STDOUT_FILENO) ||
                 ((nfd) == STDERR_FILENO)) ||
                fcntl(nfd, F_GETFL, &fl) != -1;
       nfd++)
    if (nfd == INT_MAX)
      errc(EX_SOFTWARE, ENFILE, "Could not find a file descriptor");
  if (dup2(fd, nfd) == -1)
    err(EX_OSERR, "Could not duplicate a file descriptor");
  close(fd);
  return (nfd);
}


x86 assembly (clang, -O0):
<unreservefd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     119b <unreservefd+0x4b>
mov    -0x8(%rbp),%eax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%eax
je     11a6 <unreservefd+0x56>
mov    -0x8(%rbp),%eax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%eax
je     11a6 <unreservefd+0x56>
mov    -0x8(%rbp),%eax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%eax
je     11a6 <unreservefd+0x56>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129a <unreservefd+0x14a>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
mov    $0x1,%al
mov    0x2dff(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x11(%rbp)
je     1210 <unreservefd+0xc0>
mov    -0xc(%rbp),%ecx
mov    $0x1,%al
mov    0x2e20(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x11(%rbp)
je     1210 <unreservefd+0xc0>
mov    -0xc(%rbp),%ecx
mov    $0x1,%al
mov    0x2de1(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x11(%rbp)
je     1210 <unreservefd+0xc0>
mov    -0xc(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1220 <unreservefd+0xd0>
jmp    1263 <unreservefd+0x113>
mov    -0xc(%rbp),%eax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%eax
jne    1250 <unreservefd+0x100>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
lea    0xdb5(%rip),%rdx
call   1040 <errc@plt>
jmp    1255 <unreservefd+0x105>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ad <unreservefd+0x5d>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    128c <unreservefd+0x13c>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    0xd9a(%rip),%rsi
call   1070 <err@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unreservefd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
js     1189 <unreservefd+0x39>
mov    0x2e52(%rip),%r13
mov    0x0(%r13),%edx
mov    0x2e7f(%rip),%rbx
mov    (%rbx),%ecx
mov    0x2e4e(%rip),%r14
mov    (%r14),%eax
cmp    %edi,%edx
je     1190 <unreservefd+0x40>
cmp    %edi,%ecx
je     1190 <unreservefd+0x40>
cmp    %edi,%eax
je     1190 <unreservefd+0x40>
mov    %edi,%ebp
jmp    1232 <unreservefd+0xe2>
mov    %edi,(%rsp)
xor    %ebp,%ebp
mov    0x2e44(%rip),%r15
lea    0x4(%rsp),%r12
jmp    11bc <unreservefd+0x6c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebp
mov    0x0(%r13),%edx
mov    (%rbx),%ecx
mov    (%r14),%eax
cmp    %edx,%ebp
je     11e0 <unreservefd+0x90>
cmp    %ecx,%ebp
je     11e0 <unreservefd+0x90>
cmp    %eax,%ebp
je     11e0 <unreservefd+0x90>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1205 <unreservefd+0xb5>
cmp    (%r15),%ebp
jne    11b0 <unreservefd+0x60>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
lea    0xe02(%rip),%rdx
call   1040 <errc@plt>
jmp    11b0 <unreservefd+0x60>
mov    (%rsp),%ebx
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    122b <unreservefd+0xdb>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
call   1070 <err@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@F_GETFL = global i32 0
@INT_MAX = global i32 0
@EX_SOFTWARE = global i32 0
@ENFILE = global i32 0
@.str = [33 x i8] c"Could not find a file descriptor\00"
@EX_OSERR = global i32 0
@.str.1 = [38 x i8] c"Could not duplicate a file descriptor\00"
define i32 @unreservefd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %20, label %8
8: ; preds = %1
%9 = load i32, i32* %3
%10 = load i32, i32* @STDIN_FILENO
%11 = icmp eq i32 %9, %10
br i1 %11, label %22, label %12
12: ; preds = %8
%13 = load i32, i32* %3
%14 = load i32, i32* @STDOUT_FILENO
%15 = icmp eq i32 %13, %14
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = load i32, i32* %3
%18 = load i32, i32* @STDERR_FILENO
%19 = icmp eq i32 %17, %18
br i1 %19, label %22, label %20
20: ; preds = %16, %1
%21 = load i32, i32* %3
store i32 %21, i32* %2
br label %66
22: ; preds = %16, %12, %8
store i32 0, i32* %4
br label %23
23: ; preds = %51, %22
%24 = load i32, i32* %4
%25 = load i32, i32* @STDIN_FILENO
%26 = icmp eq i32 %24, %25
br i1 %26, label %40, label %27
27: ; preds = %23
%28 = load i32, i32* %4
%29 = load i32, i32* @STDOUT_FILENO
%30 = icmp eq i32 %28, %29
br i1 %30, label %40, label %31
31: ; preds = %27
%32 = load i32, i32* %4
%33 = load i32, i32* @STDERR_FILENO
%34 = icmp eq i32 %32, %33
br i1 %34, label %40, label %35
35: ; preds = %31
%36 = load i32, i32* %4
%37 = load i32, i32* @F_GETFL
%38 = call i32 @fcntl(i32 %36, i32 %37, i32* %5)
%39 = icmp ne i32 %38, -1
br label %40
40: ; preds = %35, %31, %27, %23
%41 = phi i1 [ true, %31 ], [ true, %27 ], [ true, %23 ], [ %39, %35 ]
br i1 %41, label %42, label %54
42: ; preds = %40
%43 = load i32, i32* %4
%44 = load i32, i32* @INT_MAX
%45 = icmp eq i32 %43, %44
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = load i32, i32* @EX_SOFTWARE
%48 = load i32, i32* @ENFILE
%49 = call i32 @errc(i32 %47, i32 %48, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %50
50: ; preds = %46, %42
br label %51
51: ; preds = %50
%52 = load i32, i32* %4
%53 = add nsw i32 %52, 1
store i32 %53, i32* %4
br label %23
54: ; preds = %40
%55 = load i32, i32* %3
%56 = load i32, i32* %4
%57 = call i32 @dup2(i32 %55, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = load i32, i32* @EX_OSERR
%61 = call i32 @err(i32 %60, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
br label %62
62: ; preds = %59, %54
%63 = load i32, i32* %3
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %4
store i32 %65, i32* %2
br label %66
66: ; preds = %62, %20
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@F_GETFL = global i32 0
@INT_MAX = global i32 0
@EX_SOFTWARE = global i32 0
@ENFILE = global i32 0
@.str = [33 x i8] c"Could not find a file descriptor\00"
@EX_OSERR = global i32 0
@.str.1 = [38 x i8] c"Could not duplicate a file descriptor\00"
define i32 @unreservefd(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = icmp slt i32 %0, 0
br i1 %4, label %48, label %5
5: ; preds = %1
%6 = load i32, i32* @STDIN_FILENO
%7 = icmp eq i32 %6, %0
%8 = load i32, i32* @STDOUT_FILENO
%9 = icmp eq i32 %8, %0
%10 = select i1 %7, i1 true, i1 %9
%11 = load i32, i32* @STDERR_FILENO
%12 = icmp eq i32 %11, %0
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %14, label %48
14: ; preds = %5, %35
%15 = phi i32 [ %39, %35 ], [ %11, %5 ]
%16 = phi i32 [ %38, %35 ], [ %8, %5 ]
%17 = phi i32 [ %37, %35 ], [ %6, %5 ]
%18 = phi i32 [ %36, %35 ], [ 0, %5 ]
%19 = icmp eq i32 %18, %17
%20 = icmp eq i32 %18, %16
%21 = select i1 %19, i1 true, i1 %20
%22 = icmp eq i32 %18, %15
%23 = select i1 %21, i1 true, i1 %22
br i1 %23, label %28, label %24
24: ; preds = %14
%25 = load i32, i32* @F_GETFL
%26 = call i32 @fcntl(i32 %18, i32 %25, i32* %2)
%27 = icmp eq i32 %26, -1
br i1 %27, label %40, label %28
28: ; preds = %14, %24
%29 = load i32, i32* @INT_MAX
%30 = icmp eq i32 %18, %29
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i32, i32* @EX_SOFTWARE
%33 = load i32, i32* @ENFILE
%34 = call i32 @errc(i32 %32, i32 %33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %28, %31
%36 = add nuw nsw i32 %18, 1
%37 = load i32, i32* @STDIN_FILENO
%38 = load i32, i32* @STDOUT_FILENO
%39 = load i32, i32* @STDERR_FILENO
br label %14
40: ; preds = %24
%41 = call i32 @dup2(i32 %0, i32 %18)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* @EX_OSERR
%45 = call i32 @err(i32 %44, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
br label %46
46: ; preds = %43, %40
%47 = call i32 @close(i32 %0)
br label %48
48: ; preds = %1, %5, %46
%49 = phi i32 [ %18, %46 ], [ %0, %5 ], [ %0, %1 ]
ret i32 %49
}