#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(int *);
scalar_t__ malloc(int);
extern int chk_malloc(void) {
  int *j, i;
  j = (int *)malloc(2000 * sizeof(int));
  for (i = 0; i < 2000; i++)
    j[i] = 0;
  for (i = 0; i < 2000; i++)
    if (j[i] != 0)
      break;
  free(j);
  return i == 2000;
}


x86 assembly (clang, -O0):
<chk_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x1f40,%edi
call   1040 <malloc@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x7d0,-0xc(%rbp)
jge    1167 <chk_malloc+0x47>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113d <chk_malloc+0x1d>
movl   $0x0,-0xc(%rbp)
cmpl   $0x7d0,-0xc(%rbp)
jge    11a5 <chk_malloc+0x85>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     1192 <chk_malloc+0x72>
jmp    11a5 <chk_malloc+0x85>
jmp    1197 <chk_malloc+0x77>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116e <chk_malloc+0x4e>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x7d0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chk_malloc>:
push   %rbp
push   %rbx
push   %rax
mov    $0x1f40,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    $0x1f40,%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpl   $0x0,0x4(%rbx,%rbp,4)
jne    1195 <chk_malloc+0x65>
cmpl   $0x0,0x8(%rbx,%rbp,4)
jne    119b <chk_malloc+0x6b>
cmpl   $0x0,0xc(%rbx,%rbp,4)
jne    11a1 <chk_malloc+0x71>
cmpl   $0x0,0x10(%rbx,%rbp,4)
jne    11a7 <chk_malloc+0x77>
cmp    $0x7cb,%rbp
je     11ca <chk_malloc+0x9a>
lea    0x5(%rbp),%rax
cmpl   $0x0,0x14(%rbx,%rbp,4)
mov    %rax,%rbp
je     1160 <chk_malloc+0x30>
jmp    11ae <chk_malloc+0x7e>
add    $0x1,%rbp
jmp    11ab <chk_malloc+0x7b>
add    $0x2,%rbp
jmp    11ab <chk_malloc+0x7b>
add    $0x3,%rbp
jmp    11ab <chk_malloc+0x7b>
add    $0x4,%rbp
mov    %rbp,%rax
xor    %ebp,%ebp
cmp    $0x7d0,%eax
sete   %bpl
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x1,%ebp
jmp    11b9 <chk_malloc+0x89>

LLVM IR (-O0):
define i32 @chk_malloc() {
%1 = alloca i32*
%2 = alloca i32
%3 = call i64 @malloc(i32 8000)
%4 = inttoptr i64 %3 to i32*
store i32* %4, i32** %1
store i32 0, i32* %2
br label %5
5: ; preds = %13, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 2000
br i1 %7, label %8, label %16
8: ; preds = %5
%9 = load i32*, i32** %1
%10 = load i32, i32* %2
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i32, i32* %9, i64 %11
store i32 0, i32* %12
br label %13
13: ; preds = %8
%14 = load i32, i32* %2
%15 = add nsw i32 %14, 1
store i32 %15, i32* %2
br label %5
16: ; preds = %5
store i32 0, i32* %2
br label %17
17: ; preds = %29, %16
%18 = load i32, i32* %2
%19 = icmp slt i32 %18, 2000
br i1 %19, label %20, label %32
20: ; preds = %17
%21 = load i32*, i32** %1
%22 = load i32, i32* %2
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %20
br label %32
28: ; preds = %20
br label %29
29: ; preds = %28
%30 = load i32, i32* %2
%31 = add nsw i32 %30, 1
store i32 %31, i32* %2
br label %17
32: ; preds = %27, %17
%33 = load i32*, i32** %1
%34 = call i32 @free(i32* %33)
%35 = load i32, i32* %2
%36 = icmp eq i32 %35, 2000
%37 = zext i1 %36 to i32
ret i32 %37
}

LLVM IR (-O2):
define i32 @chk_malloc() {
%1 = tail call i64 @malloc(i32 8000)
%2 = inttoptr i64 %1 to i32*
%3 = inttoptr i64 %1 to i8*
br label %4
4: ; preds = %28, %0
%5 = phi i64 [ 0, %0 ], [ %26, %28 ]
%6 = add nuw nsw i64 %5, 1
%7 = getelementptr inbounds i32, i32* %2, i64 %6
%8 = load i32, i32* %7
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %32
10: ; preds = %4
%11 = add nuw nsw i64 %5, 2
%12 = getelementptr inbounds i32, i32* %2, i64 %11
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %10
%16 = add nuw nsw i64 %5, 3
%17 = getelementptr inbounds i32, i32* %2, i64 %16
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %15
%21 = add nuw nsw i64 %5, 4
%22 = getelementptr inbounds i32, i32* %2, i64 %21
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %20
%26 = add nuw nsw i64 %5, 5
%27 = icmp eq i64 %26, 2000
br i1 %27, label %37, label %28
28: ; preds = %25
%29 = getelementptr inbounds i32, i32* %2, i64 %26
%30 = load i32, i32* %29
%31 = icmp eq i32 %30, 0
br i1 %31, label %4, label %32
32: ; preds = %28, %20, %15, %10, %4
%33 = phi i64 [ %6, %4 ], [ %11, %10 ], [ %16, %15 ], [ %21, %20 ], [ %26, %28 ]
%34 = trunc i64 %33 to i32
%35 = icmp eq i32 %34, 2000
%36 = zext i1 %35 to i32
br label %37
37: ; preds = %25, %32
%38 = phi i32 [ %36, %32 ], [ 1, %25 ]
%39 = tail call i32 @free(i32* %2)
ret i32 %38
}