#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 /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getdtablesize();
int /*<<< orphan*/ stderr;
main() {
  register int i;
  for (i = 0; i < getdtablesize(); i++) {
    if (fcntl(i, F_GETFD, 0) != -1)
      fprintf(stderr, "fd %d: open\n", i);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1060 <getdtablesize@plt>
mov    %eax,%ecx
mov    -0xc(%rbp),%eax
cmp    %ecx,%eax
jge    11b7 <main+0x77>
mov    -0x8(%rbp),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11a4 <main+0x64>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe61(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11a9 <main+0x69>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1156 <main+0x16>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <getdtablesize@plt>
test   %eax,%eax
jle    119d <main+0x5d>
mov    0x2e7f(%rip),%rbx
mov    0x2e80(%rip),%r15
lea    0xe99(%rip),%r14
xor    %ebp,%ebp
jmp    117e <main+0x3e>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
xor    %eax,%eax
call   1060 <getdtablesize@plt>
cmp    %eax,%ebp
jge    119d <main+0x5d>
mov    (%rbx),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1170 <main+0x30>
mov    (%r15),%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <fprintf@plt>
jmp    1170 <main+0x30>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@F_GETFD = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"fd %d: open\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
br label %3
3: ; preds = %17, %0
%4 = load i32, i32* %2
%5 = call i32 (...) @getdtablesize()
%6 = icmp slt i32 %4, %5
br i1 %6, label %7, label %20
7: ; preds = %3
%8 = load i32, i32* %2
%9 = load i32, i32* @F_GETFD
%10 = call i32 @fcntl(i32 %8, i32 %9, i32 0)
%11 = icmp ne i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = load i32, i32* %2
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %12, %7
br label %17
17: ; preds = %16
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 1
store i32 %19, i32* %2
br label %3
20: ; preds = %3
%21 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"fd %d: open\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @getdtablesize()
%2 = icmp sgt i32 %1, 0
br i1 %2, label %3, label %15
3: ; preds = %0, %11
%4 = phi i32 [ %12, %11 ], [ 0, %0 ]
%5 = load i32, i32* @F_GETFD
%6 = tail call i32 @fcntl(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %11, label %8
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %11
11: ; preds = %3, %8
%12 = add nuw nsw i32 %4, 1
%13 = tail call i32 (...) @getdtablesize()
%14 = icmp slt i32 %12, %13
br i1 %14, label %3, label %15
15: ; preds = %11, %0
%16 = tail call i32 @exit(i32 0)
unreachable
}