void m_transpose(double *A, double *B, int nrow, int ncol) {
  int i, j;
  double *p, *q, *r;
  p = A;
  r = B;
  for (i = 0; i < nrow; i++) {
    q = r;
    for (j = 0; j < ncol; j++, q += nrow)
      *q = *p++;
    r++;
  }
}


x86 assembly (clang, -O0):
<m_transpose>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a8 <m_transpose+0xa8>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    118e <m_transpose+0x8e>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x28(%rbp)
movsd  (%rax),%xmm0
mov    -0x30(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    1144 <m_transpose+0x44>
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1129 <m_transpose+0x29>
pop    %rbp
ret

x86 assembly (clang, -O2):
<m_transpose>:
push   %rbp
push   %r15
push   %r14
push   %rbx
test   %edx,%edx
jle    122a <m_transpose+0x12a>
mov    %edx,%eax
lea    -0x1(%rcx),%r8d
mov    %ecx,%r9d
and    $0x7,%r9d
mov    %ecx,%r10d
and    $0xfffffff8,%r10d
mov    %rax,%r11
shl    $0x6,%r11
shl    $0x3,%rax
xor    %r14d,%r14d
jmp    1151 <m_transpose+0x51>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x8,%rsi
add    $0x1,%r14d
cmp    %edx,%r14d
je     122a <m_transpose+0x12a>
test   %ecx,%ecx
jle    1140 <m_transpose+0x40>
cmp    $0x7,%r8d
jae    1170 <m_transpose+0x70>
mov    %rsi,%rbp
test   %r9d,%r9d
jne    1200 <m_transpose+0x100>
jmp    1140 <m_transpose+0x40>
nopl   0x0(%rax)
mov    %r10d,%r15d
mov    %rsi,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rbp
movsd  (%rdi),%xmm0
movsd  %xmm0,(%rbx)
movsd  0x8(%rdi),%xmm0
add    %rax,%rbx
movsd  %xmm0,0x0(%rbp,%rax,1)
movsd  0x10(%rdi),%xmm0
movsd  %xmm0,(%rax,%rbx,1)
add    %rax,%rbx
movsd  0x18(%rdi),%xmm0
movsd  %xmm0,(%rax,%rbx,1)
add    %rax,%rbx
movsd  0x20(%rdi),%xmm0
movsd  %xmm0,(%rax,%rbx,1)
add    %rax,%rbx
movsd  0x28(%rdi),%xmm0
movsd  %xmm0,(%rax,%rbx,1)
add    %rax,%rbx
movsd  0x30(%rdi),%xmm0
movsd  %xmm0,(%rax,%rbx,1)
add    %rax,%rbx
movsd  0x38(%rdi),%xmm0
add    $0x40,%rdi
movsd  %xmm0,(%rax,%rbx,1)
add    %rax,%rbx
add    %rax,%rbx
add    $0xfffffff8,%r15d
jne    1180 <m_transpose+0x80>
add    %r11,%rbp
test   %r9d,%r9d
je     1140 <m_transpose+0x40>
mov    %r9d,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%rdi),%xmm0
add    $0x8,%rdi
movsd  %xmm0,0x0(%rbp)
add    %rax,%rbp
add    $0xffffffff,%ebx
jne    1210 <m_transpose+0x110>
jmp    1140 <m_transpose+0x40>
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @m_transpose(double* nocapture %0, double* nocapture writeonly %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %67
6: ; preds = %4
%7 = icmp sgt i32 %3, 0
%8 = zext i32 %2 to i64
%9 = add i32 %3, -1
%10 = and i32 %3, 7
%11 = icmp ult i32 %9, 7
%12 = and i32 %3, -8
%13 = icmp eq i32 %10, 0
br label %14
14: ; preds = %6, %62
%15 = phi double* [ %1, %6 ], [ %64, %62 ]
%16 = phi double* [ %0, %6 ], [ %63, %62 ]
%17 = phi i32 [ 0, %6 ], [ %65, %62 ]
br i1 %7, label %18, label %62
18: ; preds = %14
br i1 %11, label %49, label %19
19: ; preds = %18, %19
%20 = phi double* [ %46, %19 ], [ %15, %18 ]
%21 = phi double* [ %44, %19 ], [ %16, %18 ]
%22 = phi i32 [ %47, %19 ], [ 0, %18 ]
%23 = getelementptr inbounds double, double* %21, i64 1
%24 = load double, double* %21
store double %24, double* %20
%25 = getelementptr inbounds double, double* %20, i64 %8
%26 = getelementptr inbounds double, double* %21, i64 2
%27 = load double, double* %23
store double %27, double* %25
%28 = getelementptr inbounds double, double* %25, i64 %8
%29 = getelementptr inbounds double, double* %21, i64 3
%30 = load double, double* %26
store double %30, double* %28
%31 = getelementptr inbounds double, double* %28, i64 %8
%32 = getelementptr inbounds double, double* %21, i64 4
%33 = load double, double* %29
store double %33, double* %31
%34 = getelementptr inbounds double, double* %31, i64 %8
%35 = getelementptr inbounds double, double* %21, i64 5
%36 = load double, double* %32
store double %36, double* %34
%37 = getelementptr inbounds double, double* %34, i64 %8
%38 = getelementptr inbounds double, double* %21, i64 6
%39 = load double, double* %35
store double %39, double* %37
%40 = getelementptr inbounds double, double* %37, i64 %8
%41 = getelementptr inbounds double, double* %21, i64 7
%42 = load double, double* %38
store double %42, double* %40
%43 = getelementptr inbounds double, double* %40, i64 %8
%44 = getelementptr inbounds double, double* %21, i64 8
%45 = load double, double* %41
store double %45, double* %43
%46 = getelementptr inbounds double, double* %43, i64 %8
%47 = add i32 %22, 8
%48 = icmp eq i32 %47, %12
br i1 %48, label %49, label %19
49: ; preds = %19, %18
%50 = phi double* [ undef, %18 ], [ %44, %19 ]
%51 = phi double* [ %15, %18 ], [ %46, %19 ]
%52 = phi double* [ %16, %18 ], [ %44, %19 ]
br i1 %13, label %62, label %53
53: ; preds = %49, %53
%54 = phi double* [ %59, %53 ], [ %51, %49 ]
%55 = phi double* [ %57, %53 ], [ %52, %49 ]
%56 = phi i32 [ %60, %53 ], [ 0, %49 ]
%57 = getelementptr inbounds double, double* %55, i64 1
%58 = load double, double* %55
store double %58, double* %54
%59 = getelementptr inbounds double, double* %54, i64 %8
%60 = add i32 %56, 1
%61 = icmp eq i32 %60, %10
br i1 %61, label %62, label %53
62: ; preds = %49, %53, %14
%63 = phi double* [ %16, %14 ], [ %50, %49 ], [ %57, %53 ]
%64 = getelementptr inbounds double, double* %15, i64 1
%65 = add nuw nsw i32 %17, 1
%66 = icmp eq i32 %65, %2
br i1 %66, label %67, label %14
67: ; preds = %62, %4
ret void
}