#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 uint64_t;
struct timespec {
  int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC_RAW;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ fast_matrix_multiplication(int **, int **);
int /*<<< orphan*/ init_matrix(int **);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ slow_matrix_multiplication(int **, int **);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  struct timespec start, end;
  uint64_t delta_us;
  int a[500][500], b[500][500];
  printf("principle of locality\n");
  srand(time(NULL));
  init_matrix(a);
  init_matrix(b);
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  slow_matrix_multiplication(a, b);
  clock_gettime(CLOCK_MONOTONIC_RAW, &end);
  delta_us = (end.tv_sec - start.tv_sec) * 1000000 +
             (end.tv_nsec - start.tv_nsec) / 1000;
  printf("time delta: %ld nanoseconds\n\n", delta_us);
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  fast_matrix_multiplication(a, b);
  clock_gettime(CLOCK_MONOTONIC_RAW, &end);
  delta_us = (end.tv_sec - start.tv_sec) * 1000000 +
             (end.tv_nsec - start.tv_nsec) / 1000;
  printf("time delta: %ld nanoseconds\n", delta_us);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1e84b0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
lea    -0xf4260(%rbp),%rdi
call   1080 <init_matrix@plt>
lea    -0x1e84a0(%rbp),%rdi
call   1080 <init_matrix@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0xf4260(%rbp),%rdi
lea    -0x1e84a0(%rbp),%rsi
call   1090 <slow_matrix_multiplication@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%eax
sub    -0x10(%rbp),%eax
imul   $0xf4240,%eax,%eax
mov    %eax,-0x1e84a8(%rbp)
mov    -0x14(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1e84a8(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0xf4260(%rbp),%rdi
lea    -0x1e84a0(%rbp),%rsi
call   1070 <fast_matrix_multiplication@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%eax
sub    -0x10(%rbp),%eax
imul   $0xf4240,%eax,%eax
mov    %eax,-0x1e84a4(%rbp)
mov    -0x14(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1e84a4(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x1e84b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1e8490,%rsp
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
lea    0xf4250(%rsp),%r14
mov    %r14,%rdi
call   1090 <init_matrix@plt>
lea    0x10(%rsp),%r13
mov    %r13,%rdi
call   1090 <init_matrix@plt>
mov    0x2e1a(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%r15
mov    %r15,%rsi
call   1040 <clock_gettime@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   10a0 <slow_matrix_multiplication@plt>
mov    (%rbx),%edi
mov    %rsp,%r12
mov    %r12,%rsi
call   1040 <clock_gettime@plt>
mov    (%rsp),%eax
mov    0x4(%rsp),%ecx
sub    0x8(%rsp),%eax
imul   $0xf4240,%eax,%eax
sub    0xc(%rsp),%ecx
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rsi
mov    %rsi,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rsi
add    %ecx,%esi
add    %eax,%esi
lea    0xdd6(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%edi
mov    %r15,%rsi
call   1040 <clock_gettime@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   1080 <fast_matrix_multiplication@plt>
mov    (%rbx),%edi
mov    %r12,%rsi
call   1040 <clock_gettime@plt>
mov    (%rsp),%eax
mov    0x4(%rsp),%ecx
sub    0x8(%rsp),%eax
imul   $0xf4240,%eax,%eax
sub    0xc(%rsp),%ecx
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rsi
mov    %rsi,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rsi
add    %ecx,%esi
add    %eax,%esi
lea    0xd99(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x1e8490,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [23 x i8] c"principle of locality\0A\00"
@CLOCK_MONOTONIC_RAW = global i32 0
@.str.1 = [30 x i8] c"time delta: %ld nanoseconds\0A\0A\00"
@.str.2 = [29 x i8] c"time delta: %ld nanoseconds\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = alloca [500 x [500 x i32]]
%6 = alloca [500 x [500 x i32]]
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @time(i32* null)
%9 = call i32 @srand(i32 %8)
%10 = getelementptr inbounds [500 x [500 x i32]], [500 x [500 x i32]]* %5, i64 0, i64 0
%11 = bitcast [500 x i32]* %10 to i32**
%12 = call i32 @init_matrix(i32** %11)
%13 = getelementptr inbounds [500 x [500 x i32]], [500 x [500 x i32]]* %6, i64 0, i64 0
%14 = bitcast [500 x i32]* %13 to i32**
%15 = call i32 @init_matrix(i32** %14)
%16 = load i32, i32* @CLOCK_MONOTONIC_RAW
%17 = call i32 @clock_gettime(i32 %16, %struct.timespec* %2)
%18 = getelementptr inbounds [500 x [500 x i32]], [500 x [500 x i32]]* %5, i64 0, i64 0
%19 = bitcast [500 x i32]* %18 to i32**
%20 = getelementptr inbounds [500 x [500 x i32]], [500 x [500 x i32]]* %6, i64 0, i64 0
%21 = bitcast [500 x i32]* %20 to i32**
%22 = call i32 @slow_matrix_multiplication(i32** %19, i32** %21)
%23 = load i32, i32* @CLOCK_MONOTONIC_RAW
%24 = call i32 @clock_gettime(i32 %23, %struct.timespec* %3)
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%28 = load i32, i32* %27
%29 = sub nsw i32 %26, %28
%30 = mul nsw i32 %29, 1000000
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%34 = load i32, i32* %33
%35 = sub nsw i32 %32, %34
%36 = sdiv i32 %35, 1000
%37 = add nsw i32 %30, %36
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* @CLOCK_MONOTONIC_RAW
%41 = call i32 @clock_gettime(i32 %40, %struct.timespec* %2)
%42 = getelementptr inbounds [500 x [500 x i32]], [500 x [500 x i32]]* %5, i64 0, i64 0
%43 = bitcast [500 x i32]* %42 to i32**
%44 = getelementptr inbounds [500 x [500 x i32]], [500 x [500 x i32]]* %6, i64 0, i64 0
%45 = bitcast [500 x i32]* %44 to i32**
%46 = call i32 @fast_matrix_multiplication(i32** %43, i32** %45)
%47 = load i32, i32* @CLOCK_MONOTONIC_RAW
%48 = call i32 @clock_gettime(i32 %47, %struct.timespec* %3)
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%50 = load i32, i32* %49
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%52 = load i32, i32* %51
%53 = sub nsw i32 %50, %52
%54 = mul nsw i32 %53, 1000000
%55 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%56 = load i32, i32* %55
%57 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%58 = load i32, i32* %57
%59 = sub nsw i32 %56, %58
%60 = sdiv i32 %59, 1000
%61 = add nsw i32 %54, %60
store i32 %61, i32* %4
%62 = load i32, i32* %4
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %62)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
@.str.1 = [30 x i8] c"time delta: %ld nanoseconds\0A\0A\00"
@.str.2 = [29 x i8] c"time delta: %ld nanoseconds\0A\00"
@str = [22 x i8] c"principle of locality\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca [500 x [500 x i32]]
%4 = alloca [500 x [500 x i32]]
%5 = bitcast %struct.timespec* %1 to i8*
%6 = bitcast %struct.timespec* %2 to i8*
%7 = bitcast [500 x [500 x i32]]* %3 to i8*
%8 = bitcast [500 x [500 x i32]]* %4 to i8*
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @time(i32* null)
%11 = tail call i32 @srand(i32 %10)
%12 = bitcast [500 x [500 x i32]]* %3 to i32**
%13 = call i32 @init_matrix(i32** %12)
%14 = bitcast [500 x [500 x i32]]* %4 to i32**
%15 = call i32 @init_matrix(i32** %14)
%16 = load i32, i32* @CLOCK_MONOTONIC_RAW
%17 = call i32 @clock_gettime(i32 %16, %struct.timespec* %1)
%18 = call i32 @slow_matrix_multiplication(i32** %12, i32** %14)
%19 = load i32, i32* @CLOCK_MONOTONIC_RAW
%20 = call i32 @clock_gettime(i32 %19, %struct.timespec* %2)
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%24 = load i32, i32* %23
%25 = sub nsw i32 %22, %24
%26 = mul nsw i32 %25, 1000000
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%30 = load i32, i32* %29
%31 = sub nsw i32 %28, %30
%32 = sdiv i32 %31, 1000
%33 = add nsw i32 %32, %26
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i32, i32* @CLOCK_MONOTONIC_RAW
%36 = call i32 @clock_gettime(i32 %35, %struct.timespec* %1)
%37 = call i32 @fast_matrix_multiplication(i32** %12, i32** %14)
%38 = load i32, i32* @CLOCK_MONOTONIC_RAW
%39 = call i32 @clock_gettime(i32 %38, %struct.timespec* %2)
%40 = load i32, i32* %21
%41 = load i32, i32* %23
%42 = sub nsw i32 %40, %41
%43 = mul nsw i32 %42, 1000000
%44 = load i32, i32* %27
%45 = load i32, i32* %29
%46 = sub nsw i32 %44, %45
%47 = sdiv i32 %46, 1000
%48 = add nsw i32 %47, %43
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %48)
ret i32 0
}