#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*/ uint8_t;
typedef int uint32_t;
typedef scalar_t__ uint16_t;
typedef int /*<<< orphan*/ mem_head;
typedef int /*<<< orphan*/ head;

/* Variables and functions */
int /*<<< orphan*/ memcpy(void **, void *, int);
void *mem_acquire(void **mem_head) {
  if (*mem_head) {
    uint16_t free_count;
    void *head;
    void *mem;
    free_count = *(
        (uint16_t *)(((uint32_t)((uint8_t *)*mem_head + sizeof(mem_head)) + 3) &
                     (~((uint32_t)3))));
    free_count--;
    mem = *mem_head;
    memcpy(&head, mem, sizeof(head));
    if (head) {
      *((uint16_t *)(((uint32_t)((uint8_t *)head + sizeof(head)) + 3) &
                     (~((uint32_t)3)))) = free_count;
    }
    *mem_head = head;
    return mem;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<mem_acquire>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11a7 <mem_acquire+0x97>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    $0x20,%rax
add    $0x3,%eax
and    $0xfffffffc,%eax
cltq
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x8,%edx
call   1030 <memcpy@plt>
cmpq   $0x0,-0x20(%rbp)
je     118f <mem_acquire+0x7f>
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    $0x20,%rax
add    $0x3,%eax
and    $0xfffffffc,%eax
cltq
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11af <mem_acquire+0x9f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_acquire>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    (%rdi),%rbx
test   %rbx,%rbx
je     1172 <mem_acquire+0x62>
mov    %rdi,%r14
lea    0x23(%rbx),%eax
cltq
and    $0xfffffffffffffffc,%rax
mov    (%rax),%r15
lea    0x8(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     116f <mem_acquire+0x5f>
movabs $0x300000000,%rcx
add    $0xffffffffffffffff,%r15
lea    0x20(%rax),%edx
shl    $0x20,%rdx
add    %rcx,%rdx
sar    $0x20,%rdx
and    $0xfffffffffffffffc,%rdx
mov    %r15,(%rdx)
mov    %rax,(%r14)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @mem_acquire(i8** %0) {
%2 = alloca i8*
%3 = alloca i8**
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
store i8** %0, i8*** %3
%7 = load i8**, i8*** %3
%8 = load i8*, i8** %7
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %43
10: ; preds = %1
%11 = load i8**, i8*** %3
%12 = load i8*, i8** %11
%13 = bitcast i8* %12 to i32*
%14 = getelementptr inbounds i32, i32* %13, i64 8
%15 = ptrtoint i32* %14 to i32
%16 = add nsw i32 %15, 3
%17 = and i32 %16, -4
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to i64*
%20 = load i64, i64* %19
store i64 %20, i64* %4
%21 = load i64, i64* %4
%22 = add nsw i64 %21, -1
store i64 %22, i64* %4
%23 = load i8**, i8*** %3
%24 = load i8*, i8** %23
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = call i32 @memcpy(i8** %5, i8* %25, i32 8)
%27 = load i8*, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %39
29: ; preds = %10
%30 = load i64, i64* %4
%31 = load i8*, i8** %5
%32 = bitcast i8* %31 to i32*
%33 = getelementptr inbounds i32, i32* %32, i64 8
%34 = ptrtoint i32* %33 to i32
%35 = add nsw i32 %34, 3
%36 = and i32 %35, -4
%37 = sext i32 %36 to i64
%38 = inttoptr i64 %37 to i64*
store i64 %30, i64* %38
br label %39
39: ; preds = %29, %10
%40 = load i8*, i8** %5
%41 = load i8**, i8*** %3
store i8* %40, i8** %41
%42 = load i8*, i8** %6
store i8* %42, i8** %2
br label %44
43: ; preds = %1
store i8* null, i8** %2
br label %44
44: ; preds = %43, %39
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
define i8* @mem_acquire(i8** nocapture %0) {
%2 = alloca i8*
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %28, label %5
5: ; preds = %1
%6 = bitcast i8** %2 to i8*
%7 = getelementptr inbounds i8, i8* %3, i64 32
%8 = ptrtoint i8* %7 to i64
%9 = shl i64 %8, 32
%10 = add i64 %9, 12884901888
%11 = ashr exact i64 %10, 32
%12 = and i64 %11, -4
%13 = inttoptr i64 %12 to i64*
%14 = load i64, i64* %13
%15 = call i32 @memcpy(i8** %2, i8* %3, i32 8)
%16 = load i8*, i8** %2
%17 = icmp eq i8* %16, null
br i1 %17, label %27, label %18
18: ; preds = %5
%19 = add nsw i64 %14, -1
%20 = getelementptr inbounds i8, i8* %16, i64 32
%21 = ptrtoint i8* %20 to i64
%22 = shl i64 %21, 32
%23 = add i64 %22, 12884901888
%24 = ashr exact i64 %23, 32
%25 = and i64 %24, -4
%26 = inttoptr i64 %25 to i64*
store i64 %19, i64* %26
br label %27
27: ; preds = %18, %5
store i8* %16, i8** %0
br label %28
28: ; preds = %1, %27
ret i8* %3
}