#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;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ printf(char *, int, int);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  struct rlimit rlim = {0};
  if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
    fprintf(stderr, "Error getting fd limit!\n");
    exit(1);
  }
  printf("Fd limits (soft == %d, hard == %d)\n", rlim.rlim_cur, rlim.rlim_max);
  rlim.rlim_cur = 4096;
  rlim.rlim_max = 8192;
  if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
    fprintf(stderr, "Failed to set fd limits!\n");
    exit(1);
  }
  if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
    fprintf(stderr, "Error getting fd limit!\n");
    exit(1);
  }
  printf("Fd limits (soft == %d, hard == %d)\n", rlim.rlim_cur, rlim.rlim_max);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1080 <getrlimit@plt>
cmp    $0x0,%rax
jge    11c1 <main+0x61>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe4b(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1000,-0x20(%rbp)
movl   $0x2000,-0x1c(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1060 <setrlimit@plt>
cmp    $0x0,%rax
jge    121c <main+0xbc>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1080 <getrlimit@plt>
cmp    $0x0,%rax
jge    1257 <main+0xf7>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xdb5(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
mov    0x2e7c(%rip),%rbx
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1070 <getrlimit@plt>
test   %rax,%rax
js     11d5 <main+0x85>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
lea    0xe90(%rip),%rdi
call   1030 <printf@plt>
movabs $0x200000001000,%rax
mov    %rax,(%rsp)
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1050 <setrlimit@plt>
test   %rax,%rax
js     11e7 <main+0x97>
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1070 <getrlimit@plt>
test   %rax,%rax
js     11d5 <main+0x85>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
lea    0xe51(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
jmp    11f7 <main+0xa7>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Error getting fd limit!\0A\00"
@.str.1 = [36 x i8] c"Fd limits (soft == %d, hard == %d)\0A\00"
@.str.2 = [26 x i8] c"Failed to set fd limits!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.rlimit
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = bitcast %struct.rlimit* %6 to i8*
%8 = load i32, i32* @RLIMIT_NOFILE
%9 = call i64 @getrlimit(i32 %8, %struct.rlimit* %6)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
%19 = load i32, i32* %18
%20 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %19)
%21 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
store i32 4096, i32* %21
%22 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
store i32 8192, i32* %22
%23 = load i32, i32* @RLIMIT_NOFILE
%24 = call i64 @setrlimit(i32 %23, %struct.rlimit* %6)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %15
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %15
%31 = load i32, i32* @RLIMIT_NOFILE
%32 = call i64 @getrlimit(i32 %31, %struct.rlimit* %6)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
%42 = load i32, i32* %41
%43 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %42)
ret i32 0
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Error getting fd limit!\0A\00"
@.str.1 = [36 x i8] c"Fd limits (soft == %d, hard == %d)\0A\00"
@.str.2 = [26 x i8] c"Failed to set fd limits!\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.rlimit
%4 = bitcast %struct.rlimit* %3 to i8*
%5 = load i32, i32* @RLIMIT_NOFILE
%6 = call i64 @getrlimit(i32 %5, %struct.rlimit* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 1
%16 = load i32, i32* %15
%17 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %16)
store i32 4096, i32* %13
store i32 8192, i32* %15
%18 = load i32, i32* @RLIMIT_NOFILE
%19 = call i64 @setrlimit(i32 %18, %struct.rlimit* %3)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %12
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %12
%26 = load i32, i32* @RLIMIT_NOFILE
%27 = call i64 @getrlimit(i32 %26, %struct.rlimit* %3)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %25
%34 = load i32, i32* %13
%35 = load i32, i32* %15
%36 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %34, i32 %35)
ret i32 0
}