#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*/ _SC_OPEN_MAX;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
int open_max(void) {
  static int openmax = 0;
  if (openmax == 0) {
    errno = 0;
    if ((openmax = sysconf(_SC_OPEN_MAX)) < 0) {
      if (errno == 0)
        openmax = 256;
      else
        fprintf(stderr, "sysconf error for _SC_OPEN_MAX\n");
    }
  }
  return openmax;
}


x86 assembly (clang, -O0):
<open_max>:
push   %rbp
mov    %rsp,%rbp
cmpl   $0x0,0x2f0d(%rip)
jne    119b <open_max+0x7b>
mov    0x2e98(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e9a(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %eax,0x2ee5(%rip)
cmp    $0x0,%eax
jge    1196 <open_max+0x76>
mov    0x2e6d(%rip),%rax
cmpq   $0x0,(%rax)
jne    117c <open_max+0x5c>
movl   $0x100,0x2ec1(%rip)
jmp    1191 <open_max+0x71>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
call   1030 <fprintf@plt>
jmp    1196 <open_max+0x76>
jmp    119b <open_max+0x7b>
mov    0x2e97(%rip),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_max>:
push   %rbx
mov    0x2f11(%rip),%eax
test   %eax,%eax
je     112d <open_max+0xd>
pop    %rbx
ret
mov    0x2e9c(%rip),%rbx
movq   $0x0,(%rbx)
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %eax,0x2ee9(%rip)
test   %eax,%eax
jns    112b <open_max+0xb>
cmpq   $0x0,(%rbx)
je     1176 <open_max+0x56>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
lea    0xe97(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2ec4(%rip),%eax
pop    %rbx
ret
movl   $0x100,0x2eb8(%rip)
mov    $0x100,%eax
pop    %rbx
ret

LLVM IR (-O0):
@open_max.openmax = internal global i32 0
@errno = global i64 0
@_SC_OPEN_MAX = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"sysconf error for _SC_OPEN_MAX\0A\00"
define i32 @open_max() {
%1 = load i32, i32* @open_max.openmax
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %16
3: ; preds = %0
store i64 0, i64* @errno
%4 = load i32, i32* @_SC_OPEN_MAX
%5 = call i32 @sysconf(i32 %4)
store i32 %5, i32* @open_max.openmax
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %3
%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* @open_max.openmax
br label %14
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %10
br label %15
15: ; preds = %14, %3
br label %16
16: ; preds = %15, %0
%17 = load i32, i32* @open_max.openmax
ret i32 %17
}

LLVM IR (-O2):
@open_max.openmax = internal unnamed_addr global i32 0
@errno = global i64 0
@_SC_OPEN_MAX = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"sysconf error for _SC_OPEN_MAX\0A\00"
define i32 @open_max() {
%1 = load i32, i32* @open_max.openmax
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %15
3: ; preds = %0
store i64 0, i64* @errno
%4 = load i32, i32* @_SC_OPEN_MAX
%5 = tail call i32 @sysconf(i32 %4)
store i32 %5, i32* @open_max.openmax
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %3
%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* @open_max.openmax
br label %15
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @open_max.openmax
br label %15
15: ; preds = %3, %11, %10, %0
%16 = phi i32 [ %5, %3 ], [ %14, %11 ], [ 256, %10 ], [ %1, %0 ]
ret i32 %16
}