#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 {
  scalar_t__ rlim_cur;
  scalar_t__ rlim_max;
};
typedef scalar_t__ rlim_t;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
scalar_t__ RLIM_INFINITY;
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
void raiserlimit(void) {
  {
    struct rlimit lim;
    if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {
      rlim_t min = lim.rlim_cur;
      rlim_t max = 1 << 20;
      if (lim.rlim_max != RLIM_INFINITY) {
        min = lim.rlim_max;
        max = lim.rlim_max;
      }
      do {
        lim.rlim_cur = min + (max - min) / 2;
        if (setrlimit(RLIMIT_NOFILE, &lim)) {
          max = lim.rlim_cur;
        } else {
          min = lim.rlim_cur;
        }
      } while (min + 1 < max);
    }
  }
}


x86 assembly (clang, -O0):
<raiserlimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2eb9(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <getrlimit@plt>
cmp    $0x0,%rax
jne    1200 <raiserlimit+0xe0>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     1200 <raiserlimit+0xe0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x100000,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
je     1186 <raiserlimit+0x66>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    118b <raiserlimit+0x6b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <setrlimit@plt>
cmp    $0x0,%rax
je     11dc <raiserlimit+0xbc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11e4 <raiserlimit+0xc4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11e9 <raiserlimit+0xc9>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
cmp    -0x20(%rbp),%rax
jl     118b <raiserlimit+0x6b>
jmp    1200 <raiserlimit+0xe0>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<raiserlimit>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2eb6(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1040 <getrlimit@plt>
test   %rax,%rax
jne    11b0 <raiserlimit+0x90>
mov    0x8(%rsp),%r12
mov    0x10(%rsp),%rax
cmp    %rax,%r12
je     11b0 <raiserlimit+0x90>
mov    0x2e7e(%rip),%rcx
cmp    (%rcx),%rax
cmovne %rax,%r12
mov    $0x100000,%ebx
cmovne %rax,%rbx
lea    0x8(%rsp),%r15
nop
mov    %rbx,%rax
sub    %r12,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
add    %rax,%rcx
sar    %rcx
add    %r12,%rcx
mov    %rcx,0x8(%rsp)
mov    (%r14),%edi
mov    %r15,%rsi
call   1030 <setrlimit@plt>
test   %rax,%rax
mov    0x8(%rsp),%rax
cmove  %rax,%r12
cmovne %rax,%rbx
lea    0x1(%r12),%rax
cmp    %rbx,%rax
jl     1170 <raiserlimit+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@RLIMIT_NOFILE = global i32 0
@RLIM_INFINITY = global i64 0
define void @raiserlimit() {
%1 = alloca %struct.rlimit
%2 = alloca i64
%3 = alloca i64
%4 = load i32, i32* @RLIMIT_NOFILE
%5 = call i64 @getrlimit(i32 %4, %struct.rlimit* %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %50
7: ; preds = %0
%8 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
%9 = load i64, i64* %8
%10 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 1
%11 = load i64, i64* %10
%12 = icmp ne i64 %9, %11
br i1 %12, label %13, label %50
13: ; preds = %7
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
%15 = load i64, i64* %14
store i64 %15, i64* %2
store i64 1048576, i64* %3
%16 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 1
%17 = load i64, i64* %16
%18 = load i64, i64* @RLIM_INFINITY
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 1
%22 = load i64, i64* %21
store i64 %22, i64* %2
%23 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 1
%24 = load i64, i64* %23
store i64 %24, i64* %3
br label %25
25: ; preds = %20, %13
br label %26
26: ; preds = %44, %25
%27 = load i64, i64* %2
%28 = load i64, i64* %3
%29 = load i64, i64* %2
%30 = sub nsw i64 %28, %29
%31 = sdiv i64 %30, 2
%32 = add nsw i64 %27, %31
%33 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
store i64 %32, i64* %33
%34 = load i32, i32* @RLIMIT_NOFILE
%35 = call i64 @setrlimit(i32 %34, %struct.rlimit* %1)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %26
%38 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
%39 = load i64, i64* %38
store i64 %39, i64* %3
br label %43
40: ; preds = %26
%41 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
%42 = load i64, i64* %41
store i64 %42, i64* %2
br label %43
43: ; preds = %40, %37
br label %44
44: ; preds = %43
%45 = load i64, i64* %2
%46 = add nsw i64 %45, 1
%47 = load i64, i64* %3
%48 = icmp slt i64 %46, %47
br i1 %48, label %26, label %49
49: ; preds = %44
br label %50
50: ; preds = %49, %7, %0
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@RLIMIT_NOFILE = global i32 0
@RLIM_INFINITY = global i64 0
define void @raiserlimit() {
%1 = alloca %struct.rlimit
%2 = bitcast %struct.rlimit* %1 to i8*
%3 = load i32, i32* @RLIMIT_NOFILE
%4 = call i64 @getrlimit(i32 %3, %struct.rlimit* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %31
6: ; preds = %0
%7 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%8 = load i64, i64* %7
%9 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
%10 = load i64, i64* %9
%11 = icmp eq i64 %8, %10
br i1 %11, label %31, label %12
12: ; preds = %6
%13 = load i64, i64* @RLIM_INFINITY
%14 = icmp eq i64 %10, %13
%15 = select i1 %14, i64 %8, i64 %10
%16 = select i1 %14, i64 1048576, i64 %10
br label %17
17: ; preds = %17, %12
%18 = phi i64 [ %15, %12 ], [ %27, %17 ]
%19 = phi i64 [ %16, %12 ], [ %28, %17 ]
%20 = sub nsw i64 %19, %18
%21 = sdiv i64 %20, 2
%22 = add nsw i64 %21, %18
store i64 %22, i64* %7
%23 = load i32, i32* @RLIMIT_NOFILE
%24 = call i64 @setrlimit(i32 %23, %struct.rlimit* %1)
%25 = icmp eq i64 %24, 0
%26 = load i64, i64* %7
%27 = select i1 %25, i64 %26, i64 %18
%28 = select i1 %25, i64 %19, i64 %26
%29 = add nsw i64 %27, 1
%30 = icmp slt i64 %29, %28
br i1 %30, label %17, label %31
31: ; preds = %17, %6, %0
ret void
}