#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 fabs(double);
double floor(double const);
void f2rat(const double orig, const int bound, int *den, int *num) {
  double intpart = floor(orig);
  double floatpart = orig - intpart;
  double temp;
  double h2 = 0;
  double h1 = 1;
  double k2 = 1;
  double k1 = 0;
  double h0 = intpart * h1 + h2;
  double k0 = intpart * k1 + k2;
  while (k0 <= bound) {
    if (fabs(floatpart) < 1e-10)
      break;
    temp = 1.0 / floatpart;
    intpart = floor(temp);
    floatpart = temp - intpart;
    h2 = h1;
    h1 = h0;
    k2 = k1;
    k1 = k0;
    h0 = intpart * h1 + h2;
    k0 = intpart * k1 + k2;
  }
  *num = k0;
  *den = h0;
}


x86 assembly (clang, -O0):
<f2rat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1040 <floor@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x8(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  0xe9a(%rip),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  0xe8d(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm2
movsd  -0x40(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm2
movsd  -0x50(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x68(%rbp)
movsd  -0x68(%rbp),%xmm1
cvtsi2sdl -0xc(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     1286 <f2rat+0x166>
movsd  -0x30(%rbp),%xmm0
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm1
movsd  0xe26(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    11f1 <f2rat+0xd1>
jmp    1286 <f2rat+0x166>
movsd  0xe07(%rip),%xmm0
divsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1040 <floor@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x38(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x68(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm2
movsd  -0x40(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm2
movsd  -0x50(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x68(%rbp)
jmp    11b8 <f2rat+0x98>
cvttsd2si -0x68(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cvttsd2si -0x60(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f2rat>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
movsd  %xmm0,(%rsp)
call   1040 <floor@plt>
movapd %xmm0,%xmm1
xorpd  %xmm3,%xmm3
movapd %xmm0,%xmm2
addsd  %xmm3,%xmm2
mulsd  %xmm3,%xmm0
addsd  0xeaa(%rip),%xmm0
cvtsi2sd %ebp,%xmm4
ucomisd %xmm0,%xmm4
jb     1208 <f2rat+0xe8>
movsd  (%rsp),%xmm5
subsd  %xmm1,%xmm5
movsd  0xe8b(%rip),%xmm1
movsd  %xmm4,0x18(%rsp)
nopl   0x0(%rax,%rax,1)
movsd  %xmm1,0x20(%rsp)
movsd  %xmm3,0x28(%rsp)
movsd  %xmm2,0x10(%rsp)
movsd  %xmm0,0x8(%rsp)
movsd  %xmm5,(%rsp)
movapd %xmm5,%xmm0
call   1030 <fabs@plt>
test   %eax,%eax
jle    11fc <f2rat+0xdc>
movsd  0xe4e(%rip),%xmm0
divsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
call   1040 <floor@plt>
movsd  (%rsp),%xmm5
subsd  %xmm0,%xmm5
movapd %xmm0,%xmm2
movsd  0x10(%rsp),%xmm1
mulsd  %xmm1,%xmm2
addsd  0x20(%rsp),%xmm2
movsd  0x8(%rsp),%xmm3
mulsd  %xmm3,%xmm0
addsd  0x28(%rsp),%xmm0
movsd  0x18(%rsp),%xmm4
ucomisd %xmm0,%xmm4
jae    1180 <f2rat+0x60>
jmp    1208 <f2rat+0xe8>
movsd  0x10(%rsp),%xmm2
movsd  0x8(%rsp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,(%rbx)
cvttsd2si %xmm2,%eax
mov    %eax,(%r14)
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @f2rat(double %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca double
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
%17 = alloca double
store double %0, double* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%18 = load double, double* %5
%19 = call double @llvm.floor.f64(double %18)
store double %19, double* %9
%20 = load double, double* %5
%21 = load double, double* %9
%22 = fsub double %20, %21
store double %22, double* %10
store double 0.000000e+00, double* %12
store double 1.000000e+00, double* %13
store double 1.000000e+00, double* %14
store double 0.000000e+00, double* %15
%23 = load double, double* %9
%24 = load double, double* %13
%25 = load double, double* %12
%26 = call double @llvm.fmuladd.f64(double %23, double %24, double %25)
store double %26, double* %16
%27 = load double, double* %9
%28 = load double, double* %15
%29 = load double, double* %14
%30 = call double @llvm.fmuladd.f64(double %27, double %28, double %29)
store double %30, double* %17
br label %31
31: ; preds = %42, %4
%32 = load double, double* %17
%33 = load i32, i32* %6
%34 = sitofp i32 %33 to double
%35 = fcmp ole double %32, %34
br i1 %35, label %36, label %62
36: ; preds = %31
%37 = load double, double* %10
%38 = call i32 @fabs(double %37)
%39 = sitofp i32 %38 to double
%40 = fcmp olt double %39, 1.000000e-10
br i1 %40, label %41, label %42
41: ; preds = %36
br label %62
42: ; preds = %36
%43 = load double, double* %10
%44 = fdiv double 1.000000e+00, %43
store double %44, double* %11
%45 = load double, double* %11
%46 = call double @llvm.floor.f64(double %45)
store double %46, double* %9
%47 = load double, double* %11
%48 = load double, double* %9
%49 = fsub double %47, %48
store double %49, double* %10
%50 = load double, double* %13
store double %50, double* %12
%51 = load double, double* %16
store double %51, double* %13
%52 = load double, double* %15
store double %52, double* %14
%53 = load double, double* %17
store double %53, double* %15
%54 = load double, double* %9
%55 = load double, double* %13
%56 = load double, double* %12
%57 = call double @llvm.fmuladd.f64(double %54, double %55, double %56)
store double %57, double* %16
%58 = load double, double* %9
%59 = load double, double* %15
%60 = load double, double* %14
%61 = call double @llvm.fmuladd.f64(double %58, double %59, double %60)
store double %61, double* %17
br label %31
62: ; preds = %41, %31
%63 = load double, double* %17
%64 = fptosi double %63 to i32
%65 = load i32*, i32** %8
store i32 %64, i32* %65
%66 = load double, double* %16
%67 = fptosi double %66 to i32
%68 = load i32*, i32** %7
store i32 %67, i32* %68
ret void
}

LLVM IR (-O2):
define void @f2rat(double %0, i32 %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = tail call double @llvm.floor.f64(double %0)
%6 = fadd double %5, 0.000000e+00
%7 = tail call double @llvm.fmuladd.f64(double %5, double 0.000000e+00, double 1.000000e+00)
%8 = sitofp i32 %1 to double
%9 = fcmp ugt double %7, %8
br i1 %9, label %27, label %10
10: ; preds = %4
%11 = fsub double %0, %5
br label %12
12: ; preds = %10, %20
%13 = phi double [ %25, %20 ], [ %7, %10 ]
%14 = phi double [ %24, %20 ], [ %6, %10 ]
%15 = phi double [ %13, %20 ], [ 0.000000e+00, %10 ]
%16 = phi double [ %14, %20 ], [ 1.000000e+00, %10 ]
%17 = phi double [ %23, %20 ], [ %11, %10 ]
%18 = tail call i32 @fabs(double %17)
%19 = icmp slt i32 %18, 1
br i1 %19, label %27, label %20
20: ; preds = %12
%21 = fdiv double 1.000000e+00, %17
%22 = tail call double @llvm.floor.f64(double %21)
%23 = fsub double %21, %22
%24 = tail call double @llvm.fmuladd.f64(double %22, double %14, double %16)
%25 = tail call double @llvm.fmuladd.f64(double %22, double %13, double %15)
%26 = fcmp ugt double %25, %8
br i1 %26, label %27, label %12
27: ; preds = %20, %12, %4
%28 = phi double [ %6, %4 ], [ %14, %12 ], [ %24, %20 ]
%29 = phi double [ %7, %4 ], [ %13, %12 ], [ %25, %20 ]
%30 = fptosi double %29 to i32
store i32 %30, i32* %3
%31 = fptosi double %28 to i32
store i32 %31, i32* %2
ret void
}