#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*/ T;
typedef int /*<<< orphan*/ N;
typedef int /*<<< orphan*/ M;

/* Variables and functions */
int /*<<< orphan*/ fill(size_t, size_t, int **);
int /*<<< orphan*/ memcmp(int **, int **, int);
int /*<<< orphan*/ memcpy(int **, int **, int);
int /*<<< orphan*/ printf(char *, size_t, size_t, size_t);
int /*<<< orphan*/ row2col(size_t, size_t, int **, int **);
void test_cycle(size_t r, size_t c) {
  int M[r][c], N[r][c], T[r][c];
  size_t i;
  fill(r, c, M);
  memcpy(N, M, sizeof(M));
  for (i = 1;; i++) {
    row2col(r, c, N, T);
    memcpy(N, T, sizeof(T));
    if (!memcmp(M, N, sizeof(N)))
      break;
  }
  printf("%zux%zu took %zu cycles\n", r, c, i);
}


x86 assembly (clang, -O0):
<test_cycle>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rcx
mov    %rcx,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
mov    %rcx,%rdx
imul   %rax,%rdx
lea    0xf(,%rdx,4),%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x68(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x20(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rcx
mov    %rcx,-0x98(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    %rcx,%rsi
imul   %rax,%rsi
lea    0xf(,%rsi,4),%rdi
and    $0xfffffffffffffff0,%rdi
mov    %rsp,%rsi
sub    %rdi,%rsi
mov    %rsi,-0x70(%rbp)
mov    %rsi,%rsp
mov    %rcx,-0x30(%rbp)
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rcx
mov    %rcx,-0x88(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    %rcx,%rsi
imul   %rax,%rsi
lea    0xf(,%rsi,4),%rdi
and    $0xfffffffffffffff0,%rdi
mov    %rsp,%rsi
sub    %rdi,%rsi
mov    %rsi,-0x78(%rbp)
mov    %rsi,%rsp
mov    %rcx,-0x40(%rbp)
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <fill@plt>
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    -0x60(%rbp),%rcx
mov    -0x58(%rbp),%rax
imul   %rcx,%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
movq   $0x1,-0x50(%rbp)
mov    -0x70(%rbp),%rdx
mov    -0x78(%rbp),%rcx
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <row2col@plt>
mov    -0x78(%rbp),%rsi
mov    -0x80(%rbp),%rcx
mov    -0x70(%rbp),%rdi
mov    -0x88(%rbp),%rax
imul   %rcx,%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x68(%rbp),%rdi
mov    -0x70(%rbp),%rsi
mov    -0x90(%rbp),%rcx
mov    -0x98(%rbp),%rax
imul   %rcx,%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%eax
jne    12b9 <test_cycle+0x169>
jmp    12cf <test_cycle+0x17f>
jmp    12be <test_cycle+0x16e>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    124f <test_cycle+0xff>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x50(%rbp),%rcx
lea    0xd1e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_cycle>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
imul   %rdi,%r13
lea    0xf(,%r13,4),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
mov    %rdi,-0x38(%rbp)
mov    %rsi,-0x30(%rbp)
mov    %r15,%rdx
call   1060 <fill@plt>
shl    $0x2,%r13d
mov    %rbx,%rdi
mov    %r15,%r12
mov    %r15,%rsi
mov    %r13d,%edx
call   1070 <memcpy@plt>
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <row2col@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1070 <memcpy@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1050 <memcmp@plt>
add    $0x1,%r15
test   %eax,%eax
jne    11c0 <test_cycle+0x70>
lea    0xe02(%rip),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    %r15,%rcx
call   1040 <printf@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"%zux%zu took %zu cycles\0A\00"
define void @test_cycle(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i64 %0, i64* %3
store i64 %1, i64* %4
%13 = load i64, i64* %3
%14 = load i64, i64* %4
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %5
%16 = mul nuw i64 %13, %14
%17 = alloca i32, i64 %16
store i64 %13, i64* %6
store i64 %14, i64* %7
%18 = load i64, i64* %3
%19 = load i64, i64* %4
%20 = mul nuw i64 %18, %19
%21 = alloca i32, i64 %20
store i64 %18, i64* %8
store i64 %19, i64* %9
%22 = load i64, i64* %3
%23 = load i64, i64* %4
%24 = mul nuw i64 %22, %23
%25 = alloca i32, i64 %24
store i64 %22, i64* %10
store i64 %23, i64* %11
%26 = load i64, i64* %3
%27 = load i64, i64* %4
%28 = bitcast i32* %17 to i32**
%29 = call i32 @fill(i64 %26, i64 %27, i32** %28)
%30 = bitcast i32* %21 to i32**
%31 = bitcast i32* %17 to i32**
%32 = mul nuw i64 %13, %14
%33 = mul nuw i64 4, %32
%34 = trunc i64 %33 to i32
%35 = call i32 @memcpy(i32** %30, i32** %31, i32 %34)
store i64 1, i64* %12
br label %36
36: ; preds = %57, %2
%37 = load i64, i64* %3
%38 = load i64, i64* %4
%39 = bitcast i32* %21 to i32**
%40 = bitcast i32* %25 to i32**
%41 = call i32 @row2col(i64 %37, i64 %38, i32** %39, i32** %40)
%42 = bitcast i32* %21 to i32**
%43 = bitcast i32* %25 to i32**
%44 = mul nuw i64 %22, %23
%45 = mul nuw i64 4, %44
%46 = trunc i64 %45 to i32
%47 = call i32 @memcpy(i32** %42, i32** %43, i32 %46)
%48 = bitcast i32* %17 to i32**
%49 = bitcast i32* %21 to i32**
%50 = mul nuw i64 %18, %19
%51 = mul nuw i64 4, %50
%52 = trunc i64 %51 to i32
%53 = call i32 @memcmp(i32** %48, i32** %49, i32 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %56, label %55
55: ; preds = %36
br label %60
56: ; preds = %36
br label %57
57: ; preds = %56
%58 = load i64, i64* %12
%59 = add i64 %58, 1
store i64 %59, i64* %12
br label %36
60: ; preds = %55
%61 = load i64, i64* %3
%62 = load i64, i64* %4
%63 = load i64, i64* %12
%64 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %61, i64 %62, i64 %63)
%65 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"%zux%zu took %zu cycles\0A\00"
define void @test_cycle(i64 %0, i64 %1) {
%3 = mul nuw i64 %1, %0
%4 = alloca i32, i64 %3
%5 = alloca i32, i64 %3
%6 = alloca i32, i64 %3
%7 = bitcast i32* %4 to i32**
%8 = call i32 @fill(i64 %0, i64 %1, i32** %7)
%9 = bitcast i32* %5 to i32**
%10 = trunc i64 %3 to i32
%11 = shl i32 %10, 2
%12 = call i32 @memcpy(i32** %9, i32** %7, i32 %11)
%13 = bitcast i32* %6 to i32**
br label %14
14: ; preds = %14, %2
%15 = phi i64 [ 1, %2 ], [ %20, %14 ]
%16 = call i32 @row2col(i64 %0, i64 %1, i32** %9, i32** %13)
%17 = call i32 @memcpy(i32** %9, i32** %13, i32 %11)
%18 = call i32 @memcmp(i32** %7, i32** %9, i32 %11)
%19 = icmp eq i32 %18, 0
%20 = add i64 %15, 1
br i1 %19, label %21, label %14
21: ; preds = %14
%22 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %0, i64 %1, i64 %15)
ret void
}