#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);
unsigned int gcd(unsigned int, unsigned int);
int /*<<< orphan*/ printf(char *, double, long, long, long);
void double2rational(double n) {
  double whole = floor(n);
  double decimal = n - whole;
  if (decimal == 0.0)
    decimal = 1.0;
  double base = 10.0, multiplier = 1.0, tolerance = 0.0001;
  while (decimal - floor(decimal) >= tolerance) {
    decimal *= base;
    multiplier *= base;
  }
  unsigned factor = gcd((unsigned)decimal, (unsigned)multiplier);
  decimal /= (double)factor;
  multiplier /= (double)factor;
  printf("rational fraction of %.8f = %ld %ld/%ld\n", n, (long)whole,
         (long)decimal, (long)multiplier);
}


x86 assembly (clang, -O0):
<double2rational>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1050 <floor@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x8(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1180 <double2rational+0x50>
jp     1180 <double2rational+0x50>
movsd  0xe85(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xe88(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xe6b(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe66(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1050 <floor@plt>
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
subsd  %xmm1,%xmm0
ucomisd -0x30(%rbp),%xmm0
jb     11f5 <double2rational+0xc5>
movsd  -0x20(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    11a7 <double2rational+0x77>
cvttsd2si -0x18(%rbp),%rax
mov    %eax,%edi
cvttsd2si -0x28(%rbp),%rax
mov    %eax,%esi
call   1040 <gcd@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cvtsi2sd %rax,%xmm1
movsd  -0x18(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x34(%rbp),%eax
cvtsi2sd %rax,%xmm1
movsd  -0x28(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x8(%rbp),%xmm0
cvttsd2si -0x10(%rbp),%rsi
cvttsd2si -0x18(%rbp),%rdx
cvttsd2si -0x28(%rbp),%rcx
lea    0xdc1(%rip),%rdi
call   1030 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<double2rational>:
sub    $0x38,%rsp
movsd  %xmm0,0x18(%rsp)
call   1050 <floor@plt>
movsd  0x18(%rsp),%xmm2
movsd  %xmm0,0x30(%rsp)
subsd  %xmm0,%xmm2
xorpd  %xmm0,%xmm0
cmpeqsd %xmm2,%xmm0
movapd %xmm0,%xmm1
andnpd %xmm2,%xmm1
movsd  0xe98(%rip),%xmm2
andpd  %xmm2,%xmm0
orpd   %xmm1,%xmm0
movapd %xmm0,(%rsp)
call   1050 <floor@plt>
movapd (%rsp),%xmm2
movapd %xmm2,%xmm1
subsd  %xmm0,%xmm1
movsd  0xe71(%rip),%xmm0
ucomisd 0xe71(%rip),%xmm1
jb     11e1 <double2rational+0xb1>
nopl   0x0(%rax)
movsd  0xe68(%rip),%xmm1
mulsd  %xmm1,%xmm2
movapd %xmm2,(%rsp)
mulsd  %xmm1,%xmm0
movapd %xmm0,0x20(%rsp)
movapd %xmm2,%xmm0
call   1050 <floor@plt>
movapd (%rsp),%xmm2
movapd %xmm2,%xmm1
subsd  %xmm0,%xmm1
movapd 0x20(%rsp),%xmm0
ucomisd 0xe29(%rip),%xmm1
jae    11a0 <double2rational+0x70>
movapd %xmm2,(%rsp)
movapd %xmm0,0x20(%rsp)
cvttsd2si %xmm2,%rdi
cvttsd2si %xmm0,%rsi
call   1040 <gcd@plt>
mov    %eax,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movapd (%rsp),%xmm1
divsd  %xmm0,%xmm1
cvttsd2si 0x30(%rsp),%rsi
cvttsd2si %xmm1,%rdx
movapd 0x20(%rsp),%xmm1
divsd  %xmm0,%xmm1
cvttsd2si %xmm1,%rcx
lea    0xde8(%rip),%rdi
movsd  0x18(%rsp),%xmm0
add    $0x38,%rsp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"rational fraction of %.8f = %ld %ld/%ld\0A\00"
define void @double2rational(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i32
store double %0, double* %2
%9 = load double, double* %2
%10 = call double @llvm.floor.f64(double %9)
store double %10, double* %3
%11 = load double, double* %2
%12 = load double, double* %3
%13 = fsub double %11, %12
store double %13, double* %4
%14 = load double, double* %4
%15 = fcmp oeq double %14, 0.000000e+00
br i1 %15, label %16, label %17
16: ; preds = %1
store double 1.000000e+00, double* %4
br label %17
17: ; preds = %16, %1
store double 1.000000e+01, double* %5
store double 1.000000e+00, double* %6
store double 1.000000e-04, double* %7
br label %18
18: ; preds = %25, %17
%19 = load double, double* %4
%20 = load double, double* %4
%21 = call double @llvm.floor.f64(double %20)
%22 = fsub double %19, %21
%23 = load double, double* %7
%24 = fcmp oge double %22, %23
br i1 %24, label %25, label %32
25: ; preds = %18
%26 = load double, double* %5
%27 = load double, double* %4
%28 = fmul double %27, %26
store double %28, double* %4
%29 = load double, double* %5
%30 = load double, double* %6
%31 = fmul double %30, %29
store double %31, double* %6
br label %18
32: ; preds = %18
%33 = load double, double* %4
%34 = fptoui double %33 to i32
%35 = load double, double* %6
%36 = fptoui double %35 to i32
%37 = call i32 @gcd(i32 %34, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = uitofp i32 %38 to double
%40 = load double, double* %4
%41 = fdiv double %40, %39
store double %41, double* %4
%42 = load i32, i32* %8
%43 = uitofp i32 %42 to double
%44 = load double, double* %6
%45 = fdiv double %44, %43
store double %45, double* %6
%46 = load double, double* %2
%47 = load double, double* %3
%48 = fptosi double %47 to i64
%49 = load double, double* %4
%50 = fptosi double %49 to i64
%51 = load double, double* %6
%52 = fptosi double %51 to i64
%53 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), double %46, i64 %48, i64 %50, i64 %52)
ret void
}

LLVM IR (-O2):
@.str = [41 x i8] c"rational fraction of %.8f = %ld %ld/%ld\0A\00"
define void @double2rational(double %0) {
%2 = tail call double @llvm.floor.f64(double %0)
%3 = fsub double %0, %2
%4 = fcmp oeq double %3, 0.000000e+00
%5 = select i1 %4, double 1.000000e+00, double %3
%6 = tail call double @llvm.floor.f64(double %5)
%7 = fsub double %5, %6
%8 = fcmp ult double %7, 1.000000e-04
br i1 %8, label %17, label %9
9: ; preds = %1, %9
%10 = phi double [ %12, %9 ], [ %5, %1 ]
%11 = phi double [ %13, %9 ], [ 1.000000e+00, %1 ]
%12 = fmul double %10, 1.000000e+01
%13 = fmul double %11, 1.000000e+01
%14 = tail call double @llvm.floor.f64(double %12)
%15 = fsub double %12, %14
%16 = fcmp ult double %15, 1.000000e-04
br i1 %16, label %17, label %9
17: ; preds = %9, %1
%18 = phi double [ 1.000000e+00, %1 ], [ %13, %9 ]
%19 = phi double [ %5, %1 ], [ %12, %9 ]
%20 = fptoui double %19 to i32
%21 = fptoui double %18 to i32
%22 = tail call i32 @gcd(i32 %20, i32 %21)
%23 = uitofp i32 %22 to double
%24 = fdiv double %19, %23
%25 = fdiv double %18, %23
%26 = fptosi double %2 to i64
%27 = fptosi double %24 to i64
%28 = fptosi double %25 to i64
%29 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), double %0, i64 %26, i64 %27, i64 %28)
ret void
}