#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef int uint64_t;

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(size_t);
size_t BMK_findMaxMem(uint64_t requiredMem) {
  size_t step = (64U << 20);
  uint8_t *testmem = NULL;
  requiredMem = (((requiredMem >> 25) + 1) << 26);
  if (requiredMem > (1984 << 20))
    requiredMem = (1984 << 20);
  requiredMem += 2 * step;
  while (!testmem) {
    requiredMem -= step;
    testmem = malloc((size_t)requiredMem);
  }
  free(testmem);
  return (size_t)(requiredMem - step);
}


x86 assembly (clang, -O0):
<BMK_findMaxMem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x4000000,-0x10(%rbp)
04
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x19,%eax
add    $0x1,%eax
shl    $0x1a,%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x7c000000,-0x4(%rbp)
jle    115e <BMK_findMaxMem+0x3e>
movl   $0x7c000000,-0x4(%rbp)
mov    -0x10(%rbp),%rcx
shl    $0x1,%rcx
movslq -0x4(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x4(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1187 <BMK_findMaxMem+0x67>
jmp    11a7 <BMK_findMaxMem+0x87>
mov    -0x10(%rbp),%rcx
movslq -0x4(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
movslq -0x4(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
jmp    1170 <BMK_findMaxMem+0x50>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movslq -0x4(%rbp),%rax
sub    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BMK_findMaxMem>:
push   %rbx
lea    (%rdi,%rdi,1),%ecx
add    $0xc000000,%ecx
and    $0xfc000000,%ecx
add    $0xfc000000,%ecx
cs nopw 0x0(%rax,%rax,1)
movslq %ecx,%rbx
mov    %rbx,%rdi
call   1040 <malloc@plt>
lea    -0x4000000(%rbx),%ecx
test   %rax,%rax
je     1140 <BMK_findMaxMem+0x20>
mov    %rax,%rdi
call   1030 <free@plt>
add    $0xfffffffffc000000,%rbx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i64 @BMK_findMaxMem(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32*
store i32 %0, i32* %2
store i64 67108864, i64* %3
store i32* null, i32** %4
%5 = load i32, i32* %2
%6 = ashr i32 %5, 25
%7 = add nsw i32 %6, 1
%8 = shl i32 %7, 26
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp sgt i32 %9, 2080374784
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 2080374784, i32* %2
br label %12
12: ; preds = %11, %1
%13 = load i64, i64* %3
%14 = mul i64 2, %13
%15 = load i32, i32* %2
%16 = sext i32 %15 to i64
%17 = add i64 %16, %14
%18 = trunc i64 %17 to i32
store i32 %18, i32* %2
br label %19
19: ; preds = %23, %12
%20 = load i32*, i32** %4
%21 = icmp ne i32* %20, null
%22 = xor i1 %21, true
br i1 %22, label %23, label %32
23: ; preds = %19
%24 = load i64, i64* %3
%25 = load i32, i32* %2
%26 = sext i32 %25 to i64
%27 = sub i64 %26, %24
%28 = trunc i64 %27 to i32
store i32 %28, i32* %2
%29 = load i32, i32* %2
%30 = sext i32 %29 to i64
%31 = call i32* @malloc(i64 %30)
store i32* %31, i32** %4
br label %19
32: ; preds = %19
%33 = load i32*, i32** %4
%34 = call i32 @free(i32* %33)
%35 = load i32, i32* %2
%36 = sext i32 %35 to i64
%37 = load i64, i64* %3
%38 = sub i64 %36, %37
ret i64 %38
}

LLVM IR (-O2):
define i64 @BMK_findMaxMem(i32 %0) {
%2 = shl i32 %0, 1
%3 = add i32 %2, 201326592
%4 = and i32 %3, -67108864
br label %5
5: ; preds = %1, %5
%6 = phi i32 [ %4, %1 ], [ %7, %5 ]
%7 = add i32 %6, -67108864
%8 = sext i32 %7 to i64
%9 = tail call i32* @malloc(i64 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %5, label %11
11: ; preds = %5
%12 = tail call i32 @free(i32* %9)
%13 = add nsw i64 %8, -67108864
ret i64 %13
}