#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 */

/* Variables and functions */
scalar_t__ ENFILE;
scalar_t__ ENOMEM;
int /*<<< orphan*/ RLIMIT_MEMLOCK;
scalar_t__ errno;
int handle_open_failure();
int increase_limit(int /*<<< orphan*/, int);
int handle_mem_failure(size_t length) {
  if (errno == ENFILE)
    return handle_open_failure();
  else if (errno == ENOMEM)
    return increase_limit(RLIMIT_MEMLOCK, 2 * length);
  return 0;
}


x86 assembly (clang, -O0):
<handle_mem_failure>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%rax
mov    0x2e8b(%rip),%rcx
cmp    (%rcx),%rax
jne    1155 <handle_mem_failure+0x35>
mov    $0x0,%al
call   1030 <handle_open_failure@plt>
mov    %eax,-0x4(%rbp)
jmp    119b <handle_mem_failure+0x7b>
mov    0x2e74(%rip),%rax
mov    (%rax),%rax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%rax
jne    118f <handle_mem_failure+0x6f>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%esi
call   1040 <increase_limit@plt>
mov    %eax,-0x4(%rbp)
jmp    119b <handle_mem_failure+0x7b>
jmp    1194 <handle_mem_failure+0x74>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_mem_failure>:
mov    0x2ea9(%rip),%rax
mov    (%rax),%rax
mov    0x2e97(%rip),%rcx
cmp    (%rcx),%rax
jne    113d <handle_mem_failure+0x1d>
xor    %eax,%eax
jmp    1030 <handle_open_failure@plt>
mov    0x2e94(%rip),%rcx
cmp    (%rcx),%rax
jne    115c <handle_mem_failure+0x3c>
mov    %rdi,%rsi
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
add    %esi,%esi
jmp    1040 <increase_limit@plt>
xor    %eax,%eax
ret

LLVM IR (-O0):
@errno = global i64 0
@ENFILE = global i64 0
@ENOMEM = global i64 0
@RLIMIT_MEMLOCK = global i32 0
define i32 @handle_mem_failure(i64 %0) {
%2 = alloca i32
%3 = alloca i64
store i64 %0, i64* %3
%4 = load i64, i64* @errno
%5 = load i64, i64* @ENFILE
%6 = icmp eq i64 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 (...) @handle_open_failure()
store i32 %8, i32* %2
br label %21
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOMEM
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = load i32, i32* @RLIMIT_MEMLOCK
%15 = load i64, i64* %3
%16 = mul i64 2, %15
%17 = trunc i64 %16 to i32
%18 = call i32 @increase_limit(i32 %14, i32 %17)
store i32 %18, i32* %2
br label %21
19: ; preds = %9
br label %20
20: ; preds = %19
store i32 0, i32* %2
br label %21
21: ; preds = %20, %13, %7
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
@errno = global i64 0
@ENFILE = global i64 0
@ENOMEM = global i64 0
@RLIMIT_MEMLOCK = global i32 0
define i32 @handle_mem_failure(i64 %0) {
%2 = load i64, i64* @errno
%3 = load i64, i64* @ENFILE
%4 = icmp eq i64 %2, %3
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 (...) @handle_open_failure()
br label %15
7: ; preds = %1
%8 = load i64, i64* @ENOMEM
%9 = icmp eq i64 %2, %8
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = load i32, i32* @RLIMIT_MEMLOCK
%12 = trunc i64 %0 to i32
%13 = shl i32 %12, 1
%14 = tail call i32 @increase_limit(i32 %11, i32 %13)
br label %15
15: ; preds = %7, %10, %5
%16 = phi i32 [ %6, %5 ], [ %14, %10 ], [ 0, %7 ]
ret i32 %16
}