#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_GETFL;
int /*<<< orphan*/ _SC_OPEN_MAX;
int errno;
scalar_t__ fcntl(int, int /*<<< orphan*/);
long sysconf(int /*<<< orphan*/);
int lmsc_get_nofd(int resv) {
  int nofd, i, n;
  long l;
  i = errno;
  errno = 0;
  l = sysconf(_SC_OPEN_MAX);
  if (l < 0) {
    if (errno) {
      return 0;
    }
    l = 512;
  }
  errno = i;
  n = nofd = (int)l;
  for (i = 0; i < n; i++) {
    if (fcntl(i, F_GETFL) >= 0) {
      --nofd;
    }
  }
  return nofd - resv;
}


x86 assembly (clang, -O0):
<lmsc_get_nofd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e9a(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1185 <lmsc_get_nofd+0x65>
mov    0x2e70(%rip),%rax
cmpl   $0x0,(%rax)
je     117d <lmsc_get_nofd+0x5d>
movl   $0x0,-0x4(%rbp)
jmp    11ee <lmsc_get_nofd+0xce>
movq   $0x200,-0x20(%rbp)
mov    -0x10(%rbp),%ecx
mov    0x2e49(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %eax,-0xc(%rbp)
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e5 <lmsc_get_nofd+0xc5>
mov    -0x10(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jl     11d2 <lmsc_get_nofd+0xb2>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    11d7 <lmsc_get_nofd+0xb7>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11a2 <lmsc_get_nofd+0x82>
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lmsc_get_nofd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
mov    0x2ea6(%rip),%rbx
mov    (%rbx),%r12d
movl   $0x0,(%rbx)
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
test   %rax,%rax
js     115e <lmsc_get_nofd+0x3e>
mov    %rax,%r15
mov    %r12d,(%rbx)
mov    %r15d,%ebp
test   %r15d,%r15d
jg     116e <lmsc_get_nofd+0x4e>
jmp    119b <lmsc_get_nofd+0x7b>
xor    %ebp,%ebp
cmpl   $0x0,(%rbx)
jne    119e <lmsc_get_nofd+0x7e>
mov    %r12d,(%rbx)
mov    $0x200,%r15d
xor    %ebx,%ebx
mov    0x2e59(%rip),%r12
mov    %r15d,%ebp
nopw   0x0(%rax,%rax,1)
mov    (%r12),%esi
mov    %ebx,%edi
call   1030 <fcntl@plt>
sar    $0x3f,%rax
not    %eax
add    %eax,%ebp
add    $0x1,%ebx
cmp    %ebx,%r15d
jne    1180 <lmsc_get_nofd+0x60>
sub    %r14d,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@_SC_OPEN_MAX = global i32 0
@F_GETFL = global i32 0
define i32 @lmsc_get_nofd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %3
%8 = load i32, i32* @errno
store i32 %8, i32* %5
store i32 0, i32* @errno
%9 = load i32, i32* @_SC_OPEN_MAX
%10 = call i64 @sysconf(i32 %9)
store i64 %10, i64* %7
%11 = load i64, i64* %7
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @errno
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 0, i32* %2
br label %42
17: ; preds = %13
store i64 512, i64* %7
br label %18
18: ; preds = %17, %1
%19 = load i32, i32* %5
store i32 %19, i32* @errno
%20 = load i64, i64* %7
%21 = trunc i64 %20 to i32
store i32 %21, i32* %4
store i32 %21, i32* %6
store i32 0, i32* %5
br label %22
22: ; preds = %35, %18
%23 = load i32, i32* %5
%24 = load i32, i32* %6
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %38
26: ; preds = %22
%27 = load i32, i32* %5
%28 = load i32, i32* @F_GETFL
%29 = call i64 @fcntl(i32 %27, i32 %28)
%30 = icmp sge i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* %4
%33 = add nsw i32 %32, -1
store i32 %33, i32* %4
br label %34
34: ; preds = %31, %26
br label %35
35: ; preds = %34
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %22
38: ; preds = %22
%39 = load i32, i32* %4
%40 = load i32, i32* %3
%41 = sub nsw i32 %39, %40
store i32 %41, i32* %2
br label %42
42: ; preds = %38, %16
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@errno = global i32 0
@_SC_OPEN_MAX = global i32 0
@F_GETFL = global i32 0
define i32 @lmsc_get_nofd(i32 %0) {
%2 = load i32, i32* @errno
store i32 0, i32* @errno
%3 = load i32, i32* @_SC_OPEN_MAX
%4 = tail call i64 @sysconf(i32 %3)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %29
9: ; preds = %6
store i32 %2, i32* @errno
br label %13
10: ; preds = %1
store i32 %2, i32* @errno
%11 = trunc i64 %4 to i32
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %9, %10
%14 = phi i32 [ 512, %9 ], [ %11, %10 ]
br label %15
15: ; preds = %13, %15
%16 = phi i32 [ %24, %15 ], [ 0, %13 ]
%17 = phi i32 [ %23, %15 ], [ %14, %13 ]
%18 = load i32, i32* @F_GETFL
%19 = tail call i64 @fcntl(i32 %16, i32 %18)
%20 = ashr i64 %19, 63
%21 = trunc i64 %20 to i32
%22 = xor i32 %21, -1
%23 = add nsw i32 %17, %22
%24 = add nuw nsw i32 %16, 1
%25 = icmp eq i32 %24, %14
br i1 %25, label %26, label %15
26: ; preds = %15, %10
%27 = phi i32 [ %11, %10 ], [ %23, %15 ]
%28 = sub nsw i32 %27, %0
br label %29
29: ; preds = %6, %26
%30 = phi i32 [ %28, %26 ], [ 0, %6 ]
ret i32 %30
}