#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
double legend(double x, int n) {
  double p0, p1, p2, w, wn;
  if (n < 0) {
    fprintf(stderr, "Error : n < 0  in legend()\n");
    return 0.;
  }
  if (n == 0)
    return 1.;
  if (n == 1)
    return x;
  wn = (double)n;
  p0 = 1.;
  p1 = x;
  do {
    wn -= 1.;
    w = wn / (wn + 1.);
    p2 = x * p1 + w * (x * p1 - p0);
    p0 = p1;
    p1 = p2;
  } while (wn >= 2.);
  return p2;
}


x86 assembly (clang, -O0):
<legend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    114c <legend+0x3c>
mov    0x2eaf(%rip),%rax
mov    (%rax),%edi
lea    0xed6(%rip),%rsi
call   1030 <fprintf@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1256 <legend+0x146>
cmpl   $0x0,-0x14(%rbp)
jne    1168 <legend+0x58>
movsd  0xea2(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1256 <legend+0x146>
cmpl   $0x1,-0x14(%rbp)
jne    1181 <legend+0x71>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1256 <legend+0x146>
cvtsi2sdl -0x14(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  0xe6d(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  0xe51(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  0xe3b(%rip),%xmm1
addsd  -0x40(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
movsd  -0x38(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm3
movsd  -0x28(%rbp),%xmm5
movsd  -0x20(%rbp),%xmm4
movq   %xmm4,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm4
mulsd  %xmm5,%xmm3
addsd  %xmm4,%xmm3
mulsd  %xmm3,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  0xdc6(%rip),%xmm1
ucomisd %xmm1,%xmm0
jae    11a2 <legend+0x92>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<legend>:
push   %rax
test   %edi,%edi
js     1125 <legend+0x15>
je     1140 <legend+0x30>
cmp    $0x1,%edi
jne    114a <legend+0x3a>
movapd %xmm0,%xmm1
jmp    11ac <legend+0x9c>
mov    0x2eb4(%rip),%rax
mov    (%rax),%edi
lea    0xee3(%rip),%rsi
call   1030 <fprintf@plt>
xorpd  %xmm1,%xmm1
jmp    11ac <legend+0x9c>
movsd  0xeb8(%rip),%xmm1
jmp    11ac <legend+0x9c>
cvtsi2sd %edi,%xmm2
movsd  0xeaa(%rip),%xmm3
movsd  0xea9(%rip),%xmm8
movsd  0xea8(%rip),%xmm9
movapd %xmm0,%xmm1
movapd %xmm3,%xmm6
movapd %xmm1,%xmm7
addsd  %xmm8,%xmm2
movapd %xmm2,%xmm1
addsd  %xmm3,%xmm1
movapd %xmm2,%xmm4
divsd  %xmm1,%xmm4
movapd %xmm0,%xmm5
mulsd  %xmm7,%xmm5
movapd %xmm5,%xmm1
subsd  %xmm6,%xmm1
mulsd  %xmm4,%xmm1
addsd  %xmm5,%xmm1
ucomisd %xmm9,%xmm2
movapd %xmm7,%xmm6
jae    1170 <legend+0x60>
movapd %xmm1,%xmm0
pop    %rax
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Error : n < 0 in legend()\0A\00"
define double @legend(double %0, i32 %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca i32
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
store double %0, double* %4
store i32 %1, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store double 0.000000e+00, double* %3
br label %53
16: ; preds = %2
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store double 1.000000e+00, double* %3
br label %53
20: ; preds = %16
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = load double, double* %4
store double %24, double* %3
br label %53
25: ; preds = %20
%26 = load i32, i32* %5
%27 = sitofp i32 %26 to double
store double %27, double* %10
store double 1.000000e+00, double* %6
%28 = load double, double* %4
store double %28, double* %7
br label %29
29: ; preds = %48, %25
%30 = load double, double* %10
%31 = fsub double %30, 1.000000e+00
store double %31, double* %10
%32 = load double, double* %10
%33 = load double, double* %10
%34 = fadd double %33, 1.000000e+00
%35 = fdiv double %32, %34
store double %35, double* %9
%36 = load double, double* %4
%37 = load double, double* %7
%38 = load double, double* %9
%39 = load double, double* %4
%40 = load double, double* %7
%41 = load double, double* %6
%42 = fneg double %41
%43 = call double @llvm.fmuladd.f64(double %39, double %40, double %42)
%44 = fmul double %38, %43
%45 = call double @llvm.fmuladd.f64(double %36, double %37, double %44)
store double %45, double* %8
%46 = load double, double* %7
store double %46, double* %6
%47 = load double, double* %8
store double %47, double* %7
br label %48
48: ; preds = %29
%49 = load double, double* %10
%50 = fcmp oge double %49, 2.000000e+00
br i1 %50, label %29, label %51
51: ; preds = %48
%52 = load double, double* %8
store double %52, double* %3
br label %53
53: ; preds = %51, %23, %19, %13
%54 = load double, double* %3
ret double %54
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Error : n < 0 in legend()\0A\00"
define double @legend(double %0, i32 %1) {
%3 = icmp slt i32 %1, 0
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %23
7: ; preds = %2
switch i32 %1, label %9 [
i32 0, label %23
i32 1, label %8
]
8: ; preds = %7
br label %23
9: ; preds = %7
%10 = sitofp i32 %1 to double
br label %11
11: ; preds = %11, %9
%12 = phi double [ %0, %9 ], [ %21, %11 ]
%13 = phi double [ 1.000000e+00, %9 ], [ %12, %11 ]
%14 = phi double [ %10, %9 ], [ %15, %11 ]
%15 = fadd double %14, -1.000000e+00
%16 = fadd double %15, 1.000000e+00
%17 = fdiv double %15, %16
%18 = fneg double %13
%19 = tail call double @llvm.fmuladd.f64(double %0, double %12, double %18)
%20 = fmul double %19, %17
%21 = tail call double @llvm.fmuladd.f64(double %0, double %12, double %20)
%22 = fcmp ult double %15, 2.000000e+00
br i1 %22, label %23, label %11
23: ; preds = %11, %7, %8, %4
%24 = phi double [ 0.000000e+00, %4 ], [ %0, %8 ], [ 1.000000e+00, %7 ], [ %21, %11 ]
ret double %24
}