#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*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ perror(char *);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ stderr;
void check_and_set_max_fd(int n) {
  struct rlimit rlp;
  (void)getrlimit(RLIMIT_NOFILE, &rlp);
  if (rlp.rlim_cur < n) {
    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);
    };
    ;
  }
  fprintf(stderr, "Max file descriptors set to: %d\n", (int)rlp.rlim_cur);
}


x86 assembly (clang, -O0):
<check_and_set_max_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1070 <getrlimit@plt>
mov    -0x10(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11bd <check_and_set_max_fd+0x6d>
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    %eax,-0x10(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <setrlimit@plt>
cmp    $0x0,%rax
jge    11b8 <check_and_set_max_fd+0x68>
lea    0xe5b(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11bd <check_and_set_max_fd+0x6d>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
lea    0xe61(%rip),%rsi
call   1030 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_and_set_max_fd>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e8b(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1070 <getrlimit@plt>
mov    (%rsp),%edx
cmp    %ebx,%edx
jge    1189 <check_and_set_max_fd+0x39>
mov    %ebx,0x4(%rsp)
mov    %ebx,(%rsp)
mov    (%r14),%edi
mov    %rsp,%rsi
call   1040 <setrlimit@plt>
test   %rax,%rax
js     11a6 <check_and_set_max_fd+0x56>
mov    (%rsp),%edx
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
call   1030 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe53(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1060 <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
@stderr = global i32 0
@.str.1 = [33 x i8] c"Max file descriptors set to: %d\0A\00"
define void @check_and_set_max_fd(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.rlimit
store i32 %0, i32* %2
%4 = load i32, i32* @RLIMIT_NOFILE
%5 = call i32 @getrlimit(i32 %4, %struct.rlimit* %3)
%6 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
%7 = load i32, i32* %6
%8 = load i32, i32* %2
%9 = icmp slt i32 %7, %8
br i1 %9, label %10, label %22
10: ; preds = %1
%11 = load i32, i32* %2
%12 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i32, i32* @RLIMIT_NOFILE
%15 = call i64 @setrlimit(i32 %14, %struct.rlimit* %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @errno
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %10
br label %22
22: ; preds = %21, %1
%23 = load i32, i32* @stderr
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %25)
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
@stderr = global i32 0
@.str.1 = [33 x i8] c"Max file descriptors set to: %d\0A\00"
define void @check_and_set_max_fd(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 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%7 = load i32, i32* %6
%8 = icmp slt i32 %7, %0
br i1 %8, label %9, label %20
9: ; preds = %1
%10 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 1
store i32 %0, i32* %10
store i32 %0, i32* %6
%11 = load i32, i32* @RLIMIT_NOFILE
%12 = call i64 @setrlimit(i32 %11, %struct.rlimit* %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = load i32, i32* %6
br label %20
16: ; preds = %9
%17 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @errno
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %14, %1
%21 = phi i32 [ %15, %14 ], [ %7, %1 ]
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %21)
ret void
}