#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 */
typedef int /*<<< orphan*/ int64_t;

/* Variables and functions */
int /*<<< orphan*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/, int, void *);
void *malloc_test(void *id) {
  void *mem;
  for (int i = 0; i < 1000000; i++) {
    mem = malloc(i);
    printf("%ld %d %p\n", (int64_t)id, i, mem);
    free(mem);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<malloc_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0xf4240,-0x14(%rbp)
jge    118c <malloc_test+0x5c>
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,%esi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xe90(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1143 <malloc_test+0x13>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc_test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
xor    %ebp,%ebp
lea    0xebd(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x1,%ebp
cmp    $0xf4240,%ebp
jne    1150 <malloc_test+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%ld %d %p\0A\00"
define i8* @malloc_test(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
br label %5
5: ; preds = %18, %1
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 1000000
br i1 %7, label %8, label %21
8: ; preds = %5
%9 = load i32, i32* %4
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %3
%11 = load i8*, i8** %2
%12 = ptrtoint i8* %11 to i32
%13 = load i32, i32* %4
%14 = load i8*, i8** %3
%15 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13, i8* %14)
%16 = load i8*, i8** %3
%17 = call i32 @free(i8* %16)
br label %18
18: ; preds = %8
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
store i32 %20, i32* %4
br label %5
21: ; preds = %5
ret i8* null
}

LLVM IR (-O2):
@.str = [11 x i8] c"%ld %d %p\0A\00"
define noalias i8* @malloc_test(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = trunc i64 %2 to i32
br label %5
4: ; preds = %5
ret i8* null
5: ; preds = %1, %5
%6 = phi i32 [ 0, %1 ], [ %10, %5 ]
%7 = tail call i8* @malloc(i32 %6)
%8 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %6, i8* %7)
%9 = tail call i32 @free(i8* %7)
%10 = add nuw nsw i32 %6, 1
%11 = icmp eq i32 %10, 1000000
br i1 %11, label %4, label %5
}