#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*/ dscal_(int *, double *, double *, int *);
int /*<<< orphan*/ matcopy(int, double *, double *);
int /*<<< orphan*/ matexp_pade(int, int const, double *, double *);
int matexp_scale_factor(double *, int);
int /*<<< orphan*/ matpow_by_squaring(double *, int, int, double *);
void matexp(int n, const int p, double *x, double *ret) {
  int m;
  int nn = n * n;
  int one = 1;
  double tmp;
  m = matexp_scale_factor(x, n);
  if (m == 0)
    matexp_pade(n, p, x, ret);
  else {
    tmp = 1. / ((double)m);
    dscal_(&nn, &tmp, x, &one);
    matexp_pade(n, p, x, ret);
    matcopy(n, ret, x);
    matpow_by_squaring(x, n, m, ret);
  }
}


x86 assembly (clang, -O0):
<matexp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x4(%rbp),%eax
imul   -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1060 <matexp_scale_factor@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11a8 <matexp+0x58>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1070 <matexp_pade@plt>
jmp    1209 <matexp+0xb9>
cvtsi2sdl -0x1c(%rbp),%xmm1
movsd  0xe4b(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x24(%rbp),%rcx
call   1050 <dscal_@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1070 <matexp_pade@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <matcopy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1030 <matpow_by_squaring@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matexp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %esi,%r12d
mov    %edi,%ebp
mov    %edi,%eax
imul   %edi,%eax
mov    %eax,0x4(%rsp)
movl   $0x1,(%rsp)
mov    %rdx,%rdi
mov    %ebp,%esi
call   1060 <matexp_scale_factor@plt>
test   %eax,%eax
je     11ed <matexp+0x9d>
mov    %eax,%r15d
cvtsi2sd %eax,%xmm0
movsd  0xe6c(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
lea    0x4(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdx
call   1050 <dscal_@plt>
mov    %ebp,%edi
mov    %r12d,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1070 <matexp_pade@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <matcopy@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1030 <matpow_by_squaring@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
mov    %r12d,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <matexp_pade@plt>

LLVM IR (-O0):
define void @matexp(i32 %0, i32 %1, double* %2, double* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca double*
%8 = alloca double*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca double
store i32 %0, i32* %5
store i32 %1, i32* %6
store double* %2, double** %7
store double* %3, double** %8
%13 = load i32, i32* %5
%14 = load i32, i32* %5
%15 = mul nsw i32 %13, %14
store i32 %15, i32* %10
store i32 1, i32* %11
%16 = load double*, double** %7
%17 = load i32, i32* %5
%18 = call i32 @matexp_scale_factor(double* %16, i32 %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %4
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = load double*, double** %7
%25 = load double*, double** %8
%26 = call i32 @matexp_pade(i32 %22, i32 %23, double* %24, double* %25)
br label %47
27: ; preds = %4
%28 = load i32, i32* %9
%29 = sitofp i32 %28 to double
%30 = fdiv double 1.000000e+00, %29
store double %30, double* %12
%31 = load double*, double** %7
%32 = call i32 @dscal_(i32* %10, double* %12, double* %31, i32* %11)
%33 = load i32, i32* %5
%34 = load i32, i32* %6
%35 = load double*, double** %7
%36 = load double*, double** %8
%37 = call i32 @matexp_pade(i32 %33, i32 %34, double* %35, double* %36)
%38 = load i32, i32* %5
%39 = load double*, double** %8
%40 = load double*, double** %7
%41 = call i32 @matcopy(i32 %38, double* %39, double* %40)
%42 = load double*, double** %7
%43 = load i32, i32* %5
%44 = load i32, i32* %9
%45 = load double*, double** %8
%46 = call i32 @matpow_by_squaring(double* %42, i32 %43, i32 %44, double* %45)
br label %47
47: ; preds = %27, %21
ret void
}

LLVM IR (-O2):
define void @matexp(i32 %0, i32 %1, double* %2, double* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca double
%8 = bitcast i32* %5 to i8*
%9 = mul nsw i32 %0, %0
store i32 %9, i32* %5
%10 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%11 = bitcast double* %7 to i8*
%12 = tail call i32 @matexp_scale_factor(double* %2, i32 %0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = tail call i32 @matexp_pade(i32 %0, i32 %1, double* %2, double* %3)
br label %23
16: ; preds = %4
%17 = sitofp i32 %12 to double
%18 = fdiv double 1.000000e+00, %17
store double %18, double* %7
%19 = call i32 @dscal_(i32* %5, double* %7, double* %2, i32* %6)
%20 = call i32 @matexp_pade(i32 %0, i32 %1, double* %2, double* %3)
%21 = call i32 @matcopy(i32 %0, double* %3, double* %2)
%22 = call i32 @matpow_by_squaring(double* %2, i32 %0, i32 %12, double* %3)
br label %23
23: ; preds = %16, %14
ret void
}