#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 */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ alloc(void **, int);
int /*<<< orphan*/ memset(void **, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int random();
int /*<<< orphan*/ release(void **, int);
void *thread_job(void *arg) {
  void *pool[1024];
  int i;
  unsigned long count = 0;
  memset(pool, 0, sizeof(void *) * 1024);
  for (i = 0; i < 1114; i++) {
    switch (random() % 2) {
    case 0:
      alloc(pool, random() % 1024);
      break;
    case 1:
      release(pool, random() % 1024);
      break;
    default:
      printf("EER\n");
      abort();
    }
  }
  for (i = 0; i < 1024; i++) {
    if (pool[i] != NULL) {
      count++;
    }
  }
  printf("Still allocated: %lu\n", count);
  return (void *)count;
}


x86 assembly (clang, -O0):
<thread_job>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x2020(%rbp)
lea    -0x2010(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1050 <memset@plt>
movl   $0x0,-0x2014(%rbp)
cmpl   $0x45a,-0x2014(%rbp)
jge    126f <thread_job+0x10f>
xor    %eax,%eax
call   1060 <random@plt>
mov    %eax,%edx
shr    $0x1f,%edx
mov    %eax,%ecx
add    %edx,%ecx
and    $0xfffffffe,%ecx
sub    %ecx,%eax
mov    %eax,-0x2024(%rbp)
je     11e1 <thread_job+0x81>
jmp    11cd <thread_job+0x6d>
mov    -0x2024(%rbp),%eax
sub    $0x1,%eax
je     1211 <thread_job+0xb1>
jmp    1241 <thread_job+0xe1>
lea    -0x2010(%rbp),%rax
mov    %rax,-0x2030(%rbp)
mov    $0x0,%al
call   1060 <random@plt>
mov    -0x2030(%rbp),%rdi
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
call   1070 <alloc@plt>
jmp    1256 <thread_job+0xf6>
lea    -0x2010(%rbp),%rax
mov    %rax,-0x2038(%rbp)
mov    $0x0,%al
call   1060 <random@plt>
mov    -0x2038(%rbp),%rdi
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
call   1080 <release@plt>
jmp    1256 <thread_job+0xf6>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    125b <thread_job+0xfb>
mov    -0x2014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2014(%rbp)
jmp    1197 <thread_job+0x37>
movl   $0x0,-0x2014(%rbp)
cmpl   $0x400,-0x2014(%rbp)
jge    12ca <thread_job+0x16a>
movslq -0x2014(%rbp),%rax
cmpq   $0x0,-0x2010(%rbp,%rax,8)
je     12b1 <thread_job+0x151>
mov    -0x2020(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x2020(%rbp)
jmp    12b6 <thread_job+0x156>
mov    -0x2014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2014(%rbp)
jmp    1279 <thread_job+0x119>
mov    -0x2020(%rbp),%rsi
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2020(%rbp),%rax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_job>:
push   %rbp
push   %rbx
sub    $0x2008,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1060 <memset@plt>
mov    $0x45a,%ebp
jmp    11c9 <thread_job+0x59>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1070 <random@plt>
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffc00,%ecx
sub    %ecx,%eax
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <release@plt>
add    $0xffffffff,%ebp
je     1212 <thread_job+0xa2>
xor    %eax,%eax
call   1070 <random@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
and    $0xfffffffe,%ecx
sub    %ecx,%eax
cmp    $0x1,%eax
je     11a0 <thread_job+0x30>
test   %eax,%eax
jne    1271 <thread_job+0x101>
xor    %eax,%eax
call   1070 <random@plt>
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffc00,%ecx
sub    %ecx,%eax
mov    %rbx,%rdi
mov    %eax,%esi
call   1080 <alloc@plt>
add    $0xffffffff,%ebp
jne    11c9 <thread_job+0x59>
xor    %eax,%eax
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
cmpq   $0x1,(%rsp,%rax,8)
sbb    $0xffffffffffffffff,%rbx
cmpq   $0x1,0x8(%rsp,%rax,8)
sbb    $0xffffffffffffffff,%rbx
cmpq   $0x1,0x10(%rsp,%rax,8)
sbb    $0xffffffffffffffff,%rbx
cmpq   $0x1,0x18(%rsp,%rax,8)
sbb    $0xffffffffffffffff,%rbx
add    $0x4,%rax
cmp    $0x400,%rax
jne    1220 <thread_job+0xb0>
lea    0xda6(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rax
add    $0x2008,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd9e(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"EER\0A\00"
@.str.1 = [22 x i8] c"Still allocated: %lu\0A\00"
define i8* @thread_job(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8*]
%4 = alloca i32
%5 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* %5
%6 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %3, i64 0, i64 0
%7 = call i32 @memset(i8** %6, i32 0, i32 8192)
store i32 0, i32* %4
br label %8
8: ; preds = %28, %1
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 1114
br i1 %10, label %11, label %31
11: ; preds = %8
%12 = call i32 (...) @random()
%13 = srem i32 %12, 2
switch i32 %13, label %24 [
i32 0, label %14
i32 1, label %19
]
14: ; preds = %11
%15 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %3, i64 0, i64 0
%16 = call i32 (...) @random()
%17 = srem i32 %16, 1024
%18 = call i32 @alloc(i8** %15, i32 %17)
br label %27
19: ; preds = %11
%20 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %3, i64 0, i64 0
%21 = call i32 (...) @random()
%22 = srem i32 %21, 1024
%23 = call i32 @release(i8** %20, i32 %22)
br label %27
24: ; preds = %11
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %19, %14
br label %28
28: ; preds = %27
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
br label %8
31: ; preds = %8
store i32 0, i32* %4
br label %32
32: ; preds = %45, %31
%33 = load i32, i32* %4
%34 = icmp slt i32 %33, 1024
br i1 %34, label %35, label %48
35: ; preds = %32
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %3, i64 0, i64 %37
%39 = load i8*, i8** %38
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = load i64, i64* %5
%43 = add i64 %42, 1
store i64 %43, i64* %5
br label %44
44: ; preds = %41, %35
br label %45
45: ; preds = %44
%46 = load i32, i32* %4
%47 = add nsw i32 %46, 1
store i32 %47, i32* %4
br label %32
48: ; preds = %32
%49 = load i64, i64* %5
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i64 %49)
%51 = load i64, i64* %5
%52 = inttoptr i64 %51 to i8*
ret i8* %52
}

LLVM IR (-O2):
@.str.1 = [22 x i8] c"Still allocated: %lu\0A\00"
@str = [4 x i8] c"EER\00"
define i8* @thread_job(i8* nocapture readnone %0) {
%2 = alloca [1024 x i8*]
%3 = bitcast [1024 x i8*]* %2 to i8*
%4 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %2, i64 0, i64 0
%5 = call i32 @memset(i8** %4, i32 0, i32 8192)
br label %6
6: ; preds = %1, %21
%7 = phi i32 [ 0, %1 ], [ %22, %21 ]
%8 = call i32 (...) @random()
%9 = srem i32 %8, 2
switch i32 %9, label %18 [
i32 0, label %10
i32 1, label %14
]
10: ; preds = %6
%11 = call i32 (...) @random()
%12 = srem i32 %11, 1024
%13 = call i32 @alloc(i8** %4, i32 %12)
br label %21
14: ; preds = %6
%15 = call i32 (...) @random()
%16 = srem i32 %15, 1024
%17 = call i32 @release(i8** %4, i32 %16)
br label %21
18: ; preds = %6
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%20 = call i32 (...) @abort()
unreachable
21: ; preds = %10, %14
%22 = add nuw nsw i32 %7, 1
%23 = icmp eq i32 %22, 1114
br i1 %23, label %24, label %6
24: ; preds = %21, %24
%25 = phi i64 [ %50, %24 ], [ 0, %21 ]
%26 = phi i64 [ %49, %24 ], [ 0, %21 ]
%27 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %2, i64 0, i64 %25
%28 = load i8*, i8** %27
%29 = icmp ne i8* %28, null
%30 = zext i1 %29 to i64
%31 = add i64 %26, %30
%32 = or i64 %25, 1
%33 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %2, i64 0, i64 %32
%34 = load i8*, i8** %33
%35 = icmp ne i8* %34, null
%36 = zext i1 %35 to i64
%37 = add i64 %31, %36
%38 = or i64 %25, 2
%39 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %2, i64 0, i64 %38
%40 = load i8*, i8** %39
%41 = icmp ne i8* %40, null
%42 = zext i1 %41 to i64
%43 = add i64 %37, %42
%44 = or i64 %25, 3
%45 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %2, i64 0, i64 %44
%46 = load i8*, i8** %45
%47 = icmp ne i8* %46, null
%48 = zext i1 %47 to i64
%49 = add i64 %43, %48
%50 = add nuw nsw i64 %25, 4
%51 = icmp eq i64 %50, 1024
br i1 %51, label %52, label %24
52: ; preds = %24
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i64 %49)
%54 = inttoptr i64 %49 to i8*
ret i8* %54
}