#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*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setrlimit(int /*<<< orphan*/, struct rlimit *);
int main(int argc, char *argv[]) {
  int soft_nofile;
  int hard_nofile;
  int ref_nofile = 2048;
  int MAX_NOFILE = 65535;
  struct rlimit rl;
  getrlimit(RLIMIT_NOFILE, &rl);
  soft_nofile = rl.rlim_cur;
  printf("The current number of open files is: %d\n", soft_nofile);
  if (soft_nofile < ref_nofile) {
    printf("Current number of open files is not satisfied\n");
    printf("It will be changed to %d.\n", MAX_NOFILE);
    rl.rlim_cur = MAX_NOFILE / 2;
    rl.rlim_max = MAX_NOFILE;
    setrlimit(RLIMIT_NOFILE, &rl);
  } else {
    printf("nofile is passed\n");
  }
  getrlimit(RLIMIT_NOFILE, &rl);
  printf("nofile is %ld\n", rl.rlim_cur);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x800,-0x1c(%rbp)
movl   $0xffff,-0x20(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11d3 <main+0xa3>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%esi
lea    0xeb7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1040 <setrlimit@plt>
jmp    11e1 <main+0xb1>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    -0x28(%rbp),%esi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1060 <getrlimit@plt>
mov    (%rsp),%ebx
lea    0xea0(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x7ff,%ebx
jg     11ab <main+0x6b>
lea    0xeec(%rip),%rdi
call   1030 <puts@plt>
lea    0xea5(%rip),%rdi
mov    $0xffff,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movabs $0xffff00007fff,%rax
mov    %rax,(%rsp)
mov    (%r14),%edi
mov    %rsp,%rsi
call   1050 <setrlimit@plt>
jmp    11b7 <main+0x77>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%edi
mov    %rsp,%rsi
call   1060 <getrlimit@plt>
mov    (%rsp),%esi
lea    0xe78(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [41 x i8] c"The current number of open files is: %d\0A\00"
@.str.1 = [47 x i8] c"Current number of open files is not satisfied\0A\00"
@.str.2 = [27 x i8] c"It will be changed to %d.\0A\00"
@.str.3 = [18 x i8] c"nofile is passed\0A\00"
@.str.4 = [15 x i8] c"nofile is %ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.rlimit
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 2048, i32* %8
store i32 65535, i32* %9
%11 = load i32, i32* @RLIMIT_NOFILE
%12 = call i32 @getrlimit(i32 %11, %struct.rlimit* %10)
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %10, i32 0, i32 0
%14 = load i32, i32* %13
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %6
%18 = load i32, i32* %8
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %31
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %9
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %9
%25 = sdiv i32 %24, 2
%26 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %10, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %9
%28 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %10, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* @RLIMIT_NOFILE
%30 = call i32 @setrlimit(i32 %29, %struct.rlimit* %10)
br label %33
31: ; preds = %2
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %20
%34 = load i32, i32* @RLIMIT_NOFILE
%35 = call i32 @getrlimit(i32 %34, %struct.rlimit* %10)
%36 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %10, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %37)
ret i32 0
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [41 x i8] c"The current number of open files is: %d\0A\00"
@.str.2 = [27 x i8] c"It will be changed to %d.\0A\00"
@.str.4 = [15 x i8] c"nofile is %ld\0A\00"
@str = [17 x i8] c"nofile is passed\00"
@str.5 = [46 x i8] c"Current number of open files is not satisfied\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 i32 @getrlimit(i32 %5, %struct.rlimit* %3)
%7 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
%8 = load i32, i32* %7
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %8, 2048
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.5, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 65535)
store i32 32767, i32* %7
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 1
store i32 65535, i32* %14
%15 = load i32, i32* @RLIMIT_NOFILE
%16 = call i32 @setrlimit(i32 %15, %struct.rlimit* %3)
br label %19
17: ; preds = %2
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = load i32, i32* @RLIMIT_NOFILE
%21 = call i32 @getrlimit(i32 %20, %struct.rlimit* %3)
%22 = load i32, i32* %7
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %22)
ret i32 0
}