#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*/ _SC_OPEN_MAX;
int /*<<< orphan*/ errno;
int getrlimit(int /*<<< orphan*/, struct rlimit *);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ svz_log(int, char *, int, ...);
long sysconf(int /*<<< orphan*/);
int svz_openfiles(int max_sockets) {
  struct rlimit rlim;
  long openfiles;
  if ((openfiles = sysconf(_SC_OPEN_MAX)) == -1) {
    svz_log(1, "getdtablesize: %s\n", strerror(errno));
    return -1;
  }
  svz_log(3, "file descriptor table size: %d\n", openfiles);
  if (getrlimit(RLIMIT_NOFILE, &rlim) == -1) {
    svz_log(1, "getrlimit: %s\n", strerror(errno));
    return -1;
  }
  svz_log(3, "current open file limit: %d/%d\n", rlim.rlim_cur, rlim.rlim_max);
  if ((int)rlim.rlim_max < (int)max_sockets ||
      (int)rlim.rlim_cur < (int)max_sockets) {
    rlim.rlim_max = max_sockets;
    rlim.rlim_cur = max_sockets;
    if (setrlimit(RLIMIT_NOFILE, &rlim) == -1) {
      svz_log(1, "setrlimit: %s\n", strerror(errno));
      return -1;
    }
    getrlimit(RLIMIT_NOFILE, &rlim);
    svz_log(3, "open file limit set to: %d/%d\n", rlim.rlim_cur, rlim.rlim_max);
  }
  return 0;
}


x86 assembly (clang, -O0):
<svz_openfiles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,-0x18(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    11a6 <svz_openfiles+0x56>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe6d(%rip),%rsi
mov    $0x0,%al
call   1070 <svz_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <svz_openfiles+0x172>
mov    -0x18(%rbp),%rax
mov    %eax,%edx
mov    $0x3,%edi
lea    0xe5b(%rip),%rsi
mov    $0x0,%al
call   1070 <svz_log@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1050 <getrlimit@plt>
cmp    $0xffffffff,%eax
jne    1209 <svz_openfiles+0xb9>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1070 <svz_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <svz_openfiles+0x172>
mov    -0x10(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    $0x3,%edi
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1070 <svz_log@plt>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     123a <svz_openfiles+0xea>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12bb <svz_openfiles+0x16b>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <setrlimit@plt>
cmp    $0xffffffff,%eax
jne    1290 <svz_openfiles+0x140>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   1070 <svz_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <svz_openfiles+0x172>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    -0x10(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    $0x3,%edi
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1070 <svz_log@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<svz_openfiles>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
cmp    $0xffffffffffffffff,%rax
je     1217 <svz_openfiles+0xc7>
lea    0xe9a(%rip),%rsi
xor    %ebx,%ebx
mov    $0x3,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <svz_log@plt>
mov    0x2e58(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1050 <getrlimit@plt>
cmp    $0xffffffff,%eax
je     122e <svz_openfiles+0xde>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%ecx
lea    0xe8d(%rip),%rsi
mov    $0x3,%edi
xor    %eax,%eax
call   1070 <svz_log@plt>
cmp    %ebp,0xc(%rsp)
jl     11d1 <svz_openfiles+0x81>
cmp    %ebp,0x8(%rsp)
jge    126d <svz_openfiles+0x11d>
mov    %ebp,0xc(%rsp)
mov    %ebp,0x8(%rsp)
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1030 <setrlimit@plt>
cmp    $0xffffffff,%eax
je     1245 <svz_openfiles+0xf5>
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1050 <getrlimit@plt>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%ecx
lea    0xe6a(%rip),%rsi
xor    %ebx,%ebx
mov    $0x3,%edi
xor    %eax,%eax
call   1070 <svz_log@plt>
jmp    126d <svz_openfiles+0x11d>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xdd4(%rip),%rsi
jmp    125a <svz_openfiles+0x10a>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xdf0(%rip),%rsi
jmp    125a <svz_openfiles+0x10a>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe08(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <svz_log@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@_SC_OPEN_MAX = global i32 0
@.str = [19 x i8] c"getdtablesize: %s\0A\00"
@errno = global i32 0
@.str.1 = [32 x i8] c"file descriptor table size: %d\0A\00"
@RLIMIT_NOFILE = global i32 0
@.str.2 = [15 x i8] c"getrlimit: %s\0A\00"
@.str.3 = [32 x i8] c"current open file limit: %d/%d\0A\00"
@.str.4 = [15 x i8] c"setrlimit: %s\0A\00"
@.str.5 = [31 x i8] c"open file limit set to: %d/%d\0A\00"
define i32 @svz_openfiles(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.rlimit
%5 = alloca i64
store i32 %0, i32* %3
%6 = load i32, i32* @_SC_OPEN_MAX
%7 = call i64 @sysconf(i32 %6)
store i64 %7, i64* %5
%8 = icmp eq i64 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = call i32 @strerror(i32 %10)
%12 = call i32 (i32, i8*, i32, ...) @svz_log(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 -1, i32* %2
br label %60
13: ; preds = %1
%14 = load i64, i64* %5
%15 = trunc i64 %14 to i32
%16 = call i32 (i32, i8*, i32, ...) @svz_log(i32 3, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @RLIMIT_NOFILE
%18 = call i32 @getrlimit(i32 %17, %struct.rlimit* %4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 (i32, i8*, i32, ...) @svz_log(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %22)
store i32 -1, i32* %2
br label %60
24: ; preds = %13
%25 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 (i32, i8*, i32, ...) @svz_log(i32 3, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %28)
%30 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%31 = load i32, i32* %30
%32 = load i32, i32* %3
%33 = icmp slt i32 %31, %32
br i1 %33, label %39, label %34
34: ; preds = %24
%35 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* %3
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %59
39: ; preds = %34, %24
%40 = load i32, i32* %3
%41 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
store i32 %40, i32* %41
%42 = load i32, i32* %3
%43 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
store i32 %42, i32* %43
%44 = load i32, i32* @RLIMIT_NOFILE
%45 = call i32 @setrlimit(i32 %44, %struct.rlimit* %4)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %39
%48 = load i32, i32* @errno
%49 = call i32 @strerror(i32 %48)
%50 = call i32 (i32, i8*, i32, ...) @svz_log(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %49)
store i32 -1, i32* %2
br label %60
51: ; preds = %39
%52 = load i32, i32* @RLIMIT_NOFILE
%53 = call i32 @getrlimit(i32 %52, %struct.rlimit* %4)
%54 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%55 = load i32, i32* %54
%56 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%57 = load i32, i32* %56
%58 = call i32 (i32, i8*, i32, ...) @svz_log(i32 3, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %55, i32 %57)
br label %59
59: ; preds = %51, %34
store i32 0, i32* %2
br label %60
60: ; preds = %59, %47, %20, %9
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@_SC_OPEN_MAX = global i32 0
@.str = [19 x i8] c"getdtablesize: %s\0A\00"
@errno = global i32 0
@.str.1 = [32 x i8] c"file descriptor table size: %d\0A\00"
@RLIMIT_NOFILE = global i32 0
@.str.2 = [15 x i8] c"getrlimit: %s\0A\00"
@.str.3 = [32 x i8] c"current open file limit: %d/%d\0A\00"
@.str.4 = [15 x i8] c"setrlimit: %s\0A\00"
@.str.5 = [31 x i8] c"open file limit set to: %d/%d\0A\00"
define i32 @svz_openfiles(i32 %0) {
%2 = alloca %struct.rlimit
%3 = bitcast %struct.rlimit* %2 to i8*
%4 = load i32, i32* @_SC_OPEN_MAX
%5 = tail call i64 @sysconf(i32 %4)
%6 = icmp eq i64 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 (i32, i8*, i32, ...) @svz_log(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %46
11: ; preds = %1
%12 = trunc i64 %5 to i32
%13 = tail call i32 (i32, i8*, i32, ...) @svz_log(i32 3, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @RLIMIT_NOFILE
%15 = call i32 @getrlimit(i32 %14, %struct.rlimit* %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 (i32, i8*, i32, ...) @svz_log(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %46
21: ; preds = %11
%22 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 1
%25 = load i32, i32* %24
%26 = call i32 (i32, i8*, i32, ...) @svz_log(i32 3, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 %25)
%27 = load i32, i32* %24
%28 = icmp slt i32 %27, %0
%29 = load i32, i32* %22
%30 = icmp slt i32 %29, %0
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %32, label %46
32: ; preds = %21
store i32 %0, i32* %24
store i32 %0, i32* %22
%33 = load i32, i32* @RLIMIT_NOFILE
%34 = call i32 @setrlimit(i32 %33, %struct.rlimit* %2)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i32, i32* @errno
%38 = call i32 @strerror(i32 %37)
%39 = call i32 (i32, i8*, i32, ...) @svz_log(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %38)
br label %46
40: ; preds = %32
%41 = load i32, i32* @RLIMIT_NOFILE
%42 = call i32 @getrlimit(i32 %41, %struct.rlimit* %2)
%43 = load i32, i32* %22
%44 = load i32, i32* %24
%45 = call i32 (i32, i8*, i32, ...) @svz_log(i32 3, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %43, i32 %44)
br label %46
46: ; preds = %40, %21, %36, %17, %7
%47 = phi i32 [ -1, %7 ], [ -1, %17 ], [ -1, %36 ], [ 0, %21 ], [ 0, %40 ]
ret i32 %47
}