#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);
  while (1) {
    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   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0xfa00000,%rax
jae    11c2 <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    11af <main+0x8f>
mov    -0x24(%rbp),%esi
sar    $0x12,%esi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11b4 <main+0x94>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116b <main+0x4b>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0xfa00000,%rax
jae    121f <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    120c <main+0xec>
mov    -0x24(%rbp),%esi
sar    $0x12,%esi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1211 <main+0xf1>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c9 <main+0xa9>
jmp    1164 <main+0x44>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    $0x3e800000,%edi
call   1040 <malloc@plt>
mov    %rax,%r13
lea    0xebf(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xec1(%rip),%r14
lea    0xec8(%rip),%r15
nopl   0x0(%rax)
xor    %r12d,%r12d
movl   $0x7b,0x0(%r13,%r12,4)
test   $0xfffff,%r12d
jne    1185 <main+0x65>
mov    %r12d,%esi
shr    $0x12,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%r12
cmp    $0xfa00000,%r12
jne    1163 <main+0x43>
xor    %ebx,%ebx
jmp    11ad <main+0x8d>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    $0xfa00000,%rbx
je     1160 <main+0x40>
mov    0x0(%r13,%rbx,4),%eax
mov    %eax,0xc(%rsp)
test   $0xfffff,%ebx
jne    11a0 <main+0x80>
mov    %ebx,%esi
shr    $0x12,%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11a0 <main+0x80>

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)
br label %15
15: ; preds = %2, %58
store i32 0, i32* %9
br label %16
16: ; preds = %33, %15
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = icmp ult i64 %18, 262144000
br i1 %19, label %20, label %36
20: ; preds = %16
%21 = load i32*, i32** %10
%22 = load i32, i32* %9
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
store i32 123, i32* %24
%25 = load i32, i32* %9
%26 = and i32 %25, 1048575
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %20
%29 = load i32, i32* %9
%30 = ashr i32 %29, 18
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %28, %20
br label %33
33: ; preds = %32
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
br label %16
36: ; preds = %16
store i32 0, i32* %9
br label %37
37: ; preds = %55, %36
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = icmp ult i64 %39, 262144000
br i1 %40, label %41, label %58
41: ; preds = %37
%42 = load i32*, i32** %10
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %42, i64 %44
%46 = load i32, i32* %45
store volatile i32 %46, i32* %11
%47 = load i32, i32* %9
%48 = and i32 %47, 1048575
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %41
%51 = load i32, i32* %9
%52 = ashr i32 %51, 18
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %50, %41
br label %55
55: ; preds = %54
%56 = load i32, i32* %9
%57 = add nsw i32 %56, 1
store i32 %57, i32* %9
br label %37
58: ; preds = %37
br label %15
}

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)
%6 = bitcast i32* %3 to i8*
br label %7
7: ; preds = %19, %2
%8 = phi i64 [ 0, %2 ], [ %20, %19 ]
%9 = getelementptr inbounds i32, i32* %4, i64 %8
store i32 123, i32* %9
%10 = trunc i64 %8 to i32
%11 = and i32 %10, 1048575
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = lshr i32 %10, 18
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %7, %13
%17 = add nuw nsw i64 %8, 1
%18 = icmp eq i64 %17, 262144000
br i1 %18, label %21, label %19
19: ; preds = %31, %16
%20 = phi i64 [ %17, %16 ], [ 0, %31 ]
br label %7
21: ; preds = %16, %31
%22 = phi i64 [ %32, %31 ], [ 0, %16 ]
%23 = getelementptr inbounds i32, i32* %4, i64 %22
%24 = load i32, i32* %23
store volatile i32 %24, i32* %3
%25 = trunc i64 %22 to i32
%26 = and i32 %25, 1048575
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = lshr i32 %25, 18
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %28, %21
%32 = add nuw nsw i64 %22, 1
%33 = icmp eq i64 %32, 262144000
br i1 %33, label %19, label %21
}