#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*/ serial_writestring(char *);
void test_mem() {
  int i = 0x0;
  for (i = 0; i < 0x4000; i++) {
    *(int *)(i << 2) = i;
  }
  for (i = 0; i < 0x4000; i++) {
    if (*(int *)(i << 2) != i) {
      serial_writestring("Memory error\n");
    }
  }
  serial_writestring("Wrote a bunch of memory\n");
}


x86 assembly (clang, -O0):
<test_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x4000,-0x4(%rbp)
jge    114e <test_mem+0x3e>
mov    -0x4(%rbp),%ecx
mov    -0x4(%rbp),%eax
shl    $0x2,%eax
cltq
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1126 <test_mem+0x16>
movl   $0x0,-0x4(%rbp)
cmpl   $0x4000,-0x4(%rbp)
jge    1194 <test_mem+0x84>
mov    -0x4(%rbp),%eax
shl    $0x2,%eax
cltq
mov    (%rax),%eax
cmp    -0x4(%rbp),%eax
je     1181 <test_mem+0x71>
lea    0xe84(%rip),%rdi
call   1030 <serial_writestring@plt>
jmp    1186 <test_mem+0x76>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1155 <test_mem+0x45>
lea    0xe73(%rip),%rdi
call   1030 <serial_writestring@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_mem>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
xor    %ecx,%ecx
nopl   0x0(%rax)
mov    %ecx,(%rax)
mov    %ecx,%edx
or     $0x1,%edx
mov    %edx,0x4(%rax)
mov    %ecx,%edx
or     $0x2,%edx
mov    %edx,0x8(%rax)
mov    %ecx,%edx
or     $0x3,%edx
mov    %edx,0xc(%rax)
mov    %ecx,%edx
or     $0x4,%edx
mov    %edx,0x10(%rax)
mov    %ecx,%edx
or     $0x5,%edx
mov    %edx,0x14(%rax)
mov    %ecx,%edx
or     $0x6,%edx
mov    %edx,0x18(%rax)
mov    %ecx,%edx
or     $0x7,%edx
mov    %edx,0x1c(%rax)
add    $0x8,%rcx
add    $0x20,%rax
cmp    $0x4000,%rcx
jne    1120 <test_mem+0x10>
xor    %r15d,%r15d
lea    0xe8b(%rip),%r14
xor    %ebx,%ebx
jmp    1191 <test_mem+0x81>
nopl   0x0(%rax)
add    $0x1,%rbx
add    $0x4,%r15
cmp    $0x4000,%rbx
je     11a3 <test_mem+0x93>
mov    (%r15),%eax
cmp    %rax,%rbx
je     1180 <test_mem+0x70>
mov    %r14,%rdi
call   1030 <serial_writestring@plt>
jmp    1180 <test_mem+0x70>
lea    0xe64(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <serial_writestring@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"Memory error\0A\00"
@.str.1 = [25 x i8] c"Wrote a bunch of memory\0A\00"
define void @test_mem() {
%1 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %1
br label %2
2: ; preds = %11, %0
%3 = load i32, i32* %1
%4 = icmp slt i32 %3, 16384
br i1 %4, label %5, label %14
5: ; preds = %2
%6 = load i32, i32* %1
%7 = load i32, i32* %1
%8 = shl i32 %7, 2
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i32*
store i32 %6, i32* %10
br label %11
11: ; preds = %5
%12 = load i32, i32* %1
%13 = add nsw i32 %12, 1
store i32 %13, i32* %1
br label %2
14: ; preds = %2
store i32 0, i32* %1
br label %15
15: ; preds = %29, %14
%16 = load i32, i32* %1
%17 = icmp slt i32 %16, 16384
br i1 %17, label %18, label %32
18: ; preds = %15
%19 = load i32, i32* %1
%20 = shl i32 %19, 2
%21 = sext i32 %20 to i64
%22 = inttoptr i64 %21 to i32*
%23 = load i32, i32* %22
%24 = load i32, i32* %1
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @serial_writestring(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %18
br label %29
29: ; preds = %28
%30 = load i32, i32* %1
%31 = add nsw i32 %30, 1
store i32 %31, i32* %1
br label %15
32: ; preds = %15
%33 = call i32 @serial_writestring(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"Memory error\0A\00"
@.str.1 = [25 x i8] c"Wrote a bunch of memory\0A\00"
define void @test_mem() {
br label %1
1: ; preds = %1, %0
%2 = phi i64 [ 0, %0 ], [ %34, %1 ]
%3 = shl nuw nsw i64 %2, 2
%4 = inttoptr i64 %3 to i32*
%5 = trunc i64 %2 to i32
store i32 %5, i32* %4
%6 = or i64 %2, 1
%7 = shl nuw nsw i64 %6, 2
%8 = inttoptr i64 %7 to i32*
%9 = trunc i64 %6 to i32
store i32 %9, i32* %8
%10 = or i64 %2, 2
%11 = shl nuw nsw i64 %10, 2
%12 = inttoptr i64 %11 to i32*
%13 = trunc i64 %10 to i32
store i32 %13, i32* %12
%14 = or i64 %2, 3
%15 = shl nuw nsw i64 %14, 2
%16 = inttoptr i64 %15 to i32*
%17 = trunc i64 %14 to i32
store i32 %17, i32* %16
%18 = or i64 %2, 4
%19 = shl nuw nsw i64 %18, 2
%20 = inttoptr i64 %19 to i32*
%21 = trunc i64 %18 to i32
store i32 %21, i32* %20
%22 = or i64 %2, 5
%23 = shl nuw nsw i64 %22, 2
%24 = inttoptr i64 %23 to i32*
%25 = trunc i64 %22 to i32
store i32 %25, i32* %24
%26 = or i64 %2, 6
%27 = shl nuw nsw i64 %26, 2
%28 = inttoptr i64 %27 to i32*
%29 = trunc i64 %26 to i32
store i32 %29, i32* %28
%30 = or i64 %2, 7
%31 = shl nuw nsw i64 %30, 2
%32 = inttoptr i64 %31 to i32*
%33 = trunc i64 %30 to i32
store i32 %33, i32* %32
%34 = add nuw nsw i64 %2, 8
%35 = icmp eq i64 %34, 16384
br i1 %35, label %36, label %1
36: ; preds = %1, %45
%37 = phi i64 [ %46, %45 ], [ 0, %1 ]
%38 = shl nuw nsw i64 %37, 2
%39 = inttoptr i64 %38 to i32*
%40 = load i32, i32* %39
%41 = zext i32 %40 to i64
%42 = icmp eq i64 %37, %41
br i1 %42, label %45, label %43
43: ; preds = %36
%44 = tail call i32 @serial_writestring(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %36, %43
%46 = add nuw nsw i64 %37, 1
%47 = icmp eq i64 %46, 16384
br i1 %47, label %48, label %36
48: ; preds = %45
%49 = tail call i32 @serial_writestring(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
ret void
}