#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 double clock_t;

/* Variables and functions */
scalar_t__ CLOCKS_PER_SEC;
double clock();
int **malloc(int);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ system(char *);
int main() {
  int(*large)[4096] = malloc(sizeof(int) * 4096 * 4096);
  clock_t start, end;
  long sum;
  start = clock();
  sum = 1;
  for (int i = 0; i < 4096; i++) {
    for (int j = 0; j < 4096; j++) {
      large[i][j] = i * j;
    }
  }
  end = clock();
  printf("Row major access took %f seconds\n",
         (end - start) / (double)CLOCKS_PER_SEC);
  start = clock();
  sum = 1;
  for (int j = 0; j < 4096; j++) {
    for (int i = 0; i < 4096; i++) {
      large[i][j] = i * j;
    }
  }
  end = clock();
  printf("Column major access took %f seconds\n",
         (end - start) / (double)CLOCKS_PER_SEC);
  system("pause");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x4000000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
movsd  %xmm0,-0x18(%rbp)
movq   $0x1,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x1000,-0x2c(%rbp)
jge    11d7 <main+0x97>
movl   $0x0,-0x30(%rbp)
cmpl   $0x1000,-0x30(%rbp)
jge    11c4 <main+0x84>
mov    -0x2c(%rbp),%edx
imul   -0x30(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
shl    $0xe,%rcx
add    %rcx,%rax
movslq -0x30(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    118c <main+0x4c>
jmp    11c9 <main+0x89>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1178 <main+0x38>
mov    $0x0,%al
call   1030 <clock@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
mov    0x2df4(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
lea    0xdfc(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <clock@plt>
movsd  %xmm0,-0x18(%rbp)
movq   $0x1,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x1000,-0x34(%rbp)
jge    1283 <main+0x143>
movl   $0x0,-0x38(%rbp)
cmpl   $0x1000,-0x38(%rbp)
jge    1270 <main+0x130>
mov    -0x38(%rbp),%edx
imul   -0x34(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x38(%rbp),%rcx
shl    $0xe,%rcx
add    %rcx,%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1238 <main+0xf8>
jmp    1275 <main+0x135>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1224 <main+0xe4>
mov    $0x0,%al
call   1030 <clock@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
mov    0x2d48(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
lea    0xd72(%rip),%rdi
call   1050 <printf@plt>
lea    0xd8b(%rip),%rdi
call   1040 <system@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x4000000,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <clock@plt>
movsd  %xmm0,0x8(%rsp)
lea    0xc(%rbx),%rax
xor    %r8d,%r8d
xor    %r9d,%r9d
xor    %esi,%esi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %edi,%edi
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %edi,-0xc(%rax,%rcx,4)
lea    (%rsi,%rdi,1),%edx
mov    %edx,-0x8(%rax,%rcx,4)
lea    (%r9,%rdi,1),%edx
mov    %edx,-0x4(%rax,%rcx,4)
lea    (%r14,%rdi,1),%edx
mov    %edx,(%rax,%rcx,4)
add    $0x4,%rcx
add    %r8d,%edi
cmp    $0x1000,%rcx
jne    1190 <main+0x50>
add    $0x1,%rsi
add    $0x2,%r9d
add    $0x4,%r8d
add    $0x4000,%rax
add    $0x3,%r14d
cmp    $0x1000,%rsi
jne    1180 <main+0x40>
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <clock@plt>
subsd  0x8(%rsp),%xmm0
mov    0x2df8(%rip),%r14
cvtsi2sdq (%r14),%xmm1
divsd  %xmm1,%xmm0
lea    0xe00(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <clock@plt>
movsd  %xmm0,0x8(%rsp)
add    $0xc000,%rbx
xor    %r8d,%r8d
xor    %ecx,%ecx
xor    %edx,%edx
xor    %esi,%esi
xor    %edi,%edi
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %edi,-0xc000(%rbx,%rsi,1)
lea    (%rdx,%rdi,1),%eax
mov    %eax,-0x8000(%rbx,%rsi,1)
lea    (%rcx,%rdi,1),%eax
mov    %eax,-0x4000(%rbx,%rsi,1)
lea    (%r15,%rdi,1),%eax
mov    %eax,(%rbx,%rsi,1)
add    %r8d,%edi
add    $0x10000,%rsi
cmp    $0x4000000,%rsi
jne    1230 <main+0xf0>
add    $0x1,%rdx
add    $0x2,%ecx
add    $0x4,%r8d
add    $0x4,%rbx
add    $0x3,%r15d
cmp    $0x1000,%rdx
jne    1220 <main+0xe0>
xor    %eax,%eax
call   1030 <clock@plt>
subsd  0x8(%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sdq (%r14),%xmm1
divsd  %xmm1,%xmm0
lea    0xd81(%rip),%rdi
call   1050 <printf@plt>
lea    0xd9a(%rip),%rdi
call   1040 <system@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [34 x i8] c"Row major access took %f seconds\0A\00"
@CLOCKS_PER_SEC = global i64 0
@.str.1 = [37 x i8] c"Column major access took %f seconds\0A\00"
@.str.2 = [6 x i8] c"pause\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [4096 x i32]*
%3 = alloca double
%4 = alloca double
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%10 = call i32** @malloc(i32 67108864)
%11 = bitcast i32** %10 to [4096 x i32]*
store [4096 x i32]* %11, [4096 x i32]** %2
%12 = call double (...) @clock()
store double %12, double* %3
store i64 1, i64* %5
store i32 0, i32* %6
br label %13
13: ; preds = %35, %0
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 4096
br i1 %15, label %16, label %38
16: ; preds = %13
store i32 0, i32* %7
br label %17
17: ; preds = %31, %16
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 4096
br i1 %19, label %20, label %34
20: ; preds = %17
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = mul nsw i32 %21, %22
%24 = load [4096 x i32]*, [4096 x i32]** %2
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [4096 x i32], [4096 x i32]* %24, i64 %26
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [4096 x i32], [4096 x i32]* %27, i64 0, i64 %29
store i32 %23, i32* %30
br label %31
31: ; preds = %20
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %17
34: ; preds = %17
br label %35
35: ; preds = %34
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
br label %13
38: ; preds = %13
%39 = call double (...) @clock()
store double %39, double* %4
%40 = load double, double* %4
%41 = load double, double* %3
%42 = fsub double %40, %41
%43 = load i64, i64* @CLOCKS_PER_SEC
%44 = sitofp i64 %43 to double
%45 = fdiv double %42, %44
%46 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), double %45)
%47 = call double (...) @clock()
store double %47, double* %3
store i64 1, i64* %5
store i32 0, i32* %8
br label %48
48: ; preds = %70, %38
%49 = load i32, i32* %8
%50 = icmp slt i32 %49, 4096
br i1 %50, label %51, label %73
51: ; preds = %48
store i32 0, i32* %9
br label %52
52: ; preds = %66, %51
%53 = load i32, i32* %9
%54 = icmp slt i32 %53, 4096
br i1 %54, label %55, label %69
55: ; preds = %52
%56 = load i32, i32* %9
%57 = load i32, i32* %8
%58 = mul nsw i32 %56, %57
%59 = load [4096 x i32]*, [4096 x i32]** %2
%60 = load i32, i32* %9
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [4096 x i32], [4096 x i32]* %59, i64 %61
%63 = load i32, i32* %8
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [4096 x i32], [4096 x i32]* %62, i64 0, i64 %64
store i32 %58, i32* %65
br label %66
66: ; preds = %55
%67 = load i32, i32* %9
%68 = add nsw i32 %67, 1
store i32 %68, i32* %9
br label %52
69: ; preds = %52
br label %70
70: ; preds = %69
%71 = load i32, i32* %8
%72 = add nsw i32 %71, 1
store i32 %72, i32* %8
br label %48
73: ; preds = %48
%74 = call double (...) @clock()
store double %74, double* %4
%75 = load double, double* %4
%76 = load double, double* %3
%77 = fsub double %75, %76
%78 = load i64, i64* @CLOCKS_PER_SEC
%79 = sitofp i64 %78 to double
%80 = fdiv double %77, %79
%81 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), double %80)
%82 = call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%83 = load i32, i32* %1
ret i32 %83
}

LLVM IR (-O2):
@.str = [34 x i8] c"Row major access took %f seconds\0A\00"
@CLOCKS_PER_SEC = global i64 0
@.str.1 = [37 x i8] c"Column major access took %f seconds\0A\00"
@.str.2 = [6 x i8] c"pause\00"
define i32 @main() {
%1 = tail call i32** @malloc(i32 67108864)
%2 = bitcast i32** %1 to [4096 x i32]*
%3 = tail call double (...) @clock()
br label %4
4: ; preds = %0, %14
%5 = phi i64 [ 0, %0 ], [ %15, %14 ]
br label %17
6: ; preds = %14
%7 = tail call double (...) @clock()
%8 = fsub double %7, %3
%9 = load i64, i64* @CLOCKS_PER_SEC
%10 = sitofp i64 %9 to double
%11 = fdiv double %8, %10
%12 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), double %11)
%13 = tail call double (...) @clock()
br label %36
14: ; preds = %17
%15 = add nuw nsw i64 %5, 1
%16 = icmp eq i64 %15, 4096
br i1 %16, label %6, label %4
17: ; preds = %17, %4
%18 = phi i64 [ 0, %4 ], [ %34, %17 ]
%19 = mul nuw nsw i64 %18, %5
%20 = getelementptr inbounds [4096 x i32], [4096 x i32]* %2, i64 %5, i64 %18
%21 = trunc i64 %19 to i32
store i32 %21, i32* %20
%22 = or i64 %18, 1
%23 = mul nuw nsw i64 %22, %5
%24 = getelementptr inbounds [4096 x i32], [4096 x i32]* %2, i64 %5, i64 %22
%25 = trunc i64 %23 to i32
store i32 %25, i32* %24
%26 = or i64 %18, 2
%27 = mul nuw nsw i64 %26, %5
%28 = getelementptr inbounds [4096 x i32], [4096 x i32]* %2, i64 %5, i64 %26
%29 = trunc i64 %27 to i32
store i32 %29, i32* %28
%30 = or i64 %18, 3
%31 = mul nuw nsw i64 %30, %5
%32 = getelementptr inbounds [4096 x i32], [4096 x i32]* %2, i64 %5, i64 %30
%33 = trunc i64 %31 to i32
store i32 %33, i32* %32
%34 = add nuw nsw i64 %18, 4
%35 = icmp eq i64 %34, 4096
br i1 %35, label %14, label %17
36: ; preds = %6, %46
%37 = phi i64 [ 0, %6 ], [ %47, %46 ]
br label %49
38: ; preds = %46
%39 = tail call double (...) @clock()
%40 = fsub double %39, %13
%41 = load i64, i64* @CLOCKS_PER_SEC
%42 = sitofp i64 %41 to double
%43 = fdiv double %40, %42
%44 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), double %43)
%45 = tail call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
46: ; preds = %49
%47 = add nuw nsw i64 %37, 1
%48 = icmp eq i64 %47, 4096
br i1 %48, label %38, label %36
49: ; preds = %49, %36
%50 = phi i64 [ 0, %36 ], [ %66, %49 ]
%51 = mul nuw nsw i64 %50, %37
%52 = getelementptr inbounds [4096 x i32], [4096 x i32]* %2, i64 %50, i64 %37
%53 = trunc i64 %51 to i32
store i32 %53, i32* %52
%54 = or i64 %50, 1
%55 = mul nuw nsw i64 %54, %37
%56 = getelementptr inbounds [4096 x i32], [4096 x i32]* %2, i64 %54, i64 %37
%57 = trunc i64 %55 to i32
store i32 %57, i32* %56
%58 = or i64 %50, 2
%59 = mul nuw nsw i64 %58, %37
%60 = getelementptr inbounds [4096 x i32], [4096 x i32]* %2, i64 %58, i64 %37
%61 = trunc i64 %59 to i32
store i32 %61, i32* %60
%62 = or i64 %50, 3
%63 = mul nuw nsw i64 %62, %37
%64 = getelementptr inbounds [4096 x i32], [4096 x i32]* %2, i64 %62, i64 %37
%65 = trunc i64 %63 to i32
store i32 %65, i32* %64
%66 = add nuw nsw i64 %50, 4
%67 = icmp eq i64 %66, 4096
br i1 %67, label %46, label %49
}