#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 {
  unsigned int rlim_max;
  int rlim_cur;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
int os_set_fdlimit(unsigned int max_descriptors) {
  struct rlimit limit;
  if (!getrlimit(RLIMIT_NOFILE, &limit)) {
    if (limit.rlim_max < max_descriptors)
      return limit.rlim_max;
    limit.rlim_cur = limit.rlim_max;
    return setrlimit(RLIMIT_NOFILE, &limit);
  }
  return 0;
}


x86 assembly (clang, -O0):
<os_set_fdlimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2eb6(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <getrlimit@plt>
cmp    $0x0,%eax
jne    117d <os_set_fdlimit+0x5d>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    115d <os_set_fdlimit+0x3d>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1184 <os_set_fdlimit+0x64>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <setrlimit@plt>
mov    %eax,-0x4(%rbp)
jmp    1184 <os_set_fdlimit+0x64>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_set_fdlimit>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ebb(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1040 <getrlimit@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    1156 <os_set_fdlimit+0x36>
mov    (%rsp),%eax
cmp    %ebx,%eax
jb     1156 <os_set_fdlimit+0x36>
mov    %eax,0x4(%rsp)
mov    (%r14),%edi
mov    %rsp,%rsi
call   1030 <setrlimit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@RLIMIT_NOFILE = global i32 0
define i32 @os_set_fdlimit(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.rlimit
store i32 %0, i32* %3
%5 = load i32, i32* @RLIMIT_NOFILE
%6 = call i32 @getrlimit(i32 %5, %struct.rlimit* %4)
%7 = icmp ne i32 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %1
%9 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%10 = load i32, i32* %9
%11 = load i32, i32* %3
%12 = icmp ult i32 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%15 = load i32, i32* %14
store i32 %15, i32* %2
br label %23
16: ; preds = %8
%17 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @RLIMIT_NOFILE
%21 = call i32 @setrlimit(i32 %20, %struct.rlimit* %4)
store i32 %21, i32* %2
br label %23
22: ; preds = %1
store i32 0, i32* %2
br label %23
23: ; preds = %22, %16, %13
%24 = load i32, i32* %2
ret i32 %24
}

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