#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;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ close(int);
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
void close_fds(fd_set *except_fds) {
  struct rlimit r;
  int maxfd;
  if (getrlimit(RLIMIT_NOFILE, &r) == 0)
    maxfd = r.rlim_cur;
  else
    maxfd = 1024;
  for (int fd = 0; fd < maxfd; fd++)
    if (!FD_ISSET(fd, except_fds))
      (void)close(fd);
}


x86 assembly (clang, -O0):
<close_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1050 <getrlimit@plt>
cmp    $0x0,%rax
jne    1163 <close_fds+0x33>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    116a <close_fds+0x3a>
movl   $0x400,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ad <close_fds+0x7d>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    119a <close_fds+0x6a>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
jmp    119f <close_fds+0x6f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1171 <close_fds+0x41>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_fds>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1050 <getrlimit@plt>
mov    $0x400,%ebx
test   %rax,%rax
jne    115c <close_fds+0x2c>
mov    0x8(%rsp),%ebx
test   %ebx,%ebx
jle    118e <close_fds+0x5e>
xor    %ebp,%ebp
jmp    1177 <close_fds+0x47>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%ebp
cmp    %ebp,%ebx
je     118e <close_fds+0x5e>
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
jne    1170 <close_fds+0x40>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1170 <close_fds+0x40>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@RLIMIT_NOFILE = global i32 0
define void @close_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
%7 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = select i1 %6, i32 %8, i32 1024
%10 = icmp sgt i32 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %18, %1
ret void
12: ; preds = %1, %18
%13 = phi i32 [ %19, %18 ], [ 0, %1 ]
%14 = call i32 @FD_ISSET(i32 %13, i32* %0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @close(i32 %13)
br label %18
18: ; preds = %12, %16
%19 = add nuw nsw i32 %13, 1
%20 = icmp eq i32 %19, %9
br i1 %20, label %11, label %12
}