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

/* Variables and functions */
void *malloc(size_t);
void *aligned_malloc(size_t size) {
  if (size <= 0)
    return NULL;
  uint32_t blocks = size / 4096;
  uint32_t fragment = size % 4096;
  if ((fragment + 16) > 4096)
    blocks += 2;
  else
    blocks += 1;
  size_t aligned_size = blocks * 4096 - 16;
  return malloc(aligned_size);
  return malloc(size);
}


x86 assembly (clang, -O0):
<aligned_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
ja     1134 <aligned_malloc+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1190 <aligned_malloc+0x80>
mov    -0x10(%rbp),%rax
shr    $0xc,%rax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
and    $0xfff,%rax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x10,%eax
cmp    $0x1000,%eax
jle    116b <aligned_malloc+0x5b>
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
jmp    1174 <aligned_malloc+0x64>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
shl    $0xc,%eax
sub    $0x10,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aligned_malloc>:
test   %rdi,%rdi
je     1139 <aligned_malloc+0x29>
mov    %edi,%eax
shr    $0xc,%eax
and    $0xfff,%edi
cmp    $0xff1,%edi
sbb    $0xffffffff,%eax
shl    $0xc,%eax
or     $0xff0,%eax
movslq %eax,%rdi
jmp    1030 <malloc@plt>
xor    %eax,%eax
ret

LLVM IR (-O0):
define i8* @aligned_malloc(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i64 %0, i64* %3
%7 = load i64, i64* %3
%8 = icmp ule i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %33
10: ; preds = %1
%11 = load i64, i64* %3
%12 = udiv i64 %11, 4096
%13 = trunc i64 %12 to i32
store i32 %13, i32* %4
%14 = load i64, i64* %3
%15 = urem i64 %14, 4096
%16 = trunc i64 %15 to i32
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 16
%19 = icmp sgt i32 %18, 4096
br i1 %19, label %20, label %23
20: ; preds = %10
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 2
store i32 %22, i32* %4
br label %26
23: ; preds = %10
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* %4
%28 = mul nsw i32 %27, 4096
%29 = sub nsw i32 %28, 16
%30 = sext i32 %29 to i64
store i64 %30, i64* %6
%31 = load i64, i64* %6
%32 = call i8* @malloc(i64 %31)
store i8* %32, i8** %2
br label %33
33: ; preds = %26, %9
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
define noalias i8* @aligned_malloc(i64 %0) {
%2 = icmp eq i64 %0, 0
br i1 %2, label %14, label %3
3: ; preds = %1
%4 = lshr i64 %0, 12
%5 = trunc i64 %4 to i32
%6 = and i64 %0, 4095
%7 = icmp ugt i64 %6, 4080
%8 = select i1 %7, i32 2, i32 1
%9 = add nsw i32 %8, %5
%10 = shl nsw i32 %9, 12
%11 = add nsw i32 %10, -16
%12 = sext i32 %11 to i64
%13 = tail call i8* @malloc(i64 %12)
br label %14
14: ; preds = %1, %3
%15 = phi i8* [ %13, %3 ], [ null, %1 ]
ret i8* %15
}