#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*/ matcopy(int, double *, double *);
int /*<<< orphan*/ mateye(int, double *);
int /*<<< orphan*/ matprod(int, double *, double *, double *);
void matpow_by_squaring(double *A, int n, int b, double *P) {
  double *TMP;
  mateye(n, P);
  if (b == 0)
    return;
  if (b == 1) {
    matcopy(n, A, P);
    return;
  }
  TMP = malloc(n * n * sizeof(double));
  while (b) {
    if (b & 1) {
      matprod(n, P, A, TMP);
      matcopy(n, TMP, P);
    }
    b >>= 1;
    matprod(n, A, A, TMP);
    matcopy(n, TMP, A);
  }
  free(TMP);
}


x86 assembly (clang, -O0):
<matpow_by_squaring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1060 <mateye@plt>
cmpl   $0x0,-0x10(%rbp)
jne    1181 <matpow_by_squaring+0x31>
jmp    1230 <matpow_by_squaring+0xe0>
cmpl   $0x1,-0x10(%rbp)
jne    11a0 <matpow_by_squaring+0x50>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <matcopy@plt>
jmp    1230 <matpow_by_squaring+0xe0>
mov    -0xc(%rbp),%eax
imul   -0xc(%rbp),%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1227 <matpow_by_squaring+0xd7>
mov    -0x10(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11f5 <matpow_by_squaring+0xa5>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <matprod@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <matcopy@plt>
mov    -0x10(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <matprod@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1040 <matcopy@plt>
jmp    11b8 <matpow_by_squaring+0x68>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matpow_by_squaring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%ebx
mov    %esi,%r15d
mov    %rdi,%r12
mov    %esi,%edi
mov    %rcx,%rsi
call   1060 <mateye@plt>
test   %ebx,%ebx
je     1195 <matpow_by_squaring+0x45>
cmp    $0x1,%ebx
jne    11a4 <matpow_by_squaring+0x54>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <matcopy@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
imul   %r15d,%edi
shl    $0x3,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
jmp    11ea <matpow_by_squaring+0x9a>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%ebp
sar    %ebp
mov    %r15d,%edi
mov    %r12,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1050 <matprod@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %r12,%rdx
call   1040 <matcopy@plt>
cmp    $0x1,%ebx
mov    %ebp,%ebx
jbe    1210 <matpow_by_squaring+0xc0>
test   $0x1,%bl
je     11c0 <matpow_by_squaring+0x70>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1050 <matprod@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   1040 <matcopy@plt>
jmp    11c0 <matpow_by_squaring+0x70>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @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 double*
store double* %0, double** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store double* %3, double** %8
%10 = load i32, i32* %6
%11 = load double*, double** %8
%12 = call i32 @mateye(i32 %10, double* %11)
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %4
br label %64
16: ; preds = %4
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = load i32, i32* %6
%21 = load double*, double** %5
%22 = load double*, double** %8
%23 = call i32 @matcopy(i32 %20, double* %21, double* %22)
br label %64
24: ; preds = %16
%25 = load i32, i32* %6
%26 = load i32, i32* %6
%27 = mul nsw i32 %25, %26
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 8
%30 = trunc i64 %29 to i32
%31 = call double* @malloc(i32 %30)
store double* %31, double** %9
br label %32
32: ; preds = %49, %24
%33 = load i32, i32* %7
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %61
35: ; preds = %32
%36 = load i32, i32* %7
%37 = and i32 %36, 1
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %49
39: ; preds = %35
%40 = load i32, i32* %6
%41 = load double*, double** %8
%42 = load double*, double** %5
%43 = load double*, double** %9
%44 = call i32 @matprod(i32 %40, double* %41, double* %42, double* %43)
%45 = load i32, i32* %6
%46 = load double*, double** %9
%47 = load double*, double** %8
%48 = call i32 @matcopy(i32 %45, double* %46, double* %47)
br label %49
49: ; preds = %39, %35
%50 = load i32, i32* %7
%51 = ashr i32 %50, 1
store i32 %51, i32* %7
%52 = load i32, i32* %6
%53 = load double*, double** %5
%54 = load double*, double** %5
%55 = load double*, double** %9
%56 = call i32 @matprod(i32 %52, double* %53, double* %54, double* %55)
%57 = load i32, i32* %6
%58 = load double*, double** %9
%59 = load double*, double** %5
%60 = call i32 @matcopy(i32 %57, double* %58, double* %59)
br label %32
61: ; preds = %32
%62 = load double*, double** %9
%63 = call i32 @free(double* %62)
br label %64
64: ; preds = %61, %19, %15
ret void
}

LLVM IR (-O2):
define void @matpow_by_squaring(double* %0, i32 %1, i32 %2, double* %3) {
%5 = tail call i32 @mateye(i32 %1, double* %3)
switch i32 %2, label %8 [
i32 0, label %26
i32 1, label %6
]
6: ; preds = %4
%7 = tail call i32 @matcopy(i32 %1, double* %0, double* %3)
br label %26
8: ; preds = %4
%9 = shl i32 %1, 3
%10 = mul i32 %9, %1
%11 = tail call double* @malloc(i32 %10)
br label %12
12: ; preds = %8, %19
%13 = phi i32 [ %2, %8 ], [ %20, %19 ]
%14 = and i32 %13, 1
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = tail call i32 @matprod(i32 %1, double* %3, double* %0, double* %11)
%18 = tail call i32 @matcopy(i32 %1, double* %11, double* %3)
br label %19
19: ; preds = %16, %12
%20 = ashr i32 %13, 1
%21 = tail call i32 @matprod(i32 %1, double* %0, double* %0, double* %11)
%22 = tail call i32 @matcopy(i32 %1, double* %11, double* %0)
%23 = icmp ult i32 %13, 2
br i1 %23, label %24, label %12
24: ; preds = %19
%25 = tail call i32 @free(double* %11)
br label %26
26: ; preds = %4, %24, %6
ret void
}