#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 int uint16_t;
typedef int /*<<< orphan*/ next;
typedef int /*<<< orphan*/ mem_pool;

/* Variables and functions */
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, void *, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
void mem_init(void *mem_pool, uint16_t mem_size, uint16_t mem_count,
              void **mem_head) {
  *mem_head = mem_pool;
  *((uint16_t *)(((uint32_t)((uint8_t *)mem_pool + sizeof(mem_pool)) + 3) &
                 (~((uint32_t)3)))) = mem_count;
  memset(((uint8_t *)mem_pool + (mem_size * (--mem_count))), 0,
         sizeof(mem_pool));
  while (mem_count--) {
    uint32_t next;
    next = (uint32_t)((uint8_t *)mem_pool + (mem_size * (mem_count + 1)));
    memcpy(((uint8_t *)mem_pool + (mem_size * mem_count)), (void *)&next,
           sizeof(next));
  }
}


x86 assembly (clang, -O0):
<mem_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
add    $0x20,%rax
add    $0x3,%eax
and    $0xfffffffc,%eax
cltq
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x10(%rbp)
imul   %ecx,%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x10(%rbp)
cmp    $0x0,%eax
je     11d6 <mem_init+0xb6>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%edx
add    $0x1,%edx
imul   %edx,%ecx
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
imul   -0x10(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
jmp    117e <mem_init+0x5e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%r12
mov    %rdi,(%rcx)
lea    0x23(%rdi),%eax
cltq
and    $0xfffffffffffffffc,%rax
mov    %edx,(%rax)
lea    -0x1(%rbp),%ebx
mov    %ebx,%r13d
imul   %esi,%r13d
movslq %r13d,%rax
lea    (%rdi,%rax,4),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
test   %ebx,%ebx
je     11b6 <mem_init+0x96>
add    $0xfffffffe,%ebp
movslq %ebp,%rbx
movslq %r14d,%r15
mov    %r15,%rax
imul   %rbx,%rax
add    $0x1,%rbx
lea    (%r12,%rax,4),%rbp
shl    $0x2,%r15
neg    %r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r12,%r13,4),%eax
mov    %eax,0x4(%rsp)
mov    %rbp,%rdi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
sub    %r14d,%r13d
add    %r15,%rbp
add    $0xffffffffffffffff,%rbx
jne    1190 <mem_init+0x70>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @mem_init(i8* %0, i32 %1, i32 %2, i8** nocapture writeonly %3) {
%5 = alloca i32
store i8* %0, i8** %3
%6 = bitcast i8* %0 to i32*
%7 = getelementptr inbounds i8, i8* %0, 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 i32*
store i32 %2, i32* %13
%14 = add nsw i32 %2, -1
%15 = mul nsw i32 %14, %1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i32, i32* %6, i64 %16
%18 = tail call i32 @memset(i32* %17, i32 0, i32 8)
%19 = icmp eq i32 %14, 0
br i1 %19, label %39, label %20
20: ; preds = %4
%21 = add i32 %2, -2
%22 = bitcast i32* %5 to i8*
%23 = sext i32 %21 to i64
%24 = sext i32 %1 to i64
br label %25
25: ; preds = %20, %25
%26 = phi i64 [ %23, %20 ], [ %36, %25 ]
%27 = phi i32 [ %14, %20 ], [ %38, %25 ]
%28 = mul nsw i32 %27, %1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %6, i64 %29
%31 = ptrtoint i32* %30 to i64
%32 = trunc i64 %31 to i32
store i32 %32, i32* %5
%33 = mul nsw i64 %26, %24
%34 = getelementptr inbounds i32, i32* %6, i64 %33
%35 = call i32 @memcpy(i32* %34, i8* %22, i32 4)
%36 = add nsw i64 %26, -1
%37 = icmp eq i64 %26, 0
%38 = trunc i64 %26 to i32
br i1 %37, label %39, label %25
39: ; preds = %25, %4
ret void
}