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

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int RLIM_INFINITY;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
void zed_file_close_from(int lowfd) {
  const int maxfd_def = 256;
  int errno_bak;
  struct rlimit rl;
  int maxfd;
  int fd;
  errno_bak = errno;
  if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
    maxfd = maxfd_def;
  } else if (rl.rlim_max == RLIM_INFINITY) {
    maxfd = maxfd_def;
  } else {
    maxfd = rl.rlim_max;
  }
  for (fd = lowfd; fd < maxfd; fd++)
    (void)close(fd);
  errno = errno_bak;
}


x86 assembly (clang, -O0):
<zed_file_close_from>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x100,-0x8(%rbp)
mov    0x2e9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <getrlimit@plt>
cmp    $0x0,%rax
jge    1166 <zed_file_close_from+0x46>
movl   $0x100,-0x14(%rbp)
jmp    118f <zed_file_close_from+0x6f>
mov    -0x10(%rbp),%eax
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%eax
jne    1184 <zed_file_close_from+0x64>
movl   $0x100,-0x14(%rbp)
jmp    118a <zed_file_close_from+0x6a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    118f <zed_file_close_from+0x6f>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b7 <zed_file_close_from+0x97>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1195 <zed_file_close_from+0x75>
mov    -0xc(%rbp),%ecx
mov    0x2e17(%rip),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zed_file_close_from>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ea8(%rip),%r14
mov    (%r14),%r15d
mov    0x2eae(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1040 <getrlimit@plt>
mov    (%rsp),%ebp
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%ebp
mov    $0x100,%ecx
cmove  %ecx,%ebp
test   %rax,%rax
cmovs  %ecx,%ebp
cmp    %ebx,%ebp
jle    117e <zed_file_close_from+0x5e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
call   1030 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1170 <zed_file_close_from+0x50>
mov    %r15d,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i32 }
@errno = global i32 0
@RLIMIT_NOFILE = global i32 0
@RLIM_INFINITY = global i32 0
define void @zed_file_close_from(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.rlimit
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
store i32 256, i32* %3
%8 = load i32, i32* @errno
store i32 %8, i32* %4
%9 = load i32, i32* @RLIMIT_NOFILE
%10 = call i64 @getrlimit(i32 %9, %struct.rlimit* %5)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 256, i32* %6
br label %23
13: ; preds = %1
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @RLIM_INFINITY
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 256, i32* %6
br label %22
19: ; preds = %13
%20 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 0
%21 = load i32, i32* %20
store i32 %21, i32* %6
br label %22
22: ; preds = %19, %18
br label %23
23: ; preds = %22, %12
%24 = load i32, i32* %2
store i32 %24, i32* %7
br label %25
25: ; preds = %32, %23
%26 = load i32, i32* %7
%27 = load i32, i32* %6
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %29
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %25
35: ; preds = %25
%36 = load i32, i32* %4
store i32 %36, i32* @errno
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@errno = global i32 0
@RLIMIT_NOFILE = global i32 0
@RLIM_INFINITY = global i32 0
define void @zed_file_close_from(i32 %0) {
%2 = alloca %struct.rlimit
%3 = bitcast %struct.rlimit* %2 to i8*
%4 = load i32, i32* @errno
%5 = load i32, i32* @RLIMIT_NOFILE
%6 = call i64 @getrlimit(i32 %5, %struct.rlimit* %2)
%7 = icmp slt i64 %6, 0
%8 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @RLIM_INFINITY
%11 = icmp eq i32 %9, %10
%12 = select i1 %7, i1 true, i1 %11
%13 = select i1 %12, i32 256, i32 %9
%14 = icmp sgt i32 %13, %0
br i1 %14, label %15, label %20
15: ; preds = %1, %15
%16 = phi i32 [ %18, %15 ], [ %0, %1 ]
%17 = call i32 @close(i32 %16)
%18 = add i32 %16, 1
%19 = icmp eq i32 %18, %13
br i1 %19, label %20, label %15
20: ; preds = %15, %1
store i32 %4, i32* @errno
ret void
}