#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 */
float CLOCKS_PER_SEC;
scalar_t__ clock();
int /*<<< orphan*/ free(float *);
int /*<<< orphan*/ gemm_cpu(int, int, int, int, int, int, float *, int, float *,
                            int, int, float *, int);
int /*<<< orphan*/ printf(char *, int, int, int, int, int, int, float);
float *random_matrix(int, int);
void time_random_matrix(int TA, int TB, int m, int k, int n) {
  float *a;
  if (!TA)
    a = random_matrix(m, k);
  else
    a = random_matrix(k, m);
  int lda = (!TA) ? k : m;
  float *b;
  if (!TB)
    b = random_matrix(k, n);
  else
    b = random_matrix(n, k);
  int ldb = (!TB) ? n : k;
  float *c = random_matrix(m, n);
  int i;
  clock_t start = clock(), end;
  for (i = 0; i < 10; ++i) {
    gemm_cpu(TA, TB, m, n, k, 1, a, lda, b, ldb, 1, c, n);
  }
  end = clock();
  printf("Matrix Multiplication %dx%d * %dx%d, TA=%d, TB=%d: %lf ms\n", m, k, k,
         n, TA, TB, (float)(end - start) / CLOCKS_PER_SEC);
  free(a);
  free(b);
  free(c);
}


x86 assembly (clang, -O0):
<time_random_matrix>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
mov    %edi,-0x1c(%rbp)
mov    %esi,-0x20(%rbp)
mov    %edx,-0x24(%rbp)
mov    %ecx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    118e <time_random_matrix+0x3e>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1060 <random_matrix@plt>
mov    %rax,-0x38(%rbp)
jmp    119d <time_random_matrix+0x4d>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1060 <random_matrix@plt>
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11b2 <time_random_matrix+0x62>
mov    -0x28(%rbp),%eax
mov    %eax,-0x74(%rbp)
jmp    11b8 <time_random_matrix+0x68>
mov    -0x24(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x74(%rbp),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11dc <time_random_matrix+0x8c>
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1060 <random_matrix@plt>
mov    %rax,-0x48(%rbp)
jmp    11eb <time_random_matrix+0x9b>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1060 <random_matrix@plt>
mov    %rax,-0x48(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1200 <time_random_matrix+0xb0>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x78(%rbp)
jmp    1206 <time_random_matrix+0xb6>
mov    -0x28(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    -0x78(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1060 <random_matrix@plt>
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x68(%rbp)
movl   $0x0,-0x5c(%rbp)
cmpl   $0xa,-0x5c(%rbp)
jge    129b <time_random_matrix+0x14b>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%r8d
mov    -0x38(%rbp),%r15
mov    -0x3c(%rbp),%r14d
mov    -0x48(%rbp),%rbx
mov    -0x4c(%rbp),%r11d
mov    -0x58(%rbp),%r10
mov    -0x2c(%rbp),%eax
mov    $0x1,%r9d
mov    %r15,(%rsp)
mov    %r14d,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r11d,0x18(%rsp)
movl   $0x1,0x20(%rsp)
mov    %r10,0x28(%rsp)
mov    %eax,0x30(%rsp)
call   1070 <gemm_cpu@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    122d <time_random_matrix+0xdd>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x70(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
mov    -0x20(%rbp),%eax
mov    -0x70(%rbp),%rdi
sub    -0x68(%rbp),%rdi
cvtsi2ss %rdi,%xmm0
mov    0x2d1a(%rip),%rdi
divss  (%rdi),%xmm0
lea    0xd27(%rip),%rdi
mov    %eax,(%rsp)
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_random_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8d,%ebx
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %esi,%r15d
test   %edi,%edi
mov    %edi,0x1c(%rsp)
mov    %edx,0xc(%rsp)
je     1183 <time_random_matrix+0x33>
mov    %r12d,%edi
mov    %ebp,%esi
call   1060 <random_matrix@plt>
mov    %ebp,%ecx
jmp    1190 <time_random_matrix+0x40>
mov    %ebp,%edi
mov    %r12d,%esi
call   1060 <random_matrix@plt>
mov    %r12d,%ecx
mov    %rcx,0x40(%rsp)
mov    %rax,0x28(%rsp)
mov    %r15,0x30(%rsp)
test   %r15d,%r15d
je     11bb <time_random_matrix+0x6b>
mov    %ebx,%edi
mov    %r12d,%esi
call   1060 <random_matrix@plt>
mov    %rax,0x10(%rsp)
mov    %r12d,%r13d
mov    %r12d,%r14d
jmp    11d0 <time_random_matrix+0x80>
mov    %r12d,%r13d
mov    %r12d,%edi
mov    %ebx,%esi
call   1060 <random_matrix@plt>
mov    %rax,0x10(%rsp)
mov    %ebx,%r14d
mov    0xc(%rsp),%edi
mov    %ebx,%esi
call   1060 <random_matrix@plt>
mov    %rax,0x20(%rsp)
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,0x38(%rsp)
mov    $0xa,%ebp
mov    0x1c(%rsp),%r15d
mov    0x40(%rsp),%r12
nopl   0x0(%rax,%rax,1)
sub    $0x8,%rsp
mov    %r15d,%edi
mov    0x38(%rsp),%rsi
mov    0x14(%rsp),%edx
mov    %ebx,%ecx
mov    %r13d,%r8d
mov    $0x1,%r9d
push   %rbx
push   0x30(%rsp)
push   $0x1
push   %r14
push   0x38(%rsp)
push   %r12
push   0x60(%rsp)
call   1070 <gemm_cpu@plt>
add    $0x40,%rsp
add    $0xffffffff,%ebp
jne    1200 <time_random_matrix+0xb0>
xor    %eax,%eax
call   1040 <clock@plt>
sub    0x38(%rsp),%rax
cvtsi2ss %rax,%xmm0
mov    0x2d94(%rip),%rax
divss  (%rax),%xmm0
sub    $0x8,%rsp
lea    0xd9d(%rip),%rdi
mov    0x14(%rsp),%esi
mov    %r13d,%edx
mov    %r13d,%ecx
mov    %ebx,%r8d
mov    %r15d,%r9d
push   0x38(%rsp)
call   1050 <printf@plt>
add    $0x10,%rsp
mov    0x28(%rsp),%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x20(%rsp),%rdi
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

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

LLVM IR (-O2):
@.str = [59 x i8] c"Matrix Multiplication %dx%d * %dx%d, TA=%d, TB=%d: %lf ms\0A\00"
@CLOCKS_PER_SEC = global float 0.000000e+00
define void @time_random_matrix(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = icmp eq i32 %0, 0
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = tail call float* @random_matrix(i32 %2, i32 %3)
br label %11
9: ; preds = %5
%10 = tail call float* @random_matrix(i32 %3, i32 %2)
br label %11
11: ; preds = %9, %7
%12 = phi i32 [ %2, %9 ], [ %3, %7 ]
%13 = phi float* [ %10, %9 ], [ %8, %7 ]
%14 = icmp eq i32 %1, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call float* @random_matrix(i32 %3, i32 %4)
br label %19
17: ; preds = %11
%18 = tail call float* @random_matrix(i32 %4, i32 %3)
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ %3, %17 ], [ %4, %15 ]
%21 = phi float* [ %18, %17 ], [ %16, %15 ]
%22 = tail call float* @random_matrix(i32 %2, i32 %4)
%23 = tail call i64 (...) @clock()
br label %24
24: ; preds = %19, %24
%25 = phi i32 [ 0, %19 ], [ %27, %24 ]
%26 = tail call i32 @gemm_cpu(i32 %0, i32 %1, i32 %2, i32 %4, i32 %3, i32 1, float* %13, i32 %12, float* %21, i32 %20, i32 1, float* %22, i32 %4)
%27 = add nuw nsw i32 %25, 1
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %24
29: ; preds = %24
%30 = tail call i64 (...) @clock()
%31 = sub nsw i64 %30, %23
%32 = sitofp i64 %31 to float
%33 = load float, float* @CLOCKS_PER_SEC
%34 = fdiv float %32, %33
%35 = tail call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %3, i32 %3, i32 %4, i32 %0, i32 %1, float %34)
%36 = tail call i32 @free(float* %13)
%37 = tail call i32 @free(float* %21)
%38 = tail call i32 @free(float* %22)
ret void
}