#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 *);
double *malloc(int);
int /*<<< orphan*/ p_matcopy(double *, int *, double *, int *);
int /*<<< orphan*/ p_mateye(double *, int *);
int /*<<< orphan*/ p_matprod(double *, int *, double *, int *, double *, int *);
int /*<<< orphan*/ pdims_(int *, int *, int *);
void p_matpow_by_squaring(double *A, int *desca, int b, double *P) {
  int n, m;
  int ldm[2];
  int blacs[5];
  double *TMP;
  p_mateye(P, desca);
  if (b == 0)
    return;
  if (b == 1) {
    p_matcopy(A, desca, P, desca);
    return;
  }
  pdims_(desca, ldm, blacs);
  m = ldm[0];
  n = ldm[1];
  TMP = malloc(m * n * sizeof(double));
  while (b) {
    if (b & 1) {
      p_matprod(P, desca, A, desca, TMP, desca);
      p_matcopy(TMP, desca, P, desca);
    }
    b >>= 1;
    p_matprod(A, desca, A, desca, TMP, desca);
    p_matcopy(TMP, desca, A, desca);
  }
  free(TMP);
}


x86 assembly (clang, -O0):
<p_matpow_by_squaring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <p_mateye@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1193 <p_matpow_by_squaring+0x33>
jmp    1280 <p_matpow_by_squaring+0x120>
cmpl   $0x1,-0x14(%rbp)
jne    11b7 <p_matpow_by_squaring+0x57>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
call   1080 <p_matcopy@plt>
jmp    1280 <p_matpow_by_squaring+0x120>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1040 <pdims_@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
imul   -0x24(%rbp),%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x58(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1277 <p_matpow_by_squaring+0x117>
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1237 <p_matpow_by_squaring+0xd7>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x58(%rbp),%r8
mov    -0x10(%rbp),%r9
call   1050 <p_matprod@plt>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
call   1080 <p_matcopy@plt>
mov    -0x14(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x58(%rbp),%r8
mov    -0x10(%rbp),%r9
call   1050 <p_matprod@plt>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
call   1080 <p_matcopy@plt>
jmp    11ec <p_matpow_by_squaring+0x8c>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p_matpow_by_squaring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rcx,%rdi
call   1060 <p_mateye@plt>
test   %r13d,%r13d
je     124e <p_matpow_by_squaring+0xee>
cmp    $0x1,%r13d
jne    11b0 <p_matpow_by_squaring+0x50>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <p_matcopy@plt>
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
call   1040 <pdims_@plt>
mov    0x8(%rsp),%edi
imul   0xc(%rsp),%edi
shl    $0x3,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
jmp    1216 <p_matpow_by_squaring+0xb6>
nopl   0x0(%rax,%rax,1)
mov    %r13d,%ebp
sar    %ebp
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
mov    %r12,%r8
mov    %rbx,%r9
call   1050 <p_matprod@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1080 <p_matcopy@plt>
cmp    $0x1,%r13d
mov    %ebp,%r13d
jbe    1246 <p_matpow_by_squaring+0xe6>
test   $0x1,%r13b
je     11e0 <p_matpow_by_squaring+0x80>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
mov    %r12,%r8
mov    %rbx,%r9
call   1050 <p_matprod@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1080 <p_matcopy@plt>
jmp    11e0 <p_matpow_by_squaring+0x80>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @p_matpow_by_squaring(double* %0, i32* %1, i32 %2, double* %3) {
%5 = alloca double*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca double*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [2 x i32]
%12 = alloca [5 x i32]
%13 = alloca double*
store double* %0, double** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store double* %3, double** %8
%14 = load double*, double** %8
%15 = load i32*, i32** %6
%16 = call i32 @p_mateye(double* %14, i32* %15)
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %4
br label %83
20: ; preds = %4
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load double*, double** %5
%25 = load i32*, i32** %6
%26 = load double*, double** %8
%27 = load i32*, i32** %6
%28 = call i32 @p_matcopy(double* %24, i32* %25, double* %26, i32* %27)
br label %83
29: ; preds = %20
%30 = load i32*, i32** %6
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%32 = getelementptr inbounds [5 x i32], [5 x i32]* %12, i64 0, i64 0
%33 = call i32 @pdims_(i32* %30, i32* %31, i32* %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%35 = load i32, i32* %34
store i32 %35, i32* %10
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%37 = load i32, i32* %36
store i32 %37, i32* %9
%38 = load i32, i32* %10
%39 = load i32, i32* %9
%40 = mul nsw i32 %38, %39
%41 = sext i32 %40 to i64
%42 = mul i64 %41, 8
%43 = trunc i64 %42 to i32
%44 = call double* @malloc(i32 %43)
store double* %44, double** %13
br label %45
45: ; preds = %65, %29
%46 = load i32, i32* %7
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %80
48: ; preds = %45
%49 = load i32, i32* %7
%50 = and i32 %49, 1
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %65
52: ; preds = %48
%53 = load double*, double** %8
%54 = load i32*, i32** %6
%55 = load double*, double** %5
%56 = load i32*, i32** %6
%57 = load double*, double** %13
%58 = load i32*, i32** %6
%59 = call i32 @p_matprod(double* %53, i32* %54, double* %55, i32* %56, double* %57, i32* %58)
%60 = load double*, double** %13
%61 = load i32*, i32** %6
%62 = load double*, double** %8
%63 = load i32*, i32** %6
%64 = call i32 @p_matcopy(double* %60, i32* %61, double* %62, i32* %63)
br label %65
65: ; preds = %52, %48
%66 = load i32, i32* %7
%67 = ashr i32 %66, 1
store i32 %67, i32* %7
%68 = load double*, double** %5
%69 = load i32*, i32** %6
%70 = load double*, double** %5
%71 = load i32*, i32** %6
%72 = load double*, double** %13
%73 = load i32*, i32** %6
%74 = call i32 @p_matprod(double* %68, i32* %69, double* %70, i32* %71, double* %72, i32* %73)
%75 = load double*, double** %13
%76 = load i32*, i32** %6
%77 = load double*, double** %5
%78 = load i32*, i32** %6
%79 = call i32 @p_matcopy(double* %75, i32* %76, double* %77, i32* %78)
br label %45
80: ; preds = %45
%81 = load double*, double** %13
%82 = call i32 @free(double* %81)
br label %83
83: ; preds = %80, %23, %19
ret void
}

LLVM IR (-O2):
define void @p_matpow_by_squaring(double* %0, i32* %1, i32 %2, double* %3) {
%5 = alloca [2 x i32]
%6 = alloca [5 x i32]
%7 = bitcast [2 x i32]* %5 to i8*
%8 = bitcast [5 x i32]* %6 to i8*
%9 = tail call i32 @p_mateye(double* %3, i32* %1)
switch i32 %2, label %12 [
i32 0, label %36
i32 1, label %10
]
10: ; preds = %4
%11 = tail call i32 @p_matcopy(double* %0, i32* %1, double* %3, i32* %1)
br label %36
12: ; preds = %4
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%14 = getelementptr inbounds [5 x i32], [5 x i32]* %6, i64 0, i64 0
%15 = call i32 @pdims_(i32* %1, i32* %13, i32* %14)
%16 = load i32, i32* %13
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%18 = load i32, i32* %17
%19 = shl i32 %16, 3
%20 = mul i32 %19, %18
%21 = call double* @malloc(i32 %20)
br label %22
22: ; preds = %12, %29
%23 = phi i32 [ %2, %12 ], [ %30, %29 ]
%24 = and i32 %23, 1
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = call i32 @p_matprod(double* %3, i32* %1, double* %0, i32* %1, double* %21, i32* %1)
%28 = call i32 @p_matcopy(double* %21, i32* %1, double* %3, i32* %1)
br label %29
29: ; preds = %26, %22
%30 = ashr i32 %23, 1
%31 = call i32 @p_matprod(double* %0, i32* %1, double* %0, i32* %1, double* %21, i32* %1)
%32 = call i32 @p_matcopy(double* %21, i32* %1, double* %0, i32* %1)
%33 = icmp ult i32 %23, 2
br i1 %33, label %34, label %22
34: ; preds = %29
%35 = call i32 @free(double* %21)
br label %36
36: ; preds = %4, %34, %10
ret void
}