#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 rlimit {
  scalar_t__ rlim_max;
  scalar_t__ rlim_cur;
};

/* Variables and functions */
int EBADF;
scalar_t__ FD_SETSIZE;
int /*<<< orphan*/ RLIMIT_NOFILE;
scalar_t__ close(int /*<<< orphan*/);
int errno;
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ setrlimit(int /*<<< orphan*/, struct rlimit *);
extern int closefrom(int fd) {
  struct rlimit lim;
  unsigned found = 0;
  int saved_errno;
  if (getrlimit(RLIMIT_NOFILE, &lim))
    return -1;
  saved_errno = errno;
  if (lim.rlim_max > FD_SETSIZE) {
    if (lim.rlim_cur > FD_SETSIZE)
      lim.rlim_cur = FD_SETSIZE;
    lim.rlim_max = FD_SETSIZE;
    setrlimit(RLIMIT_NOFILE, &lim);
  }
  while ((unsigned)fd < lim.rlim_max)
    if (close(fd++) == 0)
      found++;
  if (found == 0) {
    errno = EBADF;
    return -1;
  }
  errno = saved_errno;
  return 0;
}


x86 assembly (clang, -O0):
<closefrom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e9f(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <getrlimit@plt>
cmp    $0x0,%rax
je     116a <closefrom+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <closefrom+0x111>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%rax
jle    11cc <closefrom+0x9c>
mov    -0x10(%rbp),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
jle    11ac <closefrom+0x7c>
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <setrlimit@plt>
jmp    11d1 <closefrom+0xa1>
mov    -0x8(%rbp),%eax
cmp    -0x18(%rbp),%rax
jge    1206 <closefrom+0xd6>
mov    -0x8(%rbp),%edi
mov    %edi,%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
call   1030 <close@plt>
cmp    $0x0,%rax
jne    1201 <closefrom+0xd1>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d1 <closefrom+0xa1>
cmpl   $0x0,-0x1c(%rbp)
jne    122e <closefrom+0xfe>
mov    0x2db1(%rip),%rax
mov    (%rax),%ecx
mov    0x2db0(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <closefrom+0x111>
mov    -0x20(%rbp),%ecx
mov    0x2d98(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<closefrom>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r12d
mov    0x2ea2(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %rsp,%rsi
call   1050 <getrlimit@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    11e5 <closefrom+0xb5>
mov    0x2e67(%rip),%r14
mov    (%r14),%r15d
mov    (%rsp),%rax
mov    0x2e61(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jle    119e <closefrom+0x6e>
cmp    %rcx,0x8(%rsp)
jle    118b <closefrom+0x5b>
mov    %rcx,0x8(%rsp)
mov    %rcx,(%rsp)
mov    0x0(%rbp),%edi
mov    %rsp,%rsi
call   1040 <setrlimit@plt>
mov    (%rsp),%rax
mov    %r12d,%ecx
cmp    %rcx,%rax
jle    11d3 <closefrom+0xa3>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
lea    0x1(%r12),%ebx
mov    %r12d,%edi
call   1030 <close@plt>
cmp    $0x1,%rax
adc    $0x0,%ebp
mov    %ebx,%r12d
cmp    %rbx,(%rsp)
jg     11b0 <closefrom+0x80>
xor    %eax,%eax
test   %ebp,%ebp
jne    11e2 <closefrom+0xb2>
mov    0x2dee(%rip),%rax
mov    (%rax),%r15d
mov    $0xffffffff,%eax
mov    %r15d,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@RLIMIT_NOFILE = global i32 0
@errno = global i32 0
@FD_SETSIZE = global i64 0
@EBADF = global i32 0
define i32 @closefrom(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.rlimit
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %5
%7 = load i32, i32* @RLIMIT_NOFILE
%8 = call i64 @getrlimit(i32 %7, %struct.rlimit* %4)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %53
11: ; preds = %1
%12 = load i32, i32* @errno
store i32 %12, i32* %6
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* @FD_SETSIZE
%16 = icmp sgt i64 %14, %15
br i1 %16, label %17, label %30
17: ; preds = %11
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%19 = load i64, i64* %18
%20 = load i64, i64* @FD_SETSIZE
%21 = icmp sgt i64 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i64, i64* @FD_SETSIZE
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
store i64 %23, i64* %24
br label %25
25: ; preds = %22, %17
%26 = load i64, i64* @FD_SETSIZE
%27 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
store i64 %26, i64* %27
%28 = load i32, i32* @RLIMIT_NOFILE
%29 = call i32 @setrlimit(i32 %28, %struct.rlimit* %4)
br label %30
30: ; preds = %25, %11
br label %31
31: ; preds = %45, %30
%32 = load i32, i32* %3
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%35 = load i64, i64* %34
%36 = icmp slt i64 %33, %35
br i1 %36, label %37, label %46
37: ; preds = %31
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 1
store i32 %39, i32* %3
%40 = call i64 @close(i32 %38)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i32, i32* %5
%44 = add i32 %43, 1
store i32 %44, i32* %5
br label %45
45: ; preds = %42, %37
br label %31
46: ; preds = %31
%47 = load i32, i32* %5
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = load i32, i32* @EBADF
store i32 %50, i32* @errno
store i32 -1, i32* %2
br label %53
51: ; preds = %46
%52 = load i32, i32* %6
store i32 %52, i32* @errno
store i32 0, i32* %2
br label %53
53: ; preds = %51, %49, %10
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@RLIMIT_NOFILE = global i32 0
@errno = global i32 0
@FD_SETSIZE = global i64 0
@EBADF = global i32 0
define i32 @closefrom(i32 %0) {
%2 = alloca %struct.rlimit
%3 = bitcast %struct.rlimit* %2 to i8*
%4 = load i32, i32* @RLIMIT_NOFILE
%5 = call i64 @getrlimit(i32 %4, %struct.rlimit* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %44
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @FD_SETSIZE
%12 = icmp sgt i64 %10, %11
br i1 %12, label %13, label %22
13: ; preds = %7
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 1
%15 = load i64, i64* %14
%16 = icmp sgt i64 %15, %11
br i1 %16, label %17, label %18
17: ; preds = %13
store i64 %11, i64* %14
br label %18
18: ; preds = %17, %13
store i64 %11, i64* %9
%19 = load i32, i32* @RLIMIT_NOFILE
%20 = call i32 @setrlimit(i32 %19, %struct.rlimit* %2)
%21 = load i64, i64* %9
br label %22
22: ; preds = %18, %7
%23 = phi i64 [ %21, %18 ], [ %10, %7 ]
%24 = zext i32 %0 to i64
%25 = icmp sgt i64 %23, %24
br i1 %25, label %26, label %39
26: ; preds = %22, %26
%27 = phi i32 [ %33, %26 ], [ 0, %22 ]
%28 = phi i32 [ %29, %26 ], [ %0, %22 ]
%29 = add nsw i32 %28, 1
%30 = call i64 @close(i32 %28)
%31 = icmp eq i64 %30, 0
%32 = zext i1 %31 to i32
%33 = add i32 %27, %32
%34 = zext i32 %29 to i64
%35 = load i64, i64* %9
%36 = icmp sgt i64 %35, %34
br i1 %36, label %26, label %37
37: ; preds = %26
%38 = icmp eq i32 %33, 0
br i1 %38, label %39, label %41
39: ; preds = %22, %37
%40 = load i32, i32* @EBADF
br label %41
41: ; preds = %37, %39
%42 = phi i32 [ %40, %39 ], [ %8, %37 ]
%43 = phi i32 [ -1, %39 ], [ 0, %37 ]
store i32 %42, i32* @errno
br label %44
44: ; preds = %41, %1
%45 = phi i32 [ -1, %1 ], [ %43, %41 ]
ret i32 %45
}