#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 */
struct timespec {
  int dummy;
};
typedef int max_block_size;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
void *MEM_ALLOC(void *, int);
int /*<<< orphan*/ MEM_FREE(void *, void *);
void *MEM_INIT(void *, int);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int lrand48();
void *malloc(int);
int /*<<< orphan*/ memset(void *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int pool_size = 1234567;
  int max_block_size = 1512;
  int init_requests = 1500, requests = 100000;
  int i, j, k, used, fail;
  size_t inuse = 0;
  struct req {
    void *ptr;
    unsigned int size;
  } m[requests];
  void *pool, *mpool;
  struct timespec t1, t2;
  if ((pool = malloc(pool_size)) == NULL) {
    printf("Malloc return NULL\n");
    return 1;
  }
  memset(pool, 0, pool_size);
  for (j = 0; j < requests; j++) {
    m[j].ptr = NULL;
    m[j].size = 0;
  }
  mpool = MEM_INIT(pool, pool_size);
  used = 0;
  fail = 0;
  for (j = 0; j < init_requests; j++) {
    m[j].size = lrand48() % max_block_size + 4;
    clock_gettime(CLOCK_REALTIME, &t1);
    m[j].ptr = MEM_ALLOC(mpool, m[j].size);
    clock_gettime(CLOCK_REALTIME, &t2);
    if (m[j].ptr != NULL) {
      memset(m[j].ptr, 5, m[j].size);
      used++;
      inuse += m[j].size;

      ;
    } else {
      fail++;
      ;
      ;
      break;
    }
  }
  printf("Start of tests (j=%d, fail=%d, inuse=%lu)!\n", j, fail, inuse);
  fail = 0;
  for (i = 0; i < requests; i++) {
    if (lrand48() & 1) {
      for (j = 0; j < requests && m[j].ptr != NULL; j++)
        ;
      if (j >= requests) {
        printf("No free element in m[]!\n");
        break;
      }
      m[j].size = lrand48() % (max_block_size) + 4;
      clock_gettime(CLOCK_REALTIME, &t1);
      m[j].ptr = MEM_ALLOC(mpool, m[j].size);
      clock_gettime(CLOCK_REALTIME, &t2);
      if (m[j].ptr != NULL) {
        memset(m[j].ptr, 3, m[j].size);
        used++;
        inuse += m[j].size;

        ;
      } else {
        fail++;
        if (fail == 1)
          printf("\tFirst fail (i=%d)!\n", i);
      }
    } else {
      while (used > 0) {
        k = lrand48() % requests;
        if (m[k].ptr != NULL) {
          MEM_FREE(mpool, m[k].ptr);
          m[k].ptr = NULL;
          used--;
          inuse -= m[j].size;
          break;
        }
      }
    }
  }
  printf("End of tests (i=%d, fail=%d, inuse=%lu)!\n", i, fail, inuse);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x12d687,-0x8(%rbp)
movl   $0x5e8,-0xc(%rbp)
movl   $0x5dc,-0x10(%rbp)
movl   $0x186a0,-0x14(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
shl    $0x4,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    120f <main+0x8f>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    163b <main+0x4bb>
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1269 <main+0xe9>
mov    -0x70(%rbp),%rax
movslq -0x1c(%rbp),%rdx
shl    $0x4,%rdx
mov    %rax,%rcx
add    %rdx,%rcx
movq   $0x0,(%rcx)
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
movl   $0x0,0x8(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1224 <main+0xa4>
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1090 <MEM_INIT@plt>
mov    %rax,-0x50(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    139d <main+0x21d>
mov    $0x0,%al
call   1080 <lrand48@plt>
cltd
idivl  -0xc(%rbp)
mov    -0x70(%rbp),%rax
add    $0x4,%edx
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    %edx,0x8(%rax)
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x70(%rbp),%rax
mov    -0x50(%rbp),%rdi
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%esi
call   10a0 <MEM_ALLOC@plt>
mov    %rax,%rcx
mov    -0x70(%rbp),%rax
movslq -0x1c(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,(%rax)
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x70(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
cmpq   $0x0,(%rax)
je     137c <main+0x1fc>
mov    -0x70(%rbp),%rax
movslq -0x1c(%rbp),%rdx
shl    $0x4,%rdx
mov    %rax,%rcx
add    %rdx,%rcx
mov    (%rcx),%rdi
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%edx
mov    $0x5,%esi
call   1050 <memset@plt>
mov    -0x70(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%eax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    138a <main+0x20a>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    139d <main+0x21d>
jmp    138f <main+0x20f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    128e <main+0x10e>
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x30(%rbp),%rcx
lea    0xc66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1615 <main+0x495>
mov    $0x0,%al
call   1080 <lrand48@plt>
and    $0x1,%eax
cmp    $0x0,%eax
je     1565 <main+0x3e5>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x71(%rbp)
jge    1413 <main+0x293>
mov    -0x70(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
cmpq   $0x0,(%rax)
setne  %al
mov    %al,-0x71(%rbp)
mov    -0x71(%rbp),%al
test   $0x1,%al
jne    1423 <main+0x2a3>
jmp    1436 <main+0x2b6>
jmp    1428 <main+0x2a8>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    13e9 <main+0x269>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1455 <main+0x2d5>
lea    0xbf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1615 <main+0x495>
mov    $0x0,%al
call   1080 <lrand48@plt>
cltd
idivl  -0xc(%rbp)
mov    -0x70(%rbp),%rax
add    $0x4,%edx
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    %edx,0x8(%rax)
mov    0x2b64(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x70(%rbp),%rax
mov    -0x50(%rbp),%rdi
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%esi
call   10a0 <MEM_ALLOC@plt>
mov    %rax,%rcx
mov    -0x70(%rbp),%rax
movslq -0x1c(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,(%rax)
mov    0x2b22(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x70(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
cmpq   $0x0,(%rax)
je     1537 <main+0x3b7>
mov    -0x70(%rbp),%rax
movslq -0x1c(%rbp),%rdx
shl    $0x4,%rdx
mov    %rax,%rcx
add    %rdx,%rcx
mov    (%rcx),%rdi
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%edx
mov    $0x3,%esi
call   1050 <memset@plt>
mov    -0x70(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%eax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1560 <main+0x3e0>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
jne    155b <main+0x3db>
mov    -0x18(%rbp),%esi
lea    0xb05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1560 <main+0x3e0>
jmp    1602 <main+0x482>
jmp    156a <main+0x3ea>
cmpl   $0x0,-0x24(%rbp)
jle    15fd <main+0x47d>
mov    $0x0,%al
call   1080 <lrand48@plt>
cltd
idivl  -0x14(%rbp)
mov    -0x70(%rbp),%rax
mov    %edx,-0x20(%rbp)
movslq -0x20(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
cmpq   $0x0,(%rax)
je     15f8 <main+0x478>
mov    -0x70(%rbp),%rax
mov    -0x50(%rbp),%rdi
movslq -0x20(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rsi
call   1060 <MEM_FREE@plt>
mov    -0x70(%rbp),%rax
movslq -0x20(%rbp),%rdx
shl    $0x4,%rdx
mov    %rax,%rcx
add    %rdx,%rcx
movq   $0x0,(%rcx)
mov    -0x24(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x24(%rbp)
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%eax
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    15fd <main+0x47d>
jmp    156a <main+0x3ea>
jmp    1602 <main+0x482>
jmp    1607 <main+0x487>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    13c3 <main+0x243>
mov    -0x18(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x30(%rbp),%rcx
lea    0xa48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x186a28,%rsp
mov    $0x12d687,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12c0 <main+0x130>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x12d687,%edx
call   1060 <memset@plt>
mov    $0x18,%eax
nopl   0x0(%rax,%rax,1)
movq   $0x0,0x8(%rsp,%rax,1)
movl   $0x0,0x10(%rsp,%rax,1)
movq   $0x0,0x18(%rsp,%rax,1)
movl   $0x0,0x20(%rsp,%rax,1)
add    $0x20,%rax
cmp    $0x186a18,%rax
jne    11d0 <main+0x40>
mov    %rbx,%rdi
mov    $0x12d687,%esi
call   10a0 <MEM_INIT@plt>
mov    %rax,0x8(%rsp)
lea    0x28(%rsp),%r12
xor    %ebx,%ebx
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <lrand48@plt>
cltq
imul   $0xffffffffad602b59,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0xa,%ecx
add    %edx,%ecx
imul   $0x5e8,%ecx,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%ebp
add    $0x4,%ebp
mov    %ebp,(%r12)
mov    0x2d87(%rip),%r13
mov    0x0(%r13),%edi
lea    0x18(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x8(%rsp),%rdi
mov    %ebp,%esi
call   10b0 <MEM_ALLOC@plt>
mov    %rax,%r14
mov    %rax,-0x8(%r12)
mov    0x0(%r13),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %r14,%r14
je     12d6 <main+0x146>
mov    %r14,%rdi
mov    $0x5,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
add    $0x1,%rbx
mov    %ebp,%eax
add    %rax,%r15
add    $0x10,%r12
cmp    $0x5dc,%rbx
jne    1220 <main+0x90>
xor    %edx,%edx
mov    $0x5dc,%ebx
jmp    12db <main+0x14b>
lea    0xdbc(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    151d <main+0x38d>
mov    $0x1,%edx
lea    0xd1e(%rip),%rdi
mov    %ebx,%esi
mov    %r15,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %r14d,%r14d
mov    %ebx,%r12d
movl   $0x0,0x4(%rsp)
jmp    13b8 <main+0x228>
mov    %r12d,(%rsp)
add    $0xfffffffffffffffd,%rbx
xor    %eax,%eax
call   1090 <lrand48@plt>
cltq
imul   $0xffffffffad602b59,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0xa,%ecx
add    %edx,%ecx
imul   $0x5e8,%ecx,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%r13d
add    $0x4,%r13d
mov    %ebx,%r12d
shl    $0x4,%r12
mov    %r13d,0x28(%rsp,%r12,1)
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
lea    0x18(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x8(%rsp),%rdi
mov    %r13d,%esi
call   10b0 <MEM_ALLOC@plt>
mov    %rax,%rbp
mov    %rax,0x20(%rsp,%r12,1)
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %rbp,%rbp
je     1489 <main+0x2f9>
mov    %rbp,%rdi
mov    $0x3,%esi
mov    %r13d,%edx
call   1060 <memset@plt>
mov    (%rsp),%r12d
add    $0x1,%r12d
mov    %r13d,%eax
add    %rax,%r15
add    $0x1,%r14d
cmp    $0x186a0,%r14d
je     14fd <main+0x36d>
xor    %eax,%eax
call   1090 <lrand48@plt>
test   $0x1,%al
jne    1440 <main+0x2b0>
test   %r12d,%r12d
jle    13a7 <main+0x217>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <lrand48@plt>
cltq
imul   $0x14f8b589,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x2d,%rcx
add    %edx,%ecx
imul   $0x186a0,%ecx,%ecx
sub    %ecx,%eax
cltq
shl    $0x4,%rax
mov    0x20(%rsp,%rax,1),%rsi
test   %rsi,%rsi
je     13d0 <main+0x240>
lea    (%rsp,%rax,1),%rbp
add    $0x20,%rbp
mov    0x8(%rsp),%rdi
call   1070 <MEM_FREE@plt>
movq   $0x0,0x0(%rbp)
add    $0xffffffff,%r12d
movslq %ebx,%rax
shl    $0x4,%rax
mov    0x28(%rsp,%rax,1),%eax
sub    %rax,%r15
jmp    13a7 <main+0x217>
cs nopw 0x0(%rax,%rax,1)
mov    $0x4,%ebx
lea    0x60(%rsp),%rax
nopw   0x0(%rax,%rax,1)
cmpq   $0x0,-0x40(%rax)
je     14a1 <main+0x311>
cmpq   $0x0,-0x30(%rax)
je     1301 <main+0x171>
cmpq   $0x0,-0x20(%rax)
je     14ae <main+0x31e>
cmpq   $0x0,-0x10(%rax)
je     14bb <main+0x32b>
cmpq   $0x0,(%rax)
je     14c8 <main+0x338>
add    $0x5,%rbx
add    $0x50,%rax
cmp    $0x186a4,%rbx
jne    1450 <main+0x2c0>
jmp    14ef <main+0x35f>
mov    0x4(%rsp),%eax
test   %eax,%eax
mov    (%rsp),%r12d
je     14d1 <main+0x341>
add    $0x1,%eax
mov    %eax,0x4(%rsp)
jmp    13a7 <main+0x217>
mov    %r12d,(%rsp)
add    $0xfffffffffffffffc,%rbx
jmp    1309 <main+0x179>
mov    %r12d,(%rsp)
add    $0xfffffffffffffffe,%rbx
jmp    1309 <main+0x179>
mov    %r12d,(%rsp)
add    $0xffffffffffffffff,%rbx
jmp    1309 <main+0x179>
mov    %r12d,(%rsp)
jmp    1309 <main+0x179>
lea    0xb54(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x1,0x4(%rsp)
jmp    13a7 <main+0x217>
lea    0xb75(%rip),%rdi
call   1030 <puts@plt>
jmp    1503 <main+0x373>
mov    $0x186a0,%r14d
lea    0xb37(%rip),%rdi
xor    %ebx,%ebx
mov    %r14d,%esi
mov    0x4(%rsp),%edx
mov    %r15,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x186a28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
%struct.req = type { i8*, i32 }
@.str = [20 x i8] c"Malloc return NULL\0A\00"
@CLOCK_REALTIME = global i32 0
@.str.1 = [44 x i8] c"Start of tests (j=%d, fail=%d, inuse=%lu)!\0A\00"
@.str.2 = [25 x i8] c"No free element in m[]!\0A\00"
@.str.3 = [21 x i8] c"\09First fail (i=%d)!\0A\00"
@.str.4 = [42 x i8] c"End of tests (i=%d, fail=%d, inuse=%lu)!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca %struct.timespec
%17 = alloca %struct.timespec
%18 = alloca i32
store i32 0, i32* %1
store i32 1234567, i32* %2
store i32 1512, i32* %3
store i32 1500, i32* %4
store i32 100000, i32* %5
store i64 0, i64* %11
%19 = load i32, i32* %5
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %12
%22 = alloca %struct.req, i64 %20
store i64 %20, i64* %13
%23 = load i32, i32* %2
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %14
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
store i32 1, i32* %18
br label %261
28: ; preds = %0
%29 = load i8*, i8** %14
%30 = load i32, i32* %2
%31 = call i32 @memset(i8* %29, i32 0, i32 %30)
store i32 0, i32* %7
br label %32
32: ; preds = %45, %28
%33 = load i32, i32* %7
%34 = load i32, i32* %5
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %48
36: ; preds = %32
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %38
%40 = getelementptr inbounds %struct.req, %struct.req* %39, i32 0, i32 0
store i8* null, i8** %40
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %42
%44 = getelementptr inbounds %struct.req, %struct.req* %43, i32 0, i32 1
store i32 0, i32* %44
br label %45
45: ; preds = %36
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
br label %32
48: ; preds = %32
%49 = load i8*, i8** %14
%50 = load i32, i32* %2
%51 = call i8* @MEM_INIT(i8* %49, i32 %50)
store i8* %51, i8** %15
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %7
br label %52
52: ; preds = %112, %48
%53 = load i32, i32* %7
%54 = load i32, i32* %4
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %115
56: ; preds = %52
%57 = call i32 (...) @lrand48()
%58 = load i32, i32* %3
%59 = srem i32 %57, %58
%60 = add nsw i32 %59, 4
%61 = load i32, i32* %7
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %62
%64 = getelementptr inbounds %struct.req, %struct.req* %63, i32 0, i32 1
store i32 %60, i32* %64
%65 = load i32, i32* @CLOCK_REALTIME
%66 = call i32 @clock_gettime(i32 %65, %struct.timespec* %16)
%67 = load i8*, i8** %15
%68 = load i32, i32* %7
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %69
%71 = getelementptr inbounds %struct.req, %struct.req* %70, i32 0, i32 1
%72 = load i32, i32* %71
%73 = call i8* @MEM_ALLOC(i8* %67, i32 %72)
%74 = load i32, i32* %7
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %75
%77 = getelementptr inbounds %struct.req, %struct.req* %76, i32 0, i32 0
store i8* %73, i8** %77
%78 = load i32, i32* @CLOCK_REALTIME
%79 = call i32 @clock_gettime(i32 %78, %struct.timespec* %17)
%80 = load i32, i32* %7
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %81
%83 = getelementptr inbounds %struct.req, %struct.req* %82, i32 0, i32 0
%84 = load i8*, i8** %83
%85 = icmp ne i8* %84, null
br i1 %85, label %86, label %108
86: ; preds = %56
%87 = load i32, i32* %7
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %88
%90 = getelementptr inbounds %struct.req, %struct.req* %89, i32 0, i32 0
%91 = load i8*, i8** %90
%92 = load i32, i32* %7
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %93
%95 = getelementptr inbounds %struct.req, %struct.req* %94, i32 0, i32 1
%96 = load i32, i32* %95
%97 = call i32 @memset(i8* %91, i32 5, i32 %96)
%98 = load i32, i32* %9
%99 = add nsw i32 %98, 1
store i32 %99, i32* %9
%100 = load i32, i32* %7
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %101
%103 = getelementptr inbounds %struct.req, %struct.req* %102, i32 0, i32 1
%104 = load i32, i32* %103
%105 = zext i32 %104 to i64
%106 = load i64, i64* %11
%107 = add i64 %106, %105
store i64 %107, i64* %11
br label %111
108: ; preds = %56
%109 = load i32, i32* %10
%110 = add nsw i32 %109, 1
store i32 %110, i32* %10
br label %115
111: ; preds = %86
br label %112
112: ; preds = %111
%113 = load i32, i32* %7
%114 = add nsw i32 %113, 1
store i32 %114, i32* %7
br label %52
115: ; preds = %108, %52
%116 = load i32, i32* %7
%117 = load i32, i32* %10
%118 = load i64, i64* %11
%119 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %116, i32 %117, i64 %118)
store i32 0, i32* %10
store i32 0, i32* %6
br label %120
120: ; preds = %253, %115
%121 = load i32, i32* %6
%122 = load i32, i32* %5
%123 = icmp slt i32 %121, %122
br i1 %123, label %124, label %256
124: ; preds = %120
%125 = call i32 (...) @lrand48()
%126 = and i32 %125, 1
%127 = icmp ne i32 %126, 0
br i1 %127, label %128, label %214
128: ; preds = %124
store i32 0, i32* %7
br label %129
129: ; preds = %143, %128
%130 = load i32, i32* %7
%131 = load i32, i32* %5
%132 = icmp slt i32 %130, %131
br i1 %132, label %133, label %140
133: ; preds = %129
%134 = load i32, i32* %7
%135 = sext i32 %134 to i64
%136 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %135
%137 = getelementptr inbounds %struct.req, %struct.req* %136, i32 0, i32 0
%138 = load i8*, i8** %137
%139 = icmp ne i8* %138, null
br label %140
140: ; preds = %133, %129
%141 = phi i1 [ false, %129 ], [ %139, %133 ]
br i1 %141, label %142, label %146
142: ; preds = %140
br label %143
143: ; preds = %142
%144 = load i32, i32* %7
%145 = add nsw i32 %144, 1
store i32 %145, i32* %7
br label %129
146: ; preds = %140
%147 = load i32, i32* %7
%148 = load i32, i32* %5
%149 = icmp sge i32 %147, %148
br i1 %149, label %150, label %152
150: ; preds = %146
%151 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %256
152: ; preds = %146
%153 = call i32 (...) @lrand48()
%154 = load i32, i32* %3
%155 = srem i32 %153, %154
%156 = add nsw i32 %155, 4
%157 = load i32, i32* %7
%158 = sext i32 %157 to i64
%159 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %158
%160 = getelementptr inbounds %struct.req, %struct.req* %159, i32 0, i32 1
store i32 %156, i32* %160
%161 = load i32, i32* @CLOCK_REALTIME
%162 = call i32 @clock_gettime(i32 %161, %struct.timespec* %16)
%163 = load i8*, i8** %15
%164 = load i32, i32* %7
%165 = sext i32 %164 to i64
%166 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %165
%167 = getelementptr inbounds %struct.req, %struct.req* %166, i32 0, i32 1
%168 = load i32, i32* %167
%169 = call i8* @MEM_ALLOC(i8* %163, i32 %168)
%170 = load i32, i32* %7
%171 = sext i32 %170 to i64
%172 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %171
%173 = getelementptr inbounds %struct.req, %struct.req* %172, i32 0, i32 0
store i8* %169, i8** %173
%174 = load i32, i32* @CLOCK_REALTIME
%175 = call i32 @clock_gettime(i32 %174, %struct.timespec* %17)
%176 = load i32, i32* %7
%177 = sext i32 %176 to i64
%178 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %177
%179 = getelementptr inbounds %struct.req, %struct.req* %178, i32 0, i32 0
%180 = load i8*, i8** %179
%181 = icmp ne i8* %180, null
br i1 %181, label %182, label %204
182: ; preds = %152
%183 = load i32, i32* %7
%184 = sext i32 %183 to i64
%185 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %184
%186 = getelementptr inbounds %struct.req, %struct.req* %185, i32 0, i32 0
%187 = load i8*, i8** %186
%188 = load i32, i32* %7
%189 = sext i32 %188 to i64
%190 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %189
%191 = getelementptr inbounds %struct.req, %struct.req* %190, i32 0, i32 1
%192 = load i32, i32* %191
%193 = call i32 @memset(i8* %187, i32 3, i32 %192)
%194 = load i32, i32* %9
%195 = add nsw i32 %194, 1
store i32 %195, i32* %9
%196 = load i32, i32* %7
%197 = sext i32 %196 to i64
%198 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %197
%199 = getelementptr inbounds %struct.req, %struct.req* %198, i32 0, i32 1
%200 = load i32, i32* %199
%201 = zext i32 %200 to i64
%202 = load i64, i64* %11
%203 = add i64 %202, %201
store i64 %203, i64* %11
br label %213
204: ; preds = %152
%205 = load i32, i32* %10
%206 = add nsw i32 %205, 1
store i32 %206, i32* %10
%207 = load i32, i32* %10
%208 = icmp eq i32 %207, 1
br i1 %208, label %209, label %212
209: ; preds = %204
%210 = load i32, i32* %6
%211 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %210)
br label %212
212: ; preds = %209, %204
br label %213
213: ; preds = %212, %182
br label %252
214: ; preds = %124
br label %215
215: ; preds = %250, %214
%216 = load i32, i32* %9
%217 = icmp sgt i32 %216, 0
br i1 %217, label %218, label %251
218: ; preds = %215
%219 = call i32 (...) @lrand48()
%220 = load i32, i32* %5
%221 = srem i32 %219, %220
store i32 %221, i32* %8
%222 = load i32, i32* %8
%223 = sext i32 %222 to i64
%224 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %223
%225 = getelementptr inbounds %struct.req, %struct.req* %224, i32 0, i32 0
%226 = load i8*, i8** %225
%227 = icmp ne i8* %226, null
br i1 %227, label %228, label %250
228: ; preds = %218
%229 = load i8*, i8** %15
%230 = load i32, i32* %8
%231 = sext i32 %230 to i64
%232 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %231
%233 = getelementptr inbounds %struct.req, %struct.req* %232, i32 0, i32 0
%234 = load i8*, i8** %233
%235 = call i32 @MEM_FREE(i8* %229, i8* %234)
%236 = load i32, i32* %8
%237 = sext i32 %236 to i64
%238 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %237
%239 = getelementptr inbounds %struct.req, %struct.req* %238, i32 0, i32 0
store i8* null, i8** %239
%240 = load i32, i32* %9
%241 = add nsw i32 %240, -1
store i32 %241, i32* %9
%242 = load i32, i32* %7
%243 = sext i32 %242 to i64
%244 = getelementptr inbounds %struct.req, %struct.req* %22, i64 %243
%245 = getelementptr inbounds %struct.req, %struct.req* %244, i32 0, i32 1
%246 = load i32, i32* %245
%247 = zext i32 %246 to i64
%248 = load i64, i64* %11
%249 = sub i64 %248, %247
store i64 %249, i64* %11
br label %251
250: ; preds = %218
br label %215
251: ; preds = %228, %215
br label %252
252: ; preds = %251, %213
br label %253
253: ; preds = %252
%254 = load i32, i32* %6
%255 = add nsw i32 %254, 1
store i32 %255, i32* %6
br label %120
256: ; preds = %150, %120
%257 = load i32, i32* %6
%258 = load i32, i32* %10
%259 = load i64, i64* %11
%260 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %257, i32 %258, i64 %259)
store i32 0, i32* %1
store i32 1, i32* %18
br label %261
261: ; preds = %256, %26
%262 = load i8*, i8** %12
%263 = load i32, i32* %1
ret i32 %263
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
%struct.req = type { i8*, i32 }
@CLOCK_REALTIME = global i32 0
@.str.1 = [44 x i8] c"Start of tests (j=%d, fail=%d, inuse=%lu)!\0A\00"
@.str.3 = [21 x i8] c"\09First fail (i=%d)!\0A\00"
@.str.4 = [42 x i8] c"End of tests (i=%d, fail=%d, inuse=%lu)!\0A\00"
@str = [24 x i8] c"No free element in m[]!\00"
@str.5 = [19 x i8] c"Malloc return NULL\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca [100000 x %struct.req]
%4 = bitcast %struct.timespec* %1 to i8*
%5 = bitcast %struct.timespec* %2 to i8*
%6 = tail call dereferenceable_or_null(1234567) i8* @malloc(i32 1234567)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
br label %143
10: ; preds = %0
%11 = tail call i32 @memset(i8* %6, i32 0, i32 1234567)
br label %12
12: ; preds = %12, %10
%13 = phi i64 [ 0, %10 ], [ %19, %12 ]
%14 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %13, i32 0
store i8* null, i8** %14
%15 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %13, i32 1
store i32 0, i32* %15
%16 = or i64 %13, 1
%17 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %16, i32 0
store i8* null, i8** %17
%18 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %16, i32 1
store i32 0, i32* %18
%19 = add nuw nsw i64 %13, 2
%20 = icmp eq i64 %19, 100000
br i1 %20, label %21, label %12
21: ; preds = %12
%22 = tail call i8* @MEM_INIT(i8* %6, i32 1234567)
br label %23
23: ; preds = %21, %37
%24 = phi i64 [ 0, %21 ], [ %39, %37 ]
%25 = phi i64 [ 0, %21 ], [ %41, %37 ]
%26 = call i32 (...) @lrand48()
%27 = srem i32 %26, 1512
%28 = add nsw i32 %27, 4
%29 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %24, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* @CLOCK_REALTIME
%31 = call i32 @clock_gettime(i32 %30, %struct.timespec* %1)
%32 = call i8* @MEM_ALLOC(i8* %22, i32 %28)
%33 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %24, i32 0
store i8* %32, i8** %33
%34 = load i32, i32* @CLOCK_REALTIME
%35 = call i32 @clock_gettime(i32 %34, %struct.timespec* %2)
%36 = icmp eq i8* %32, null
br i1 %36, label %43, label %37
37: ; preds = %23
%38 = call i32 @memset(i8* %32, i32 5, i32 %28)
%39 = add nuw nsw i64 %24, 1
%40 = zext i32 %28 to i64
%41 = add i64 %25, %40
%42 = icmp eq i64 %39, 1500
br i1 %42, label %45, label %23
43: ; preds = %23
%44 = trunc i64 %24 to i32
br label %45
45: ; preds = %37, %43
%46 = phi i64 [ %25, %43 ], [ %41, %37 ]
%47 = phi i32 [ %44, %43 ], [ 1500, %37 ]
%48 = phi i32 [ 1, %43 ], [ 0, %37 ]
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %47, i32 %48, i64 %46)
br label %50
50: ; preds = %45, %131
%51 = phi i32 [ 0, %45 ], [ %136, %131 ]
%52 = phi i32 [ %47, %45 ], [ %135, %131 ]
%53 = phi i32 [ %47, %45 ], [ %134, %131 ]
%54 = phi i64 [ %46, %45 ], [ %133, %131 ]
%55 = phi i32 [ 0, %45 ], [ %132, %131 ]
%56 = call i32 (...) @lrand48()
%57 = and i32 %56, 1
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %50
%60 = icmp sgt i32 %53, 0
br i1 %60, label %116, label %131
61: ; preds = %50, %86
%62 = phi i64 [ %87, %86 ], [ 0, %50 ]
%63 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %62, i32 0
%64 = load i8*, i8** %63
%65 = icmp eq i8* %64, null
br i1 %65, label %91, label %66
66: ; preds = %61
%67 = add nuw nsw i64 %62, 1
%68 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %67, i32 0
%69 = load i8*, i8** %68
%70 = icmp eq i8* %69, null
br i1 %70, label %91, label %71
71: ; preds = %66
%72 = add nuw nsw i64 %62, 2
%73 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %72, i32 0
%74 = load i8*, i8** %73
%75 = icmp eq i8* %74, null
br i1 %75, label %91, label %76
76: ; preds = %71
%77 = add nuw nsw i64 %62, 3
%78 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %77, i32 0
%79 = load i8*, i8** %78
%80 = icmp eq i8* %79, null
br i1 %80, label %91, label %81
81: ; preds = %76
%82 = add nuw nsw i64 %62, 4
%83 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %82, i32 0
%84 = load i8*, i8** %83
%85 = icmp eq i8* %84, null
br i1 %85, label %91, label %86
86: ; preds = %81
%87 = add nuw nsw i64 %62, 5
%88 = icmp eq i64 %87, 100000
br i1 %88, label %89, label %61
89: ; preds = %86
%90 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %138
91: ; preds = %81, %76, %71, %66, %61
%92 = phi i64 [ %62, %61 ], [ %67, %66 ], [ %72, %71 ], [ %77, %76 ], [ %82, %81 ]
%93 = trunc i64 %92 to i32
%94 = call i32 (...) @lrand48()
%95 = srem i32 %94, 1512
%96 = add nsw i32 %95, 4
%97 = and i64 %92, 4294967295
%98 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %97, i32 1
store i32 %96, i32* %98
%99 = load i32, i32* @CLOCK_REALTIME
%100 = call i32 @clock_gettime(i32 %99, %struct.timespec* %1)
%101 = call i8* @MEM_ALLOC(i8* %22, i32 %96)
%102 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %97, i32 0
store i8* %101, i8** %102
%103 = load i32, i32* @CLOCK_REALTIME
%104 = call i32 @clock_gettime(i32 %103, %struct.timespec* %2)
%105 = icmp eq i8* %101, null
br i1 %105, label %111, label %106
106: ; preds = %91
%107 = call i32 @memset(i8* %101, i32 3, i32 %96)
%108 = add nsw i32 %53, 1
%109 = zext i32 %96 to i64
%110 = add i64 %54, %109
br label %131
111: ; preds = %91
%112 = add nsw i32 %55, 1
%113 = icmp eq i32 %55, 0
br i1 %113, label %114, label %131
114: ; preds = %111
%115 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %51)
br label %131
116: ; preds = %59, %116
%117 = call i32 (...) @lrand48()
%118 = srem i32 %117, 100000
%119 = sext i32 %118 to i64
%120 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %119, i32 0
%121 = load i8*, i8** %120
%122 = icmp eq i8* %121, null
br i1 %122, label %116, label %123
123: ; preds = %116
%124 = call i32 @MEM_FREE(i8* %22, i8* %121)
store i8* null, i8** %120
%125 = add nsw i32 %53, -1
%126 = sext i32 %52 to i64
%127 = getelementptr inbounds [100000 x %struct.req], [100000 x %struct.req]* %3, i64 0, i64 %126, i32 1
%128 = load i32, i32* %127
%129 = zext i32 %128 to i64
%130 = sub i64 %54, %129
br label %131
131: ; preds = %59, %111, %114, %106, %123
%132 = phi i32 [ %55, %106 ], [ 1, %114 ], [ %112, %111 ], [ %55, %123 ], [ %55, %59 ]
%133 = phi i64 [ %110, %106 ], [ %54, %114 ], [ %54, %111 ], [ %130, %123 ], [ %54, %59 ]
%134 = phi i32 [ %108, %106 ], [ %53, %114 ], [ %53, %111 ], [ %125, %123 ], [ %53, %59 ]
%135 = phi i32 [ %93, %106 ], [ %93, %114 ], [ %93, %111 ], [ %52, %123 ], [ %52, %59 ]
%136 = add nuw nsw i32 %51, 1
%137 = icmp eq i32 %136, 100000
br i1 %137, label %138, label %50
138: ; preds = %131, %89
%139 = phi i32 [ %55, %89 ], [ %132, %131 ]
%140 = phi i64 [ %54, %89 ], [ %133, %131 ]
%141 = phi i32 [ %51, %89 ], [ 100000, %131 ]
%142 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %141, i32 %139, i64 %140)
br label %143
143: ; preds = %138, %8
%144 = phi i32 [ 1, %8 ], [ 0, %138 ]
ret i32 %144
}