#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*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
size_t b_malloc_sparse(void *dummy) {
  void *p[10000];
  size_t i;
  for (i = 0; i < sizeof p / sizeof *p; i++) {
    p[i] = malloc(4000);
    memset(p[i], 0, 4000);
  }
  for (i = 0; i < sizeof p / sizeof *p; i++)
    if (i % 150)
      free(p[i]);
  return 0;
}


x86 assembly (clang, -O0):
<b_malloc_sparse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x138a0,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x13898(%rbp)
cmpq   $0x2710,-0x13898(%rbp)
jae    11a9 <b_malloc_sparse+0x79>
mov    $0xfa0,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x13898(%rbp),%rax
mov    %rcx,-0x13890(%rbp,%rax,8)
ff
mov    -0x13898(%rbp),%rax
mov    -0x13890(%rbp,%rax,8),%rdi
ff
xor    %esi,%esi
mov    $0xfa0,%edx
call   1040 <memset@plt>
mov    -0x13898(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x13898(%rbp)
jmp    114a <b_malloc_sparse+0x1a>
movq   $0x0,-0x13898(%rbp)
cmpq   $0x2710,-0x13898(%rbp)
jae    1210 <b_malloc_sparse+0xe0>
mov    -0x13898(%rbp),%rax
mov    $0x96,%ecx
xor    %edx,%edx
div    %rcx
cmp    $0x0,%rdx
je     11f4 <b_malloc_sparse+0xc4>
mov    -0x13898(%rbp),%rax
mov    -0x13890(%rbp,%rax,8),%rdi
ff
call   1030 <free@plt>
jmp    11f9 <b_malloc_sparse+0xc9>
mov    -0x13898(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x13898(%rbp)
jmp    11b4 <b_malloc_sparse+0x84>
xor    %eax,%eax
add    $0x138a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<b_malloc_sparse>:
push   %rbp
push   %r14
push   %rbx
sub    $0x13880,%rsp
xor    %ebx,%ebx
nopl   (%rax)
mov    $0xfa0,%edi
call   1050 <malloc@plt>
mov    %rax,(%rsp,%rbx,8)
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xfa0,%edx
call   1040 <memset@plt>
add    $0x1,%rbx
cmp    $0x2710,%rbx
jne    1140 <b_malloc_sparse+0x10>
mov    $0xfffffffffffec780,%r14
xor    %ebp,%ebp
xor    %ebx,%ebx
jmp    118c <b_malloc_sparse+0x5c>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
add    $0x1,%ebp
add    $0x8,%r14
je     11b4 <b_malloc_sparse+0x84>
movzwl %bp,%eax
shr    %eax
imul   $0xda75,%eax,%eax
shr    $0x16,%eax
imul   $0x96,%eax,%eax
add    %bx,%ax
je     1180 <b_malloc_sparse+0x50>
mov    0x13880(%rsp,%r14,1),%rdi
call   1030 <free@plt>
jmp    1180 <b_malloc_sparse+0x50>
xor    %eax,%eax
add    $0x13880,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i64 @b_malloc_sparse(i8* %0) {
%2 = alloca i8*
%3 = alloca [10000 x i8*]
%4 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* %4
br label %5
5: ; preds = %16, %1
%6 = load i64, i64* %4
%7 = icmp ult i64 %6, 10000
br i1 %7, label %8, label %19
8: ; preds = %5
%9 = call i8* @malloc(i32 4000)
%10 = load i64, i64* %4
%11 = getelementptr inbounds [10000 x i8*], [10000 x i8*]* %3, i64 0, i64 %10
store i8* %9, i8** %11
%12 = load i64, i64* %4
%13 = getelementptr inbounds [10000 x i8*], [10000 x i8*]* %3, i64 0, i64 %12
%14 = load i8*, i8** %13
%15 = call i32 @memset(i8* %14, i32 0, i32 4000)
br label %16
16: ; preds = %8
%17 = load i64, i64* %4
%18 = add i64 %17, 1
store i64 %18, i64* %4
br label %5
19: ; preds = %5
store i64 0, i64* %4
br label %20
20: ; preds = %33, %19
%21 = load i64, i64* %4
%22 = icmp ult i64 %21, 10000
br i1 %22, label %23, label %36
23: ; preds = %20
%24 = load i64, i64* %4
%25 = urem i64 %24, 150
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i64, i64* %4
%29 = getelementptr inbounds [10000 x i8*], [10000 x i8*]* %3, i64 0, i64 %28
%30 = load i8*, i8** %29
%31 = call i32 @free(i8* %30)
br label %32
32: ; preds = %27, %23
br label %33
33: ; preds = %32
%34 = load i64, i64* %4
%35 = add i64 %34, 1
store i64 %35, i64* %4
br label %20
36: ; preds = %20
ret i64 0
}

LLVM IR (-O2):
define i64 @b_malloc_sparse(i8* nocapture readnone %0) {
%2 = alloca [10000 x i8*]
%3 = bitcast [10000 x i8*]* %2 to i8*
br label %4
4: ; preds = %1, %4
%5 = phi i64 [ 0, %1 ], [ %9, %4 ]
%6 = tail call dereferenceable_or_null(4000) i8* @malloc(i32 4000)
%7 = getelementptr inbounds [10000 x i8*], [10000 x i8*]* %2, i64 0, i64 %5
store i8* %6, i8** %7
%8 = tail call i32 @memset(i8* %6, i32 0, i32 4000)
%9 = add nuw nsw i64 %5, 1
%10 = icmp eq i64 %9, 10000
br i1 %10, label %11, label %4
11: ; preds = %4, %20
%12 = phi i64 [ %21, %20 ], [ 0, %4 ]
%13 = trunc i64 %12 to i16
%14 = urem i16 %13, 150
%15 = icmp eq i16 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = getelementptr inbounds [10000 x i8*], [10000 x i8*]* %2, i64 0, i64 %12
%18 = load i8*, i8** %17
%19 = tail call i32 @free(i8* %18)
br label %20
20: ; preds = %11, %16
%21 = add nuw nsw i64 %12, 1
%22 = icmp eq i64 %21, 10000
br i1 %22, label %23, label %11
23: ; preds = %20
ret i64 0
}