#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*/ EXIT_FAILURE;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ cctools_debug(long long, char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
scalar_t__ sysconf(int /*<<< orphan*/);
int fd_max(void) {
  errno = 0;
  int max = (int)sysconf(_SC_OPEN_MAX);
  if (max == -1) {
    if (errno == 0) {
      max = 256;
    } else {
      cctools_debug((1LL << 3), "sysconf(_SC_OPEN_MAX) error: %s",
                    strerror(errno));
      exit(EXIT_FAILURE);
    }
  }
  return max;
}


x86 assembly (clang, -O0):
<fd_max>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11c3 <fd_max+0x83>
mov    0x2e60(%rip),%rax
cmpq   $0x0,(%rax)
jne    118e <fd_max+0x4e>
movl   $0x100,-0x4(%rbp)
jmp    11be <fd_max+0x7e>
mov    0x2e43(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %eax,%edx
mov    $0x8,%edi
lea    0xe55(%rip),%rsi
call   1030 <cctools_debug@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    11c3 <fd_max+0x83>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_max>:
push   %rbx
mov    0x2e90(%rip),%rbx
movq   $0x0,(%rbx)
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
cmp    $0xffffffff,%eax
jne    116f <fd_max+0x2f>
mov    (%rbx),%rdi
mov    $0x100,%eax
test   %rdi,%rdi
jne    1171 <fd_max+0x31>
pop    %rbx
ret
call   1060 <strerror@plt>
lea    0xe83(%rip),%rsi
mov    $0x8,%edi
mov    %eax,%edx
call   1030 <cctools_debug@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@_SC_OPEN_MAX = global i32 0
@.str = [32 x i8] c"sysconf(_SC_OPEN_MAX) error: %s\00"
@EXIT_FAILURE = global i32 0
define i32 @fd_max() {
%1 = alloca i32
store i64 0, i64* @errno
%2 = load i32, i32* @_SC_OPEN_MAX
%3 = call i64 @sysconf(i32 %2)
%4 = trunc i64 %3 to i32
store i32 %4, i32* %1
%5 = load i32, i32* %1
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %18
7: ; preds = %0
%8 = load i64, i64* @errno
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %7
store i32 256, i32* %1
br label %17
11: ; preds = %7
%12 = load i64, i64* @errno
%13 = call i32 @strerror(i64 %12)
%14 = call i32 @cctools_debug(i64 8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
br label %18
18: ; preds = %17, %0
%19 = load i32, i32* %1
ret i32 %19
}

LLVM IR (-O2):
@errno = global i64 0
@_SC_OPEN_MAX = global i32 0
@.str = [32 x i8] c"sysconf(_SC_OPEN_MAX) error: %s\00"
@EXIT_FAILURE = global i32 0
define i32 @fd_max() {
store i64 0, i64* @errno
%1 = load i32, i32* @_SC_OPEN_MAX
%2 = tail call i64 @sysconf(i32 %1)
%3 = trunc i64 %2 to i32
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %13
5: ; preds = %0
%6 = load i64, i64* @errno
%7 = icmp eq i64 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %5
%9 = tail call i32 @strerror(i64 %6)
%10 = tail call i32 @cctools_debug(i64 8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %5, %0
%14 = phi i32 [ %3, %0 ], [ 256, %5 ]
ret i32 %14
}