#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(unsigned char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int, int);
int /*<<< orphan*/ printf(char *);
void test2(int size) {
  unsigned char *data = NULL;
  int i = 0;
  while (1) {
    data = (unsigned char *)malloc(size);
    memset(data, 0, size);
    for (i = 0; i < 1000; ++i) {
      memset(data, 0xff, size);
      memset(data, 0, size);
    }
    free(data);
    data = 0;
    printf("done!\n");
  }
}


x86 assembly (clang, -O0):
<test2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x3e8,-0x14(%rbp)
jge    11b5 <test2+0x75>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
mov    $0xff,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117b <test2+0x3b>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
lea    0xe33(%rip),%rdi
call   1040 <printf@plt>
jmp    115a <test2+0x1a>

x86 assembly (clang, -O2):
<test2>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
lea    0xeaf(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1050 <memset@plt>
mov    $0x3e8,%ebp
nopl   (%rax)
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r15d,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1050 <memset@plt>
add    $0xffffffff,%ebp
jne    1180 <test2+0x40>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
jmp    1160 <test2+0x20>

LLVM IR (-O0):
@.str = [7 x i8] c"done!\0A\00"
define void @test2(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 %0, i32* %2
store i8* null, i8** %3
store i32 0, i32* %4
br label %5
5: ; preds = %1, %25
%6 = load i32, i32* %2
%7 = call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = load i32, i32* %2
%11 = call i32 @memset(i8* %9, i32 0, i32 %10)
store i32 0, i32* %4
br label %12
12: ; preds = %22, %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 1000
br i1 %14, label %15, label %25
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = load i32, i32* %2
%18 = call i32 @memset(i8* %16, i32 255, i32 %17)
%19 = load i8*, i8** %3
%20 = load i32, i32* %2
%21 = call i32 @memset(i8* %19, i32 0, i32 %20)
br label %22
22: ; preds = %15
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %12
25: ; preds = %12
%26 = load i8*, i8** %3
%27 = call i32 @free(i8* %26)
store i8* null, i8** %3
%28 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %5
}

LLVM IR (-O2):
@str = [6 x i8] c"done!\00"
define void @test2(i32 %0) {
br label %2
2: ; preds = %12, %1
%3 = tail call i64 @malloc(i32 %0)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @memset(i8* %4, i32 0, i32 %0)
br label %6
6: ; preds = %2, %6
%7 = phi i32 [ 0, %2 ], [ %10, %6 ]
%8 = tail call i32 @memset(i8* %4, i32 255, i32 %0)
%9 = tail call i32 @memset(i8* %4, i32 0, i32 %0)
%10 = add nuw nsw i32 %7, 1
%11 = icmp eq i32 %10, 1000
br i1 %11, label %12, label %6
12: ; preds = %6
%13 = tail call i32 @free(i8* %4)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %2
}