#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 */

/* Variables and functions */
int /*<<< orphan*/ free(double *);
scalar_t__ malloc(int);
int /*<<< orphan*/ mtranspose(double *, int, int, double *);
int rank_c(double *, int, int);
int rank(double *A, int M, int N) {
  int rnk;
  double *AT;
  AT = (double *)malloc(sizeof(double) * M * N);
  if (M < N) {
    mtranspose(A, M, N, AT);
    rnk = rank_c(AT, N, M);
  } else {
    rnk = rank_c(A, M, N);
  }
  free(AT);
  return rnk;
}


x86 assembly (clang, -O0):
<rank>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
movslq -0x10(%rbp),%rcx
imul   %rcx,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11a3 <rank+0x63>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1040 <mtranspose@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1050 <rank_c@plt>
mov    %eax,-0x14(%rbp)
jmp    11b5 <rank+0x75>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1050 <rank_c@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rank>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r15
mov    %esi,%edi
imul   %edx,%edi
shl    $0x3,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
cmp    %ebp,%ebx
jge    117a <rank+0x3a>
mov    %ebx,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1040 <mtranspose@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
jmp    117e <rank+0x3e>
mov    %ebx,%esi
mov    %ebp,%edx
call   1050 <rank_c@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rank(double* %0, i32 %1, i32 %2) {
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca double*
store double* %0, double** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = mul i64 8, %10
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = mul i64 %11, %13
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to double*
store double* %17, double** %8
%18 = load i32, i32* %5
%19 = load i32, i32* %6
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %31
21: ; preds = %3
%22 = load double*, double** %4
%23 = load i32, i32* %5
%24 = load i32, i32* %6
%25 = load double*, double** %8
%26 = call i32 @mtranspose(double* %22, i32 %23, i32 %24, double* %25)
%27 = load double*, double** %8
%28 = load i32, i32* %6
%29 = load i32, i32* %5
%30 = call i32 @rank_c(double* %27, i32 %28, i32 %29)
store i32 %30, i32* %7
br label %36
31: ; preds = %3
%32 = load double*, double** %4
%33 = load i32, i32* %5
%34 = load i32, i32* %6
%35 = call i32 @rank_c(double* %32, i32 %33, i32 %34)
store i32 %35, i32* %7
br label %36
36: ; preds = %31, %21
%37 = load double*, double** %8
%38 = call i32 @free(double* %37)
%39 = load i32, i32* %7
ret i32 %39
}

LLVM IR (-O2):
define i32 @rank(double* %0, i32 %1, i32 %2) {
%4 = shl i32 %1, 3
%5 = mul i32 %4, %2
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to double*
%8 = icmp slt i32 %1, %2
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = tail call i32 @mtranspose(double* %0, i32 %1, i32 %2, double* %7)
%11 = tail call i32 @rank_c(double* %7, i32 %2, i32 %1)
br label %14
12: ; preds = %3
%13 = tail call i32 @rank_c(double* %0, i32 %1, i32 %2)
br label %14
14: ; preds = %12, %9
%15 = phi i32 [ %11, %9 ], [ %13, %12 ]
%16 = tail call i32 @free(double* %7)
ret i32 %15
}