#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 */

/* Variables and functions */
int STDERR_FILENO;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ close(int);
long sysconf(int /*<<< orphan*/);
void closeNonStdDescriptors(void) {
  long maxFd = sysconf(_SC_OPEN_MAX);
  if (maxFd < 0)
    maxFd = 4096;
  int fd;
  for (fd = STDERR_FILENO + 1; fd < maxFd; fd++)
    close(fd);
}


x86 assembly (clang, -O0):
<closeNonStdDescriptors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2eb1(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    114d <closeNonStdDescriptors+0x2d>
movq   $0x1000,-0x8(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    -0x8(%rbp),%rax
jge    1180 <closeNonStdDescriptors+0x60>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    115c <closeNonStdDescriptors+0x3c>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<closeNonStdDescriptors>:
push   %r14
push   %rbx
push   %rax
mov    0x2eb5(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
test   %rax,%rax
mov    $0x1000,%r14d
cmovns %rax,%r14
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
movslq %eax,%rbx
cmp    %rbx,%r14
jle    1170 <closeNonStdDescriptors+0x50>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
call   1030 <close@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jg     1160 <closeNonStdDescriptors+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@_SC_OPEN_MAX = global i32 0
@STDERR_FILENO = global i32 0
define void @closeNonStdDescriptors() {
%1 = alloca i64
%2 = alloca i32
%3 = load i32, i32* @_SC_OPEN_MAX
%4 = call i64 @sysconf(i32 %3)
store i64 %4, i64* %1
%5 = load i64, i64* %1
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i64 4096, i64* %1
br label %8
8: ; preds = %7, %0
%9 = load i32, i32* @STDERR_FILENO
%10 = add nsw i32 %9, 1
store i32 %10, i32* %2
br label %11
11: ; preds = %19, %8
%12 = load i32, i32* %2
%13 = sext i32 %12 to i64
%14 = load i64, i64* %1
%15 = icmp slt i64 %13, %14
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = load i32, i32* %2
%18 = call i32 @close(i32 %17)
br label %19
19: ; preds = %16
%20 = load i32, i32* %2
%21 = add nsw i32 %20, 1
store i32 %21, i32* %2
br label %11
22: ; preds = %11
ret void
}

LLVM IR (-O2):
@_SC_OPEN_MAX = global i32 0
@STDERR_FILENO = global i32 0
define void @closeNonStdDescriptors() {
%1 = load i32, i32* @_SC_OPEN_MAX
%2 = tail call i64 @sysconf(i32 %1)
%3 = icmp slt i64 %2, 0
%4 = select i1 %3, i64 4096, i64 %2
%5 = load i32, i32* @STDERR_FILENO
%6 = add nsw i32 %5, 1
%7 = sext i32 %6 to i64
%8 = icmp sgt i64 %4, %7
br i1 %8, label %9, label %15
9: ; preds = %0, %9
%10 = phi i64 [ %13, %9 ], [ %7, %0 ]
%11 = trunc i64 %10 to i32
%12 = tail call i32 @close(i32 %11)
%13 = add nsw i64 %10, 1
%14 = icmp sgt i64 %4, %13
br i1 %14, label %9, label %15
15: ; preds = %9, %0
ret void
}