#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 int /*<<< orphan*/ Y;
typedef int /*<<< orphan*/ X;
typedef int /*<<< orphan*/ M;
typedef int /*<<< orphan*/ B;

/* Variables and functions */
int /*<<< orphan*/ dump(size_t, size_t, int **);
int /*<<< orphan*/ fill(size_t, size_t, int **);
int memcmp(int **, int **, int);
int /*<<< orphan*/ memcpy(int **, int **, int);
int /*<<< orphan*/ printf(char *, int, size_t, size_t);
int /*<<< orphan*/ row2col(size_t, size_t, int **, int **);
int /*<<< orphan*/ transpose(size_t, size_t, int **, int **);
void test_transpose(size_t r, size_t c, int n) {
  int M[r][c];
  int A[r][c], B[r][c];
  int X[r][c], Y[c][r];
  int i, m;
  if (r == 0 || c == 0)
    return;
  fill(r, c, M);
  memcpy(A, M, sizeof(M));
  memcpy(X, M, sizeof(M));
  for (i = 0; i < n; i++) {
    row2col(r, c, A, B);
    if (!(i & 1)) {
      transpose(r, c, X, Y);
      m = memcmp(B, Y, sizeof(Y));
    } else {
      transpose(c, r, Y, X);
      m = memcmp(B, X, sizeof(X));
    }
    memcpy(A, B, sizeof(B));
    if (m) {
      printf("#%d %zux%zu mismatch\n", i, r, c);
      dump(r, c, B);
      if (!(i & 1))
        dump(c, r, Y);
      else
        dump(r, c, X);
    }
  }
}


x86 assembly (clang, -O0):
<test_transpose>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rcx
mov    %rcx,-0xe8(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x20(%rbp)
mov    %rcx,%rdx
imul   %rax,%rdx
lea    0xf(,%rdx,4),%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0xd8(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x28(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,%rdx
imul   %rax,%rdx
lea    0xf(,%rdx,4),%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0xd0(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x38(%rbp)
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rcx
mov    %rcx,-0xc8(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    %rcx,%rdx
imul   %rax,%rdx
lea    0xf(,%rdx,4),%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0xb8(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x48(%rbp)
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rcx
mov    %rcx,-0xb0(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    %rcx,%rdx
imul   %rax,%rdx
lea    0xf(,%rdx,4),%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0xa0(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x58(%rbp)
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rcx
mov    %rcx,-0x98(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    %rcx,%rdx
imul   %rax,%rdx
lea    0xf(,%rdx,4),%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x88(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x68(%rbp)
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     12da <test_transpose+0x16a>
cmpq   $0x0,-0x10(%rbp)
jne    12e6 <test_transpose+0x176>
movl   $0x1,-0x7c(%rbp)
jmp    14df <test_transpose+0x36f>
mov    -0xd8(%rbp),%rdx
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <fill@plt>
mov    -0xd0(%rbp),%rdi
mov    -0xd8(%rbp),%rsi
mov    -0xe0(%rbp),%rcx
mov    -0xe8(%rbp),%rax
imul   %rcx,%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    -0xa0(%rbp),%rdi
mov    -0xd8(%rbp),%rsi
mov    -0xe0(%rbp),%rcx
mov    -0xe8(%rbp),%rax
imul   %rcx,%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
movl   $0x0,-0x74(%rbp)
mov    -0x74(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    14d8 <test_transpose+0x368>
mov    -0xb8(%rbp),%rcx
mov    -0xd0(%rbp),%rdx
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <row2col@plt>
mov    -0x74(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    13db <test_transpose+0x26b>
mov    -0x88(%rbp),%rcx
mov    -0xa0(%rbp),%rdx
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <transpose@plt>
mov    -0xb8(%rbp),%rdi
mov    -0x88(%rbp),%rsi
mov    -0x90(%rbp),%rcx
mov    -0x98(%rbp),%rax
imul   %rcx,%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1060 <memcmp@plt>
mov    %eax,-0x78(%rbp)
jmp    1424 <test_transpose+0x2b4>
mov    -0xa0(%rbp),%rcx
mov    -0x88(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <transpose@plt>
mov    -0xb8(%rbp),%rdi
mov    -0xa0(%rbp),%rsi
mov    -0xa8(%rbp),%rcx
mov    -0xb0(%rbp),%rax
imul   %rcx,%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1060 <memcmp@plt>
mov    %eax,-0x78(%rbp)
mov    -0xc0(%rbp),%rcx
mov    -0xc8(%rbp),%rax
mov    -0xb8(%rbp),%rsi
mov    -0xd0(%rbp),%rdi
imul   %rcx,%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
cmpl   $0x0,-0x78(%rbp)
je     14c5 <test_transpose+0x355>
mov    -0x74(%rbp),%esi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xb95(%rip),%rdi
call   1040 <printf@plt>
mov    -0xb8(%rbp),%rdx
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <dump@plt>
mov    -0x74(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    14ac <test_transpose+0x33c>
mov    -0x88(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <dump@plt>
jmp    14c0 <test_transpose+0x350>
mov    -0xa0(%rbp),%rdx
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <dump@plt>
jmp    14c5 <test_transpose+0x355>
jmp    14ca <test_transpose+0x35a>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    1357 <test_transpose+0x1e7>
movl   $0x0,-0x7c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_transpose>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,-0x2c(%rbp)
mov    %rsi,%rax
imul   %rdi,%rax
mov    %rax,%r12
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
mov    %rsp,%rcx
sub    %rax,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
mov    %rsp,%rcx
sub    %rax,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rsp,%rcx
sub    %rax,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
test   %rdi,%rdi
je     1317 <test_transpose+0x1a7>
mov    %rsi,%r15
test   %rsi,%rsi
je     1317 <test_transpose+0x1a7>
mov    %rdi,%rbx
mov    %r15,%rsi
mov    %r13,%rdx
call   1070 <fill@plt>
shl    $0x2,%r12d
mov    -0x48(%rbp),%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1090 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    %r13,%rsi
mov    %r12,-0x60(%rbp)
mov    %r12d,%edx
call   1090 <memcpy@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    1317 <test_transpose+0x1a7>
xor    %r13d,%r13d
mov    %r15,-0x58(%rbp)
mov    %rbx,-0x50(%rbp)
jmp    125d <test_transpose+0xed>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    -0x38(%rbp),%rdx
call   1050 <dump@plt>
add    $0x1,%r13d
cmp    %r13d,-0x2c(%rbp)
je     1317 <test_transpose+0x1a7>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    -0x48(%rbp),%rdx
mov    %r14,%rcx
call   1030 <row2col@plt>
test   $0x1,%r13b
jne    1290 <test_transpose+0x120>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rbx
jmp    129e <test_transpose+0x12e>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %rbx,%rsi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rbx
mov    %rbx,%rcx
call   1080 <transpose@plt>
mov    %rbx,%rsi
mov    %r14,%rdi
mov    -0x60(%rbp),%r15
mov    %r15d,%edx
call   1060 <memcmp@plt>
mov    %eax,%r12d
mov    -0x48(%rbp),%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1090 <memcpy@plt>
test   %r12d,%r12d
mov    -0x58(%rbp),%r15
mov    -0x50(%rbp),%rbx
je     124f <test_transpose+0xdf>
lea    0xd1e(%rip),%rdi
mov    %r13d,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <dump@plt>
test   $0x1,%r13b
jne    1240 <test_transpose+0xd0>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    -0x40(%rbp),%rdx
jmp    124a <test_transpose+0xda>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"#%d %zux%zu mismatch\0A\00"
define void @test_transpose(i64 %0, i64 %1, i32 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
store i64 %0, i64* %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%21 = load i64, i64* %4
%22 = load i64, i64* %5
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %7
%24 = mul nuw i64 %21, %22
%25 = alloca i32, i64 %24
store i64 %21, i64* %8
store i64 %22, i64* %9
%26 = load i64, i64* %4
%27 = load i64, i64* %5
%28 = mul nuw i64 %26, %27
%29 = alloca i32, i64 %28
store i64 %26, i64* %10
store i64 %27, i64* %11
%30 = load i64, i64* %4
%31 = load i64, i64* %5
%32 = mul nuw i64 %30, %31
%33 = alloca i32, i64 %32
store i64 %30, i64* %12
store i64 %31, i64* %13
%34 = load i64, i64* %4
%35 = load i64, i64* %5
%36 = mul nuw i64 %34, %35
%37 = alloca i32, i64 %36
store i64 %34, i64* %14
store i64 %35, i64* %15
%38 = load i64, i64* %5
%39 = load i64, i64* %4
%40 = mul nuw i64 %38, %39
%41 = alloca i32, i64 %40
store i64 %38, i64* %16
store i64 %39, i64* %17
%42 = load i64, i64* %4
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %3
%45 = load i64, i64* %5
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %44, %3
store i32 1, i32* %20
br label %139
48: ; preds = %44
%49 = load i64, i64* %4
%50 = load i64, i64* %5
%51 = bitcast i32* %25 to i32**
%52 = call i32 @fill(i64 %49, i64 %50, i32** %51)
%53 = bitcast i32* %29 to i32**
%54 = bitcast i32* %25 to i32**
%55 = mul nuw i64 %21, %22
%56 = mul nuw i64 4, %55
%57 = trunc i64 %56 to i32
%58 = call i32 @memcpy(i32** %53, i32** %54, i32 %57)
%59 = bitcast i32* %37 to i32**
%60 = bitcast i32* %25 to i32**
%61 = mul nuw i64 %21, %22
%62 = mul nuw i64 4, %61
%63 = trunc i64 %62 to i32
%64 = call i32 @memcpy(i32** %59, i32** %60, i32 %63)
store i32 0, i32* %18
br label %65
65: ; preds = %135, %48
%66 = load i32, i32* %18
%67 = load i32, i32* %6
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %138
69: ; preds = %65
%70 = load i64, i64* %4
%71 = load i64, i64* %5
%72 = bitcast i32* %29 to i32**
%73 = bitcast i32* %33 to i32**
%74 = call i32 @row2col(i64 %70, i64 %71, i32** %72, i32** %73)
%75 = load i32, i32* %18
%76 = and i32 %75, 1
%77 = icmp ne i32 %76, 0
br i1 %77, label %90, label %78
78: ; preds = %69
%79 = load i64, i64* %4
%80 = load i64, i64* %5
%81 = bitcast i32* %37 to i32**
%82 = bitcast i32* %41 to i32**
%83 = call i32 @transpose(i64 %79, i64 %80, i32** %81, i32** %82)
%84 = bitcast i32* %33 to i32**
%85 = bitcast i32* %41 to i32**
%86 = mul nuw i64 %38, %39
%87 = mul nuw i64 4, %86
%88 = trunc i64 %87 to i32
%89 = call i32 @memcmp(i32** %84, i32** %85, i32 %88)
store i32 %89, i32* %19
br label %102
90: ; preds = %69
%91 = load i64, i64* %5
%92 = load i64, i64* %4
%93 = bitcast i32* %41 to i32**
%94 = bitcast i32* %37 to i32**
%95 = call i32 @transpose(i64 %91, i64 %92, i32** %93, i32** %94)
%96 = bitcast i32* %33 to i32**
%97 = bitcast i32* %37 to i32**
%98 = mul nuw i64 %34, %35
%99 = mul nuw i64 4, %98
%100 = trunc i64 %99 to i32
%101 = call i32 @memcmp(i32** %96, i32** %97, i32 %100)
store i32 %101, i32* %19
br label %102
102: ; preds = %90, %78
%103 = bitcast i32* %29 to i32**
%104 = bitcast i32* %33 to i32**
%105 = mul nuw i64 %30, %31
%106 = mul nuw i64 4, %105
%107 = trunc i64 %106 to i32
%108 = call i32 @memcpy(i32** %103, i32** %104, i32 %107)
%109 = load i32, i32* %19
%110 = icmp ne i32 %109, 0
br i1 %110, label %111, label %134
111: ; preds = %102
%112 = load i32, i32* %18
%113 = load i64, i64* %4
%114 = load i64, i64* %5
%115 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %112, i64 %113, i64 %114)
%116 = load i64, i64* %4
%117 = load i64, i64* %5
%118 = bitcast i32* %33 to i32**
%119 = call i32 @dump(i64 %116, i64 %117, i32** %118)
%120 = load i32, i32* %18
%121 = and i32 %120, 1
%122 = icmp ne i32 %121, 0
br i1 %122, label %128, label %123
123: ; preds = %111
%124 = load i64, i64* %5
%125 = load i64, i64* %4
%126 = bitcast i32* %41 to i32**
%127 = call i32 @dump(i64 %124, i64 %125, i32** %126)
br label %133
128: ; preds = %111
%129 = load i64, i64* %4
%130 = load i64, i64* %5
%131 = bitcast i32* %37 to i32**
%132 = call i32 @dump(i64 %129, i64 %130, i32** %131)
br label %133
133: ; preds = %128, %123
br label %134
134: ; preds = %133, %102
br label %135
135: ; preds = %134
%136 = load i32, i32* %18
%137 = add nsw i32 %136, 1
store i32 %137, i32* %18
br label %65
138: ; preds = %65
store i32 0, i32* %20
br label %139
139: ; preds = %138, %47
%140 = load i8*, i8** %7
%141 = load i32, i32* %20
switch i32 %141, label %143 [
i32 0, label %142
i32 1, label %142
]
142: ; preds = %139, %139
ret void
143: ; preds = %139
unreachable
}

LLVM IR (-O2):
@.str = [22 x i8] c"#%d %zux%zu mismatch\0A\00"
define void @test_transpose(i64 %0, i64 %1, i32 %2) {
%4 = mul nuw i64 %1, %0
%5 = alloca i32, i64 %4
%6 = alloca i32, i64 %4
%7 = alloca i32, i64 %4
%8 = alloca i32, i64 %4
%9 = alloca i32, i64 %4
%10 = icmp eq i64 %0, 0
%11 = icmp eq i64 %1, 0
%12 = or i1 %10, %11
br i1 %12, label %50, label %13
13: ; preds = %3
%14 = bitcast i32* %5 to i32**
%15 = call i32 @fill(i64 %0, i64 %1, i32** %14)
%16 = bitcast i32* %6 to i32**
%17 = trunc i64 %4 to i32
%18 = shl i32 %17, 2
%19 = call i32 @memcpy(i32** %16, i32** %14, i32 %18)
%20 = bitcast i32* %8 to i32**
%21 = call i32 @memcpy(i32** %20, i32** %14, i32 %18)
%22 = icmp sgt i32 %2, 0
br i1 %22, label %23, label %50
23: ; preds = %13
%24 = bitcast i32* %7 to i32**
%25 = bitcast i32* %9 to i32**
br label %26
26: ; preds = %23, %47
%27 = phi i32 [ 0, %23 ], [ %48, %47 ]
%28 = call i32 @row2col(i64 %0, i64 %1, i32** %16, i32** %24)
%29 = and i32 %27, 1
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @transpose(i64 %0, i64 %1, i32** %20, i32** %25)
br label %35
33: ; preds = %26
%34 = call i32 @transpose(i64 %1, i64 %0, i32** %25, i32** %20)
br label %35
35: ; preds = %33, %31
%36 = phi i32** [ %20, %33 ], [ %25, %31 ]
%37 = call i32 @memcmp(i32** %24, i32** %36, i32 %18)
%38 = call i32 @memcpy(i32** %16, i32** %24, i32 %18)
%39 = icmp eq i32 %37, 0
br i1 %39, label %47, label %40
40: ; preds = %35
%41 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %27, i64 %0, i64 %1)
%42 = call i32 @dump(i64 %0, i64 %1, i32** %24)
br i1 %30, label %43, label %45
43: ; preds = %40
%44 = call i32 @dump(i64 %1, i64 %0, i32** %25)
br label %47
45: ; preds = %40
%46 = call i32 @dump(i64 %0, i64 %1, i32** %20)
br label %47
47: ; preds = %35, %45, %43
%48 = add nuw nsw i32 %27, 1
%49 = icmp eq i32 %48, %2
br i1 %49, label %50, label %26
50: ; preds = %47, %13, %3
ret void
}