#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;
  scalar_t__ rlim_cur;
};

/* Variables and functions */
scalar_t__ EPERM;
int /*<<< orphan*/ RLIMIT_NOFILE;
scalar_t__ errno;
int /*<<< orphan*/ fatal(char *, ...);
scalar_t__ geteuid();
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
scalar_t__ getuid();
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ strerror(scalar_t__);
void increment_nofile_rlimit() {
  struct rlimit r;
  if (getrlimit(RLIMIT_NOFILE, &r))
    fatal("increment_nofile_rlimit: getrlimit (%s)", strerror(errno));
  r.rlim_cur = r.rlim_max + 1;
  r.rlim_max = r.rlim_max + 1;
  if (setrlimit(RLIMIT_NOFILE, &r)) {
    if (errno == EPERM) {
      if (getuid() == 0 || geteuid() == 0)
        fatal("system open file limit reached");
      fatal("open file limit reached and unable to increase limit. retry as "
            "root");
    }
    fatal("increment_nofile_rlimit: setrlimit (%s)", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<increment_nofile_rlimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1060 <getrlimit@plt>
cmp    $0x0,%rax
je     11a3 <increment_nofile_rlimit+0x43>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1080 <fatal@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1050 <setrlimit@plt>
cmp    $0x0,%rax
je     124e <increment_nofile_rlimit+0xee>
mov    0x2dfa(%rip),%rax
mov    (%rax),%rax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%rax
jne    122f <increment_nofile_rlimit+0xcf>
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
je     1213 <increment_nofile_rlimit+0xb3>
mov    $0x0,%al
call   1040 <geteuid@plt>
cmp    $0x0,%rax
jne    1221 <increment_nofile_rlimit+0xc1>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1080 <fatal@plt>
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1080 <fatal@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1080 <fatal@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<increment_nofile_rlimit>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e7c(%rip),%rbx
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1060 <getrlimit@plt>
test   %rax,%rax
je     119a <increment_nofile_rlimit+0x3a>
mov    0x2e56(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
lea    0xe6f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <fatal@plt>
mov    (%rsp),%rax
add    $0x1,%rax
mov    %rax,0x8(%rsp)
mov    %rax,(%rsp)
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1050 <setrlimit@plt>
test   %rax,%rax
je     121c <increment_nofile_rlimit+0xbc>
mov    0x2e17(%rip),%rbx
mov    (%rbx),%rdi
mov    0x2dfd(%rip),%rax
cmp    (%rax),%rdi
jne    1207 <increment_nofile_rlimit+0xa7>
xor    %eax,%eax
call   1030 <getuid@plt>
test   %rax,%rax
je     11e8 <increment_nofile_rlimit+0x88>
xor    %eax,%eax
call   1040 <geteuid@plt>
test   %rax,%rax
jne    11f6 <increment_nofile_rlimit+0x96>
lea    0xe39(%rip),%rdi
xor    %eax,%eax
call   1080 <fatal@plt>
lea    0xe4a(%rip),%rdi
xor    %eax,%eax
call   1080 <fatal@plt>
mov    (%rbx),%rdi
call   1070 <strerror@plt>
lea    0xe78(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <fatal@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@RLIMIT_NOFILE = global i32 0
@.str = [40 x i8] c"increment_nofile_rlimit: getrlimit (%s)\00"
@errno = global i64 0
@EPERM = global i64 0
@.str.1 = [31 x i8] c"system open file limit reached\00"
@.str.2 = [68 x i8] c"open file limit reached and unable to increase limit. retry as root\00"
@.str.3 = [40 x i8] c"increment_nofile_rlimit: setrlimit (%s)\00"
define void @increment_nofile_rlimit() {
%1 = alloca %struct.rlimit
%2 = load i32, i32* @RLIMIT_NOFILE
%3 = call i64 @getrlimit(i32 %2, %struct.rlimit* %1)
%4 = icmp ne i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i64, i64* @errno
%7 = call i32 @strerror(i64 %6)
%8 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %5, %0
%10 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
%11 = load i64, i64* %10
%12 = add nsw i64 %11, 1
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 1
store i64 %12, i64* %13
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
%15 = load i64, i64* %14
%16 = add nsw i64 %15, 1
%17 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
store i64 %16, i64* %17
%18 = load i32, i32* @RLIMIT_NOFILE
%19 = call i64 @setrlimit(i32 %18, %struct.rlimit* %1)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %39
21: ; preds = %9
%22 = load i64, i64* @errno
%23 = load i64, i64* @EPERM
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %35
25: ; preds = %21
%26 = call i64 (...) @getuid()
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = call i64 (...) @geteuid()
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28, %25
%32 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %21
%36 = load i64, i64* @errno
%37 = call i32 @strerror(i64 %36)
%38 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %35, %9
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@RLIMIT_NOFILE = global i32 0
@.str = [40 x i8] c"increment_nofile_rlimit: getrlimit (%s)\00"
@errno = global i64 0
@EPERM = global i64 0
@.str.1 = [31 x i8] c"system open file limit reached\00"
@.str.2 = [68 x i8] c"open file limit reached and unable to increase limit. retry as root\00"
@.str.3 = [40 x i8] c"increment_nofile_rlimit: setrlimit (%s)\00"
define void @increment_nofile_rlimit() {
%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 eq i64 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %0
%7 = load i64, i64* @errno
%8 = call i32 @strerror(i64 %7)
%9 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %6, %0
%11 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%12 = load i64, i64* %11
%13 = add nsw i64 %12, 1
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
store i64 %13, i64* %14
store i64 %13, i64* %11
%15 = load i32, i32* @RLIMIT_NOFILE
%16 = call i64 @setrlimit(i32 %15, %struct.rlimit* %1)
%17 = icmp eq i64 %16, 0
br i1 %17, label %37, label %18
18: ; preds = %10
%19 = load i64, i64* @errno
%20 = load i64, i64* @EPERM
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %33
22: ; preds = %18
%23 = call i64 (...) @getuid()
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = call i64 (...) @geteuid()
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25, %22
%29 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0))
%32 = load i64, i64* @errno
br label %33
33: ; preds = %30, %18
%34 = phi i64 [ %32, %30 ], [ %19, %18 ]
%35 = call i32 @strerror(i64 %34)
%36 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %10
ret void
}