#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 factorial(int);
int /*<<< orphan*/ free(int *);
int *initPerm(int);
int *nextPerm(int *, int, int, int);
int permDiagProduct(double **, int *, int);
double det(double **P, int d) {
  int *p;
  int signum = 1;
  int i, numPerms;
  double det = 0;
  p = initPerm(d);
  numPerms = factorial(d);
  for (i = 0; i < numPerms; i++) {
    det += signum * permDiagProduct(P, p, d);
    p = nextPerm(p, i + 1, d - 1, d);
    signum = -1 * signum;
  }
  free(p);
  return (det);
}


x86 assembly (clang, -O0):
<det>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <initPerm@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <factorial@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11f8 <det+0xa8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <permDiagProduct@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
imul   %ecx,%eax
cvtsi2sd %eax,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
mov    -0xc(%rbp),%ecx
call   1060 <nextPerm@plt>
mov    %rax,-0x18(%rbp)
imul   $0xffffffff,-0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    118c <det+0x3c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x30(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<det>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,%r13
mov    %esi,%edi
call   1070 <initPerm@plt>
mov    %rax,%rbx
mov    %r15d,%edi
call   1040 <factorial@plt>
test   %eax,%eax
jle    11f5 <det+0xa5>
mov    %eax,%r12d
lea    -0x1(%r15),%eax
mov    %eax,0x14(%rsp)
neg    %r12d
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    $0x1,%ebp
mov    $0x1,%r14d
nopl   (%rax)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1050 <permDiagProduct@plt>
imul   %r14d,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    0x14(%rsp),%edx
mov    %r15d,%ecx
call   1060 <nextPerm@plt>
mov    %rax,%rbx
neg    %r14d
lea    (%r12,%rbp,1),%eax
add    $0x1,%eax
mov    %ebp,%ecx
add    $0x1,%ecx
mov    %ecx,%ebp
cmp    $0x1,%eax
jne    11a0 <det+0x50>
jmp    11ff <det+0xaf>
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <free@plt>
movsd  0x8(%rsp),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @det(double** %0, i32 %1) {
%3 = alloca double**
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
store double** %0, double*** %3
store i32 %1, i32* %4
store i32 1, i32* %6
store double 0.000000e+00, double* %9
%10 = load i32, i32* %4
%11 = call i32* @initPerm(i32 %10)
store i32* %11, i32** %5
%12 = load i32, i32* %4
%13 = call i32 @factorial(i32 %12)
store i32 %13, i32* %8
store i32 0, i32* %7
br label %14
14: ; preds = %37, %2
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %40
18: ; preds = %14
%19 = load i32, i32* %6
%20 = load double**, double*** %3
%21 = load i32*, i32** %5
%22 = load i32, i32* %4
%23 = call i32 @permDiagProduct(double** %20, i32* %21, i32 %22)
%24 = mul nsw i32 %19, %23
%25 = sitofp i32 %24 to double
%26 = load double, double* %9
%27 = fadd double %26, %25
store double %27, double* %9
%28 = load i32*, i32** %5
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
%31 = load i32, i32* %4
%32 = sub nsw i32 %31, 1
%33 = load i32, i32* %4
%34 = call i32* @nextPerm(i32* %28, i32 %30, i32 %32, i32 %33)
store i32* %34, i32** %5
%35 = load i32, i32* %6
%36 = mul nsw i32 -1, %35
store i32 %36, i32* %6
br label %37
37: ; preds = %18
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %14
40: ; preds = %14
%41 = load i32*, i32** %5
%42 = call i32 @free(i32* %41)
%43 = load double, double* %9
ret double %43
}

LLVM IR (-O2):
define double @det(double** %0, i32 %1) {
%3 = tail call i32* @initPerm(i32 %1)
%4 = tail call i32 @factorial(i32 %1)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %21
6: ; preds = %2
%7 = add nsw i32 %1, -1
br label %8
8: ; preds = %6, %8
%9 = phi double [ 0.000000e+00, %6 ], [ %16, %8 ]
%10 = phi i32* [ %3, %6 ], [ %18, %8 ]
%11 = phi i32 [ 0, %6 ], [ %17, %8 ]
%12 = phi i32 [ 1, %6 ], [ %19, %8 ]
%13 = tail call i32 @permDiagProduct(double** %0, i32* %10, i32 %1)
%14 = mul nsw i32 %13, %12
%15 = sitofp i32 %14 to double
%16 = fadd double %9, %15
%17 = add nuw nsw i32 %11, 1
%18 = tail call i32* @nextPerm(i32* %10, i32 %17, i32 %7, i32 %1)
%19 = sub nsw i32 0, %12
%20 = icmp eq i32 %17, %4
br i1 %20, label %21, label %8
21: ; preds = %8, %2
%22 = phi i32* [ %3, %2 ], [ %18, %8 ]
%23 = phi double [ 0.000000e+00, %2 ], [ %16, %8 ]
%24 = tail call i32 @free(i32* %22)
ret double %23
}