#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 */
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ printf(char *, ...);
void alignedFree(void *p) {
  printf("alignedFree()\n");
  unsigned int *offsetPtr = (unsigned int *)(p - sizeof(unsigned int));
  unsigned int offset = *offsetPtr;
  printf("offset at %ld is %d\n", (long)(p - sizeof(unsigned int)), offset);
  void *originalPtr = p - offset;
  free(originalPtr);
}


x86 assembly (clang, -O0):
<alignedFree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xecd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
add    $0xfffffffffffffffc,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
add    $0xfffffffffffffffc,%rsi
mov    -0x14(%rbp),%edx
lea    0xeae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alignedFree>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xed7(%rip),%rdi
call   1040 <puts@plt>
lea    -0x4(%rbx),%rsi
mov    -0x4(%rbx),%r14d
lea    0xeae(%rip),%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
sub    %r14,%rbx
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"alignedFree()\0A\00"
@.str.1 = [21 x i8] c"offset at %ld is %d\0A\00"
define void @alignedFree(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%7 = load i8*, i8** %2
%8 = getelementptr i8, i8* %7, i64 -4
%9 = bitcast i8* %8 to i32*
store i32* %9, i32** %3
%10 = load i32*, i32** %3
%11 = load i32, i32* %10
store i32 %11, i32* %4
%12 = load i8*, i8** %2
%13 = getelementptr i8, i8* %12, i64 -4
%14 = ptrtoint i8* %13 to i64
%15 = load i32, i32* %4
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i64 %14, i32 %15)
%17 = load i8*, i8** %2
%18 = load i32, i32* %4
%19 = zext i32 %18 to i64
%20 = sub i64 0, %19
%21 = getelementptr i8, i8* %17, i64 %20
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = call i32 @free(i8* %22)
ret void
}

LLVM IR (-O2):
@.str.1 = [21 x i8] c"offset at %ld is %d\0A\00"
@str = [14 x i8] c"alignedFree()\00"
define void @alignedFree(i8* %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%3 = getelementptr i8, i8* %0, i64 -4
%4 = bitcast i8* %3 to i32*
%5 = load i32, i32* %4
%6 = ptrtoint i8* %3 to i64
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i64 %6, i32 %5)
%8 = zext i32 %5 to i64
%9 = sub nsw i64 0, %8
%10 = getelementptr i8, i8* %0, i64 %9
%11 = tail call i32 @free(i8* %10)
ret void
}