#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*/ fabs(double);
int /*<<< orphan*/ predict(double, double, double);
int /*<<< orphan*/ printf(char *, ...);
void test(double r, size_t n, double (*measure)(void *), void *ud) {
  double a, x;
  size_t i;
  printf("iters %zu\n", n);
  x = measure(ud);
  for (i = 1; i <= n; i++) {
    printf("%zu r %.2f x %.2f err %.3f\n", i, r, x, fabs(r - x));
    a = 1.0 / (i + 1);
    x = predict(x, a, measure(ud));
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xedc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rdi
call   *%rax
movsd  %xmm0,-0x30(%rbp)
movq   $0x1,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
ja     124d <test+0x11d>
mov    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x30(%rbp),%xmm1
movsd  %xmm1,-0x50(%rbp)
subsd  %xmm1,%xmm0
call   1040 <fabs@plt>
mov    -0x60(%rbp),%rsi
movsd  -0x58(%rbp),%xmm0
movsd  -0x50(%rbp),%xmm1
mov    %eax,%edx
lea    0xe7f(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
movq   %rax,%xmm1
movaps 0xe39(%rip),%xmm0
punpckldq %xmm0,%xmm1
movapd 0xe3d(%rip),%xmm0
subpd  %xmm0,%xmm1
movaps %xmm1,%xmm0
unpckhpd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe06(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rdi
call   *%rax
movsd  -0x48(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x40(%rbp),%xmm0
call   1050 <predict@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1172 <test+0x42>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
movsd  %xmm0,0x8(%rsp)
lea    0xec5(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
call   *%r15
test   %r12,%r12
je     1222 <test+0xe2>
movapd %xmm0,%xmm1
mov    $0x1,%ebx
lea    0xea7(%rip),%r13
nopl   0x0(%rax)
movsd  %xmm1,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
subsd  %xmm1,%xmm0
call   1050 <fabs@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
movsd  0x8(%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
mov    %eax,%edx
mov    $0x2,%al
call   1040 <printf@plt>
add    $0x1,%rbx
movq   %rbx,%xmm0
punpckldq 0xe2f(%rip),%xmm0
subpd  0xe37(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe33(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,0x18(%rsp)
mov    %r14,%rdi
call   *%r15
movapd %xmm0,%xmm2
movsd  0x10(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
call   1060 <predict@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
cmp    %r12,%rbx
jbe    1190 <test+0x50>
mov    $0xa,%edi
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"iters %zu\0A\00"
@.str.1 = [28 x i8] c"%zu r %.2f x %.2f err %.3f\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define void @test(double %0, i64 %1, double (i8*)* %2, i8* %3) {
%5 = alloca double
%6 = alloca i64
%7 = alloca double (i8*)*
%8 = alloca i8*
%9 = alloca double
%10 = alloca double
%11 = alloca i64
store double %0, double* %5
store i64 %1, i64* %6
store double (i8*)* %2, double (i8*)** %7
store i8* %3, i8** %8
%12 = load i64, i64* %6
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %12)
%14 = load double (i8*)*, double (i8*)** %7
%15 = load i8*, i8** %8
%16 = call double %14(i8* %15)
store double %16, double* %10
store i64 1, i64* %11
br label %17
17: ; preds = %41, %4
%18 = load i64, i64* %11
%19 = load i64, i64* %6
%20 = icmp ule i64 %18, %19
br i1 %20, label %21, label %44
21: ; preds = %17
%22 = load i64, i64* %11
%23 = load double, double* %5
%24 = load double, double* %10
%25 = load double, double* %5
%26 = load double, double* %10
%27 = fsub double %25, %26
%28 = call i32 @fabs(double %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %22, double %23, double %24, i32 %28)
%30 = load i64, i64* %11
%31 = add i64 %30, 1
%32 = uitofp i64 %31 to double
%33 = fdiv double 1.000000e+00, %32
store double %33, double* %9
%34 = load double, double* %10
%35 = load double, double* %9
%36 = load double (i8*)*, double (i8*)** %7
%37 = load i8*, i8** %8
%38 = call double %36(i8* %37)
%39 = call i32 @predict(double %34, double %35, double %38)
%40 = sitofp i32 %39 to double
store double %40, double* %10
br label %41
41: ; preds = %21
%42 = load i64, i64* %11
%43 = add i64 %42, 1
store i64 %43, i64* %11
br label %17
44: ; preds = %17
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"iters %zu\0A\00"
@.str.1 = [28 x i8] c"%zu r %.2f x %.2f err %.3f\0A\00"
define void @test(double %0, i64 %1, double (i8*)* nocapture %2, i8* %3) {
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %1)
%6 = tail call double %2(i8* %3)
%7 = icmp eq i64 %1, 0
br i1 %7, label %21, label %8
8: ; preds = %4, %8
%9 = phi i64 [ %14, %8 ], [ 1, %4 ]
%10 = phi double [ %19, %8 ], [ %6, %4 ]
%11 = fsub double %0, %10
%12 = tail call i32 @fabs(double %11)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %9, double %0, double %10, i32 %12)
%14 = add i64 %9, 1
%15 = uitofp i64 %14 to double
%16 = fdiv double 1.000000e+00, %15
%17 = tail call double %2(i8* %3)
%18 = tail call i32 @predict(double %10, double %16, double %17)
%19 = sitofp i32 %18 to double
%20 = icmp ugt i64 %14, %1
br i1 %20, label %21, label %8
21: ; preds = %8, %4
%22 = tail call i32 @putchar(i32 10)
ret void
}