#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 /*<<< orphan*/ uint64_t;

/* Variables and functions */
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ mprotect(int /*<<< orphan*/ *, int, int);
scalar_t__ munmap(int /*<<< orphan*/ *, int /*<<< orphan*/);
void free_thread(void *p) {
  uint64_t *ptr = (uint64_t *)p;
  if (mprotect(ptr + 512, 512 * 3 * sizeof(uint64_t), PROT_READ | PROT_WRITE) !=
      0)
    abort();
  uint64_t size = ptr[512];
  memset(p, 0, size);
  if (munmap(ptr, size) != 0)
    abort();
}


x86 assembly (clang, -O0):
<free_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x800,%rdi
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
mov    0x2e69(%rip),%rax
or     (%rax),%edx
mov    $0x1800,%esi
call   1060 <mprotect@plt>
cmp    $0x0,%rax
je     118c <free_thread+0x4c>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
mov    0x800(%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <munmap@plt>
cmp    $0x0,%rax
je     11c4 <free_thread+0x84>
mov    $0x0,%al
call   1030 <abort@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<free_thread>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
add    $0x800,%rdi
mov    0x2ea4(%rip),%rax
mov    0x2e7d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    $0x1800,%esi
call   1060 <mprotect@plt>
test   %rax,%rax
jne    1196 <free_thread+0x56>
mov    0x800(%rbx),%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <munmap@plt>
test   %rax,%rax
jne    1196 <free_thread+0x56>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define void @free_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = bitcast i8* %5 to i32*
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = getelementptr inbounds i32, i32* %7, i64 512
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %9, %10
%12 = call i64 @mprotect(i32* %8, i32 6144, i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 (...) @abort()
unreachable
16: ; preds = %1
%17 = load i32*, i32** %3
%18 = getelementptr inbounds i32, i32* %17, i64 512
%19 = load i32, i32* %18
store i32 %19, i32* %4
%20 = load i8*, i8** %2
%21 = load i32, i32* %4
%22 = call i32 @memset(i8* %20, i32 0, i32 %21)
%23 = load i32*, i32** %3
%24 = load i32, i32* %4
%25 = call i64 @munmap(i32* %23, i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 (...) @abort()
unreachable
29: ; preds = %16
ret void
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define void @free_thread(i8* %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 2048
%3 = bitcast i8* %2 to i32*
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = tail call i64 @mprotect(i32* %3, i32 6144, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = tail call i32 (...) @abort()
unreachable
11: ; preds = %1
%12 = bitcast i8* %0 to i32*
%13 = load i32, i32* %3
%14 = tail call i32 @memset(i8* %0, i32 0, i32 %13)
%15 = tail call i64 @munmap(i32* %12, i32 %13)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %11
%18 = tail call i32 (...) @abort()
unreachable
19: ; preds = %11
ret void
}