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

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
void set_max_fd(int n) {
  struct rlimit rlp;
  rlp.rlim_cur = rlp.rlim_max = n;
  if (setrlimit(RLIMIT_NOFILE, &rlp) < 0) {
    perror(" Failed to set maxfile descriptors"
           ", run as root\n");
    exit(errno);
  };
  ;
}


x86 assembly (clang, -O0):
<set_max_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    %eax,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <setrlimit@plt>
cmp    $0x0,%rax
jge    117a <set_max_fd+0x4a>
lea    0xe99(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_max_fd>:
push   %rax
mov    %edi,0x4(%rsp)
mov    %edi,(%rsp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <setrlimit@plt>
test   %rax,%rax
js     1150 <set_max_fd+0x20>
pop    %rax
ret
lea    0xea9(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [49 x i8] c" Failed to set maxfile descriptors, run as root\0A\00"
@errno = global i32 0
define void @set_max_fd(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.rlimit
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 1
store i32 %4, i32* %5
%6 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
store i32 %4, i32* %6
%7 = load i32, i32* @RLIMIT_NOFILE
%8 = call i64 @setrlimit(i32 %7, %struct.rlimit* %3)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @errno
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %1
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [49 x i8] c" Failed to set maxfile descriptors, run as root\0A\00"
@errno = global i32 0
define void @set_max_fd(i32 %0) {
%2 = alloca %struct.rlimit
%3 = bitcast %struct.rlimit* %2 to i8*
%4 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 1
store i32 %0, i32* %4
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
store i32 %0, i32* %5
%6 = load i32, i32* @RLIMIT_NOFILE
%7 = call i64 @setrlimit(i32 %6, %struct.rlimit* %2)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @errno
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
ret void
}