#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_cur;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ close(int);
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ xerror(char *);
void close_higher_fds(int last_kept_fd) {
  struct rlimit buf;
  if (getrlimit(RLIMIT_NOFILE, &buf) != 0)
    xerror("getrlimit: RLIMIT_NOFILE");
  for (int fd = last_kept_fd + 1; fd < (int)buf.rlim_cur; fd++)
    close(fd);
}


x86 assembly (clang, -O0):
<close_higher_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1050 <getrlimit@plt>
cmp    $0x0,%rax
je     1163 <close_higher_fds+0x33>
lea    0xea2(%rip),%rdi
call   1030 <xerror@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    %ecx,%eax
jge    1191 <close_higher_fds+0x61>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116c <close_higher_fds+0x3c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_higher_fds>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2eaa(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1050 <getrlimit@plt>
test   %rax,%rax
je     1167 <close_higher_fds+0x37>
lea    0xeaa(%rip),%rdi
call   1030 <xerror@plt>
jmp    1167 <close_higher_fds+0x37>
nopl   (%rax)
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebx
cmp    0x8(%rsp),%ebx
jl     1160 <close_higher_fds+0x30>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.rlimit = type { i64 }
@RLIMIT_NOFILE = global i32 0
@.str = [25 x i8] c"getrlimit: RLIMIT_NOFILE\00"
define void @close_higher_fds(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.rlimit
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* @RLIMIT_NOFILE
%6 = call i64 @getrlimit(i32 %5, %struct.rlimit* %3)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @xerror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = load i32, i32* %2
%12 = add nsw i32 %11, 1
store i32 %12, i32* %4
br label %13
13: ; preds = %22, %10
%14 = load i32, i32* %4
%15 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
%16 = load i64, i64* %15
%17 = trunc i64 %16 to i32
%18 = icmp slt i32 %14, %17
br i1 %18, label %19, label %25
19: ; preds = %13
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
br label %22
22: ; preds = %19
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %13
25: ; preds = %13
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i64 }
@RLIMIT_NOFILE = global i32 0
@.str = [25 x i8] c"getrlimit: RLIMIT_NOFILE\00"
define void @close_higher_fds(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 %9, label %7
7: ; preds = %1
%8 = call i32 @xerror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%11 = add nsw i32 %0, 1
%12 = load i64, i64* %10
%13 = trunc i64 %12 to i32
%14 = icmp slt i32 %11, %13
br i1 %14, label %16, label %15
15: ; preds = %16, %9
ret void
16: ; preds = %9, %16
%17 = phi i32 [ %19, %16 ], [ %11, %9 ]
%18 = call i32 @close(i32 %17)
%19 = add nsw i32 %17, 1
%20 = load i64, i64* %10
%21 = trunc i64 %20 to i32
%22 = icmp slt i32 %19, %21
br i1 %22, label %16, label %15
}