#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 */
struct timespec {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
double *alloc_matrix(int);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ matrix_mul_blocked(double *, double *, double *, int, int);
int /*<<< orphan*/ printf(char *, int, int, double);
double *random_matrix(int);
double seconds(struct timespec, struct timespec);
void benchmark_blocks(int n, int repeats) {
  double *a = random_matrix(n);
  double *b = random_matrix(n);
  double *res = alloc_matrix(n);
  struct timespec start, stop;
  int i;
  int blocks = 4;
  for (blocks = 4; blocks <= 1024; blocks *= 2) {
    clock_gettime(CLOCK_REALTIME, &start);
    for (i = 0; i < repeats; ++i) {
      matrix_mul_blocked(res, a, b, n, blocks);
    }
    clock_gettime(CLOCK_REALTIME, &stop);
    double blocked = seconds(start, stop);
    printf("%d, blocks: %d, blocked: %.4lf\n", n, blocks, blocked);
  }
  free(a);
  free(b);
  free(res);
}


x86 assembly (clang, -O0):
<benchmark_blocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1080 <random_matrix@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1080 <random_matrix@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <alloc_matrix@plt>
mov    %rax,-0x20(%rbp)
movl   $0x4,-0x38(%rbp)
movl   $0x4,-0x38(%rbp)
cmpl   $0x400,-0x38(%rbp)
jg     124f <benchmark_blocks+0xdf>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1040 <clock_gettime@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1208 <benchmark_blocks+0x98>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x4(%rbp),%ecx
mov    -0x38(%rbp),%r8d
call   1090 <matrix_mul_blocked@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d6 <benchmark_blocks+0x66>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1050 <seconds@plt>
movsd  %xmm0,-0x40(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x38(%rbp),%edx
movsd  -0x40(%rbp),%xmm0
lea    0xdc4(%rip),%rdi
call   1070 <printf@plt>
mov    -0x38(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11b0 <benchmark_blocks+0x40>
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    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark_blocks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,0x4(%rsp)
mov    %edi,%r13d
call   1080 <random_matrix@plt>
mov    %rax,%r15
mov    %r13d,%edi
call   1080 <random_matrix@plt>
mov    %rax,%r12
mov    %r13d,%edi
call   1060 <alloc_matrix@plt>
mov    %rax,%rbx
mov    $0x4,%ebp
mov    0x2e31(%rip),%r14
jmp    11fc <benchmark_blocks+0x8c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2e19(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x10(%rsp),%edi
mov    0x8(%rsp),%esi
call   1050 <seconds@plt>
lea    0xe18(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
call   1070 <printf@plt>
add    %ebp,%ebp
cmp    $0x401,%ebp
jae    123c <benchmark_blocks+0xcc>
mov    (%r14),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
cmpl   $0x0,0x4(%rsp)
jle    11c0 <benchmark_blocks+0x50>
mov    0x4(%rsp),%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13d,%ecx
mov    %ebp,%r8d
call   1090 <matrix_mul_blocked@plt>
add    $0xffffffff,%r14d
jne    1220 <benchmark_blocks+0xb0>
jmp    11c0 <benchmark_blocks+0x50>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_REALTIME = global i32 0
@.str = [32 x i8] c"%d, blocks: %d, blocked: %.4lf\0A\00"
define void @benchmark_blocks(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca double*
%6 = alloca double*
%7 = alloca double*
%8 = alloca %struct.timespec
%9 = alloca %struct.timespec
%10 = alloca i32
%11 = alloca i32
%12 = alloca double
store i32 %0, i32* %3
store i32 %1, i32* %4
%13 = load i32, i32* %3
%14 = call double* @random_matrix(i32 %13)
store double* %14, double** %5
%15 = load i32, i32* %3
%16 = call double* @random_matrix(i32 %15)
store double* %16, double** %6
%17 = load i32, i32* %3
%18 = call double* @alloc_matrix(i32 %17)
store double* %18, double** %7
store i32 4, i32* %11
store i32 4, i32* %11
br label %19
19: ; preds = %51, %2
%20 = load i32, i32* %11
%21 = icmp sle i32 %20, 1024
br i1 %21, label %22, label %54
22: ; preds = %19
%23 = load i32, i32* @CLOCK_REALTIME
%24 = call i32 @clock_gettime(i32 %23, %struct.timespec* %8)
store i32 0, i32* %10
br label %25
25: ; preds = %36, %22
%26 = load i32, i32* %10
%27 = load i32, i32* %4
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %39
29: ; preds = %25
%30 = load double*, double** %7
%31 = load double*, double** %5
%32 = load double*, double** %6
%33 = load i32, i32* %3
%34 = load i32, i32* %11
%35 = call i32 @matrix_mul_blocked(double* %30, double* %31, double* %32, i32 %33, i32 %34)
br label %36
36: ; preds = %29
%37 = load i32, i32* %10
%38 = add nsw i32 %37, 1
store i32 %38, i32* %10
br label %25
39: ; preds = %25
%40 = load i32, i32* @CLOCK_REALTIME
%41 = call i32 @clock_gettime(i32 %40, %struct.timespec* %9)
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call double @seconds(i32 %43, i32 %45)
store double %46, double* %12
%47 = load i32, i32* %3
%48 = load i32, i32* %11
%49 = load double, double* %12
%50 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %47, i32 %48, double %49)
br label %51
51: ; preds = %39
%52 = load i32, i32* %11
%53 = mul nsw i32 %52, 2
store i32 %53, i32* %11
br label %19
54: ; preds = %19
%55 = load double*, double** %5
%56 = call i32 @free(double* %55)
%57 = load double*, double** %6
%58 = call i32 @free(double* %57)
%59 = load double*, double** %7
%60 = call i32 @free(double* %59)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_REALTIME = global i32 0
@.str = [32 x i8] c"%d, blocks: %d, blocked: %.4lf\0A\00"
define void @benchmark_blocks(i32 %0, i32 %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = tail call double* @random_matrix(i32 %0)
%6 = tail call double* @random_matrix(i32 %0)
%7 = tail call double* @alloc_matrix(i32 %0)
%8 = bitcast %struct.timespec* %3 to i8*
%9 = bitcast %struct.timespec* %4 to i8*
%10 = icmp sgt i32 %1, 0
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
br label %13
13: ; preds = %2, %22
%14 = phi i32 [ 4, %2 ], [ %29, %22 ]
%15 = load i32, i32* @CLOCK_REALTIME
%16 = call i32 @clock_gettime(i32 %15, %struct.timespec* %3)
br i1 %10, label %17, label %22
17: ; preds = %13, %17
%18 = phi i32 [ %20, %17 ], [ 0, %13 ]
%19 = call i32 @matrix_mul_blocked(double* %7, double* %5, double* %6, i32 %0, i32 %14)
%20 = add nuw nsw i32 %18, 1
%21 = icmp eq i32 %20, %1
br i1 %21, label %22, label %17
22: ; preds = %17, %13
%23 = load i32, i32* @CLOCK_REALTIME
%24 = call i32 @clock_gettime(i32 %23, %struct.timespec* %4)
%25 = load i32, i32* %11
%26 = load i32, i32* %12
%27 = call double @seconds(i32 %25, i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %14, double %27)
%29 = shl nsw i32 %14, 1
%30 = icmp ult i32 %29, 1025
br i1 %30, label %13, label %31
31: ; preds = %22
%32 = call i32 @free(double* %5)
%33 = call i32 @free(double* %6)
%34 = call i32 @free(double* %7)
ret void
}