#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 */
typedef int /*<<< orphan*/ mpf_t;

/* Variables and functions */
int /*<<< orphan*/ mpf_div_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpf_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpf_mul(int /*<<< orphan*/, int /*<<< orphan*/ const,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpf_mul_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpf_set(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpf_set_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpf_sub(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpf_swap(int /*<<< orphan*/, int /*<<< orphan*/);
void legendre_d2(mpf_t Pn, int n, const mpf_t x) {
  int i, i0 = 1;
  static int init = 0;
  static mpf_t a, b, P_im1, P_i;
  if (!init)
    init = 1, mpf_init(a), mpf_init(b), mpf_init(P_im1), mpf_init(P_i);
  (mpf_set_ui(P_im1, 0), mpf_set_ui(P_i, 3));
  for (i = i0 + 1; i < n; ++i) {
    mpf_mul(a, x, P_i);
    mpf_mul_ui(a, a, 2 * i + 1);
    mpf_mul_ui(b, P_im1, i + 2);
    mpf_sub(a, a, b);
    mpf_swap(P_im1, P_i);
    mpf_div_ui(P_i, a, i - 1);
  }
  mpf_set(Pn, n > i0 ? P_i : P_im1);
}


x86 assembly (clang, -O0):
<legendre_d2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,0x2ec5(%rip)
jne    11db <legendre_d2+0x5b>
movl   $0x1,0x2eb5(%rip)
mov    0x2eb3(%rip),%edi
call   1030 <mpf_init@plt>
mov    0x2eac(%rip),%edi
call   1030 <mpf_init@plt>
mov    0x2ea5(%rip),%edi
call   1030 <mpf_init@plt>
mov    0x2e9e(%rip),%edi
call   1030 <mpf_init@plt>
mov    0x2e8f(%rip),%edi
xor    %esi,%esi
call   1060 <mpf_set_ui@plt>
mov    0x2e86(%rip),%edi
mov    $0x3,%esi
call   1060 <mpf_set_ui@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    129f <legendre_d2+0x11f>
mov    0x2e55(%rip),%edi
mov    -0xc(%rbp),%esi
mov    0x2e58(%rip),%edx
call   10a0 <mpf_mul@plt>
mov    0x2e41(%rip),%edi
mov    0x2e3b(%rip),%esi
mov    -0x10(%rbp),%edx
shl    $0x1,%edx
add    $0x1,%edx
call   1070 <mpf_mul_ui@plt>
mov    0x2e2b(%rip),%edi
mov    0x2e29(%rip),%esi
mov    -0x10(%rbp),%edx
add    $0x2,%edx
call   1070 <mpf_mul_ui@plt>
mov    0x2e10(%rip),%edi
mov    0x2e0a(%rip),%esi
mov    0x2e08(%rip),%edx
call   1080 <mpf_sub@plt>
mov    0x2e01(%rip),%edi
mov    0x2dff(%rip),%esi
call   1090 <mpf_swap@plt>
mov    0x2df4(%rip),%edi
mov    0x2de2(%rip),%esi
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
call   1050 <mpf_div_ui@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1201 <legendre_d2+0x81>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    12bf <legendre_d2+0x13f>
mov    0x2dbd(%rip),%eax
mov    %eax,-0x1c(%rbp)
jmp    12c8 <legendre_d2+0x148>
mov    0x2dab(%rip),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1040 <mpf_set@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<legendre_d2>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %esi,%r12d
mov    %edi,%r14d
cmpb   $0x0,0x2ecc(%rip)
jne    11bd <legendre_d2+0x3d>
movb   $0x1,0x2ec3(%rip)
xor    %edi,%edi
call   1030 <mpf_init@plt>
xor    %edi,%edi
call   1030 <mpf_init@plt>
xor    %edi,%edi
call   1030 <mpf_init@plt>
xor    %edi,%edi
call   1030 <mpf_init@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1060 <mpf_set_ui@plt>
xor    %edi,%edi
mov    $0x3,%esi
call   1060 <mpf_set_ui@plt>
cmp    $0x3,%r12d
jl     1248 <legendre_d2+0xc8>
neg    %r12d
mov    $0x4,%ebx
mov    $0x5,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
xor    %edi,%edi
mov    %r15d,%esi
xor    %edx,%edx
call   10a0 <mpf_mul@plt>
xor    %edi,%edi
xor    %esi,%esi
mov    %ebp,%edx
call   1070 <mpf_mul_ui@plt>
xor    %edi,%edi
xor    %esi,%esi
mov    %ebx,%edx
call   1070 <mpf_mul_ui@plt>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <mpf_sub@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1090 <mpf_swap@plt>
lea    -0x3(%rbx),%edx
xor    %edi,%edi
xor    %esi,%esi
call   1050 <mpf_div_ui@plt>
lea    (%r12,%rbx,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
add    $0x2,%ebp
mov    %ecx,%ebx
cmp    $0x2,%eax
jne    11f0 <legendre_d2+0x70>
mov    %r14d,%edi
xor    %esi,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <mpf_set@plt>

LLVM IR (-O0):
@legendre_d2.init = internal global i32 0
@legendre_d2.a = internal global i32 0
@legendre_d2.b = internal global i32 0
@legendre_d2.P_im1 = internal global i32 0
@legendre_d2.P_i = internal global i32 0
define void @legendre_d2(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 1, i32* %8
%9 = load i32, i32* @legendre_d2.init
%10 = icmp ne i32 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %3
store i32 1, i32* @legendre_d2.init
%12 = load i32, i32* @legendre_d2.a
%13 = call i32 @mpf_init(i32 %12)
%14 = load i32, i32* @legendre_d2.b
%15 = call i32 @mpf_init(i32 %14)
%16 = load i32, i32* @legendre_d2.P_im1
%17 = call i32 @mpf_init(i32 %16)
%18 = load i32, i32* @legendre_d2.P_i
%19 = call i32 @mpf_init(i32 %18)
br label %20
20: ; preds = %11, %3
%21 = load i32, i32* @legendre_d2.P_im1
%22 = call i32 @mpf_set_ui(i32 %21, i32 0)
%23 = load i32, i32* @legendre_d2.P_i
%24 = call i32 @mpf_set_ui(i32 %23, i32 3)
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
br label %27
27: ; preds = %59, %20
%28 = load i32, i32* %7
%29 = load i32, i32* %5
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %62
31: ; preds = %27
%32 = load i32, i32* @legendre_d2.a
%33 = load i32, i32* %6
%34 = load i32, i32* @legendre_d2.P_i
%35 = call i32 @mpf_mul(i32 %32, i32 %33, i32 %34)
%36 = load i32, i32* @legendre_d2.a
%37 = load i32, i32* @legendre_d2.a
%38 = load i32, i32* %7
%39 = mul nsw i32 2, %38
%40 = add nsw i32 %39, 1
%41 = call i32 @mpf_mul_ui(i32 %36, i32 %37, i32 %40)
%42 = load i32, i32* @legendre_d2.b
%43 = load i32, i32* @legendre_d2.P_im1
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 2
%46 = call i32 @mpf_mul_ui(i32 %42, i32 %43, i32 %45)
%47 = load i32, i32* @legendre_d2.a
%48 = load i32, i32* @legendre_d2.a
%49 = load i32, i32* @legendre_d2.b
%50 = call i32 @mpf_sub(i32 %47, i32 %48, i32 %49)
%51 = load i32, i32* @legendre_d2.P_im1
%52 = load i32, i32* @legendre_d2.P_i
%53 = call i32 @mpf_swap(i32 %51, i32 %52)
%54 = load i32, i32* @legendre_d2.P_i
%55 = load i32, i32* @legendre_d2.a
%56 = load i32, i32* %7
%57 = sub nsw i32 %56, 1
%58 = call i32 @mpf_div_ui(i32 %54, i32 %55, i32 %57)
br label %59
59: ; preds = %31
%60 = load i32, i32* %7
%61 = add nsw i32 %60, 1
store i32 %61, i32* %7
br label %27
62: ; preds = %27
%63 = load i32, i32* %4
%64 = load i32, i32* %5
%65 = load i32, i32* %8
%66 = icmp sgt i32 %64, %65
br i1 %66, label %67, label %69
67: ; preds = %62
%68 = load i32, i32* @legendre_d2.P_i
br label %71
69: ; preds = %62
%70 = load i32, i32* @legendre_d2.P_im1
br label %71
71: ; preds = %69, %67
%72 = phi i32 [ %68, %67 ], [ %70, %69 ]
%73 = call i32 @mpf_set(i32 %63, i32 %72)
ret void
}

LLVM IR (-O2):
@legendre_d2.init = internal unnamed_addr global i1 false
define void @legendre_d2(i32 %0, i32 %1, i32 %2) {
%4 = load i1, i1* @legendre_d2.init
br i1 %4, label %10, label %5
5: ; preds = %3
store i1 true, i1* @legendre_d2.init
%6 = tail call i32 @mpf_init(i32 0)
%7 = tail call i32 @mpf_init(i32 0)
%8 = tail call i32 @mpf_init(i32 0)
%9 = tail call i32 @mpf_init(i32 0)
br label %10
10: ; preds = %5, %3
%11 = tail call i32 @mpf_set_ui(i32 0, i32 0)
%12 = tail call i32 @mpf_set_ui(i32 0, i32 3)
%13 = icmp sgt i32 %1, 2
br i1 %13, label %14, label %28
14: ; preds = %10, %14
%15 = phi i32 [ %26, %14 ], [ 2, %10 ]
%16 = tail call i32 @mpf_mul(i32 0, i32 %2, i32 0)
%17 = shl nuw nsw i32 %15, 1
%18 = or i32 %17, 1
%19 = tail call i32 @mpf_mul_ui(i32 0, i32 0, i32 %18)
%20 = add nuw nsw i32 %15, 2
%21 = tail call i32 @mpf_mul_ui(i32 0, i32 0, i32 %20)
%22 = tail call i32 @mpf_sub(i32 0, i32 0, i32 0)
%23 = tail call i32 @mpf_swap(i32 0, i32 0)
%24 = add nsw i32 %15, -1
%25 = tail call i32 @mpf_div_ui(i32 0, i32 0, i32 %24)
%26 = add nuw nsw i32 %15, 1
%27 = icmp eq i32 %26, %1
br i1 %27, label %28, label %14
28: ; preds = %14, %10
%29 = tail call i32 @mpf_set(i32 %0, i32 0)
ret void
}