#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 */
struct rlimit {
  scalar_t__ rlim_max;
};

/* Variables and functions */
unsigned int INT_MAX;
int /*<<< orphan*/ RLIMIT_NOFILE;
scalar_t__ RLIM_INFINITY;
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
unsigned int get_max_fd(void) {
  struct rlimit r;
  if (getrlimit(RLIMIT_NOFILE, &r) < 0)
    return INT_MAX;
  if (r.rlim_max == RLIM_INFINITY)
    return INT_MAX;
  return (unsigned int)r.rlim_max;
}


x86 assembly (clang, -O0):
<get_max_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ec1(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <getrlimit@plt>
cmp    $0x0,%rax
jge    1145 <get_max_fd+0x35>
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1171 <get_max_fd+0x61>
mov    -0x10(%rbp),%rax
mov    0x2e80(%rip),%rcx
cmp    (%rcx),%rax
jne    116a <get_max_fd+0x5a>
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1171 <get_max_fd+0x61>
mov    -0x10(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_max_fd>:
push   %rax
mov    0x2ec8(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <getrlimit@plt>
test   %rax,%rax
js     1137 <get_max_fd+0x27>
mov    (%rsp),%rax
mov    0x2e9e(%rip),%rcx
cmp    (%rcx),%rax
jne    1140 <get_max_fd+0x30>
mov    0x2eaa(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.rlimit = type { i64 }
@RLIMIT_NOFILE = global i32 0
@INT_MAX = global i32 0
@RLIM_INFINITY = global i64 0
define i32 @get_max_fd() {
%1 = alloca i32
%2 = alloca %struct.rlimit
%3 = load i32, i32* @RLIMIT_NOFILE
%4 = call i64 @getrlimit(i32 %3, %struct.rlimit* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = load i32, i32* @INT_MAX
store i32 %7, i32* %1
br label %19
8: ; preds = %0
%9 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @RLIM_INFINITY
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = load i32, i32* @INT_MAX
store i32 %14, i32* %1
br label %19
15: ; preds = %8
%16 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%17 = load i64, i64* %16
%18 = trunc i64 %17 to i32
store i32 %18, i32* %1
br label %19
19: ; preds = %15, %13, %6
%20 = load i32, i32* %1
ret i32 %20
}

LLVM IR (-O2):
%struct.rlimit = type { i64 }
@RLIMIT_NOFILE = global i32 0
@INT_MAX = global i32 0
@RLIM_INFINITY = global i64 0
define i32 @get_max_fd() {
%1 = alloca %struct.rlimit
%2 = bitcast %struct.rlimit* %1 to i8*
%3 = load i32, i32* @RLIMIT_NOFILE
%4 = call i64 @getrlimit(i32 %3, %struct.rlimit* %1)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = load i32, i32* @INT_MAX
br label %17
8: ; preds = %0
%9 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @RLIM_INFINITY
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = load i32, i32* @INT_MAX
br label %17
15: ; preds = %8
%16 = trunc i64 %10 to i32
br label %17
17: ; preds = %15, %13, %6
%18 = phi i32 [ %7, %6 ], [ %14, %13 ], [ %16, %15 ]
ret i32 %18
}