#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*/ assert(int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ munmap(void *, size_t);
void free_huge_pages(void *ptr) {
  if (ptr == NULL)
    return;
  void *real_ptr = (char *)ptr - (2 * 1024 * 1024);
  size_t real_size = *((size_t *)real_ptr);
  assert(real_size % (2 * 1024 * 1024) == 0);
  if (real_size != 0)
    munmap(real_ptr, real_size);
  else
    free(real_ptr);
}


x86 assembly (clang, -O0):
<free_huge_pages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    114c <free_huge_pages+0x1c>
jmp    11a6 <free_huge_pages+0x76>
mov    -0x8(%rbp),%rax
add    $0xffffffffffe00000,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
and    $0x1fffff,%rax
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
je     119d <free_huge_pages+0x6d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <munmap@plt>
jmp    11a6 <free_huge_pages+0x76>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<free_huge_pages>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1173 <free_huge_pages+0x43>
mov    %rdi,%rbx
mov    -0x200000(%rdi),%r14
add    $0xffffffffffe00000,%rbx
xor    %edi,%edi
test   $0x1fffff,%r14d
sete   %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
test   %r14,%r14
je     117b <free_huge_pages+0x4b>
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <munmap@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @free_huge_pages(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %1
br label %28
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = getelementptr inbounds i8, i8* %9, i64 -2097152
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = bitcast i8* %11 to i64*
%13 = load i64, i64* %12
store i64 %13, i64* %4
%14 = load i64, i64* %4
%15 = urem i64 %14, 2097152
%16 = icmp eq i64 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i64, i64* %4
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %8
%22 = load i8*, i8** %3
%23 = load i64, i64* %4
%24 = call i32 @munmap(i8* %22, i64 %23)
br label %28
25: ; preds = %8
%26 = load i8*, i8** %3
%27 = call i32 @free(i8* %26)
br label %28
28: ; preds = %7, %25, %21
ret void
}

LLVM IR (-O2):
define void @free_huge_pages(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %16, label %3
3: ; preds = %1
%4 = getelementptr inbounds i8, i8* %0, i64 -2097152
%5 = bitcast i8* %4 to i64*
%6 = load i64, i64* %5
%7 = and i64 %6, 2097151
%8 = icmp eq i64 %7, 0
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = icmp eq i64 %6, 0
br i1 %11, label %14, label %12
12: ; preds = %3
%13 = tail call i32 @munmap(i8* %4, i64 %6)
br label %16
14: ; preds = %3
%15 = tail call i32 @free(i8* %4)
br label %16
16: ; preds = %12, %14, %1
ret void
}