#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 STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int getdtablesize();
void closeAllOpenfds(int fd) {
  int j;
  for (j = getdtablesize(); j >= 0; --j) {
    if (j != STDIN_FILENO && j != STDOUT_FILENO && j != STDERR_FILENO &&
        j != fd) {
      close(j);
    }
  }
}


x86 assembly (clang, -O0):
<closeAllOpenfds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getdtablesize@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     119c <closeAllOpenfds+0x7c>
mov    -0x8(%rbp),%eax
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%eax
je     1189 <closeAllOpenfds+0x69>
mov    -0x8(%rbp),%eax
mov    0x2e95(%rip),%rcx
cmp    (%rcx),%eax
je     1189 <closeAllOpenfds+0x69>
mov    -0x8(%rbp),%eax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%eax
je     1189 <closeAllOpenfds+0x69>
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     1189 <closeAllOpenfds+0x69>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
jmp    118e <closeAllOpenfds+0x6e>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    1135 <closeAllOpenfds+0x15>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<closeAllOpenfds>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <getdtablesize@plt>
test   %eax,%eax
js     118c <closeAllOpenfds+0x6c>
mov    %eax,%ebx
mov    0x2e92(%rip),%r14
mov    (%r14),%eax
mov    0x2ea8(%rip),%r15
mov    (%r15),%ecx
mov    0x2e86(%rip),%r12
mov    (%r12),%edx
jmp    1169 <closeAllOpenfds+0x49>
nopl   0x0(%rax,%rax,1)
lea    -0x1(%rbx),%esi
test   %ebx,%ebx
mov    %esi,%ebx
jle    118c <closeAllOpenfds+0x6c>
cmp    %eax,%ebx
je     1160 <closeAllOpenfds+0x40>
cmp    %ecx,%ebx
je     1160 <closeAllOpenfds+0x40>
cmp    %edx,%ebx
je     1160 <closeAllOpenfds+0x40>
cmp    %ebx,%ebp
je     1160 <closeAllOpenfds+0x40>
mov    %ebx,%edi
call   1030 <close@plt>
mov    (%r14),%eax
mov    (%r15),%ecx
mov    (%r12),%edx
jmp    1160 <closeAllOpenfds+0x40>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @closeAllOpenfds(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = call i32 (...) @getdtablesize()
store i32 %4, i32* %3
br label %5
5: ; preds = %28, %1
%6 = load i32, i32* %3
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %31
8: ; preds = %5
%9 = load i32, i32* %3
%10 = load i32, i32* @STDIN_FILENO
%11 = icmp ne i32 %9, %10
br i1 %11, label %12, label %27
12: ; preds = %8
%13 = load i32, i32* %3
%14 = load i32, i32* @STDOUT_FILENO
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %27
16: ; preds = %12
%17 = load i32, i32* %3
%18 = load i32, i32* @STDERR_FILENO
%19 = icmp ne i32 %17, %18
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = load i32, i32* %3
%22 = load i32, i32* %2
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
br label %27
27: ; preds = %24, %20, %16, %12, %8
br label %28
28: ; preds = %27
%29 = load i32, i32* %3
%30 = add nsw i32 %29, -1
store i32 %30, i32* %3
br label %5
31: ; preds = %5
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @closeAllOpenfds(i32 %0) {
%2 = tail call i32 (...) @getdtablesize()
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %31
4: ; preds = %1
%5 = load i32, i32* @STDIN_FILENO
%6 = load i32, i32* @STDOUT_FILENO
%7 = load i32, i32* @STDERR_FILENO
br label %8
8: ; preds = %4, %25
%9 = phi i32 [ %26, %25 ], [ %7, %4 ]
%10 = phi i32 [ %27, %25 ], [ %6, %4 ]
%11 = phi i32 [ %28, %25 ], [ %5, %4 ]
%12 = phi i32 [ %29, %25 ], [ %2, %4 ]
%13 = icmp eq i32 %12, %11
%14 = icmp eq i32 %12, %10
%15 = select i1 %13, i1 true, i1 %14
%16 = icmp eq i32 %12, %9
%17 = select i1 %15, i1 true, i1 %16
%18 = icmp eq i32 %12, %0
%19 = select i1 %17, i1 true, i1 %18
br i1 %19, label %25, label %20
20: ; preds = %8
%21 = tail call i32 @close(i32 %12)
%22 = load i32, i32* @STDIN_FILENO
%23 = load i32, i32* @STDOUT_FILENO
%24 = load i32, i32* @STDERR_FILENO
br label %25
25: ; preds = %8, %20
%26 = phi i32 [ %9, %8 ], [ %24, %20 ]
%27 = phi i32 [ %10, %8 ], [ %23, %20 ]
%28 = phi i32 [ %11, %8 ], [ %22, %20 ]
%29 = add nsw i32 %12, -1
%30 = icmp sgt i32 %12, 0
br i1 %30, label %8, label %31
31: ; preds = %25, %1
ret void
}