void dgemm_transpose(double *a, double *b, double *c, int n) {
  int i, j, k;
  for (i = 0; i < n; i++) {
    for (k = 0; k < n; k++) {
      for (j = 0; j < n; j++) {
        *(c + i * n + j) += *(a + i * n + k) * *(b + k * n + j);
      }
    }
  }
}


x86 assembly (clang, -O0):
<dgemm_transpose>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11e7 <dgemm_transpose+0xe7>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11d4 <dgemm_transpose+0xd4>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11c1 <dgemm_transpose+0xc1>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
movslq -0x28(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1140 <dgemm_transpose+0x40>
jmp    11c6 <dgemm_transpose+0xc6>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    112d <dgemm_transpose+0x2d>
jmp    11d9 <dgemm_transpose+0xd9>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    111a <dgemm_transpose+0x1a>
pop    %rbp
ret

x86 assembly (clang, -O2):
<dgemm_transpose>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,-0x60(%rsp)
mov    %rdi,-0x50(%rsp)
test   %ecx,%ecx
jle    1385 <dgemm_transpose+0x285>
mov    %rsi,%r11
mov    %ecx,%r15d
mov    %r15d,%eax
and    $0xfffffffc,%eax
mov    %rax,-0x28(%rsp)
mov    %r15,%rax
neg    %rax
mov    %rax,-0x8(%rsp)
lea    0x10(%rsi),%rax
mov    %rax,-0x38(%rsp)
lea    0x0(,%r15,8),%r8
mov    -0x60(%rsp),%rax
lea    0x10(%rax),%rbx
mov    %r8,%r14
and    $0xffffffffffffffe0,%r14
add    $0x8,%rax
xor    %edx,%edx
mov    %r8,-0x58(%rsp)
mov    %ecx,-0x64(%rsp)
jmp    1188 <dgemm_transpose+0x88>
nopw   0x0(%rax,%rax,1)
mov    -0x30(%rsp),%rdx
add    $0x1,%rdx
add    %r8,%rbx
add    %r8,%rax
cmp    %r15,%rdx
je     1385 <dgemm_transpose+0x285>
mov    %rdx,-0x30(%rsp)
mov    %rdx,%rdi
imul   %r15,%rdi
mov    -0x60(%rsp),%rsi
lea    (%rsi,%rdi,8),%rdx
mov    %rdx,-0x48(%rsp)
lea    (%rdi,%r15,1),%rdx
lea    (%rsi,%rdx,8),%rdx
mov    %rdx,-0x18(%rsp)
lea    0x1(%rdi),%rdx
mov    %rdx,-0x20(%rsp)
mov    -0x50(%rsp),%rdx
mov    %rdi,-0x10(%rsp)
lea    (%rdx,%rdi,8),%rdx
mov    %r11,%r9
mov    -0x38(%rsp),%r12
xor    %r13d,%r13d
jmp    11ef <dgemm_transpose+0xef>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r13
add    %r8,%r12
add    %r8,%r9
cmp    %r15,%r13
je     1170 <dgemm_transpose+0x70>
mov    %r13,%rdi
imul   %r15,%rdi
lea    (%r11,%rdi,8),%rbp
cmp    $0x4,%ecx
mov    %rbp,-0x40(%rsp)
jae    12a0 <dgemm_transpose+0x1a0>
xor    %ebp,%ebp
mov    %rbp,%r10
test   $0x1,%r15b
je     1239 <dgemm_transpose+0x139>
movsd  (%rdx,%r13,8),%xmm0
mov    -0x40(%rsp),%rsi
mulsd  (%rsi,%rbp,8),%xmm0
mov    -0x48(%rsp),%rsi
addsd  (%rsi,%rbp,8),%xmm0
movsd  %xmm0,(%rsi,%rbp,8)
mov    %rbp,%r10
or     $0x1,%r10
not    %rbp
cmp    -0x8(%rsp),%rbp
je     11e0 <dgemm_transpose+0xe0>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%rdx,%r13,8),%xmm0
mulsd  (%r9,%r10,8),%xmm0
addsd  -0x8(%rax,%r10,8),%xmm0
movsd  %xmm0,-0x8(%rax,%r10,8)
movsd  (%rdx,%r13,8),%xmm0
mulsd  0x8(%r9,%r10,8),%xmm0
addsd  (%rax,%r10,8),%xmm0
movsd  %xmm0,(%rax,%r10,8)
add    $0x2,%r10
cmp    %r10,%r15
jne    1250 <dgemm_transpose+0x150>
jmp    11e0 <dgemm_transpose+0xe0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %r15,%rdi
mov    %r11,%rsi
lea    (%r11,%rdi,8),%r10
mov    -0x20(%rsp),%rcx
lea    (%rcx,%r13,1),%rdi
mov    -0x50(%rsp),%rcx
lea    (%rcx,%rdi,8),%rdi
mov    -0x10(%rsp),%rbp
add    %r13,%rbp
lea    (%rcx,%rbp,8),%rbp
mov    -0x48(%rsp),%r8
cmp    %rdi,%r8
setb   %r11b
mov    -0x18(%rsp),%rdi
cmp    %rdi,%rbp
setb   %cl
cmp    %r10,%r8
setb   %r10b
cmp    %rdi,-0x40(%rsp)
setb   %dil
test   %cl,%r11b
jne    1372 <dgemm_transpose+0x272>
mov    $0x0,%ebp
and    %dil,%r10b
mov    -0x64(%rsp),%ecx
mov    %rsi,%r11
mov    -0x58(%rsp),%r8
jne    120a <dgemm_transpose+0x10a>
movsd  (%rdx,%r13,8),%xmm0
unpcklpd %xmm0,%xmm0
xor    %edi,%edi
nopw   0x0(%rax,%rax,1)
movupd -0x10(%r12,%rdi,1),%xmm1
movupd (%r12,%rdi,1),%xmm2
movupd -0x10(%rbx,%rdi,1),%xmm3
movupd (%rbx,%rdi,1),%xmm4
mulpd  %xmm0,%xmm1
addpd  %xmm3,%xmm1
mulpd  %xmm0,%xmm2
addpd  %xmm4,%xmm2
movupd %xmm1,-0x10(%rbx,%rdi,1)
movupd %xmm2,(%rbx,%rdi,1)
add    $0x20,%rdi
cmp    %rdi,%r14
jne    1320 <dgemm_transpose+0x220>
mov    -0x28(%rsp),%rsi
mov    %rsi,%rbp
cmp    %r15,%rsi
je     11e0 <dgemm_transpose+0xe0>
jmp    120a <dgemm_transpose+0x10a>
xor    %ebp,%ebp
mov    -0x64(%rsp),%ecx
mov    %rsi,%r11
mov    -0x58(%rsp),%r8
jmp    120a <dgemm_transpose+0x10a>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @dgemm_transpose(double* %0, double* %1, double* %2, i32 %3) {
%5 = alloca double*
%6 = alloca double*
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store double* %0, double** %5
store double* %1, double** %6
store double* %2, double** %7
store i32 %3, i32* %8
store i32 0, i32* %9
br label %12
12: ; preds = %66, %4
%13 = load i32, i32* %9
%14 = load i32, i32* %8
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %69
16: ; preds = %12
store i32 0, i32* %11
br label %17
17: ; preds = %62, %16
%18 = load i32, i32* %11
%19 = load i32, i32* %8
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %65
21: ; preds = %17
store i32 0, i32* %10
br label %22
22: ; preds = %58, %21
%23 = load i32, i32* %10
%24 = load i32, i32* %8
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %61
26: ; preds = %22
%27 = load double*, double** %5
%28 = load i32, i32* %9
%29 = load i32, i32* %8
%30 = mul nsw i32 %28, %29
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds double, double* %27, i64 %31
%33 = load i32, i32* %11
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %32, i64 %34
%36 = load double, double* %35
%37 = load double*, double** %6
%38 = load i32, i32* %11
%39 = load i32, i32* %8
%40 = mul nsw i32 %38, %39
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double, double* %37, i64 %41
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds double, double* %42, i64 %44
%46 = load double, double* %45
%47 = load double*, double** %7
%48 = load i32, i32* %9
%49 = load i32, i32* %8
%50 = mul nsw i32 %48, %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds double, double* %47, i64 %51
%53 = load i32, i32* %10
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds double, double* %52, i64 %54
%56 = load double, double* %55
%57 = call double @llvm.fmuladd.f64(double %36, double %46, double %56)
store double %57, double* %55
br label %58
58: ; preds = %26
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
store i32 %60, i32* %10
br label %22
61: ; preds = %22
br label %62
62: ; preds = %61
%63 = load i32, i32* %11
%64 = add nsw i32 %63, 1
store i32 %64, i32* %11
br label %17
65: ; preds = %17
br label %66
66: ; preds = %65
%67 = load i32, i32* %9
%68 = add nsw i32 %67, 1
store i32 %68, i32* %9
br label %12
69: ; preds = %12
ret void
}

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