#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;
};

/* 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 *);
int ulimit_expand(int newfd) {
  struct rlimit rlim;
  if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
    return (-1);
  }
  if ((rlim.rlim_cur == RLIM_INFINITY) && (rlim.rlim_max == RLIM_INFINITY)) {
    return (0);
  }
  if (rlim.rlim_cur != RLIM_INFINITY) {
    rlim.rlim_cur += (64 * 64) * 5;
  }
  if (rlim.rlim_max != RLIM_INFINITY) {
    rlim.rlim_max += (64 * 64) * 5;
  }
  if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<ulimit_expand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2eb6(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <getrlimit@plt>
cmp    $0x0,%rax
je     1153 <ulimit_expand+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <ulimit_expand+0xda>
mov    -0x18(%rbp),%rax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%rax
jne    1187 <ulimit_expand+0x67>
mov    -0x10(%rbp),%rax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
jne    1187 <ulimit_expand+0x67>
movl   $0x0,-0x4(%rbp)
jmp    11fa <ulimit_expand+0xda>
mov    -0x18(%rbp),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
je     11a9 <ulimit_expand+0x89>
mov    -0x18(%rbp),%rax
add    $0x5000,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e24(%rip),%rcx
cmp    (%rcx),%rax
je     11cb <ulimit_expand+0xab>
mov    -0x10(%rbp),%rax
add    $0x5000,%rax
mov    %rax,-0x10(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <setrlimit@plt>
cmp    $0x0,%rax
je     11f3 <ulimit_expand+0xd3>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <ulimit_expand+0xda>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ulimit_expand>:
push   %rbx
sub    $0x10,%rsp
mov    0x2ebc(%rip),%rbx
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1040 <getrlimit@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1192 <ulimit_expand+0x72>
mov    (%rsp),%rdx
mov    0x8(%rsp),%rcx
mov    0x2e85(%rip),%rax
mov    (%rax),%rsi
cmp    %rsi,%rdx
jne    1162 <ulimit_expand+0x42>
xor    %eax,%eax
cmp    %rsi,%rcx
je     1192 <ulimit_expand+0x72>
cmp    %rsi,%rdx
je     1172 <ulimit_expand+0x52>
add    $0x5000,%rdx
mov    %rdx,(%rsp)
cmp    %rsi,%rcx
je     1183 <ulimit_expand+0x63>
add    $0x5000,%rcx
mov    %rcx,0x8(%rsp)
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1030 <setrlimit@plt>
neg    %rax
sbb    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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