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

/* Variables and functions */
double CLOCKS_PER_SEC;
double *alloc_matrix(int);
scalar_t__ clock();
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ matrix_mul_basic(double *, double *, double *, int);
int /*<<< orphan*/ matrix_mul_blocked(double *, double *, double *, int, int);
int /*<<< orphan*/ matrix_mul_transposed(double *, double *, double *, int);
int /*<<< orphan*/ printf(char *, int, double, double, double);
double *random_matrix(int);
void benchmark_mul(int n, int repeats) {
  double *a = random_matrix(n);
  double *b = random_matrix(n);
  double *res = alloc_matrix(n);
  clock_t start, end;
  int i;
  double basic = 0.0;
  if (n < 1000) {
    start = clock();
    for (i = 0; i < repeats; ++i) {
      matrix_mul_basic(res, a, b, n);
    }
    end = clock();
    basic = (double)(end - start) / CLOCKS_PER_SEC / (double)repeats;
  }
  start = clock();
  for (i = 0; i < repeats; ++i) {
    matrix_mul_transposed(res, a, b, n);
  }
  end = clock();
  double transposed = (double)(end - start) / CLOCKS_PER_SEC / (double)repeats;
  start = clock();
  for (i = 0; i < repeats; ++i) {
    matrix_mul_blocked(res, a, b, n, 128);
  }
  end = clock();
  double blocked = (double)(end - start) / CLOCKS_PER_SEC / (double)repeats;
  printf("%d basic: %.5lf, transposed: %.5lf, blocked: %.5lf\n", n, basic,
         transposed, blocked);
  free(a);
  free(b);
  free(res);
}


x86 assembly (clang, -O0):
<benchmark_mul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <random_matrix@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <random_matrix@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <alloc_matrix@plt>
mov    %rax,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
cmpl   $0x3e8,-0x4(%rbp)
jge    1238 <benchmark_mul+0xb8>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1207 <benchmark_mul+0x87>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x4(%rbp),%ecx
call   1090 <matrix_mul_basic@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d9 <benchmark_mul+0x59>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x28(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2dc2(%rip),%rax
divsd  (%rax),%xmm0
cvtsi2sdl -0x8(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1278 <benchmark_mul+0xf8>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x4(%rbp),%ecx
call   10a0 <matrix_mul_transposed@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    124a <benchmark_mul+0xca>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x28(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2d51(%rip),%rax
divsd  (%rax),%xmm0
cvtsi2sdl -0x8(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12ef <benchmark_mul+0x16f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x4(%rbp),%ecx
mov    $0x80,%r8d
call   1080 <matrix_mul_blocked@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12bb <benchmark_mul+0x13b>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x28(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2cda(%rip),%rax
divsd  (%rax),%xmm0
cvtsi2sdl -0x8(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x4(%rbp),%esi
movsd  -0x40(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm1
movsd  -0x50(%rbp),%xmm2
lea    0xcc7(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark_mul>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r13d
mov    %edi,%ebp
call   1070 <random_matrix@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1070 <random_matrix@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   1050 <alloc_matrix@plt>
mov    %rax,%r12
xorpd  %xmm1,%xmm1
cvtsi2sd %r13d,%xmm0
movsd  %xmm0,0x10(%rsp)
cmp    $0x3e7,%ebp
jg     121a <benchmark_mul+0x9a>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,0x8(%rsp)
test   %r13d,%r13d
jle    11f5 <benchmark_mul+0x75>
mov    %r13d,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %ebp,%ecx
call   1090 <matrix_mul_basic@plt>
add    $0xffffffff,%ebx
jne    11e0 <benchmark_mul+0x60>
xor    %eax,%eax
call   1040 <clock@plt>
sub    0x8(%rsp),%rax
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
mov    0x2dd8(%rip),%rax
divsd  (%rax),%xmm1
divsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x8(%rsp)
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,(%rsp)
test   %r13d,%r13d
jle    1255 <benchmark_mul+0xd5>
mov    %r13d,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %ebp,%ecx
call   10a0 <matrix_mul_transposed@plt>
add    $0xffffffff,%ebx
jne    1240 <benchmark_mul+0xc0>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%rbx
mov    0x2d82(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,0x18(%rsp)
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,0x20(%rsp)
test   %r13d,%r13d
jle    12ac <benchmark_mul+0x12c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %ebp,%ecx
mov    $0x80,%r8d
call   1080 <matrix_mul_blocked@plt>
add    $0xffffffff,%r13d
jne    1290 <benchmark_mul+0x110>
sub    (%rsp),%rbx
xorps  %xmm0,%xmm0
cvtsi2sd %rbx,%xmm0
divsd  0x18(%rsp),%xmm0
divsd  0x10(%rsp),%xmm0
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1040 <clock@plt>
sub    0x20(%rsp),%rax
cvtsi2sd %rax,%xmm2
mov    0x2d07(%rip),%rax
divsd  (%rax),%xmm2
divsd  0x10(%rsp),%xmm2
lea    0xd0e(%rip),%rdi
mov    %ebp,%esi
movsd  0x8(%rsp),%xmm0
movsd  (%rsp),%xmm1
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str = [52 x i8] c"%d basic: %.5lf, transposed: %.5lf, blocked: %.5lf\0A\00"
define void @benchmark_mul(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca double*
%6 = alloca double*
%7 = alloca double*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca double
%12 = alloca double
%13 = alloca double
store i32 %0, i32* %3
store i32 %1, i32* %4
%14 = load i32, i32* %3
%15 = call double* @random_matrix(i32 %14)
store double* %15, double** %5
%16 = load i32, i32* %3
%17 = call double* @random_matrix(i32 %16)
store double* %17, double** %6
%18 = load i32, i32* %3
%19 = call double* @alloc_matrix(i32 %18)
store double* %19, double** %7
store double 0.000000e+00, double* %11
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 1000
br i1 %21, label %22, label %48
22: ; preds = %2
%23 = call i64 (...) @clock()
store i64 %23, i64* %8
store i32 0, i32* %10
br label %24
24: ; preds = %34, %22
%25 = load i32, i32* %10
%26 = load i32, i32* %4
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %37
28: ; preds = %24
%29 = load double*, double** %7
%30 = load double*, double** %5
%31 = load double*, double** %6
%32 = load i32, i32* %3
%33 = call i32 @matrix_mul_basic(double* %29, double* %30, double* %31, i32 %32)
br label %34
34: ; preds = %28
%35 = load i32, i32* %10
%36 = add nsw i32 %35, 1
store i32 %36, i32* %10
br label %24
37: ; preds = %24
%38 = call i64 (...) @clock()
store i64 %38, i64* %9
%39 = load i64, i64* %9
%40 = load i64, i64* %8
%41 = sub nsw i64 %39, %40
%42 = sitofp i64 %41 to double
%43 = load double, double* @CLOCKS_PER_SEC
%44 = fdiv double %42, %43
%45 = load i32, i32* %4
%46 = sitofp i32 %45 to double
%47 = fdiv double %44, %46
store double %47, double* %11
br label %48
48: ; preds = %37, %2
%49 = call i64 (...) @clock()
store i64 %49, i64* %8
store i32 0, i32* %10
br label %50
50: ; preds = %60, %48
%51 = load i32, i32* %10
%52 = load i32, i32* %4
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %63
54: ; preds = %50
%55 = load double*, double** %7
%56 = load double*, double** %5
%57 = load double*, double** %6
%58 = load i32, i32* %3
%59 = call i32 @matrix_mul_transposed(double* %55, double* %56, double* %57, i32 %58)
br label %60
60: ; preds = %54
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
store i32 %62, i32* %10
br label %50
63: ; preds = %50
%64 = call i64 (...) @clock()
store i64 %64, i64* %9
%65 = load i64, i64* %9
%66 = load i64, i64* %8
%67 = sub nsw i64 %65, %66
%68 = sitofp i64 %67 to double
%69 = load double, double* @CLOCKS_PER_SEC
%70 = fdiv double %68, %69
%71 = load i32, i32* %4
%72 = sitofp i32 %71 to double
%73 = fdiv double %70, %72
store double %73, double* %12
%74 = call i64 (...) @clock()
store i64 %74, i64* %8
store i32 0, i32* %10
br label %75
75: ; preds = %85, %63
%76 = load i32, i32* %10
%77 = load i32, i32* %4
%78 = icmp slt i32 %76, %77
br i1 %78, label %79, label %88
79: ; preds = %75
%80 = load double*, double** %7
%81 = load double*, double** %5
%82 = load double*, double** %6
%83 = load i32, i32* %3
%84 = call i32 @matrix_mul_blocked(double* %80, double* %81, double* %82, i32 %83, i32 128)
br label %85
85: ; preds = %79
%86 = load i32, i32* %10
%87 = add nsw i32 %86, 1
store i32 %87, i32* %10
br label %75
88: ; preds = %75
%89 = call i64 (...) @clock()
store i64 %89, i64* %9
%90 = load i64, i64* %9
%91 = load i64, i64* %8
%92 = sub nsw i64 %90, %91
%93 = sitofp i64 %92 to double
%94 = load double, double* @CLOCKS_PER_SEC
%95 = fdiv double %93, %94
%96 = load i32, i32* %4
%97 = sitofp i32 %96 to double
%98 = fdiv double %95, %97
store double %98, double* %13
%99 = load i32, i32* %3
%100 = load double, double* %11
%101 = load double, double* %12
%102 = load double, double* %13
%103 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %99, double %100, double %101, double %102)
%104 = load double*, double** %5
%105 = call i32 @free(double* %104)
%106 = load double*, double** %6
%107 = call i32 @free(double* %106)
%108 = load double*, double** %7
%109 = call i32 @free(double* %108)
ret void
}

LLVM IR (-O2):
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str = [52 x i8] c"%d basic: %.5lf, transposed: %.5lf, blocked: %.5lf\0A\00"
define void @benchmark_mul(i32 %0, i32 %1) {
%3 = tail call double* @random_matrix(i32 %0)
%4 = tail call double* @random_matrix(i32 %0)
%5 = tail call double* @alloc_matrix(i32 %0)
%6 = icmp slt i32 %0, 1000
br i1 %6, label %7, label %23
7: ; preds = %2
%8 = tail call i64 (...) @clock()
%9 = icmp sgt i32 %1, 0
br i1 %9, label %10, label %15
10: ; preds = %7, %10
%11 = phi i32 [ %13, %10 ], [ 0, %7 ]
%12 = tail call i32 @matrix_mul_basic(double* %5, double* %3, double* %4, i32 %0)
%13 = add nuw nsw i32 %11, 1
%14 = icmp eq i32 %13, %1
br i1 %14, label %15, label %10
15: ; preds = %10, %7
%16 = tail call i64 (...) @clock()
%17 = sub nsw i64 %16, %8
%18 = sitofp i64 %17 to double
%19 = load double, double* @CLOCKS_PER_SEC
%20 = fdiv double %18, %19
%21 = sitofp i32 %1 to double
%22 = fdiv double %20, %21
br label %23
23: ; preds = %15, %2
%24 = phi double [ %22, %15 ], [ 0.000000e+00, %2 ]
%25 = tail call i64 (...) @clock()
%26 = icmp sgt i32 %1, 0
br i1 %26, label %27, label %32
27: ; preds = %23, %27
%28 = phi i32 [ %30, %27 ], [ 0, %23 ]
%29 = tail call i32 @matrix_mul_transposed(double* %5, double* %3, double* %4, i32 %0)
%30 = add nuw nsw i32 %28, 1
%31 = icmp eq i32 %30, %1
br i1 %31, label %32, label %27
32: ; preds = %27, %23
%33 = tail call i64 (...) @clock()
%34 = load double, double* @CLOCKS_PER_SEC
%35 = sitofp i32 %1 to double
%36 = tail call i64 (...) @clock()
br i1 %26, label %37, label %42
37: ; preds = %32, %37
%38 = phi i32 [ %40, %37 ], [ 0, %32 ]
%39 = tail call i32 @matrix_mul_blocked(double* %5, double* %3, double* %4, i32 %0, i32 128)
%40 = add nuw nsw i32 %38, 1
%41 = icmp eq i32 %40, %1
br i1 %41, label %42, label %37
42: ; preds = %37, %32
%43 = sub nsw i64 %33, %25
%44 = sitofp i64 %43 to double
%45 = fdiv double %44, %34
%46 = fdiv double %45, %35
%47 = tail call i64 (...) @clock()
%48 = sub nsw i64 %47, %36
%49 = sitofp i64 %48 to double
%50 = load double, double* @CLOCKS_PER_SEC
%51 = fdiv double %49, %50
%52 = fdiv double %51, %35
%53 = tail call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %0, double %24, double %46, double %52)
%54 = tail call i32 @free(double* %3)
%55 = tail call i32 @free(double* %4)
%56 = tail call i32 @free(double* %5)
ret void
}