#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 */
double floor(double);
double sqrt(int);
int sqrt_period(int n) {
  double a = floor(sqrt(n));
  double a0 = a;
  double m = 0.0;
  double d = 1.0;
  int period = 0;
  while (a != a0 * 2) {
    period++;
    m = d * a - m;
    d = (n - m * m) / d;
    a = floor((a0 + m) / d);
  }
  return period;
}


x86 assembly (clang, -O0):
<sqrt_period>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <sqrt@plt>
call   1040 <floor@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xea9(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe98(%rip),%xmm1
mulsd  -0x18(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jne    118a <sqrt_period+0x6a>
jp     118a <sqrt_period+0x6a>
jmp    121c <sqrt_period+0xfc>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
cvtsi2sdl -0x4(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
divsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
addsd  -0x20(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
call   1040 <floor@plt>
movsd  %xmm0,-0x10(%rbp)
jmp    1163 <sqrt_period+0x43>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqrt_period>:
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebx
call   1030 <sqrt@plt>
call   1040 <floor@plt>
movapd %xmm0,%xmm3
addsd  %xmm0,%xmm3
movsd  %xmm0,0x8(%rsp)
ucomisd %xmm3,%xmm0
jne    114b <sqrt_period+0x2b>
jnp    11dc <sqrt_period+0xbc>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
movsd  %xmm0,0x10(%rsp)
xorpd  %xmm2,%xmm2
movsd  0xe9c(%rip),%xmm1
xor    %ebx,%ebx
movsd  0x8(%rsp),%xmm0
movsd  %xmm3,0x18(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mulsd  %xmm1,%xmm0
subsd  %xmm2,%xmm0
movapd %xmm0,%xmm2
movsd  %xmm0,0x28(%rsp)
mulsd  %xmm0,%xmm0
movsd  0x10(%rsp),%xmm3
subsd  %xmm0,%xmm3
divsd  %xmm1,%xmm3
movsd  %xmm3,0x20(%rsp)
movsd  0x8(%rsp),%xmm0
addsd  %xmm2,%xmm0
divsd  %xmm3,%xmm0
add    $0x1,%ebx
call   1040 <floor@plt>
movsd  0x18(%rsp),%xmm3
movsd  0x28(%rsp),%xmm2
ucomisd %xmm3,%xmm0
movsd  0x20(%rsp),%xmm1
jne    1180 <sqrt_period+0x60>
jp     1180 <sqrt_period+0x60>
jmp    11de <sqrt_period+0xbe>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @sqrt_period(i32 %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = call double @sqrt(i32 %8)
%10 = call double @llvm.floor.f64(double %9)
store double %10, double* %3
%11 = load double, double* %3
store double %11, double* %4
store double 0.000000e+00, double* %5
store double 1.000000e+00, double* %6
store i32 0, i32* %7
br label %12
12: ; preds = %17, %1
%13 = load double, double* %3
%14 = load double, double* %4
%15 = fmul double %14, 2.000000e+00
%16 = fcmp une double %13, %15
br i1 %16, label %17, label %39
17: ; preds = %12
%18 = load i32, i32* %7
%19 = add nsw i32 %18, 1
store i32 %19, i32* %7
%20 = load double, double* %6
%21 = load double, double* %3
%22 = load double, double* %5
%23 = fneg double %22
%24 = call double @llvm.fmuladd.f64(double %20, double %21, double %23)
store double %24, double* %5
%25 = load i32, i32* %2
%26 = sitofp i32 %25 to double
%27 = load double, double* %5
%28 = load double, double* %5
%29 = fneg double %27
%30 = call double @llvm.fmuladd.f64(double %29, double %28, double %26)
%31 = load double, double* %6
%32 = fdiv double %30, %31
store double %32, double* %6
%33 = load double, double* %4
%34 = load double, double* %5
%35 = fadd double %33, %34
%36 = load double, double* %6
%37 = fdiv double %35, %36
%38 = call double @llvm.floor.f64(double %37)
store double %38, double* %3
br label %12
39: ; preds = %12
%40 = load i32, i32* %7
ret i32 %40
}

LLVM IR (-O2):
define i32 @sqrt_period(i32 %0) {
%2 = tail call double @sqrt(i32 %0)
%3 = tail call double @llvm.floor.f64(double %2)
%4 = fmul double %3, 2.000000e+00
%5 = fcmp une double %3, %4
br i1 %5, label %6, label %23
6: ; preds = %1
%7 = sitofp i32 %0 to double
br label %8
8: ; preds = %6, %8
%9 = phi i32 [ 0, %6 ], [ %13, %8 ]
%10 = phi double [ 1.000000e+00, %6 ], [ %18, %8 ]
%11 = phi double [ 0.000000e+00, %6 ], [ %15, %8 ]
%12 = phi double [ %3, %6 ], [ %21, %8 ]
%13 = add nuw nsw i32 %9, 1
%14 = fneg double %11
%15 = tail call double @llvm.fmuladd.f64(double %10, double %12, double %14)
%16 = fneg double %15
%17 = tail call double @llvm.fmuladd.f64(double %16, double %15, double %7)
%18 = fdiv double %17, %10
%19 = fadd double %3, %15
%20 = fdiv double %19, %18
%21 = tail call double @llvm.floor.f64(double %20)
%22 = fcmp une double %21, %4
br i1 %22, label %8, label %23
23: ; preds = %8, %1
%24 = phi i32 [ 0, %1 ], [ %13, %8 ]
ret i32 %24
}