#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 */

/* Variables and functions */
int /*<<< orphan*/ gemm_nn(int const, int const, int const, double const,
                           double const *, int const, double const *, int const,
                           double *, int const);
int /*<<< orphan*/ gemm_nt(int const, int const, int const, double const,
                           double const *, int const, double const *, int const,
                           double *, int const);
int /*<<< orphan*/ gemm_tn(int const, int const, int const, double const,
                           double const *, int const, double const *, int const,
                           double *, int const);
int /*<<< orphan*/ gemm_tt(int const, int const, int const, double const,
                           double const *, int const, double const *, int const,
                           double *, int const);
void blas_gemm(const int TA, const int TB, const int M, const int N,
               const int K, const double ALPHA, const double *A, const int lda,
               const double *B, const int ldb, const double BETA, double *C,
               const int ldc) {
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < N; ++j) {
      C[i * ldc + j] *= BETA;
    }
  }
  if (!TA && !TB) {
    gemm_nn(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
  } else if (TA && !TB) {
    gemm_tn(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
  } else if (!TA && TB) {
    gemm_nt(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
  } else {
    gemm_tt(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
  }
}


x86 assembly (clang, -O0):
<blas_gemm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x30(%rbp),%eax
mov    0x28(%rbp),%rax
mov    0x20(%rbp),%eax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%eax
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8d,-0x14(%rbp)
movsd  %xmm0,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
movsd  %xmm1,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11de <blas_gemm+0x9e>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11cb <blas_gemm+0x8b>
movsd  -0x30(%rbp),%xmm0
mov    0x28(%rbp),%rax
mov    -0x34(%rbp),%ecx
imul   0x30(%rbp),%ecx
add    -0x38(%rbp),%ecx
movslq %ecx,%rcx
mulsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1191 <blas_gemm+0x51>
jmp    11d0 <blas_gemm+0x90>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    117e <blas_gemm+0x3e>
cmpl   $0x0,-0x4(%rbp)
jne    122e <blas_gemm+0xee>
cmpl   $0x0,-0x8(%rbp)
jne    122e <blas_gemm+0xee>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
movsd  -0x20(%rbp),%xmm0
mov    -0x28(%rbp),%rcx
mov    0x10(%rbp),%r8d
mov    0x18(%rbp),%r9
mov    0x20(%rbp),%r11d
mov    0x28(%rbp),%r10
mov    0x30(%rbp),%eax
mov    %r11d,(%rsp)
mov    %r10,0x8(%rsp)
mov    %eax,0x10(%rsp)
call   1050 <gemm_nn@plt>
jmp    130f <blas_gemm+0x1cf>
cmpl   $0x0,-0x4(%rbp)
je     127e <blas_gemm+0x13e>
cmpl   $0x0,-0x8(%rbp)
jne    127e <blas_gemm+0x13e>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
movsd  -0x20(%rbp),%xmm0
mov    -0x28(%rbp),%rcx
mov    0x10(%rbp),%r8d
mov    0x18(%rbp),%r9
mov    0x20(%rbp),%r11d
mov    0x28(%rbp),%r10
mov    0x30(%rbp),%eax
mov    %r11d,(%rsp)
mov    %r10,0x8(%rsp)
mov    %eax,0x10(%rsp)
call   1040 <gemm_tn@plt>
jmp    130a <blas_gemm+0x1ca>
cmpl   $0x0,-0x4(%rbp)
jne    12ce <blas_gemm+0x18e>
cmpl   $0x0,-0x8(%rbp)
je     12ce <blas_gemm+0x18e>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
movsd  -0x20(%rbp),%xmm0
mov    -0x28(%rbp),%rcx
mov    0x10(%rbp),%r8d
mov    0x18(%rbp),%r9
mov    0x20(%rbp),%r11d
mov    0x28(%rbp),%r10
mov    0x30(%rbp),%eax
mov    %r11d,(%rsp)
mov    %r10,0x8(%rsp)
mov    %eax,0x10(%rsp)
call   1060 <gemm_nt@plt>
jmp    1305 <blas_gemm+0x1c5>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
movsd  -0x20(%rbp),%xmm0
mov    -0x28(%rbp),%rcx
mov    0x10(%rbp),%r8d
mov    0x18(%rbp),%r9
mov    0x20(%rbp),%r11d
mov    0x28(%rbp),%r10
mov    0x30(%rbp),%eax
mov    %r11d,(%rsp)
mov    %r10,0x8(%rsp)
mov    %eax,0x10(%rsp)
call   1030 <gemm_tt@plt>
jmp    130a <blas_gemm+0x1ca>
jmp    130f <blas_gemm+0x1cf>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<blas_gemm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,0x8(%rsp)
mov    %r8d,0x4(%rsp)
mov    %esi,0x14(%rsp)
mov    %edi,0x10(%rsp)
test   %edx,%edx
jle    12b9 <blas_gemm+0x179>
mov    0x70(%rsp),%eax
movslq %eax,%rdi
mov    %edx,%ebx
mov    %ecx,%eax
mov    %eax,%r11d
and    $0xfffffffc,%r11d
lea    -0x4(%r11),%r12
mov    %r12,%r13
shr    $0x2,%r13
add    $0x1,%r13
movapd %xmm1,%xmm2
unpcklpd %xmm1,%xmm2
mov    %r13,%rsi
and    $0xfffffffffffffffe,%rsi
mov    0x68(%rsp),%r15
lea    0x30(%r15),%r14
lea    0x0(,%rdi,8),%r10
xor    %r9d,%r9d
jmp    11c3 <blas_gemm+0x83>
xchg   %ax,%ax
add    $0x1,%r9
add    %r10,%r14
add    %r10,%r15
cmp    %rbx,%r9
je     12b9 <blas_gemm+0x179>
test   %ecx,%ecx
jle    11b0 <blas_gemm+0x70>
cmp    $0x4,%ecx
jae    11e0 <blas_gemm+0xa0>
xor    %ebp,%ebp
jmp    1290 <blas_gemm+0x150>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %r12,%r12
je     12ae <blas_gemm+0x16e>
mov    %rsi,%rbp
xor    %r8d,%r8d
nop
movupd -0x30(%r14,%r8,8),%xmm3
movupd -0x20(%r14,%r8,8),%xmm4
movupd -0x10(%r14,%r8,8),%xmm5
movupd (%r14,%r8,8),%xmm6
mulpd  %xmm2,%xmm3
mulpd  %xmm2,%xmm4
movupd %xmm3,-0x30(%r14,%r8,8)
movupd %xmm4,-0x20(%r14,%r8,8)
mulpd  %xmm2,%xmm5
mulpd  %xmm2,%xmm6
movupd %xmm5,-0x10(%r14,%r8,8)
movupd %xmm6,(%r14,%r8,8)
add    $0x8,%r8
add    $0xfffffffffffffffe,%rbp
jne    11f0 <blas_gemm+0xb0>
test   $0x1,%r13b
je     1279 <blas_gemm+0x139>
mov    %r9,%rbp
imul   %rdi,%rbp
add    %rbp,%r8
mov    0x68(%rsp),%rbp
movupd 0x0(%rbp,%r8,8),%xmm3
movupd 0x10(%rbp,%r8,8),%xmm4
mulpd  %xmm2,%xmm3
mulpd  %xmm2,%xmm4
movupd %xmm3,0x0(%rbp,%r8,8)
movupd %xmm4,0x10(%rbp,%r8,8)
mov    %r11,%rbp
cmp    %rax,%r11
je     11b0 <blas_gemm+0x70>
cs nopw 0x0(%rax,%rax,1)
nop
movsd  (%r15,%rbp,8),%xmm3
mulsd  %xmm1,%xmm3
movsd  %xmm3,(%r15,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%rax
jne    1290 <blas_gemm+0x150>
jmp    11b0 <blas_gemm+0x70>
xor    %r8d,%r8d
test   $0x1,%r13b
jne    1246 <blas_gemm+0x106>
jmp    1279 <blas_gemm+0x139>
mov    0x60(%rsp),%eax
mov    0x58(%rsp),%r10
mov    0x50(%rsp),%r11d
mov    0x14(%rsp),%edi
mov    %edi,%esi
mov    0x10(%rsp),%ebp
or     %ebp,%esi
je     1305 <blas_gemm+0x1c5>
test   %ebp,%ebp
je     132d <blas_gemm+0x1ed>
test   %edi,%edi
jne    132d <blas_gemm+0x1ed>
sub    $0x8,%rsp
mov    %edx,%edi
mov    %ecx,%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%rcx
mov    %r11d,%r8d
mov    %r10,%r9
mov    0x78(%rsp),%ebp
push   %rbp
push   0x78(%rsp)
push   %rax
call   1040 <gemm_tn@plt>
jmp    1383 <blas_gemm+0x243>
sub    $0x8,%rsp
mov    %edx,%edi
mov    %ecx,%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%rcx
mov    %r11d,%r8d
mov    %r10,%r9
mov    0x78(%rsp),%ebp
push   %rbp
push   0x78(%rsp)
push   %rax
call   1050 <gemm_nn@plt>
jmp    1383 <blas_gemm+0x243>
test   %ebp,%ebp
jne    135d <blas_gemm+0x21d>
test   %edi,%edi
je     135d <blas_gemm+0x21d>
sub    $0x8,%rsp
mov    %edx,%edi
mov    %ecx,%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%rcx
mov    %r11d,%r8d
mov    %r10,%r9
mov    0x78(%rsp),%ebp
push   %rbp
push   0x78(%rsp)
push   %rax
call   1060 <gemm_nt@plt>
jmp    1383 <blas_gemm+0x243>
sub    $0x8,%rsp
mov    %edx,%edi
mov    %ecx,%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%rcx
mov    %r11d,%r8d
mov    %r10,%r9
mov    0x78(%rsp),%ebp
push   %rbp
push   0x78(%rsp)
push   %rax
call   1030 <gemm_tt@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @blas_gemm(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, double %5, double* %6, i32 %7, double* %8, i32 %9, double %10, double* %11, i32 %12) {
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca double
%20 = alloca double*
%21 = alloca i32
%22 = alloca double*
%23 = alloca i32
%24 = alloca double
%25 = alloca double*
%26 = alloca i32
%27 = alloca i32
%28 = alloca i32
store i32 %0, i32* %14
store i32 %1, i32* %15
store i32 %2, i32* %16
store i32 %3, i32* %17
store i32 %4, i32* %18
store double %5, double* %19
store double* %6, double** %20
store i32 %7, i32* %21
store double* %8, double** %22
store i32 %9, i32* %23
store double %10, double* %24
store double* %11, double** %25
store i32 %12, i32* %26
store i32 0, i32* %27
br label %29
29: ; preds = %54, %13
%30 = load i32, i32* %27
%31 = load i32, i32* %16
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %57
33: ; preds = %29
store i32 0, i32* %28
br label %34
34: ; preds = %50, %33
%35 = load i32, i32* %28
%36 = load i32, i32* %17
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %53
38: ; preds = %34
%39 = load double, double* %24
%40 = load double*, double** %25
%41 = load i32, i32* %27
%42 = load i32, i32* %26
%43 = mul nsw i32 %41, %42
%44 = load i32, i32* %28
%45 = add nsw i32 %43, %44
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds double, double* %40, i64 %46
%48 = load double, double* %47
%49 = fmul double %48, %39
store double %49, double* %47
br label %50
50: ; preds = %38
%51 = load i32, i32* %28
%52 = add nsw i32 %51, 1
store i32 %52, i32* %28
br label %34
53: ; preds = %34
br label %54
54: ; preds = %53
%55 = load i32, i32* %27
%56 = add nsw i32 %55, 1
store i32 %56, i32* %27
br label %29
57: ; preds = %29
%58 = load i32, i32* %14
%59 = icmp ne i32 %58, 0
br i1 %59, label %75, label %60
60: ; preds = %57
%61 = load i32, i32* %15
%62 = icmp ne i32 %61, 0
br i1 %62, label %75, label %63
63: ; preds = %60
%64 = load i32, i32* %16
%65 = load i32, i32* %17
%66 = load i32, i32* %18
%67 = load double, double* %19
%68 = load double*, double** %20
%69 = load i32, i32* %21
%70 = load double*, double** %22
%71 = load i32, i32* %23
%72 = load double*, double** %25
%73 = load i32, i32* %26
%74 = call i32 @gemm_nn(i32 %64, i32 %65, i32 %66, double %67, double* %68, i32 %69, double* %70, i32 %71, double* %72, i32 %73)
br label %125
75: ; preds = %60, %57
%76 = load i32, i32* %14
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %93
78: ; preds = %75
%79 = load i32, i32* %15
%80 = icmp ne i32 %79, 0
br i1 %80, label %93, label %81
81: ; preds = %78
%82 = load i32, i32* %16
%83 = load i32, i32* %17
%84 = load i32, i32* %18
%85 = load double, double* %19
%86 = load double*, double** %20
%87 = load i32, i32* %21
%88 = load double*, double** %22
%89 = load i32, i32* %23
%90 = load double*, double** %25
%91 = load i32, i32* %26
%92 = call i32 @gemm_tn(i32 %82, i32 %83, i32 %84, double %85, double* %86, i32 %87, double* %88, i32 %89, double* %90, i32 %91)
br label %124
93: ; preds = %78, %75
%94 = load i32, i32* %14
%95 = icmp ne i32 %94, 0
br i1 %95, label %111, label %96
96: ; preds = %93
%97 = load i32, i32* %15
%98 = icmp ne i32 %97, 0
br i1 %98, label %99, label %111
99: ; preds = %96
%100 = load i32, i32* %16
%101 = load i32, i32* %17
%102 = load i32, i32* %18
%103 = load double, double* %19
%104 = load double*, double** %20
%105 = load i32, i32* %21
%106 = load double*, double** %22
%107 = load i32, i32* %23
%108 = load double*, double** %25
%109 = load i32, i32* %26
%110 = call i32 @gemm_nt(i32 %100, i32 %101, i32 %102, double %103, double* %104, i32 %105, double* %106, i32 %107, double* %108, i32 %109)
br label %123
111: ; preds = %96, %93
%112 = load i32, i32* %16
%113 = load i32, i32* %17
%114 = load i32, i32* %18
%115 = load double, double* %19
%116 = load double*, double** %20
%117 = load i32, i32* %21
%118 = load double*, double** %22
%119 = load i32, i32* %23
%120 = load double*, double** %25
%121 = load i32, i32* %26
%122 = call i32 @gemm_tt(i32 %112, i32 %113, i32 %114, double %115, double* %116, i32 %117, double* %118, i32 %119, double* %120, i32 %121)
br label %123
123: ; preds = %111, %99
br label %124
124: ; preds = %123, %81
br label %125
125: ; preds = %124, %63
ret void
}

LLVM IR (-O2):
define void @blas_gemm(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, double %5, double* %6, i32 %7, double* %8, i32 %9, double %10, double* %11, i32 %12) {
%14 = icmp sgt i32 %2, 0
br i1 %14, label %15, label %86
15: ; preds = %13
%16 = icmp sgt i32 %3, 0
%17 = sext i32 %12 to i64
%18 = zext i32 %2 to i64
%19 = zext i32 %3 to i64
%20 = and i64 %19, 4294967292
%21 = add nsw i64 %20, -4
%22 = lshr exact i64 %21, 2
%23 = add nuw nsw i64 %22, 1
%24 = icmp ult i32 %3, 4
%25 = and i64 %19, 4294967292
%26 = insertelement <2 x double> poison, double %10, i64 0
%27 = shufflevector <2 x double> %26, <2 x double> poison, <2 x i32> zeroinitializer
%28 = insertelement <2 x double> poison, double %10, i64 0
%29 = shufflevector <2 x double> %28, <2 x double> poison, <2 x i32> zeroinitializer
%30 = and i64 %23, 1
%31 = icmp eq i64 %21, 0
%32 = and i64 %23, 9223372036854775806
%33 = icmp eq i64 %30, 0
%34 = icmp eq i64 %25, %19
br label %35
35: ; preds = %15, %90
%36 = phi i64 [ 0, %15 ], [ %91, %90 ]
br i1 %16, label %37, label %90
37: ; preds = %35
%38 = mul nsw i64 %36, %17
br i1 %24, label %84, label %39
39: ; preds = %37
br i1 %31, label %69, label %40
40: ; preds = %39, %40
%41 = phi i64 [ %66, %40 ], [ 0, %39 ]
%42 = phi i64 [ %67, %40 ], [ 0, %39 ]
%43 = add nsw i64 %41, %38
%44 = getelementptr inbounds double, double* %11, i64 %43
%45 = bitcast double* %44 to <2 x double>*
%46 = load <2 x double>, <2 x double>* %45
%47 = getelementptr inbounds double, double* %44, i64 2
%48 = bitcast double* %47 to <2 x double>*
%49 = load <2 x double>, <2 x double>* %48
%50 = fmul <2 x double> %46, %27
%51 = fmul <2 x double> %49, %29
%52 = bitcast double* %44 to <2 x double>*
store <2 x double> %50, <2 x double>* %52
%53 = bitcast double* %47 to <2 x double>*
store <2 x double> %51, <2 x double>* %53
%54 = or i64 %41, 4
%55 = add nsw i64 %54, %38
%56 = getelementptr inbounds double, double* %11, i64 %55
%57 = bitcast double* %56 to <2 x double>*
%58 = load <2 x double>, <2 x double>* %57
%59 = getelementptr inbounds double, double* %56, i64 2
%60 = bitcast double* %59 to <2 x double>*
%61 = load <2 x double>, <2 x double>* %60
%62 = fmul <2 x double> %58, %27
%63 = fmul <2 x double> %61, %29
%64 = bitcast double* %56 to <2 x double>*
store <2 x double> %62, <2 x double>* %64
%65 = bitcast double* %59 to <2 x double>*
store <2 x double> %63, <2 x double>* %65
%66 = add nuw i64 %41, 8
%67 = add i64 %42, 2
%68 = icmp eq i64 %67, %32
br i1 %68, label %69, label %40
69: ; preds = %40, %39
%70 = phi i64 [ 0, %39 ], [ %66, %40 ]
br i1 %33, label %83, label %71
71: ; preds = %69
%72 = add nsw i64 %70, %38
%73 = getelementptr inbounds double, double* %11, i64 %72
%74 = bitcast double* %73 to <2 x double>*
%75 = load <2 x double>, <2 x double>* %74
%76 = getelementptr inbounds double, double* %73, i64 2
%77 = bitcast double* %76 to <2 x double>*
%78 = load <2 x double>, <2 x double>* %77
%79 = fmul <2 x double> %75, %27
%80 = fmul <2 x double> %78, %29
%81 = bitcast double* %73 to <2 x double>*
store <2 x double> %79, <2 x double>* %81
%82 = bitcast double* %76 to <2 x double>*
store <2 x double> %80, <2 x double>* %82
br label %83
83: ; preds = %69, %71
br i1 %34, label %90, label %84
84: ; preds = %37, %83
%85 = phi i64 [ 0, %37 ], [ %25, %83 ]
br label %93
86: ; preds = %90, %13
%87 = icmp ne i32 %1, 0
%88 = or i32 %1, %0
%89 = icmp eq i32 %88, 0
br i1 %89, label %101, label %103
90: ; preds = %93, %83, %35
%91 = add nuw nsw i64 %36, 1
%92 = icmp eq i64 %91, %18
br i1 %92, label %86, label %35
93: ; preds = %84, %93
%94 = phi i64 [ %99, %93 ], [ %85, %84 ]
%95 = add nsw i64 %94, %38
%96 = getelementptr inbounds double, double* %11, i64 %95
%97 = load double, double* %96
%98 = fmul double %97, %10
store double %98, double* %96
%99 = add nuw nsw i64 %94, 1
%100 = icmp eq i64 %99, %19
br i1 %100, label %90, label %93
101: ; preds = %86
%102 = tail call i32 @gemm_nn(i32 %2, i32 %3, i32 %4, double %5, double* %6, i32 %7, double* %8, i32 %9, double* %11, i32 %12)
br label %114
103: ; preds = %86
%104 = icmp eq i32 %0, 0
%105 = or i1 %104, %87
br i1 %105, label %108, label %106
106: ; preds = %103
%107 = tail call i32 @gemm_tn(i32 %2, i32 %3, i32 %4, double %5, double* %6, i32 %7, double* %8, i32 %9, double* %11, i32 %12)
br label %114
108: ; preds = %103
%109 = and i1 %104, %87
br i1 %109, label %110, label %112
110: ; preds = %108
%111 = tail call i32 @gemm_nt(i32 %2, i32 %3, i32 %4, double %5, double* %6, i32 %7, double* %8, i32 %9, double* %11, i32 %12)
br label %114
112: ; preds = %108
%113 = tail call i32 @gemm_tt(i32 %2, i32 %3, i32 %4, double %5, double* %6, i32 %7, double* %8, i32 %9, double* %11, i32 %12)
br label %114
114: ; preds = %106, %112, %110, %101
ret void
}