#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*/ const mpf_t;

/* Variables and functions */
int /*<<< orphan*/ mpf_div_ui(int /*<<< orphan*/ const,
                              int /*<<< orphan*/ const, int);
int /*<<< orphan*/ mpf_init(int /*<<< orphan*/ const);
int /*<<< orphan*/ mpf_mul(int /*<<< orphan*/ const, int /*<<< orphan*/ const,
                           int /*<<< orphan*/ const);
int /*<<< orphan*/ mpf_mul_ui(int /*<<< orphan*/ const,
                              int /*<<< orphan*/ const, int);
int /*<<< orphan*/ mpf_set(int /*<<< orphan*/ const, int /*<<< orphan*/ const);
int /*<<< orphan*/ mpf_set_ui(int /*<<< orphan*/ const, int);
int /*<<< orphan*/ mpf_sub(int /*<<< orphan*/ const, int /*<<< orphan*/ const,
                           int /*<<< orphan*/ const);
int /*<<< orphan*/ mpf_swap(int /*<<< orphan*/ const, int /*<<< orphan*/ const);
void legendre(mpf_t Pn, int n, const mpf_t x) {
  int i, i0 = 0;
  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, 1), mpf_set(P_i, x));
  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);
    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>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,0x2ec5(%rip)
jne    11db <legendre+0x5b>
movl   $0x1,0x2eb5(%rip)
mov    0xe4b(%rip),%edi
call   1030 <mpf_init@plt>
mov    0xe44(%rip),%edi
call   1030 <mpf_init@plt>
mov    0xe3d(%rip),%edi
call   1030 <mpf_init@plt>
mov    0xe36(%rip),%edi
call   1030 <mpf_init@plt>
mov    0xe27(%rip),%edi
mov    $0x1,%esi
call   1060 <mpf_set_ui@plt>
mov    0xe1b(%rip),%edi
mov    -0xc(%rbp),%esi
call   1040 <mpf_set@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    129d <legendre+0x11d>
mov    0xdec(%rip),%edi
mov    -0xc(%rbp),%esi
mov    0xdef(%rip),%edx
call   10a0 <mpf_mul@plt>
mov    0xdd8(%rip),%edi
mov    0xdd2(%rip),%esi
mov    -0x10(%rbp),%edx
shl    $0x1,%edx
add    $0x1,%edx
call   1070 <mpf_mul_ui@plt>
mov    0xdc2(%rip),%edi
mov    0xdc0(%rip),%esi
mov    -0x10(%rbp),%edx
call   1070 <mpf_mul_ui@plt>
mov    0xdaa(%rip),%edi
mov    0xda4(%rip),%esi
mov    0xda2(%rip),%edx
call   1080 <mpf_sub@plt>
mov    0xd9b(%rip),%edi
mov    0xd99(%rip),%esi
call   1090 <mpf_swap@plt>
mov    0xd8e(%rip),%edi
mov    0xd7c(%rip),%esi
mov    -0x10(%rbp),%edx
add    $0x1,%edx
call   1050 <mpf_div_ui@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1202 <legendre+0x82>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    12bd <legendre+0x13d>
mov    0xd57(%rip),%eax
mov    %eax,-0x1c(%rbp)
jmp    12c6 <legendre+0x146>
mov    0xd45(%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>:
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+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>
mov    $0x1,%ebp
xor    %edi,%edi
mov    $0x1,%esi
call   1060 <mpf_set_ui@plt>
xor    %edi,%edi
mov    %r15d,%esi
call   1040 <mpf_set@plt>
cmp    $0x2,%r12d
jl     123c <legendre+0xbc>
mov    $0x3,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edi,%edi
mov    %r15d,%esi
xor    %edx,%edx
call   10a0 <mpf_mul@plt>
xor    %edi,%edi
xor    %esi,%esi
mov    %ebx,%edx
call   1070 <mpf_mul_ui@plt>
xor    %edi,%edi
xor    %esi,%esi
mov    %ebp,%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>
add    $0x1,%ebp
xor    %edi,%edi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <mpf_div_ui@plt>
add    $0x2,%ebx
cmp    %ebp,%r12d
jne    11f0 <legendre+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.init = internal global i32 0
@legendre.a = internal constant i32 0
@legendre.b = internal constant i32 0
@legendre.P_im1 = internal constant i32 0
@legendre.P_i = internal constant i32 0
define void @legendre(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 0, i32* %8
%9 = load i32, i32* @legendre.init
%10 = icmp ne i32 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %3
store i32 1, i32* @legendre.init
%12 = load i32, i32* @legendre.a
%13 = call i32 @mpf_init(i32 %12)
%14 = load i32, i32* @legendre.b
%15 = call i32 @mpf_init(i32 %14)
%16 = load i32, i32* @legendre.P_im1
%17 = call i32 @mpf_init(i32 %16)
%18 = load i32, i32* @legendre.P_i
%19 = call i32 @mpf_init(i32 %18)
br label %20
20: ; preds = %11, %3
%21 = load i32, i32* @legendre.P_im1
%22 = call i32 @mpf_set_ui(i32 %21, i32 1)
%23 = load i32, i32* @legendre.P_i
%24 = load i32, i32* %6
%25 = call i32 @mpf_set(i32 %23, i32 %24)
%26 = load i32, i32* %8
%27 = add nsw i32 %26, 1
store i32 %27, i32* %7
br label %28
28: ; preds = %59, %20
%29 = load i32, i32* %7
%30 = load i32, i32* %5
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %62
32: ; preds = %28
%33 = load i32, i32* @legendre.a
%34 = load i32, i32* %6
%35 = load i32, i32* @legendre.P_i
%36 = call i32 @mpf_mul(i32 %33, i32 %34, i32 %35)
%37 = load i32, i32* @legendre.a
%38 = load i32, i32* @legendre.a
%39 = load i32, i32* %7
%40 = mul nsw i32 2, %39
%41 = add nsw i32 %40, 1
%42 = call i32 @mpf_mul_ui(i32 %37, i32 %38, i32 %41)
%43 = load i32, i32* @legendre.b
%44 = load i32, i32* @legendre.P_im1
%45 = load i32, i32* %7
%46 = call i32 @mpf_mul_ui(i32 %43, i32 %44, i32 %45)
%47 = load i32, i32* @legendre.a
%48 = load i32, i32* @legendre.a
%49 = load i32, i32* @legendre.b
%50 = call i32 @mpf_sub(i32 %47, i32 %48, i32 %49)
%51 = load i32, i32* @legendre.P_im1
%52 = load i32, i32* @legendre.P_i
%53 = call i32 @mpf_swap(i32 %51, i32 %52)
%54 = load i32, i32* @legendre.P_i
%55 = load i32, i32* @legendre.a
%56 = load i32, i32* %7
%57 = add nsw i32 %56, 1
%58 = call i32 @mpf_div_ui(i32 %54, i32 %55, i32 %57)
br label %59
59: ; preds = %32
%60 = load i32, i32* %7
%61 = add nsw i32 %60, 1
store i32 %61, i32* %7
br label %28
62: ; preds = %28
%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.P_i
br label %71
69: ; preds = %62
%70 = load i32, i32* @legendre.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.init = internal unnamed_addr global i1 false
define void @legendre(i32 %0, i32 %1, i32 %2) {
%4 = load i1, i1* @legendre.init
br i1 %4, label %10, label %5
5: ; preds = %3
store i1 true, i1* @legendre.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 1)
%12 = tail call i32 @mpf_set(i32 0, i32 %2)
%13 = icmp sgt i32 %1, 1
br i1 %13, label %14, label %26
14: ; preds = %10, %14
%15 = phi i32 [ %23, %14 ], [ 1, %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 = tail call i32 @mpf_mul_ui(i32 0, i32 0, i32 %15)
%21 = tail call i32 @mpf_sub(i32 0, i32 0, i32 0)
%22 = tail call i32 @mpf_swap(i32 0, i32 0)
%23 = add nuw nsw i32 %15, 1
%24 = tail call i32 @mpf_div_ui(i32 0, i32 0, i32 %23)
%25 = icmp eq i32 %23, %1
br i1 %25, label %26, label %14
26: ; preds = %14, %10
%27 = tail call i32 @mpf_set(i32 %0, i32 0)
ret void
}