#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 {
  unsigned int rlim_cur;
  unsigned int rlim_max;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
int /*<<< orphan*/ log_emerg(char *, int /*<<< orphan*/);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void change_limits(void) {
  FILE *f;
  struct rlimit lim;
  unsigned int nr_open = 1024 * 1024;
  f = fopen("/proc/sys/fs/nr_open", "r");
  if (f) {
    fscanf(f, "%d", &nr_open);
    fclose(f);
  }
  lim.rlim_cur = nr_open;
  lim.rlim_max = nr_open;
  if (setrlimit(RLIMIT_NOFILE, &lim))
    log_emerg("main: setrlimit: %s\n", strerror(errno));
}


x86 assembly (clang, -O0):
<change_limits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x100000,-0x14(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe98(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11ae <change_limits+0x4e>
mov    -0x8(%rbp),%rdi
lea    0xe7b(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1060 <setrlimit@plt>
cmp    $0x0,%rax
je     11f2 <change_limits+0x92>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe2d(%rip),%rdi
call   1040 <log_emerg@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_limits>:
push   %rbx
sub    $0x10,%rsp
movl   $0x100000,0x4(%rsp)
lea    0xe8c(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11aa <change_limits+0x4a>
mov    %rax,%rbx
lea    0xe88(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x4(%rsp),%eax
jmp    11af <change_limits+0x4f>
mov    $0x100000,%eax
mov    %eax,0x8(%rsp)
mov    %eax,0xc(%rsp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1060 <setrlimit@plt>
test   %rax,%rax
je     11eb <change_limits+0x8b>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe36(%rip),%rdi
mov    %eax,%esi
call   1040 <log_emerg@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@.str = [21 x i8] c"/proc/sys/fs/nr_open\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@RLIMIT_NOFILE = global i32 0
@.str.3 = [21 x i8] c"main: setrlimit: %s\0A\00"
@errno = global i32 0
define void @change_limits() {
%1 = alloca i32*
%2 = alloca %struct.rlimit
%3 = alloca i32
store i32 1048576, i32* %3
%4 = call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %1
%5 = load i32*, i32** %1
%6 = icmp ne i32* %5, null
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32*, i32** %1
%9 = call i32 @fscanf(i32* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%10 = load i32*, i32** %1
%11 = call i32 @fclose(i32* %10)
br label %12
12: ; preds = %7, %0
%13 = load i32, i32* %3
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* %3
%16 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @RLIMIT_NOFILE
%18 = call i64 @setrlimit(i32 %17, %struct.rlimit* %2)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %12
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @log_emerg(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %20, %12
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@.str = [21 x i8] c"/proc/sys/fs/nr_open\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@RLIMIT_NOFILE = global i32 0
@.str.3 = [21 x i8] c"main: setrlimit: %s\0A\00"
@errno = global i32 0
define void @change_limits() {
%1 = alloca %struct.rlimit
%2 = alloca i32
%3 = bitcast %struct.rlimit* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 1048576, i32* %2
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%9 = call i32 @fclose(i32* %5)
%10 = load i32, i32* %2
br label %11
11: ; preds = %7, %0
%12 = phi i32 [ %10, %7 ], [ 1048576, %0 ]
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
store i32 %12, i32* %14
%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 %22, label %18
18: ; preds = %11
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @log_emerg(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %18, %11
ret void
}