#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 {
  long rlim_cur;
  long rlim_max;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_DATA;
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ RLIMIT_STACK;
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ printf(char *, long, long);
int /*<<< orphan*/ setrlimit(int /*<<< orphan*/, struct rlimit *);
long srv_capacity(void) {
  static long flim = 0;
  if (flim)
    return flim;
  struct rlimit rlim;
  getrlimit(RLIMIT_NOFILE, &rlim);
  rlim.rlim_cur = rlim.rlim_max;
  setrlimit(RLIMIT_NOFILE, &rlim);
  getrlimit(RLIMIT_NOFILE, &rlim);
  if (flim < rlim.rlim_cur)
    flim = rlim.rlim_cur;
  getrlimit(RLIMIT_STACK, &rlim);
  if (flim * 4096 > rlim.rlim_cur && flim * 4096 < rlim.rlim_max) {
    rlim.rlim_cur = flim * 4096;
    setrlimit(RLIMIT_STACK, &rlim);
    getrlimit(RLIMIT_STACK, &rlim);
  }
  if (flim > rlim.rlim_cur / 4096) {
    flim = rlim.rlim_cur / 4096;
  }
  getrlimit(RLIMIT_DATA, &rlim);
  if (flim > rlim.rlim_cur / (24 * 1024)) {
    printf("The current maximum file limit (%ld) if reduced due to system "
           "memory "
           "limits(%ld)\n",
           flim, (long)(rlim.rlim_cur / (24 * 1024)));
    flim = rlim.rlim_cur / (24 * 1024);
  } else {
  }
  return flim;
}


x86 assembly (clang, -O0):
<srv_capacity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
cmpq   $0x0,0x2f00(%rip)
je     1156 <srv_capacity+0x26>
mov    0x2ef3(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12e6 <srv_capacity+0x1b6>
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <setrlimit@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    0x2ea5(%rip),%rax
cmp    -0x18(%rbp),%rax
jge    11b0 <srv_capacity+0x80>
mov    -0x18(%rbp),%rax
mov    %rax,0x2e90(%rip)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    0x2e77(%rip),%rax
shl    $0xc,%rax
cmp    -0x18(%rbp),%rax
jle    121f <srv_capacity+0xef>
mov    0x2e62(%rip),%rax
shl    $0xc,%rax
cmp    -0x10(%rbp),%rax
jge    121f <srv_capacity+0xef>
mov    0x2e4d(%rip),%rax
shl    $0xc,%rax
mov    %rax,-0x18(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <setrlimit@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    0x2e1a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x1000,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jle    125d <srv_capacity+0x12d>
mov    -0x18(%rbp),%rax
mov    $0x1000,%ecx
cqto
idiv   %rcx
mov    %rax,0x2de3(%rip)
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    0x2dca(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x6000,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jle    12d6 <srv_capacity+0x1a6>
mov    0x2da1(%rip),%rsi
mov    -0x18(%rbp),%rax
mov    $0x6000,%ecx
cqto
idiv   %rcx
mov    %rax,%rdx
lea    0xd49(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    $0x6000,%ecx
cqto
idiv   %rcx
mov    %rax,0x2d6f(%rip)
jmp    12db <srv_capacity+0x1ab>
jmp    12db <srv_capacity+0x1ab>
mov    0x2d5e(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<srv_capacity>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2f02(%rip),%rsi
test   %rsi,%rsi
jne    127c <srv_capacity+0x14c>
mov    0x2e92(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1050 <getrlimit@plt>
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    (%r14),%edi
mov    %rbx,%rsi
call   1040 <setrlimit@plt>
mov    (%r14),%edi
mov    %rbx,%rsi
call   1050 <getrlimit@plt>
mov    0x8(%rsp),%rax
cmp    %rax,0x2eb6(%rip)
jge    1193 <srv_capacity+0x63>
mov    %rax,0x2ead(%rip)
mov    0x2e36(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1050 <getrlimit@plt>
mov    0x2e93(%rip),%rax
mov    %rax,%rdx
shl    $0xc,%rdx
mov    0x8(%rsp),%rcx
cmp    %rcx,%rdx
jle    11ef <srv_capacity+0xbf>
cmp    0x10(%rsp),%rdx
jge    11ef <srv_capacity+0xbf>
mov    %rdx,0x8(%rsp)
mov    (%rbx),%edi
lea    0x8(%rsp),%r14
mov    %r14,%rsi
call   1040 <setrlimit@plt>
mov    (%rbx),%edi
mov    %r14,%rsi
call   1050 <getrlimit@plt>
mov    0x2e56(%rip),%rax
mov    0x8(%rsp),%rcx
lea    0xfff(%rcx),%rdx
test   %rcx,%rcx
cmovns %rcx,%rdx
sar    $0xc,%rdx
cmp    %rdx,%rax
jle    120d <srv_capacity+0xdd>
mov    %rdx,0x2e33(%rip)
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1050 <getrlimit@plt>
mov    0x2e19(%rip),%rsi
movabs $0x2aaaaaaaaaaaaaab,%rax
aa aa 2a
imulq  0x8(%rsp)
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0xc,%rdx
add    %rax,%rdx
cmp    %rdx,%rsi
jle    127c <srv_capacity+0x14c>
lea    0xdb0(%rip),%rdi
call   1030 <printf@plt>
movabs $0x2aaaaaaaaaaaaaab,%rax
aa aa 2a
imulq  0x8(%rsp)
mov    %rdx,%rsi
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0xc,%rsi
add    %rax,%rsi
mov    %rsi,0x2dc4(%rip)
mov    %rsi,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@srv_capacity.flim = internal global i64 0
@RLIMIT_NOFILE = global i32 0
@RLIMIT_STACK = global i32 0
@RLIMIT_DATA = global i32 0
@.str = [82 x i8] c"The current maximum file limit (%ld) if reduced due to system memory limits(%ld)\0A\00"
define i64 @srv_capacity() {
%1 = alloca i64
%2 = alloca %struct.rlimit
%3 = load i64, i64* @srv_capacity.flim
%4 = icmp ne i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = load i64, i64* @srv_capacity.flim
store i64 %6, i64* %1
br label %76
7: ; preds = %0
%8 = load i32, i32* @RLIMIT_NOFILE
%9 = call i32 @getrlimit(i32 %8, %struct.rlimit* %2)
%10 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 1
%11 = load i64, i64* %10
%12 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
store i64 %11, i64* %12
%13 = load i32, i32* @RLIMIT_NOFILE
%14 = call i32 @setrlimit(i32 %13, %struct.rlimit* %2)
%15 = load i32, i32* @RLIMIT_NOFILE
%16 = call i32 @getrlimit(i32 %15, %struct.rlimit* %2)
%17 = load i64, i64* @srv_capacity.flim
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%19 = load i64, i64* %18
%20 = icmp slt i64 %17, %19
br i1 %20, label %21, label %24
21: ; preds = %7
%22 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%23 = load i64, i64* %22
store i64 %23, i64* @srv_capacity.flim
br label %24
24: ; preds = %21, %7
%25 = load i32, i32* @RLIMIT_STACK
%26 = call i32 @getrlimit(i32 %25, %struct.rlimit* %2)
%27 = load i64, i64* @srv_capacity.flim
%28 = mul nsw i64 %27, 4096
%29 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%30 = load i64, i64* %29
%31 = icmp sgt i64 %28, %30
br i1 %31, label %32, label %46
32: ; preds = %24
%33 = load i64, i64* @srv_capacity.flim
%34 = mul nsw i64 %33, 4096
%35 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 1
%36 = load i64, i64* %35
%37 = icmp slt i64 %34, %36
br i1 %37, label %38, label %46
38: ; preds = %32
%39 = load i64, i64* @srv_capacity.flim
%40 = mul nsw i64 %39, 4096
%41 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
store i64 %40, i64* %41
%42 = load i32, i32* @RLIMIT_STACK
%43 = call i32 @setrlimit(i32 %42, %struct.rlimit* %2)
%44 = load i32, i32* @RLIMIT_STACK
%45 = call i32 @getrlimit(i32 %44, %struct.rlimit* %2)
br label %46
46: ; preds = %38, %32, %24
%47 = load i64, i64* @srv_capacity.flim
%48 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%49 = load i64, i64* %48
%50 = sdiv i64 %49, 4096
%51 = icmp sgt i64 %47, %50
br i1 %51, label %52, label %56
52: ; preds = %46
%53 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%54 = load i64, i64* %53
%55 = sdiv i64 %54, 4096
store i64 %55, i64* @srv_capacity.flim
br label %56
56: ; preds = %52, %46
%57 = load i32, i32* @RLIMIT_DATA
%58 = call i32 @getrlimit(i32 %57, %struct.rlimit* %2)
%59 = load i64, i64* @srv_capacity.flim
%60 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%61 = load i64, i64* %60
%62 = sdiv i64 %61, 24576
%63 = icmp sgt i64 %59, %62
br i1 %63, label %64, label %73
64: ; preds = %56
%65 = load i64, i64* @srv_capacity.flim
%66 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%67 = load i64, i64* %66
%68 = sdiv i64 %67, 24576
%69 = call i32 @printf(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str, i64 0, i64 0), i64 %65, i64 %68)
%70 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%71 = load i64, i64* %70
%72 = sdiv i64 %71, 24576
store i64 %72, i64* @srv_capacity.flim
br label %74
73: ; preds = %56
br label %74
74: ; preds = %73, %64
%75 = load i64, i64* @srv_capacity.flim
store i64 %75, i64* %1
br label %76
76: ; preds = %74, %5
%77 = load i64, i64* %1
ret i64 %77
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@srv_capacity.flim = internal unnamed_addr global i64 0
@RLIMIT_NOFILE = global i32 0
@RLIMIT_STACK = global i32 0
@RLIMIT_DATA = global i32 0
@.str = [82 x i8] c"The current maximum file limit (%ld) if reduced due to system memory limits(%ld)\0A\00"
define i64 @srv_capacity() {
%1 = alloca %struct.rlimit
%2 = load i64, i64* @srv_capacity.flim
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %55
4: ; preds = %0
%5 = bitcast %struct.rlimit* %1 to i8*
%6 = load i32, i32* @RLIMIT_NOFILE
%7 = call i32 @getrlimit(i32 %6, %struct.rlimit* %1)
%8 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
%9 = load i64, i64* %8
%10 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
store i64 %9, i64* %10
%11 = load i32, i32* @RLIMIT_NOFILE
%12 = call i32 @setrlimit(i32 %11, %struct.rlimit* %1)
%13 = load i32, i32* @RLIMIT_NOFILE
%14 = call i32 @getrlimit(i32 %13, %struct.rlimit* %1)
%15 = load i64, i64* @srv_capacity.flim
%16 = load i64, i64* %10
%17 = icmp slt i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %4
store i64 %16, i64* @srv_capacity.flim
br label %19
19: ; preds = %18, %4
%20 = load i32, i32* @RLIMIT_STACK
%21 = call i32 @getrlimit(i32 %20, %struct.rlimit* %1)
%22 = load i64, i64* @srv_capacity.flim
%23 = shl nsw i64 %22, 12
%24 = load i64, i64* %10
%25 = icmp sgt i64 %23, %24
%26 = load i64, i64* %8
%27 = icmp slt i64 %23, %26
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %29, label %36
29: ; preds = %19
store i64 %23, i64* %10
%30 = load i32, i32* @RLIMIT_STACK
%31 = call i32 @setrlimit(i32 %30, %struct.rlimit* %1)
%32 = load i32, i32* @RLIMIT_STACK
%33 = call i32 @getrlimit(i32 %32, %struct.rlimit* %1)
%34 = load i64, i64* @srv_capacity.flim
%35 = load i64, i64* %10
br label %36
36: ; preds = %29, %19
%37 = phi i64 [ %35, %29 ], [ %24, %19 ]
%38 = phi i64 [ %34, %29 ], [ %22, %19 ]
%39 = sdiv i64 %37, 4096
%40 = icmp sgt i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %36
store i64 %39, i64* @srv_capacity.flim
br label %42
42: ; preds = %41, %36
%43 = load i32, i32* @RLIMIT_DATA
%44 = call i32 @getrlimit(i32 %43, %struct.rlimit* %1)
%45 = load i64, i64* @srv_capacity.flim
%46 = load i64, i64* %10
%47 = sdiv i64 %46, 24576
%48 = icmp sgt i64 %45, %47
br i1 %48, label %49, label %53
49: ; preds = %42
%50 = call i32 @printf(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str, i64 0, i64 0), i64 %45, i64 %47)
%51 = load i64, i64* %10
%52 = sdiv i64 %51, 24576
store i64 %52, i64* @srv_capacity.flim
br label %53
53: ; preds = %42, %49
%54 = phi i64 [ %45, %42 ], [ %52, %49 ]
br label %55
55: ; preds = %0, %53
%56 = phi i64 [ %54, %53 ], [ %2, %0 ]
ret i64 %56
}