#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 */
void *buddy_malloc(size_t);
size_t internal_size(size_t);
void *large_malloc(size_t);
void *malloc(size_t size) {
  if (!size)
    return NULL;
  void *ptr = NULL;
  size = internal_size(size);
  if (size > (~((size_t)1 << ((sizeof(size_t) * 8) - 1))))
    return NULL;
  if (size > (1 << 12)) {
    ptr = large_malloc(size);
    return ptr;
  }
  ptr = buddy_malloc(size);
  return ptr;
}


x86 assembly (clang, -O0):
<malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <malloc+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11c7 <malloc+0x97>
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <internal_size@plt>
mov    %rax,-0x10(%rbp)
movabs $0x7fffffffffffffff,%rax
ff ff 7f
cmp    %rax,-0x10(%rbp)
jbe    118a <malloc+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    11c7 <malloc+0x97>
cmpq   $0x1000,-0x10(%rbp)
jbe    11b2 <malloc+0x82>
mov    -0x10(%rbp),%rdi
call   1030 <large_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c7 <malloc+0x97>
mov    -0x10(%rbp),%rdi
call   1050 <buddy_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc>:
test   %rdi,%rdi
je     1155 <malloc+0x25>
push   %rax
call   1040 <internal_size@plt>
test   %rax,%rax
lea    0x8(%rsp),%rsp
js     1155 <malloc+0x25>
mov    %rax,%rdi
cmp    $0x1001,%rax
jb     1158 <malloc+0x28>
jmp    1030 <large_malloc@plt>
xor    %eax,%eax
ret
jmp    1050 <buddy_malloc@plt>

LLVM IR (-O0):
define i8* @malloc(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i64 %0, i64* %3
%5 = load i64, i64* %3
%6 = icmp ne i64 %5, 0
br i1 %6, label %8, label %7
7: ; preds = %1
store i8* null, i8** %2
br label %25
8: ; preds = %1
store i8* null, i8** %4
%9 = load i64, i64* %3
%10 = call i64 @internal_size(i64 %9)
store i64 %10, i64* %3
%11 = load i64, i64* %3
%12 = icmp ugt i64 %11, 9223372036854775807
br i1 %12, label %13, label %14
13: ; preds = %8
store i8* null, i8** %2
br label %25
14: ; preds = %8
%15 = load i64, i64* %3
%16 = icmp ugt i64 %15, 4096
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i64, i64* %3
%19 = call i8* @large_malloc(i64 %18)
store i8* %19, i8** %4
%20 = load i8*, i8** %4
store i8* %20, i8** %2
br label %25
21: ; preds = %14
%22 = load i64, i64* %3
%23 = call i8* @buddy_malloc(i64 %22)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
store i8* %24, i8** %2
br label %25
25: ; preds = %21, %17, %13, %7
%26 = load i8*, i8** %2
ret i8* %26
}

LLVM IR (-O2):
define i8* @malloc(i64 %0) {
%2 = icmp eq i64 %0, 0
br i1 %2, label %12, label %3
3: ; preds = %1
%4 = tail call i64 @internal_size(i64 %0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = icmp ugt i64 %4, 4096
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i8* @large_malloc(i64 %4)
br label %12
10: ; preds = %6
%11 = tail call i8* @buddy_malloc(i64 %4)
br label %12
12: ; preds = %8, %10, %3, %1
%13 = phi i8* [ null, %1 ], [ %9, %8 ], [ %11, %10 ], [ null, %3 ]
ret i8* %13
}