#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 *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
void *mallocaligned(size_t size, size_t align) {
  unsigned long long int i;
  size_t total = align + align + size;
  void *ptr = malloc(total);
  printf("\n");
  printf("%p to %lu bytes allocated for %lu bytes request\n", ptr, total, size);
  unsigned long long int tmp = (unsigned long long int)ptr / align;
  printf("%llu is at tmp\n", tmp);
  void *flor = (void *)(tmp * align);
  void *ceil = (void *)((tmp + 1) * align);
  printf("%p is at ceiling\n", ceil);
  i = (unsigned long long)ceil;
  printf("0x%llx is at ceiling\n", i);
  printf("rem = %llu\n", (unsigned long long)ptr % align);
  size_t padding = ceil - ptr;
  *(size_t *)ceil = padding;
  return ceil + sizeof(size_t);
}


x86 assembly (clang, -O0):
<mallocaligned>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    -0x10(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
lea    0xefd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rax
imul   -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
imul   -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %rdx,%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x40(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
add    $0x8,%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mallocaligned>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r12
lea    (%rdi,%rsi,2),%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xea2(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rax
or     %r15,%rax
shr    $0x20,%rax
je     118a <mallocaligned+0x5a>
mov    %r14,%rax
xor    %edx,%edx
div    %r15
mov    %rdx,%r12
mov    %rax,%rbx
jmp    1197 <mallocaligned+0x67>
mov    %r14d,%eax
xor    %edx,%edx
div    %r15d
mov    %edx,%r12d
mov    %eax,%ebx
lea    0xe93(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
imul   %r15,%rbx
lea    0xe8a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe8b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe90(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rax
sub    %r14,%rax
mov    %rax,(%rbx)
add    $0x8,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [49 x i8] c"%p to %lu bytes allocated for %lu bytes request\0A\00"
@.str.2 = [16 x i8] c"%llu is at tmp\0A\00"
@.str.3 = [18 x i8] c"%p is at ceiling\0A\00"
@.str.4 = [22 x i8] c"0x%llx is at ceiling\0A\00"
@.str.5 = [12 x i8] c"rem = %llu\0A\00"
define i8* @mallocaligned(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
store i64 %0, i64* %3
store i64 %1, i64* %4
%12 = load i64, i64* %4
%13 = load i64, i64* %4
%14 = add i64 %12, %13
%15 = load i64, i64* %3
%16 = add i64 %14, %15
store i64 %16, i64* %6
%17 = load i64, i64* %6
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %7
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = load i8*, i8** %7
%21 = load i64, i64* %6
%22 = load i64, i64* %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %20, i64 %21, i64 %22)
%24 = load i8*, i8** %7
%25 = ptrtoint i8* %24 to i64
%26 = load i64, i64* %4
%27 = udiv i64 %25, %26
store i64 %27, i64* %8
%28 = load i64, i64* %8
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i64 %28)
%30 = load i64, i64* %8
%31 = load i64, i64* %4
%32 = mul i64 %30, %31
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %9
%34 = load i64, i64* %8
%35 = add i64 %34, 1
%36 = load i64, i64* %4
%37 = mul i64 %35, %36
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %10
%39 = load i8*, i8** %10
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = load i8*, i8** %10
%42 = ptrtoint i8* %41 to i64
store i64 %42, i64* %5
%43 = load i64, i64* %5
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %43)
%45 = load i8*, i8** %7
%46 = ptrtoint i8* %45 to i64
%47 = load i64, i64* %4
%48 = urem i64 %46, %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i64 %48)
%50 = load i8*, i8** %10
%51 = load i8*, i8** %7
%52 = ptrtoint i8* %50 to i64
%53 = ptrtoint i8* %51 to i64
%54 = sub i64 %52, %53
store i64 %54, i64* %11
%55 = load i64, i64* %11
%56 = load i8*, i8** %10
%57 = bitcast i8* %56 to i64*
store i64 %55, i64* %57
%58 = load i8*, i8** %10
%59 = getelementptr i8, i8* %58, i64 8
ret i8* %59
}

LLVM IR (-O2):
@.str.1 = [49 x i8] c"%p to %lu bytes allocated for %lu bytes request\0A\00"
@.str.2 = [16 x i8] c"%llu is at tmp\0A\00"
@.str.3 = [18 x i8] c"%p is at ceiling\0A\00"
@.str.4 = [22 x i8] c"0x%llx is at ceiling\0A\00"
@.str.5 = [12 x i8] c"rem = %llu\0A\00"
define i8* @mallocaligned(i64 %0, i64 %1) {
%3 = shl i64 %1, 1
%4 = add i64 %3, %0
%5 = tail call i8* @malloc(i64 %4)
%6 = tail call i32 @putchar(i32 10)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %5, i64 %4, i64 %0)
%8 = ptrtoint i8* %5 to i64
%9 = udiv i64 %8, %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i64 %9)
%11 = add i64 %9, 1
%12 = mul i64 %11, %1
%13 = inttoptr i64 %12 to i8*
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %12)
%16 = urem i64 %8, %1
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i64 %16)
%18 = sub i64 %12, %8
%19 = inttoptr i64 %12 to i64*
store i64 %18, i64* %19
%20 = getelementptr i8, i8* %13, i64 8
ret i8* %20
}