#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 */
unsigned int libmem_alloc(void *, int);
int /*<<< orphan*/ libmem_deinit(void *);
int /*<<< orphan*/ libmem_free(void *, unsigned int);
void *libmem_init(int, int);
int /*<<< orphan*/ libmem_set_flags(void *, int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/);
int main(int argc, char **argv) {
  void *obj;
  unsigned int addr1;
  unsigned int addr2;
  unsigned int addr3;
  unsigned int addr4;
  unsigned int addr5;
  int index;
  int rd;
  srand(time(0));
  obj = libmem_init(0x80000000, 64 * 1024 * 1024);
  for (index = 0; index < 256; index++) {
    rd = rand() & 0xffff;
    printf("1 rd %d\n", rd);
    addr1 = libmem_alloc(obj, rd);
    rd = rand() & 0xffff;
    printf("2 rd %d\n", rd);
    addr2 = libmem_alloc(obj, rd);
    rd = rand() & 0xffff;
    printf("3 rd %d\n", rd);
    addr3 = libmem_alloc(obj, rd);
    rd = rand() & 0xffff;
    printf("4 rd %d\n", rd);
    addr4 = libmem_alloc(obj, rd);
    addr5 = libmem_alloc(obj, rd);
    libmem_free(obj, addr1);
    printf("5\n");
    addr1 = libmem_alloc(obj, 64);
    printf("6\n");
    libmem_free(obj, addr3);
    printf("7\n");
    addr3 = libmem_alloc(obj, 64 * 1024);
    libmem_free(obj, addr5);
    addr5 = libmem_alloc(obj, 64 * 1024);
    printf("8\n");
    libmem_free(obj, addr1);
    printf("9\n");
    libmem_free(obj, addr2);
    printf("10\n");
    libmem_free(obj, addr3);
    libmem_free(obj, addr4);
    if (index == 255) {
      libmem_set_flags(obj, 1);
    }
    libmem_free(obj, addr5);
  }
  libmem_deinit(obj);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    $0x80000000,%edi
mov    $0x4000000,%esi
call   1050 <libmem_init@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x100,-0x30(%rbp)
jge    13b6 <main+0x226>
mov    $0x0,%al
call   10b0 <rand@plt>
and    $0xffff,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10a0 <libmem_alloc@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   10b0 <rand@plt>
and    $0xffff,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10a0 <libmem_alloc@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   10b0 <rand@plt>
and    $0xffff,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10a0 <libmem_alloc@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   10b0 <rand@plt>
and    $0xffff,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10a0 <libmem_alloc@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10a0 <libmem_alloc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <libmem_free@plt>
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x40,%esi
call   10a0 <libmem_alloc@plt>
mov    %eax,-0x1c(%rbp)
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <libmem_free@plt>
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x10000,%esi
call   10a0 <libmem_alloc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1070 <libmem_free@plt>
mov    -0x18(%rbp),%rdi
mov    $0x10000,%esi
call   10a0 <libmem_alloc@plt>
mov    %eax,-0x2c(%rbp)
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <libmem_free@plt>
lea    0xce8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1070 <libmem_free@plt>
lea    0xcd1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <libmem_free@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1070 <libmem_free@plt>
cmpl   $0xff,-0x30(%rbp)
jne    139c <main+0x20c>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1030 <libmem_set_flags@plt>
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1070 <libmem_free@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11ce <main+0x3e>
mov    -0x18(%rbp),%rdi
call   1080 <libmem_deinit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    $0x80000000,%edi
mov    $0x4000000,%esi
call   1060 <libmem_init@plt>
mov    %rax,%rbx
mov    $0xff,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10c0 <rand@plt>
movzwl %ax,%ebp
lea    0xe0f(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   10b0 <libmem_alloc@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   10c0 <rand@plt>
movzwl %ax,%ebp
lea    0xdf1(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   10b0 <libmem_alloc@plt>
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   10c0 <rand@plt>
movzwl %ax,%ebp
lea    0xdd2(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   10b0 <libmem_alloc@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10c0 <rand@plt>
movzwl %ax,%ebp
lea    0xdb4(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   10b0 <libmem_alloc@plt>
mov    %eax,(%rsp)
mov    %rbx,%rdi
mov    %ebp,%esi
call   10b0 <libmem_alloc@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
mov    %r12d,%esi
call   1080 <libmem_free@plt>
lea    0xd88(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0x40,%esi
call   10b0 <libmem_alloc@plt>
mov    %eax,%r12d
lea    0xd6e(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1080 <libmem_free@plt>
lea    0xd59(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0x10000,%esi
call   10b0 <libmem_alloc@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %r13d,%esi
call   1080 <libmem_free@plt>
mov    %rbx,%rdi
mov    $0x10000,%esi
call   10b0 <libmem_alloc@plt>
mov    %eax,%ebp
lea    0xd25(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1080 <libmem_free@plt>
lea    0xd10(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    0x4(%rsp),%esi
call   1080 <libmem_free@plt>
lea    0xcfa(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1080 <libmem_free@plt>
mov    %rbx,%rdi
mov    (%rsp),%esi
call   1080 <libmem_free@plt>
mov    %rbx,%rdi
sub    $0x1,%r15d
jb     1364 <main+0x1c4>
mov    %ebp,%esi
call   1080 <libmem_free@plt>
jmp    11e0 <main+0x40>
mov    $0x1,%esi
call   1040 <libmem_set_flags@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <libmem_free@plt>
mov    %rbx,%rdi
call   1090 <libmem_deinit@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"1 rd %d\0A\00"
@.str.1 = [9 x i8] c"2 rd %d\0A\00"
@.str.2 = [9 x i8] c"3 rd %d\0A\00"
@.str.3 = [9 x i8] c"4 rd %d\0A\00"
@.str.4 = [3 x i8] c"5\0A\00"
@.str.5 = [3 x i8] c"6\0A\00"
@.str.6 = [3 x i8] c"7\0A\00"
@.str.7 = [3 x i8] c"8\0A\00"
@.str.8 = [3 x i8] c"9\0A\00"
@.str.9 = [4 x i8] c"10\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call i32 @time(i32 0)
%15 = call i32 @srand(i32 %14)
%16 = call i8* @libmem_init(i32 -2147483648, i32 67108864)
store i8* %16, i8** %6
store i32 0, i32* %12
br label %17
17: ; preds = %94, %2
%18 = load i32, i32* %12
%19 = icmp slt i32 %18, 256
br i1 %19, label %20, label %97
20: ; preds = %17
%21 = call i32 (...) @rand()
%22 = and i32 %21, 65535
store i32 %22, i32* %13
%23 = load i32, i32* %13
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i8*, i8** %6
%26 = load i32, i32* %13
%27 = call i32 @libmem_alloc(i8* %25, i32 %26)
store i32 %27, i32* %7
%28 = call i32 (...) @rand()
%29 = and i32 %28, 65535
store i32 %29, i32* %13
%30 = load i32, i32* %13
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i8*, i8** %6
%33 = load i32, i32* %13
%34 = call i32 @libmem_alloc(i8* %32, i32 %33)
store i32 %34, i32* %8
%35 = call i32 (...) @rand()
%36 = and i32 %35, 65535
store i32 %36, i32* %13
%37 = load i32, i32* %13
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = load i8*, i8** %6
%40 = load i32, i32* %13
%41 = call i32 @libmem_alloc(i8* %39, i32 %40)
store i32 %41, i32* %9
%42 = call i32 (...) @rand()
%43 = and i32 %42, 65535
store i32 %43, i32* %13
%44 = load i32, i32* %13
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %44)
%46 = load i8*, i8** %6
%47 = load i32, i32* %13
%48 = call i32 @libmem_alloc(i8* %46, i32 %47)
store i32 %48, i32* %10
%49 = load i8*, i8** %6
%50 = load i32, i32* %13
%51 = call i32 @libmem_alloc(i8* %49, i32 %50)
store i32 %51, i32* %11
%52 = load i8*, i8** %6
%53 = load i32, i32* %7
%54 = call i32 @libmem_free(i8* %52, i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%56 = load i8*, i8** %6
%57 = call i32 @libmem_alloc(i8* %56, i32 64)
store i32 %57, i32* %7
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%59 = load i8*, i8** %6
%60 = load i32, i32* %9
%61 = call i32 @libmem_free(i8* %59, i32 %60)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%63 = load i8*, i8** %6
%64 = call i32 @libmem_alloc(i8* %63, i32 65536)
store i32 %64, i32* %9
%65 = load i8*, i8** %6
%66 = load i32, i32* %11
%67 = call i32 @libmem_free(i8* %65, i32 %66)
%68 = load i8*, i8** %6
%69 = call i32 @libmem_alloc(i8* %68, i32 65536)
store i32 %69, i32* %11
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%71 = load i8*, i8** %6
%72 = load i32, i32* %7
%73 = call i32 @libmem_free(i8* %71, i32 %72)
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
%75 = load i8*, i8** %6
%76 = load i32, i32* %8
%77 = call i32 @libmem_free(i8* %75, i32 %76)
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0))
%79 = load i8*, i8** %6
%80 = load i32, i32* %9
%81 = call i32 @libmem_free(i8* %79, i32 %80)
%82 = load i8*, i8** %6
%83 = load i32, i32* %10
%84 = call i32 @libmem_free(i8* %82, i32 %83)
%85 = load i32, i32* %12
%86 = icmp eq i32 %85, 255
br i1 %86, label %87, label %90
87: ; preds = %20
%88 = load i8*, i8** %6
%89 = call i32 @libmem_set_flags(i8* %88, i32 1)
br label %90
90: ; preds = %87, %20
%91 = load i8*, i8** %6
%92 = load i32, i32* %11
%93 = call i32 @libmem_free(i8* %91, i32 %92)
br label %94
94: ; preds = %90
%95 = load i32, i32* %12
%96 = add nsw i32 %95, 1
store i32 %96, i32* %12
br label %17
97: ; preds = %17
%98 = load i8*, i8** %6
%99 = call i32 @libmem_deinit(i8* %98)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"1 rd %d\0A\00"
@.str.1 = [9 x i8] c"2 rd %d\0A\00"
@.str.2 = [9 x i8] c"3 rd %d\0A\00"
@.str.3 = [9 x i8] c"4 rd %d\0A\00"
@str = [2 x i8] c"5\00"
@str.10 = [2 x i8] c"6\00"
@str.11 = [2 x i8] c"7\00"
@str.12 = [2 x i8] c"8\00"
@str.13 = [2 x i8] c"9\00"
@str.14 = [3 x i8] c"10\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @time(i32 0)
%4 = tail call i32 @srand(i32 %3)
%5 = tail call i8* @libmem_init(i32 -2147483648, i32 67108864)
br label %6
6: ; preds = %42, %2
%7 = phi i32 [ 0, %2 ], [ %44, %42 ]
%8 = tail call i32 (...) @rand()
%9 = and i32 %8, 65535
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = tail call i32 @libmem_alloc(i8* %5, i32 %9)
%12 = tail call i32 (...) @rand()
%13 = and i32 %12, 65535
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = tail call i32 @libmem_alloc(i8* %5, i32 %13)
%16 = tail call i32 (...) @rand()
%17 = and i32 %16, 65535
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = tail call i32 @libmem_alloc(i8* %5, i32 %17)
%20 = tail call i32 (...) @rand()
%21 = and i32 %20, 65535
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = tail call i32 @libmem_alloc(i8* %5, i32 %21)
%24 = tail call i32 @libmem_alloc(i8* %5, i32 %21)
%25 = tail call i32 @libmem_free(i8* %5, i32 %11)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%27 = tail call i32 @libmem_alloc(i8* %5, i32 64)
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.10, i64 0, i64 0))
%29 = tail call i32 @libmem_free(i8* %5, i32 %19)
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.11, i64 0, i64 0))
%31 = tail call i32 @libmem_alloc(i8* %5, i32 65536)
%32 = tail call i32 @libmem_free(i8* %5, i32 %24)
%33 = tail call i32 @libmem_alloc(i8* %5, i32 65536)
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.12, i64 0, i64 0))
%35 = tail call i32 @libmem_free(i8* %5, i32 %27)
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.13, i64 0, i64 0))
%37 = tail call i32 @libmem_free(i8* %5, i32 %15)
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.14, i64 0, i64 0))
%39 = tail call i32 @libmem_free(i8* %5, i32 %31)
%40 = tail call i32 @libmem_free(i8* %5, i32 %23)
%41 = icmp eq i32 %7, 255
br i1 %41, label %45, label %42
42: ; preds = %6
%43 = tail call i32 @libmem_free(i8* %5, i32 %33)
%44 = add nuw nsw i32 %7, 1
br label %6
45: ; preds = %6
%46 = tail call i32 @libmem_set_flags(i8* %5, i32 1)
%47 = tail call i32 @libmem_free(i8* %5, i32 %33)
%48 = tail call i32 @libmem_deinit(i8* %5)
ret i32 0
}