#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__ malloc(int);
int /*<<< orphan*/ printf(char *, long, unsigned int);
void *alignedMalloc(unsigned int alignment, unsigned int size) {
  printf("alignedMalloc(%d,%d)\n", alignment, size);
  char *result = (char *)malloc(sizeof(unsigned int) + alignment + size);
  unsigned int offset = alignment - ((long)result % alignment);
  printf("result ptr is %ld offset is %d\n", (long)result, offset);
  unsigned int *offsetPtr =
      (unsigned int *)(result + offset - sizeof(unsigned int));
  *offsetPtr = offset;
  printf("offset at %ld is %d\n", (long)offsetPtr, *offsetPtr);
  return (void *)(result + offset);
}


x86 assembly (clang, -O0):
<alignedMalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xec3(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x4,%rax
mov    -0x8(%rbp),%ecx
add    %rcx,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
cqto
idiv   %rcx
mov    -0x28(%rbp),%rax
sub    %rdx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe91(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    %rcx,%rax
add    $0xfffffffffffffffc,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xe80(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alignedMalloc>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebx
mov    %edi,%r15d
lea    0xecc(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <printf@plt>
lea    (%rbx,%r14,1),%edi
add    $0x4,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
shr    $0x20,%rax
je     115e <alignedMalloc+0x3e>
mov    %r14,%rax
cqto
idiv   %r15
jmp    1166 <alignedMalloc+0x46>
mov    %r14d,%eax
xor    %edx,%edx
div    %r15d
sub    %edx,%ebx
lea    0xea7(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <printf@plt>
lea    (%r14,%rbx,1),%rsi
add    $0xfffffffffffffffc,%rsi
mov    %r14,%r15
add    %rbx,%r15
mov    %ebx,-0x4(%r14,%rbx,1)
lea    0xea3(%rip),%rdi
mov    %ebx,%edx
call   1030 <printf@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [22 x i8] c"alignedMalloc(%d,%d)\0A\00"
@.str.1 = [32 x i8] c"result ptr is %ld offset is %d\0A\00"
@.str.2 = [21 x i8] c"offset at %ld is %d\0A\00"
define i8* @alignedMalloc(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %3
%9 = zext i32 %8 to i64
%10 = load i32, i32* %4
%11 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 %9, i32 %10)
%12 = load i32, i32* %3
%13 = zext i32 %12 to i64
%14 = add i64 4, %13
%15 = load i32, i32* %4
%16 = zext i32 %15 to i64
%17 = add i64 %14, %16
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %5
%21 = load i32, i32* %3
%22 = zext i32 %21 to i64
%23 = load i8*, i8** %5
%24 = ptrtoint i8* %23 to i64
%25 = load i32, i32* %3
%26 = zext i32 %25 to i64
%27 = srem i64 %24, %26
%28 = sub nsw i64 %22, %27
%29 = trunc i64 %28 to i32
store i32 %29, i32* %6
%30 = load i8*, i8** %5
%31 = ptrtoint i8* %30 to i64
%32 = load i32, i32* %6
%33 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i64 %31, i32 %32)
%34 = load i8*, i8** %5
%35 = load i32, i32* %6
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = getelementptr inbounds i8, i8* %37, i64 -4
%39 = bitcast i8* %38 to i32*
store i32* %39, i32** %7
%40 = load i32, i32* %6
%41 = load i32*, i32** %7
store i32 %40, i32* %41
%42 = load i32*, i32** %7
%43 = ptrtoint i32* %42 to i64
%44 = load i32*, i32** %7
%45 = load i32, i32* %44
%46 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 %43, i32 %45)
%47 = load i8*, i8** %5
%48 = load i32, i32* %6
%49 = zext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
ret i8* %50
}

LLVM IR (-O2):
@.str = [22 x i8] c"alignedMalloc(%d,%d)\0A\00"
@.str.1 = [32 x i8] c"result ptr is %ld offset is %d\0A\00"
@.str.2 = [21 x i8] c"offset at %ld is %d\0A\00"
define i8* @alignedMalloc(i32 %0, i32 %1) {
%3 = zext i32 %0 to i64
%4 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 %3, i32 %1)
%5 = add i32 %0, 4
%6 = add i32 %5, %1
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = srem i64 %7, %3
%10 = trunc i64 %9 to i32
%11 = sub i32 %0, %10
%12 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i64 %7, i32 %11)
%13 = zext i32 %11 to i64
%14 = getelementptr inbounds i8, i8* %8, i64 %13
%15 = getelementptr inbounds i8, i8* %14, i64 -4
%16 = bitcast i8* %15 to i32*
store i32 %11, i32* %16
%17 = ptrtoint i8* %15 to i64
%18 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 %17, i32 %11)
ret i8* %14
}