#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_cur;
};
typedef scalar_t__ rlim_t;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ br();
int errno;
long getdtablesize();
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ isaye(int, char *);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
int setMaxFiles(long nfiles) {
  struct rlimit lim;
  int flim, rv;
  if (nfiles < getdtablesize()) {
    return (0);
  }
  getrlimit(RLIMIT_NOFILE, &lim);
  lim.rlim_cur = (rlim_t)nfiles;
  rv = setrlimit(RLIMIT_NOFILE, &lim);
  flim = getdtablesize();
  if (flim != nfiles) {
    isaye(nfiles, "nfiles requested");
    isaye(flim, "nfiles set");
    isaye(rv, "setrlimit rv");
    isaye(errno, "errno");
    br();
  }
  return (rv ? errno : 0);
}


x86 assembly (clang, -O0):
<setMaxFiles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <getdtablesize@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jge    1187 <setMaxFiles+0x37>
movl   $0x0,-0x4(%rbp)
jmp    1245 <setMaxFiles+0xf5>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1060 <getrlimit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <setrlimit@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getdtablesize@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     121a <setMaxFiles+0xca>
mov    -0x10(%rbp),%rax
mov    %eax,%edi
lea    0xe25(%rip),%rsi
call   1050 <isaye@plt>
mov    -0x1c(%rbp),%edi
lea    0xe27(%rip),%rsi
call   1050 <isaye@plt>
mov    -0x20(%rbp),%edi
lea    0xe23(%rip),%rsi
call   1050 <isaye@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1050 <isaye@plt>
mov    $0x0,%al
call   1030 <br@plt>
cmpl   $0x0,-0x20(%rbp)
je     1235 <setMaxFiles+0xe5>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    123f <setMaxFiles+0xef>
xor    %eax,%eax
mov    %eax,-0x2c(%rbp)
jmp    123f <setMaxFiles+0xef>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setMaxFiles>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <getdtablesize@plt>
cmp    %rbx,%rax
jg     11fc <setMaxFiles+0xac>
mov    0x2e74(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rbp
mov    %rbp,%rsi
call   1060 <getrlimit@plt>
mov    %rbx,0x8(%rsp)
mov    (%r14),%edi
mov    %rbp,%rsi
call   1040 <setrlimit@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getdtablesize@plt>
mov    %rax,%r14
movslq %r14d,%rax
cmp    %rbx,%rax
je     11ef <setMaxFiles+0x9f>
lea    0xe51(%rip),%rsi
mov    %ebx,%edi
call   1050 <isaye@plt>
lea    0xe54(%rip),%rsi
mov    %r14d,%edi
call   1050 <isaye@plt>
lea    0xe50(%rip),%rsi
mov    %ebp,%edi
call   1050 <isaye@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1050 <isaye@plt>
xor    %eax,%eax
call   1030 <br@plt>
test   %ebp,%ebp
je     11fc <setMaxFiles+0xac>
mov    0x2dde(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i64 }
@RLIMIT_NOFILE = global i32 0
@.str = [17 x i8] c"nfiles requested\00"
@.str.1 = [11 x i8] c"nfiles set\00"
@.str.2 = [13 x i8] c"setrlimit rv\00"
@errno = global i32 0
@.str.3 = [6 x i8] c"errno\00"
define i32 @setMaxFiles(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca %struct.rlimit
%5 = alloca i32
%6 = alloca i32
store i64 %0, i64* %3
%7 = load i64, i64* %3
%8 = call i64 (...) @getdtablesize()
%9 = icmp slt i64 %7, %8
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 0, i32* %2
br label %43
11: ; preds = %1
%12 = load i32, i32* @RLIMIT_NOFILE
%13 = call i32 @getrlimit(i32 %12, %struct.rlimit* %4)
%14 = load i64, i64* %3
%15 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
store i64 %14, i64* %15
%16 = load i32, i32* @RLIMIT_NOFILE
%17 = call i32 @setrlimit(i32 %16, %struct.rlimit* %4)
store i32 %17, i32* %6
%18 = call i64 (...) @getdtablesize()
%19 = trunc i64 %18 to i32
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = load i64, i64* %3
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %35
24: ; preds = %11
%25 = load i64, i64* %3
%26 = trunc i64 %25 to i32
%27 = call i32 @isaye(i32 %26, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* %5
%29 = call i32 @isaye(i32 %28, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %6
%31 = call i32 @isaye(i32 %30, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* @errno
%33 = call i32 @isaye(i32 %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 (...) @br()
br label %35
35: ; preds = %24, %11
%36 = load i32, i32* %6
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = load i32, i32* @errno
br label %41
40: ; preds = %35
br label %41
41: ; preds = %40, %38
%42 = phi i32 [ %39, %38 ], [ 0, %40 ]
store i32 %42, i32* %2
br label %43
43: ; preds = %41, %10
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.rlimit = type { i64 }
@RLIMIT_NOFILE = global i32 0
@.str = [17 x i8] c"nfiles requested\00"
@.str.1 = [11 x i8] c"nfiles set\00"
@.str.2 = [13 x i8] c"setrlimit rv\00"
@errno = global i32 0
@.str.3 = [6 x i8] c"errno\00"
define i32 @setMaxFiles(i64 %0) {
%2 = alloca %struct.rlimit
%3 = bitcast %struct.rlimit* %2 to i8*
%4 = tail call i64 (...) @getdtablesize()
%5 = icmp sgt i64 %4, %0
br i1 %5, label %29, label %6
6: ; preds = %1
%7 = load i32, i32* @RLIMIT_NOFILE
%8 = call i32 @getrlimit(i32 %7, %struct.rlimit* %2)
%9 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
store i64 %0, i64* %9
%10 = load i32, i32* @RLIMIT_NOFILE
%11 = call i32 @setrlimit(i32 %10, %struct.rlimit* %2)
%12 = call i64 (...) @getdtablesize()
%13 = shl i64 %12, 32
%14 = ashr exact i64 %13, 32
%15 = icmp eq i64 %14, %0
br i1 %15, label %25, label %16
16: ; preds = %6
%17 = trunc i64 %12 to i32
%18 = trunc i64 %0 to i32
%19 = call i32 @isaye(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @isaye(i32 %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @isaye(i32 %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @errno
%23 = call i32 @isaye(i32 %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 (...) @br()
br label %25
25: ; preds = %16, %6
%26 = icmp eq i32 %11, 0
%27 = load i32, i32* @errno
%28 = select i1 %26, i32 0, i32 %27
br label %29
29: ; preds = %1, %25
%30 = phi i32 [ %28, %25 ], [ 0, %1 ]
ret i32 %30
}