#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 *calloc(int, int);
int /*<<< orphan*/ free(int *);
int *malloc(int);
int *realloc(int *, int);
void Foo() {
  int j = 0;
  while (j++ < 0xff) {
    int i;
    int sz = 0xffff;
    int *a = malloc(sz * sizeof(int));
    int *b = calloc(sz * sizeof(int), 1);
    int *c = malloc(sz * sizeof(int));
    int *d = malloc(sz * sizeof(int));
    int *e = realloc(a, (sz + 100) * sizeof(int));
    for (i = 0; i < sz; i++)
      d[i] = c[i] = b[i] = e[i] = 0;
    free(e);
    free(b);
    free(c);
    free(d);
  }
}


x86 assembly (clang, -O0):
<Foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x4(%rbp)
cmp    $0xff,%eax
jge    125e <Foo+0x11e>
movl   $0xffff,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%eax
add    $0x64,%eax
cltq
shl    $0x2,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1235 <Foo+0xf5>
mov    -0x38(%rbp),%rax
movslq -0x8(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
movslq -0x8(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
movslq -0x8(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
movslq -0x8(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11df <Foo+0x9f>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    114f <Foo+0xf>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Foo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xff,%ebp
nopl   (%rax)
mov    $0x3fffc,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    $0x3fffc,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
mov    $0x3fffc,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    $0x3fffc,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    $0x4018c,%esi
call   1070 <realloc@plt>
mov    %rax,%rbx
mov    $0x3fffc,%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    $0x3fffc,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    $0x3fffc,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    $0x3fffc,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0xffffffff,%ebp
jne    1160 <Foo+0x10>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @Foo() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
store i32 0, i32* %1
br label %9
9: ; preds = %65, %0
%10 = load i32, i32* %1
%11 = add nsw i32 %10, 1
store i32 %11, i32* %1
%12 = icmp slt i32 %10, 255
br i1 %12, label %13, label %74
13: ; preds = %9
store i32 65535, i32* %3
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 4
%17 = trunc i64 %16 to i32
%18 = call i32* @malloc(i32 %17)
store i32* %18, i32** %4
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 4
%22 = trunc i64 %21 to i32
%23 = call i32* @calloc(i32 %22, i32 1)
store i32* %23, i32** %5
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = mul i64 %25, 4
%27 = trunc i64 %26 to i32
%28 = call i32* @malloc(i32 %27)
store i32* %28, i32** %6
%29 = load i32, i32* %3
%30 = sext i32 %29 to i64
%31 = mul i64 %30, 4
%32 = trunc i64 %31 to i32
%33 = call i32* @malloc(i32 %32)
store i32* %33, i32** %7
%34 = load i32*, i32** %4
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 100
%37 = sext i32 %36 to i64
%38 = mul i64 %37, 4
%39 = trunc i64 %38 to i32
%40 = call i32* @realloc(i32* %34, i32 %39)
store i32* %40, i32** %8
store i32 0, i32* %2
br label %41
41: ; preds = %62, %13
%42 = load i32, i32* %2
%43 = load i32, i32* %3
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %65
45: ; preds = %41
%46 = load i32*, i32** %8
%47 = load i32, i32* %2
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
store i32 0, i32* %49
%50 = load i32*, i32** %5
%51 = load i32, i32* %2
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
store i32 0, i32* %53
%54 = load i32*, i32** %6
%55 = load i32, i32* %2
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %54, i64 %56
store i32 0, i32* %57
%58 = load i32*, i32** %7
%59 = load i32, i32* %2
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %58, i64 %60
store i32 0, i32* %61
br label %62
62: ; preds = %45
%63 = load i32, i32* %2
%64 = add nsw i32 %63, 1
store i32 %64, i32* %2
br label %41
65: ; preds = %41
%66 = load i32*, i32** %8
%67 = call i32 @free(i32* %66)
%68 = load i32*, i32** %5
%69 = call i32 @free(i32* %68)
%70 = load i32*, i32** %6
%71 = call i32 @free(i32* %70)
%72 = load i32*, i32** %7
%73 = call i32 @free(i32* %72)
br label %9
74: ; preds = %9
ret void
}

LLVM IR (-O2):
define void @Foo() {
br label %1
1: ; preds = %0, %1
%2 = phi i32 [ 1, %0 ], [ %16, %1 ]
%3 = tail call i32* @malloc(i32 262140)
%4 = tail call i32* @calloc(i32 262140, i32 1)
%5 = bitcast i32* %4 to i8*
%6 = tail call i32* @malloc(i32 262140)
%7 = bitcast i32* %6 to i8*
%8 = tail call i32* @malloc(i32 262140)
%9 = bitcast i32* %8 to i8*
%10 = tail call i32* @realloc(i32* %3, i32 262540)
%11 = bitcast i32* %10 to i8*
%12 = tail call i32 @free(i32* %10)
%13 = tail call i32 @free(i32* %4)
%14 = tail call i32 @free(i32* %6)
%15 = tail call i32 @free(i32* %8)
%16 = add nuw nsw i32 %2, 1
%17 = icmp eq i32 %16, 256
br i1 %17, label %18, label %1
18: ; preds = %1
ret void
}