#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 */
void *MEM_alloc(int);
int /*<<< orphan*/ MEM_free(void *);
int /*<<< orphan*/ printf(char *, int);
int rand();
void test_mem(void) {
  void *foo[32];
  int i, j;
  int oom_count;
  oom_count = 0;
  for (i = 0; i < 32; i++) {
    foo[i] = 0;
  }
  for (j = 0; j < 1024; j++) {
    i = rand() % 32;
    if (foo[i] != 0) {
      MEM_free(foo[i]);
    }
    foo[i] = MEM_alloc(rand() % 1000 + 10);
    if (foo[i] == 0) {
      oom_count++;
    }
  }
  for (i = 0; i < 32; i++) {
    if (foo[i] != 0) {
      MEM_free(foo[i]);
    }
  }
  printf("Couldn't allocate %d times\n", oom_count);
}


x86 assembly (clang, -O0):
<test_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x10c(%rbp)
movl   $0x0,-0x104(%rbp)
cmpl   $0x20,-0x104(%rbp)
jge    1193 <test_mem+0x53>
movslq -0x104(%rbp),%rax
movq   $0x0,-0x100(%rbp,%rax,8)
mov    -0x104(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x104(%rbp)
jmp    115f <test_mem+0x1f>
movl   $0x0,-0x108(%rbp)
cmpl   $0x400,-0x108(%rbp)
jge    1255 <test_mem+0x115>
mov    $0x0,%al
call   1060 <rand@plt>
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %edx,-0x104(%rbp)
movslq -0x104(%rbp),%rax
cmpq   $0x0,-0x100(%rbp,%rax,8)
je     11ec <test_mem+0xac>
movslq -0x104(%rbp),%rax
mov    -0x100(%rbp,%rax,8),%rdi
ff
call   1050 <MEM_free@plt>
mov    $0x0,%al
call   1060 <rand@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
add    $0xa,%edi
call   1040 <MEM_alloc@plt>
mov    %rax,%rcx
movslq -0x104(%rbp),%rax
mov    %rcx,-0x100(%rbp,%rax,8)
ff
movslq -0x104(%rbp),%rax
cmpq   $0x0,-0x100(%rbp,%rax,8)
jne    123c <test_mem+0xfc>
mov    -0x10c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10c(%rbp)
jmp    1241 <test_mem+0x101>
mov    -0x108(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x108(%rbp)
jmp    119d <test_mem+0x5d>
movl   $0x0,-0x104(%rbp)
cmpl   $0x20,-0x104(%rbp)
jge    12af <test_mem+0x16f>
movslq -0x104(%rbp),%rax
cmpq   $0x0,-0x100(%rbp,%rax,8)
je     1296 <test_mem+0x156>
movslq -0x104(%rbp),%rax
mov    -0x100(%rbp,%rax,8),%rdi
ff
call   1050 <MEM_free@plt>
jmp    129b <test_mem+0x15b>
mov    -0x104(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x104(%rbp)
jmp    125f <test_mem+0x11f>
mov    -0x10c(%rbp),%esi
lea    0xd44(%rip),%rdi
call   1030 <printf@plt>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_mem>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    $0x400,%ebp
xor    %ebx,%ebx
jmp    1200 <test_mem+0xc0>
xchg   %ax,%ax
xor    %eax,%eax
call   1060 <rand@plt>
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
imul   $0x3e8,%ecx,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0xa,%edi
call   1040 <MEM_alloc@plt>
mov    %rax,(%rsp,%r14,8)
cmp    $0x1,%rax
adc    $0x0,%ebx
add    $0xffffffff,%ebp
je     1227 <test_mem+0xe7>
xor    %eax,%eax
call   1060 <rand@plt>
lea    0x1f(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffffffe0,%ecx
sub    %ecx,%eax
movslq %eax,%r14
mov    (%rsp,%r14,8),%rdi
test   %rdi,%rdi
je     11c0 <test_mem+0x80>
call   1050 <MEM_free@plt>
jmp    11c0 <test_mem+0x80>
xor    %ebp,%ebp
jmp    123a <test_mem+0xfa>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    $0x20,%rbp
je     124a <test_mem+0x10a>
mov    (%rsp,%rbp,8),%rdi
test   %rdi,%rdi
je     1230 <test_mem+0xf0>
call   1050 <MEM_free@plt>
jmp    1230 <test_mem+0xf0>
lea    0xdaf(%rip),%rdi
mov    %ebx,%esi
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"Couldn't allocate %d times\0A\00"
define void @test_mem() {
%1 = alloca [32 x i8*]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %4
store i32 0, i32* %2
br label %5
5: ; preds = %12, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 32
br i1 %7, label %8, label %15
8: ; preds = %5
%9 = load i32, i32* %2
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [32 x i8*], [32 x i8*]* %1, i64 0, i64 %10
store i8* null, i8** %11
br label %12
12: ; preds = %8
%13 = load i32, i32* %2
%14 = add nsw i32 %13, 1
store i32 %14, i32* %2
br label %5
15: ; preds = %5
store i32 0, i32* %3
br label %16
16: ; preds = %50, %15
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 1024
br i1 %18, label %19, label %53
19: ; preds = %16
%20 = call i32 (...) @rand()
%21 = srem i32 %20, 32
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [32 x i8*], [32 x i8*]* %1, i64 0, i64 %23
%25 = load i8*, i8** %24
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %33
27: ; preds = %19
%28 = load i32, i32* %2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [32 x i8*], [32 x i8*]* %1, i64 0, i64 %29
%31 = load i8*, i8** %30
%32 = call i32 @MEM_free(i8* %31)
br label %33
33: ; preds = %27, %19
%34 = call i32 (...) @rand()
%35 = srem i32 %34, 1000
%36 = add nsw i32 %35, 10
%37 = call i8* @MEM_alloc(i32 %36)
%38 = load i32, i32* %2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [32 x i8*], [32 x i8*]* %1, i64 0, i64 %39
store i8* %37, i8** %40
%41 = load i32, i32* %2
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [32 x i8*], [32 x i8*]* %1, i64 0, i64 %42
%44 = load i8*, i8** %43
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %49
46: ; preds = %33
%47 = load i32, i32* %4
%48 = add nsw i32 %47, 1
store i32 %48, i32* %4
br label %49
49: ; preds = %46, %33
br label %50
50: ; preds = %49
%51 = load i32, i32* %3
%52 = add nsw i32 %51, 1
store i32 %52, i32* %3
br label %16
53: ; preds = %16
store i32 0, i32* %2
br label %54
54: ; preds = %70, %53
%55 = load i32, i32* %2
%56 = icmp slt i32 %55, 32
br i1 %56, label %57, label %73
57: ; preds = %54
%58 = load i32, i32* %2
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [32 x i8*], [32 x i8*]* %1, i64 0, i64 %59
%61 = load i8*, i8** %60
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %69
63: ; preds = %57
%64 = load i32, i32* %2
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [32 x i8*], [32 x i8*]* %1, i64 0, i64 %65
%67 = load i8*, i8** %66
%68 = call i32 @MEM_free(i8* %67)
br label %69
69: ; preds = %63, %57
br label %70
70: ; preds = %69
%71 = load i32, i32* %2
%72 = add nsw i32 %71, 1
store i32 %72, i32* %2
br label %54
73: ; preds = %54
%74 = load i32, i32* %4
%75 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %74)
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"Couldn't allocate %d times\0A\00"
define void @test_mem() {
%1 = alloca [32 x i8*]
%2 = bitcast [32 x i8*]* %1 to i8*
br label %3
3: ; preds = %0, %14
%4 = phi i32 [ %21, %14 ], [ 0, %0 ]
%5 = phi i32 [ %22, %14 ], [ 0, %0 ]
%6 = tail call i32 (...) @rand()
%7 = srem i32 %6, 32
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds [32 x i8*], [32 x i8*]* %1, i64 0, i64 %8
%10 = load i8*, i8** %9
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %3
%13 = tail call i32 @MEM_free(i8* %10)
br label %14
14: ; preds = %12, %3
%15 = tail call i32 (...) @rand()
%16 = srem i32 %15, 1000
%17 = add nsw i32 %16, 10
%18 = tail call i8* @MEM_alloc(i32 %17)
store i8* %18, i8** %9
%19 = icmp eq i8* %18, null
%20 = zext i1 %19 to i32
%21 = add nuw nsw i32 %4, %20
%22 = add nuw nsw i32 %5, 1
%23 = icmp eq i32 %22, 1024
br i1 %23, label %24, label %3
24: ; preds = %14, %31
%25 = phi i64 [ %32, %31 ], [ 0, %14 ]
%26 = getelementptr inbounds [32 x i8*], [32 x i8*]* %1, i64 0, i64 %25
%27 = load i8*, i8** %26
%28 = icmp eq i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %24
%30 = tail call i32 @MEM_free(i8* %27)
br label %31
31: ; preds = %24, %29
%32 = add nuw nsw i64 %25, 1
%33 = icmp eq i64 %32, 32
br i1 %33, label %34, label %24
34: ; preds = %31
%35 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %21)
ret void
}