#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*/ O_RDONLY;
int getdtablesize();
scalar_t__ isopen(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int main(void) {
  int nopen, fd, in;
  in = open("main.c", O_RDONLY);
  for (nopen = fd = 0; fd < getdtablesize(); fd++) {
    if (isopen(fd)) {
      nopen++;
    }
  }
  printf("Existem %d descritores abertos\n", nopen);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1060 <getdtablesize@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jge    11bd <main+0x7d>
mov    -0xc(%rbp),%edi
call   1040 <isopen@plt>
cmp    $0x0,%rax
je     11aa <main+0x6a>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11af <main+0x6f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1175 <main+0x35>
mov    -0x8(%rbp),%esi
lea    0xe40(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
call   1050 <open@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <getdtablesize@plt>
test   %eax,%eax
jle    118c <main+0x4c>
xor    %ebx,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %ebp,%edi
call   1040 <isopen@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebx
add    $0x1,%ebp
xor    %eax,%eax
call   1060 <getdtablesize@plt>
cmp    %eax,%ebp
jl     1170 <main+0x30>
lea    0xe74(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"main.c\00"
@O_RDONLY = global i32 0
@.str.1 = [32 x i8] c"Existem %d descritores abertos\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %4
store i32 0, i32* %3
store i32 0, i32* %2
br label %7
7: ; preds = %19, %0
%8 = load i32, i32* %3
%9 = call i32 (...) @getdtablesize()
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %22
11: ; preds = %7
%12 = load i32, i32* %3
%13 = call i64 @isopen(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
br label %18
18: ; preds = %15, %11
br label %19
19: ; preds = %18
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %7
22: ; preds = %7
%23 = load i32, i32* %2
%24 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %23)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"main.c\00"
@O_RDONLY = global i32 0
@.str.1 = [32 x i8] c"Existem %d descritores abertos\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 (...) @getdtablesize()
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %15
5: ; preds = %0, %5
%6 = phi i32 [ %11, %5 ], [ 0, %0 ]
%7 = phi i32 [ %12, %5 ], [ 0, %0 ]
%8 = tail call i64 @isopen(i32 %7)
%9 = icmp ne i64 %8, 0
%10 = zext i1 %9 to i32
%11 = add nuw nsw i32 %6, %10
%12 = add nuw nsw i32 %7, 1
%13 = tail call i32 (...) @getdtablesize()
%14 = icmp slt i32 %12, %13
br i1 %14, label %5, label %15
15: ; preds = %5, %0
%16 = phi i32 [ 0, %0 ], [ %11, %5 ]
%17 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %16)
ret i32 0
}