#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 {
  int rlim_cur;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
int main(int argc, char **argv) {
  struct rlimit lim;
  int fd;
  getrlimit(RLIMIT_NOFILE, &lim);
  for (fd = 3; fd < lim.rlim_cur; fd++)
    close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <getrlimit@plt>
movl   $0x3,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1181 <main+0x51>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115f <main+0x2f>
xor    %edi,%edi
call   1040 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2eac(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1050 <getrlimit@plt>
cmpl   $0x4,0x8(%rsp)
jl     1170 <main+0x40>
mov    $0x3,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
call   1030 <close@plt>
add    $0x1,%ebx
cmp    0x8(%rsp),%ebx
jl     1160 <main+0x30>
xor    %edi,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
@RLIMIT_NOFILE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.rlimit
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @RLIMIT_NOFILE
%9 = call i32 @getrlimit(i32 %8, %struct.rlimit* %6)
store i32 3, i32* %7
br label %10
10: ; preds = %18, %2
%11 = load i32, i32* %7
%12 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
%13 = load i32, i32* %12
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %21
15: ; preds = %10
%16 = load i32, i32* %7
%17 = call i32 @close(i32 %16)
br label %18
18: ; preds = %15
%19 = load i32, i32* %7
%20 = add nsw i32 %19, 1
store i32 %20, i32* %7
br label %10
21: ; preds = %10
%22 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@RLIMIT_NOFILE = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.rlimit
%4 = bitcast %struct.rlimit* %3 to i8*
%5 = load i32, i32* @RLIMIT_NOFILE
%6 = call i32 @getrlimit(i32 %5, %struct.rlimit* %3)
%7 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
%8 = load i32, i32* %7
%9 = icmp sgt i32 %8, 3
br i1 %9, label %10, label %16
10: ; preds = %2, %10
%11 = phi i32 [ %13, %10 ], [ 3, %2 ]
%12 = call i32 @close(i32 %11)
%13 = add nuw nsw i32 %11, 1
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %10, label %16
16: ; preds = %10, %2
%17 = call i32 @exit(i32 0)
unreachable
}