#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 int *);
unsigned int *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int max = -1;
  int mb = 0;
  char *buffer;
  int i;
  unsigned int *p = malloc(1024 * 1024 * 1000);
  printf("malloc buffer: %p\n", p);
  for (i = 0; i < 1024 * 1024 * (1000 / sizeof(int)); i++) {
    p[i] = 123;
    if ((i & 0xFFFFF) == 0) {
      printf("%dMB written\n", i >> 18);
    }
  }
  for (i = 0; i < 1024 * 1024 * (1000 / sizeof(int)); i++) {
    volatile unsigned int a;
    a = p[i];
    if ((i & 0xFFFFF) == 0) {
      printf("%dMB read\n", i >> 18);
    }
  }
  free(p);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x3e800000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0xfa00000,%rax
jae    11d2 <main+0xa2>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movl   $0x7b,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
and    $0xfffff,%eax
cmp    $0x0,%eax
jne    11bf <main+0x8f>
mov    -0x24(%rbp),%esi
sar    $0x12,%esi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11c4 <main+0x94>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    117b <main+0x4b>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0xfa00000,%rax
jae    122f <main+0xff>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
and    $0xfffff,%eax
cmp    $0x0,%eax
jne    121c <main+0xec>
mov    -0x24(%rbp),%esi
sar    $0x12,%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1221 <main+0xf1>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11d9 <main+0xa9>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x3e800000,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
lea    0xeb3(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeb5(%rip),%r14
xor    %ebx,%ebx
jmp    117d <main+0x4d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rbx
cmp    $0xfa00000,%rbx
je     119e <main+0x6e>
movl   $0x7b,(%r15,%rbx,4)
test   $0xfffff,%ebx
jne    1170 <main+0x40>
mov    %ebx,%esi
shr    $0x12,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1170 <main+0x40>
lea    0xe7c(%rip),%r14
xor    %ebx,%ebx
jmp    11bd <main+0x8d>
nopl   0x0(%rax)
add    $0x1,%rbx
cmp    $0xfa00000,%rbx
je     11de <main+0xae>
mov    (%r15,%rbx,4),%eax
mov    %eax,0xc(%rsp)
test   $0xfffff,%ebx
jne    11b0 <main+0x80>
mov    %ebx,%esi
shr    $0x12,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b0 <main+0x80>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [19 x i8] c"malloc buffer: %p\0A\00"
@.str.1 = [14 x i8] c"%dMB written\0A\00"
@.str.2 = [11 x i8] c"%dMB read\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 0, i32* %7
%12 = call i32* @malloc(i32 1048576000)
store i32* %12, i32** %10
%13 = load i32*, i32** %10
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32* %13)
store i32 0, i32* %9
br label %15
15: ; preds = %32, %2
%16 = load i32, i32* %9
%17 = sext i32 %16 to i64
%18 = icmp ult i64 %17, 262144000
br i1 %18, label %19, label %35
19: ; preds = %15
%20 = load i32*, i32** %10
%21 = load i32, i32* %9
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %20, i64 %22
store i32 123, i32* %23
%24 = load i32, i32* %9
%25 = and i32 %24, 1048575
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %19
%28 = load i32, i32* %9
%29 = ashr i32 %28, 18
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %19
br label %32
32: ; preds = %31
%33 = load i32, i32* %9
%34 = add nsw i32 %33, 1
store i32 %34, i32* %9
br label %15
35: ; preds = %15
store i32 0, i32* %9
br label %36
36: ; preds = %54, %35
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = icmp ult i64 %38, 262144000
br i1 %39, label %40, label %57
40: ; preds = %36
%41 = load i32*, i32** %10
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32, i32* %44
store volatile i32 %45, i32* %11
%46 = load i32, i32* %9
%47 = and i32 %46, 1048575
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %40
%50 = load i32, i32* %9
%51 = ashr i32 %50, 18
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %51)
br label %53
53: ; preds = %49, %40
br label %54
54: ; preds = %53
%55 = load i32, i32* %9
%56 = add nsw i32 %55, 1
store i32 %56, i32* %9
br label %36
57: ; preds = %36
%58 = load i32*, i32** %10
%59 = call i32 @free(i32* %58)
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"malloc buffer: %p\0A\00"
@.str.1 = [14 x i8] c"%dMB written\0A\00"
@.str.2 = [11 x i8] c"%dMB read\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i32* @malloc(i32 1048576000)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32* %4)
br label %8
6: ; preds = %17
%7 = bitcast i32* %3 to i8*
br label %20
8: ; preds = %2, %17
%9 = phi i64 [ 0, %2 ], [ %18, %17 ]
%10 = getelementptr inbounds i32, i32* %4, i64 %9
store i32 123, i32* %10
%11 = trunc i64 %9 to i32
%12 = and i32 %11, 1048575
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = lshr i32 %11, 18
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %8, %14
%18 = add nuw nsw i64 %9, 1
%19 = icmp eq i64 %18, 262144000
br i1 %19, label %6, label %8
20: ; preds = %6, %30
%21 = phi i64 [ 0, %6 ], [ %31, %30 ]
%22 = getelementptr inbounds i32, i32* %4, i64 %21
%23 = load i32, i32* %22
store volatile i32 %23, i32* %3
%24 = trunc i64 %21 to i32
%25 = and i32 %24, 1048575
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = lshr i32 %24, 18
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %27, %20
%31 = add nuw nsw i64 %21, 1
%32 = icmp eq i64 %31, 262144000
br i1 %32, label %33, label %20
33: ; preds = %30
%34 = tail call i32 @free(i32* %4)
ret i32 0
}