void transpose(int rows, int cols, double m[rows][cols],
               double m_trans[cols][rows]) {
  int i, j;
  for (i = 0; i < rows; i++)
    for (j = 0; j < cols; j++)
      m_trans[j][i] = m[i][j];
}


x86 assembly (clang, -O0):
<transpose>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%eax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11a7 <transpose+0xa7>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1194 <transpose+0x94>
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
imul   %rsi,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
movslq -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
imul   %rdx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
movslq -0x1c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    113a <transpose+0x3a>
jmp    1199 <transpose+0x99>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1127 <transpose+0x27>
pop    %rbp
ret

x86 assembly (clang, -O2):
<transpose>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,-0x48(%rsp)
mov    %esi,-0x4c(%rsp)
test   %edi,%edi
jle    13e4 <transpose+0x2e4>
mov    %rcx,%rsi
mov    -0x4c(%rsp),%eax
mov    %eax,%r15d
mov    %edi,%ecx
mov    %r15d,%edx
and    $0xfffffffc,%edx
mov    %rdx,-0x18(%rsp)
add    $0xfffffffffffffffc,%rdx
mov    %rdx,-0x20(%rsp)
shr    $0x2,%rdx
add    $0x1,%rdx
cmp    $0x4,%eax
setae  %al
cmp    $0x1,%edi
mov    %rcx,%rdi
sete   %cl
and    %al,%cl
mov    %cl,-0x4d(%rsp)
mov    %edx,%eax
and    $0x3,%eax
and    $0xfffffffffffffffc,%rdx
mov    %rdx,-0x38(%rsp)
mov    %r15d,%ecx
and    $0x3,%ecx
mov    %rcx,-0x10(%rsp)
mov    -0x48(%rsp),%rdx
lea    0x70(%rdx),%r14
lea    0x0(,%r15,8),%rcx
mov    %rcx,-0x8(%rsp)
mov    %rdi,%rcx
shl    $0x7,%rcx
mov    %rcx,-0x40(%rsp)
mov    %rdi,%rbp
shl    $0x5,%rbp
mov    %rax,-0x28(%rsp)
shl    $0x5,%rax
mov    %rax,-0x30(%rsp)
lea    0x0(,%rdi,8),%r8
xor    %r11d,%r11d
mov    %rsi,%r13
jmp    11dc <transpose+0xdc>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r11
mov    -0x8(%rsp),%rax
add    %rax,%r14
add    $0x8,%r13
add    %rax,%rdx
cmp    %rdi,%r11
je     13e4 <transpose+0x2e4>
cmpl   $0x0,-0x4c(%rsp)
jle    11c0 <transpose+0xc0>
cmpb   $0x0,-0x4d(%rsp)
je     1230 <transpose+0x130>
mov    %rdi,%r12
mov    %r11,%rax
imul   %r15,%rax
lea    (%rax,%r15,1),%rcx
lea    (%rsi,%r11,8),%rbx
mov    -0x48(%rsp),%rdi
lea    (%rdi,%rcx,8),%rcx
cmp    %rcx,%rbx
jae    1238 <transpose+0x138>
lea    (%r11,%r15,1),%rcx
lea    (%rsi,%rcx,8),%rcx
lea    (%rdi,%rax,8),%rax
cmp    %rcx,%rax
jae    1238 <transpose+0x138>
xor    %r9d,%r9d
mov    %r12,%rdi
jmp    1320 <transpose+0x220>
cs nopw 0x0(%rax,%rax,1)
xor    %r9d,%r9d
jmp    1320 <transpose+0x220>
mov    %rsi,%r10
cmpq   $0xc,-0x20(%rsp)
jae    124a <transpose+0x14a>
xor    %ebx,%ebx
jmp    12c8 <transpose+0x1c8>
mov    -0x38(%rsp),%rcx
mov    %r13,%rax
xor    %ebx,%ebx
mov    -0x40(%rsp),%r9
nopl   0x0(%rax)
movups -0x70(%r14,%rbx,8),%xmm0
movups -0x60(%r14,%rbx,8),%xmm1
movups %xmm0,(%rax)
movups %xmm1,0x10(%rax)
movups -0x50(%r14,%rbx,8),%xmm0
movups -0x40(%r14,%rbx,8),%xmm1
lea    (%rax,%rbp,1),%rsi
movups %xmm0,(%rax,%rbp,1)
movups %xmm1,0x10(%rax,%rbp,1)
movups -0x30(%r14,%rbx,8),%xmm0
movups -0x20(%r14,%rbx,8),%xmm1
lea    (%rsi,%rbp,1),%rdi
movups %xmm0,0x0(%rbp,%rsi,1)
movups %xmm1,0x10(%rbp,%rsi,1)
movups -0x10(%r14,%rbx,8),%xmm0
movups (%r14,%rbx,8),%xmm1
movups %xmm0,0x0(%rbp,%rdi,1)
movups %xmm1,0x10(%rbp,%rdi,1)
add    $0x10,%rbx
add    %r9,%rax
add    $0xfffffffffffffffc,%rcx
jne    1260 <transpose+0x160>
cmpq   $0x0,-0x28(%rsp)
mov    %r12,%rdi
mov    -0x30(%rsp),%rsi
je     130c <transpose+0x20c>
mov    %rdi,%rax
imul   %rbx,%rax
lea    0x0(,%rax,8),%rax
add    %r13,%rax
lea    (%rdx,%rbx,8),%rcx
xor    %ebx,%ebx
movups (%rcx,%rbx,1),%xmm0
movups 0x10(%rcx,%rbx,1),%xmm1
movups %xmm0,(%rax)
movups %xmm1,0x10(%rax)
add    $0x20,%rbx
add    %rbp,%rax
cmp    %rbx,%rsi
jne    12f0 <transpose+0x1f0>
mov    -0x18(%rsp),%rax
mov    %rax,%r9
cmp    %r15,%rax
mov    %r10,%rsi
je     11c0 <transpose+0xc0>
mov    %r9,%rcx
not    %rcx
add    %r15,%rcx
mov    -0x10(%rsp),%rbx
test   %rbx,%rbx
je     1367 <transpose+0x267>
mov    %rdi,%rax
imul   %r9,%rax
lea    0x0(,%rax,8),%rax
add    %r13,%rax
cs nopw 0x0(%rax,%rax,1)
nop
movsd  (%rdx,%r9,8),%xmm0
movsd  %xmm0,(%rax)
add    $0x1,%r9
add    %r8,%rax
add    $0xffffffffffffffff,%rbx
jne    1350 <transpose+0x250>
cmp    $0x3,%rcx
jb     11c0 <transpose+0xc0>
lea    0x3(%r9),%rax
imul   %r8,%rax
lea    0x1(%r9),%rcx
imul   %r8,%rcx
lea    0x2(%r9),%rbx
imul   %r8,%rbx
mov    %r8,%r10
imul   %r9,%r10
mov    %r13,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%rdx,%r9,8),%xmm0
movsd  %xmm0,(%r12,%r10,1)
movsd  0x8(%rdx,%r9,8),%xmm0
movsd  %xmm0,(%r12,%rcx,1)
movsd  0x10(%rdx,%r9,8),%xmm0
movsd  %xmm0,(%r12,%rbx,1)
movsd  0x18(%rdx,%r9,8),%xmm0
movsd  %xmm0,(%r12,%rax,1)
add    $0x4,%r9
add    %rbp,%r12
cmp    %r9,%r15
jne    13a0 <transpose+0x2a0>
jmp    11c0 <transpose+0xc0>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @transpose(i32 %0, i32 %1, double* %2, double* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca double*
%8 = alloca double*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store double* %2, double** %7
store double* %3, double** %8
%11 = load i32, i32* %5
%12 = zext i32 %11 to i64
%13 = load i32, i32* %6
%14 = zext i32 %13 to i64
%15 = load i32, i32* %6
%16 = zext i32 %15 to i64
%17 = load i32, i32* %5
%18 = zext i32 %17 to i64
store i32 0, i32* %9
br label %19
19: ; preds = %50, %4
%20 = load i32, i32* %9
%21 = load i32, i32* %5
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %53
23: ; preds = %19
store i32 0, i32* %10
br label %24
24: ; preds = %46, %23
%25 = load i32, i32* %10
%26 = load i32, i32* %6
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %49
28: ; preds = %24
%29 = load double*, double** %7
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = mul nsw i64 %31, %14
%33 = getelementptr inbounds double, double* %29, i64 %32
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds double, double* %33, i64 %35
%37 = load double, double* %36
%38 = load double*, double** %8
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = mul nsw i64 %40, %18
%42 = getelementptr inbounds double, double* %38, i64 %41
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds double, double* %42, i64 %44
store double %37, double* %45
br label %46
46: ; preds = %28
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
store i32 %48, i32* %10
br label %24
49: ; preds = %24
br label %50
50: ; preds = %49
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
br label %19
53: ; preds = %19
ret void
}

LLVM IR (-O2):
define void @transpose(i32 %0, i32 %1, double* nocapture %2, double* nocapture writeonly %3) {
%5 = zext i32 %1 to i64
%6 = zext i32 %0 to i64
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %159
8: ; preds = %4
%9 = icmp sgt i32 %1, 0
%10 = and i64 %5, 4294967292
%11 = add nsw i64 %10, -4
%12 = lshr exact i64 %11, 2
%13 = add nuw nsw i64 %12, 1
%14 = icmp ugt i32 %1, 3
%15 = icmp eq i32 %0, 1
%16 = and i1 %14, %15
%17 = and i64 %5, 4294967292
%18 = and i64 %13, 3
%19 = icmp ult i64 %11, 12
%20 = and i64 %13, 9223372036854775804
%21 = icmp eq i64 %18, 0
%22 = icmp eq i64 %17, %5
%23 = and i64 %5, 3
%24 = icmp eq i64 %23, 0
br label %25
25: ; preds = %8, %156
%26 = phi i64 [ 0, %8 ], [ %157, %156 ]
%27 = getelementptr double, double* %3, i64 %26
%28 = add nuw i64 %26, %5
%29 = getelementptr double, double* %3, i64 %28
%30 = mul i64 %26, %5
%31 = getelementptr double, double* %2, i64 %30
%32 = add i64 %30, %5
%33 = getelementptr double, double* %2, i64 %32
br i1 %9, label %34, label %156
34: ; preds = %25
%35 = mul nuw nsw i64 %26, %5
%36 = getelementptr inbounds double, double* %2, i64 %35
%37 = getelementptr inbounds double, double* %3, i64 %26
br i1 %16, label %38, label %116
38: ; preds = %34
%39 = icmp ult double* %27, %33
%40 = icmp ult double* %31, %29
%41 = and i1 %39, %40
br i1 %41, label %116, label %42
42: ; preds = %38
br i1 %19, label %96, label %43
43: ; preds = %42, %43
%44 = phi i64 [ %93, %43 ], [ 0, %42 ]
%45 = phi i64 [ %94, %43 ], [ 0, %42 ]
%46 = getelementptr inbounds double, double* %36, i64 %44
%47 = bitcast double* %46 to <2 x double>*
%48 = load <2 x double>, <2 x double>* %47
%49 = getelementptr inbounds double, double* %46, i64 2
%50 = bitcast double* %49 to <2 x double>*
%51 = load <2 x double>, <2 x double>* %50
%52 = mul nuw nsw i64 %44, %6
%53 = getelementptr inbounds double, double* %37, i64 %52
%54 = bitcast double* %53 to <2 x double>*
store <2 x double> %48, <2 x double>* %54
%55 = getelementptr inbounds double, double* %53, i64 2
%56 = bitcast double* %55 to <2 x double>*
store <2 x double> %51, <2 x double>* %56
%57 = or i64 %44, 4
%58 = getelementptr inbounds double, double* %36, i64 %57
%59 = bitcast double* %58 to <2 x double>*
%60 = load <2 x double>, <2 x double>* %59
%61 = getelementptr inbounds double, double* %58, i64 2
%62 = bitcast double* %61 to <2 x double>*
%63 = load <2 x double>, <2 x double>* %62
%64 = mul nuw nsw i64 %57, %6
%65 = getelementptr inbounds double, double* %37, i64 %64
%66 = bitcast double* %65 to <2 x double>*
store <2 x double> %60, <2 x double>* %66
%67 = getelementptr inbounds double, double* %65, i64 2
%68 = bitcast double* %67 to <2 x double>*
store <2 x double> %63, <2 x double>* %68
%69 = or i64 %44, 8
%70 = getelementptr inbounds double, double* %36, i64 %69
%71 = bitcast double* %70 to <2 x double>*
%72 = load <2 x double>, <2 x double>* %71
%73 = getelementptr inbounds double, double* %70, i64 2
%74 = bitcast double* %73 to <2 x double>*
%75 = load <2 x double>, <2 x double>* %74
%76 = mul nuw nsw i64 %69, %6
%77 = getelementptr inbounds double, double* %37, i64 %76
%78 = bitcast double* %77 to <2 x double>*
store <2 x double> %72, <2 x double>* %78
%79 = getelementptr inbounds double, double* %77, i64 2
%80 = bitcast double* %79 to <2 x double>*
store <2 x double> %75, <2 x double>* %80
%81 = or i64 %44, 12
%82 = getelementptr inbounds double, double* %36, i64 %81
%83 = bitcast double* %82 to <2 x double>*
%84 = load <2 x double>, <2 x double>* %83
%85 = getelementptr inbounds double, double* %82, i64 2
%86 = bitcast double* %85 to <2 x double>*
%87 = load <2 x double>, <2 x double>* %86
%88 = mul nuw nsw i64 %81, %6
%89 = getelementptr inbounds double, double* %37, i64 %88
%90 = bitcast double* %89 to <2 x double>*
store <2 x double> %84, <2 x double>* %90
%91 = getelementptr inbounds double, double* %89, i64 2
%92 = bitcast double* %91 to <2 x double>*
store <2 x double> %87, <2 x double>* %92
%93 = add nuw i64 %44, 16
%94 = add nuw i64 %45, 4
%95 = icmp eq i64 %94, %20
br i1 %95, label %96, label %43
96: ; preds = %43, %42
%97 = phi i64 [ 0, %42 ], [ %93, %43 ]
br i1 %21, label %115, label %98
98: ; preds = %96, %98
%99 = phi i64 [ %112, %98 ], [ %97, %96 ]
%100 = phi i64 [ %113, %98 ], [ 0, %96 ]
%101 = getelementptr inbounds double, double* %36, i64 %99
%102 = bitcast double* %101 to <2 x double>*
%103 = load <2 x double>, <2 x double>* %102
%104 = getelementptr inbounds double, double* %101, i64 2
%105 = bitcast double* %104 to <2 x double>*
%106 = load <2 x double>, <2 x double>* %105
%107 = mul nuw nsw i64 %99, %6
%108 = getelementptr inbounds double, double* %37, i64 %107
%109 = bitcast double* %108 to <2 x double>*
store <2 x double> %103, <2 x double>* %109
%110 = getelementptr inbounds double, double* %108, i64 2
%111 = bitcast double* %110 to <2 x double>*
store <2 x double> %106, <2 x double>* %111
%112 = add nuw i64 %99, 4
%113 = add i64 %100, 1
%114 = icmp eq i64 %113, %18
br i1 %114, label %115, label %98
115: ; preds = %98, %96
br i1 %22, label %156, label %116
116: ; preds = %38, %34, %115
%117 = phi i64 [ 0, %38 ], [ 0, %34 ], [ %17, %115 ]
%118 = xor i64 %117, -1
%119 = add nsw i64 %118, %5
br i1 %24, label %130, label %120
120: ; preds = %116, %120
%121 = phi i64 [ %127, %120 ], [ %117, %116 ]
%122 = phi i64 [ %128, %120 ], [ 0, %116 ]
%123 = getelementptr inbounds double, double* %36, i64 %121
%124 = load double, double* %123
%125 = mul nuw nsw i64 %121, %6
%126 = getelementptr inbounds double, double* %37, i64 %125
store double %124, double* %126
%127 = add nuw nsw i64 %121, 1
%128 = add i64 %122, 1
%129 = icmp eq i64 %128, %23
br i1 %129, label %130, label %120
130: ; preds = %120, %116
%131 = phi i64 [ %117, %116 ], [ %127, %120 ]
%132 = icmp ult i64 %119, 3
br i1 %132, label %156, label %133
133: ; preds = %130, %133
%134 = phi i64 [ %154, %133 ], [ %131, %130 ]
%135 = getelementptr inbounds double, double* %36, i64 %134
%136 = load double, double* %135
%137 = mul nuw nsw i64 %134, %6
%138 = getelementptr inbounds double, double* %37, i64 %137
store double %136, double* %138
%139 = add nuw nsw i64 %134, 1
%140 = getelementptr inbounds double, double* %36, i64 %139
%141 = load double, double* %140
%142 = mul nuw nsw i64 %139, %6
%143 = getelementptr inbounds double, double* %37, i64 %142
store double %141, double* %143
%144 = add nuw nsw i64 %134, 2
%145 = getelementptr inbounds double, double* %36, i64 %144
%146 = load double, double* %145
%147 = mul nuw nsw i64 %144, %6
%148 = getelementptr inbounds double, double* %37, i64 %147
store double %146, double* %148
%149 = add nuw nsw i64 %134, 3
%150 = getelementptr inbounds double, double* %36, i64 %149
%151 = load double, double* %150
%152 = mul nuw nsw i64 %149, %6
%153 = getelementptr inbounds double, double* %37, i64 %152
store double %151, double* %153
%154 = add nuw nsw i64 %134, 4
%155 = icmp eq i64 %154, %5
br i1 %155, label %156, label %133
156: ; preds = %130, %133, %115, %25
%157 = add nuw nsw i64 %26, 1
%158 = icmp eq i64 %157, %6
br i1 %158, label %159, label %25
159: ; preds = %156, %4
ret void
}