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

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ close(unsigned int);
int getrlimit(int /*<<< orphan*/, struct rlimit *);
int close_non_standard_file_descriptors(void) {
  unsigned int i;
  struct rlimit rlim;
  int num_of_fds = getrlimit(RLIMIT_NOFILE, &rlim);
  if (num_of_fds == -1)
    return 0;
  for (i = 3; i < num_of_fds; i++)
    close(i);
  return 1;
}


x86 assembly (clang, -O0):
<close_non_standard_file_descriptors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2eb9(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <getrlimit@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1153 <close_non_standard_file_descriptors+0x33>
movl   $0x0,-0x4(%rbp)
jmp    1183 <close_non_standard_file_descriptors+0x63>
movl   $0x3,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    117c <close_non_standard_file_descriptors+0x5c>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    115a <close_non_standard_file_descriptors+0x3a>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_non_standard_file_descriptors>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2eb9(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1040 <getrlimit@plt>
cmp    $0xffffffff,%eax
je     1170 <close_non_standard_file_descriptors+0x50>
mov    %eax,%ebp
mov    $0x1,%r14d
cmp    $0x4,%eax
jb     1173 <close_non_standard_file_descriptors+0x53>
mov    $0x3,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
call   1030 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1160 <close_non_standard_file_descriptors+0x40>
jmp    1173 <close_non_standard_file_descriptors+0x53>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@RLIMIT_NOFILE = global i32 0
define i32 @close_non_standard_file_descriptors() {
%1 = alloca %struct.rlimit
%2 = bitcast %struct.rlimit* %1 to i8*
%3 = load i32, i32* @RLIMIT_NOFILE
%4 = call i32 @getrlimit(i32 %3, %struct.rlimit* %1)
%5 = icmp eq i32 %4, -1
br i1 %5, label %13, label %6
6: ; preds = %0
%7 = icmp ugt i32 %4, 3
br i1 %7, label %8, label %13
8: ; preds = %6, %8
%9 = phi i32 [ %11, %8 ], [ 3, %6 ]
%10 = call i32 @close(i32 %9)
%11 = add nuw i32 %9, 1
%12 = icmp eq i32 %11, %4
br i1 %12, label %13, label %8
13: ; preds = %8, %6, %0
%14 = phi i32 [ 0, %0 ], [ 1, %6 ], [ 1, %8 ]
ret i32 %14
}