#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 */
typedef int /*<<< orphan*/ mpq_t;

/* Variables and functions */
double DBL_MAX;
int abs(int);
int /*<<< orphan*/ assert(int);
double fabs(double);
double floor(double);
int /*<<< orphan*/ fp2rat(double, double, double *, double *);
double frexp(double, int *);
int /*<<< orphan*/ mpq_add(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpq_clear(int /*<<< orphan*/);
int /*<<< orphan*/ mpq_div(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
double mpq_get_d(int /*<<< orphan*/);
int /*<<< orphan*/ mpq_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpq_mul(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpq_neg(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpq_set_d(int /*<<< orphan*/, double);
int /*<<< orphan*/ mpq_set_si(int /*<<< orphan*/, int, int);
void set_d_eps(mpq_t x, double val) {
  int s, n, j;
  double f, p, q, eps = 1e-9;
  mpq_t temp;
  assert(-DBL_MAX <= val && val <= +DBL_MAX);
  if (val == floor(val)) {
    mpq_set_d(x, val);
    goto done;
  }
  if (val > 0.0)
    s = +1;
  else if (val < 0.0)
    s = -1;
  else {
    mpq_set_si(x, 0, 1);
    goto done;
  }
  f = frexp(fabs(val), &n);
  fp2rat(f, 0.1 * eps, &p, &q);
  mpq_init(temp);
  mpq_set_d(x, p);
  mpq_set_d(temp, q);
  mpq_div(x, x, temp);
  mpq_set_si(temp, 1, 1);
  for (j = 1; j <= abs(n); j++)
    mpq_add(temp, temp, temp);
  if (n > 0)
    mpq_mul(x, x, temp);
  else if (n < 0)
    mpq_div(x, x, temp);
  mpq_clear(temp);
  if (s < 0)
    mpq_neg(x, x);
  assert(fabs(val - mpq_get_d(x)) <= eps * (1.0 + fabs(val)));
done:
  return;
}


x86 assembly (clang, -O0):
<set_d_eps>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  0xe08(%rip),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    0x2ddc(%rip),%rax
movsd  (%rax),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
movsd  -0x10(%rbp),%xmm0
xor    %eax,%eax
ucomisd %xmm1,%xmm0
mov    %al,-0x45(%rbp)
jb     124d <set_d_eps+0x6d>
movsd  -0x10(%rbp),%xmm1
mov    0x2da1(%rip),%rax
movsd  (%rax),%xmm0
ucomisd %xmm1,%xmm0
setae  %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1100 <floor@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jne    1298 <set_d_eps+0xb8>
jp     1298 <set_d_eps+0xb8>
mov    -0x4(%rbp),%edi
movsd  -0x10(%rbp),%xmm0
call   1030 <mpq_set_d@plt>
jmp    1456 <set_d_eps+0x276>
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    12b6 <set_d_eps+0xd6>
movl   $0x1,-0x14(%rbp)
jmp    12e9 <set_d_eps+0x109>
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    12d0 <set_d_eps+0xf0>
movl   $0xffffffff,-0x14(%rbp)
jmp    12e4 <set_d_eps+0x104>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
mov    $0x1,%edx
call   1090 <mpq_set_si@plt>
jmp    1456 <set_d_eps+0x276>
jmp    12e9 <set_d_eps+0x109>
movsd  -0x10(%rbp),%xmm0
movaps 0xd2b(%rip),%xmm1
pand   %xmm1,%xmm0
lea    -0x18(%rbp),%rdi
call   1060 <frexp@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xcf4(%rip),%xmm1
mulsd  -0x40(%rbp),%xmm1
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <fp2rat@plt>
mov    -0x44(%rbp),%edi
call   1080 <mpq_init@plt>
mov    -0x4(%rbp),%edi
movsd  -0x30(%rbp),%xmm0
call   1030 <mpq_set_d@plt>
mov    -0x44(%rbp),%edi
movsd  -0x38(%rbp),%xmm0
call   1030 <mpq_set_d@plt>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x44(%rbp),%edx
call   10f0 <mpq_div@plt>
mov    -0x44(%rbp),%edi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <mpq_set_si@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <abs@plt>
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
cmp    %ecx,%eax
jg     13a3 <set_d_eps+0x1c3>
mov    -0x44(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    -0x44(%rbp),%edx
call   10c0 <mpq_add@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    136c <set_d_eps+0x18c>
cmpl   $0x0,-0x18(%rbp)
jle    13c0 <set_d_eps+0x1e0>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x44(%rbp),%edx
call   10b0 <mpq_mul@plt>
jmp    13dd <set_d_eps+0x1fd>
cmpl   $0x0,-0x18(%rbp)
jge    13d8 <set_d_eps+0x1f8>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x44(%rbp),%edx
call   10f0 <mpq_div@plt>
jmp    13dd <set_d_eps+0x1fd>
mov    -0x44(%rbp),%edi
call   1050 <mpq_clear@plt>
cmpl   $0x0,-0x14(%rbp)
jge    13fa <set_d_eps+0x21a>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
call   10e0 <mpq_neg@plt>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x4(%rbp),%edi
call   10d0 <mpq_get_d@plt>
movsd  -0x60(%rbp),%xmm1
subsd  %xmm0,%xmm1
movaps 0xc04(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm3
movaps 0xbef(%rip),%xmm2
pand   %xmm2,%xmm3
movsd  0xbd3(%rip),%xmm2
addsd  %xmm3,%xmm2
mulsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_d_eps>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
movapd %xmm0,0x10(%rsp)
mov    0x2dfb(%rip),%rax
movsd  (%rax),%xmm2
movapd 0xe0f(%rip),%xmm1
xorpd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
setae  %al
ucomisd %xmm0,%xmm2
mov    %edi,%ebx
setae  %cl
and    %al,%cl
movzbl %cl,%edi
call   1060 <assert@plt>
movapd 0x10(%rsp),%xmm0
call   10f0 <floor@plt>
movapd 0x10(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jne    123b <set_d_eps+0x6b>
jp     123b <set_d_eps+0x6b>
mov    %ebx,%edi
movapd %xmm1,%xmm0
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <mpq_set_d@plt>
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
je     12ea <set_d_eps+0x11a>
movapd 0xdbf(%rip),%xmm0
andpd  %xmm1,%xmm0
lea    0xc(%rsp),%rdi
movapd %xmm0,0x30(%rsp)
call   1050 <frexp@plt>
movsd  0xdb3(%rip),%xmm1
lea    0x28(%rsp),%rdi
lea    0x20(%rsp),%rsi
call   1090 <fp2rat@plt>
call   1070 <mpq_init@plt>
movsd  0x28(%rsp),%xmm0
mov    %ebx,%edi
call   1030 <mpq_set_d@plt>
movsd  0x20(%rsp),%xmm0
call   1030 <mpq_set_d@plt>
mov    %ebx,%edi
mov    %ebx,%esi
call   10e0 <mpq_div@plt>
mov    $0x1,%esi
mov    $0x1,%edx
call   1080 <mpq_set_si@plt>
mov    0xc(%rsp),%r14d
test   %r14d,%r14d
je     130b <set_d_eps+0x13b>
mov    %r14d,%ebp
neg    %ebp
cmovs  %r14d,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   10b0 <mpq_add@plt>
add    $0xffffffff,%ebp
jne    12d0 <set_d_eps+0x100>
test   %r14d,%r14d
jle    1300 <set_d_eps+0x130>
mov    %ebx,%edi
mov    %ebx,%esi
call   10a0 <mpq_mul@plt>
jmp    130b <set_d_eps+0x13b>
mov    %ebx,%edi
xor    %esi,%esi
mov    $0x1,%edx
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <mpq_set_si@plt>
jns    130b <set_d_eps+0x13b>
mov    %ebx,%edi
mov    %ebx,%esi
call   10e0 <mpq_div@plt>
call   1040 <mpq_clear@plt>
xorpd  %xmm0,%xmm0
movapd 0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
ja     1329 <set_d_eps+0x159>
mov    %ebx,%edi
mov    %ebx,%esi
call   10d0 <mpq_neg@plt>
mov    %ebx,%edi
call   10c0 <mpq_get_d@plt>
movapd 0x10(%rsp),%xmm1
subsd  %xmm0,%xmm1
andpd  0xcce(%rip),%xmm1
movapd 0x30(%rsp),%xmm0
addsd  0xcd8(%rip),%xmm0
mulsd  0xcd8(%rip),%xmm0
xor    %edi,%edi
ucomisd %xmm1,%xmm0
setae  %dil
call   1060 <assert@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@DBL_MAX = global double 0.000000e+00
define void @set_d_eps(i32 %0, double %1) {
%3 = alloca i32
%4 = alloca double
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca i32
store i32 %0, i32* %3
store double %1, double* %4
store double 1.000000e-09, double* %11
%13 = load double, double* @DBL_MAX
%14 = fneg double %13
%15 = load double, double* %4
%16 = fcmp ole double %14, %15
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load double, double* %4
%19 = load double, double* @DBL_MAX
%20 = fcmp ole double %18, %19
br label %21
21: ; preds = %17, %2
%22 = phi i1 [ false, %2 ], [ %20, %17 ]
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load double, double* %4
%26 = load double, double* %4
%27 = call double @llvm.floor.f64(double %26)
%28 = fcmp oeq double %25, %27
br i1 %28, label %29, label %33
29: ; preds = %21
%30 = load i32, i32* %3
%31 = load double, double* %4
%32 = call i32 @mpq_set_d(i32 %30, double %31)
br label %120
33: ; preds = %21
%34 = load double, double* %4
%35 = fcmp ogt double %34, 0.000000e+00
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 1, i32* %5
br label %45
37: ; preds = %33
%38 = load double, double* %4
%39 = fcmp olt double %38, 0.000000e+00
br i1 %39, label %40, label %41
40: ; preds = %37
store i32 -1, i32* %5
br label %44
41: ; preds = %37
%42 = load i32, i32* %3
%43 = call i32 @mpq_set_si(i32 %42, i32 0, i32 1)
br label %120
44: ; preds = %40
br label %45
45: ; preds = %44, %36
%46 = load double, double* %4
%47 = call double @llvm.fabs.f64(double %46)
%48 = call double @frexp(double %47, i32* %6)
store double %48, double* %8
%49 = load double, double* %8
%50 = load double, double* %11
%51 = fmul double 1.000000e-01, %50
%52 = call i32 @fp2rat(double %49, double %51, double* %9, double* %10)
%53 = load i32, i32* %12
%54 = call i32 @mpq_init(i32 %53)
%55 = load i32, i32* %3
%56 = load double, double* %9
%57 = call i32 @mpq_set_d(i32 %55, double %56)
%58 = load i32, i32* %12
%59 = load double, double* %10
%60 = call i32 @mpq_set_d(i32 %58, double %59)
%61 = load i32, i32* %3
%62 = load i32, i32* %3
%63 = load i32, i32* %12
%64 = call i32 @mpq_div(i32 %61, i32 %62, i32 %63)
%65 = load i32, i32* %12
%66 = call i32 @mpq_set_si(i32 %65, i32 1, i32 1)
store i32 1, i32* %7
br label %67
67: ; preds = %77, %45
%68 = load i32, i32* %7
%69 = load i32, i32* %6
%70 = call i32 @abs(i32 %69)
%71 = icmp sle i32 %68, %70
br i1 %71, label %72, label %80
72: ; preds = %67
%73 = load i32, i32* %12
%74 = load i32, i32* %12
%75 = load i32, i32* %12
%76 = call i32 @mpq_add(i32 %73, i32 %74, i32 %75)
br label %77
77: ; preds = %72
%78 = load i32, i32* %7
%79 = add nsw i32 %78, 1
store i32 %79, i32* %7
br label %67
80: ; preds = %67
%81 = load i32, i32* %6
%82 = icmp sgt i32 %81, 0
br i1 %82, label %83, label %88
83: ; preds = %80
%84 = load i32, i32* %3
%85 = load i32, i32* %3
%86 = load i32, i32* %12
%87 = call i32 @mpq_mul(i32 %84, i32 %85, i32 %86)
br label %97
88: ; preds = %80
%89 = load i32, i32* %6
%90 = icmp slt i32 %89, 0
br i1 %90, label %91, label %96
91: ; preds = %88
%92 = load i32, i32* %3
%93 = load i32, i32* %3
%94 = load i32, i32* %12
%95 = call i32 @mpq_div(i32 %92, i32 %93, i32 %94)
br label %96
96: ; preds = %91, %88
br label %97
97: ; preds = %96, %83
%98 = load i32, i32* %12
%99 = call i32 @mpq_clear(i32 %98)
%100 = load i32, i32* %5
%101 = icmp slt i32 %100, 0
br i1 %101, label %102, label %106
102: ; preds = %97
%103 = load i32, i32* %3
%104 = load i32, i32* %3
%105 = call i32 @mpq_neg(i32 %103, i32 %104)
br label %106
106: ; preds = %102, %97
%107 = load double, double* %4
%108 = load i32, i32* %3
%109 = call double @mpq_get_d(i32 %108)
%110 = fsub double %107, %109
%111 = call double @llvm.fabs.f64(double %110)
%112 = load double, double* %11
%113 = load double, double* %4
%114 = call double @llvm.fabs.f64(double %113)
%115 = fadd double 1.000000e+00, %114
%116 = fmul double %112, %115
%117 = fcmp ole double %111, %116
%118 = zext i1 %117 to i32
%119 = call i32 @assert(i32 %118)
br label %120
120: ; preds = %106, %41, %29
ret void
}

LLVM IR (-O2):
@DBL_MAX = global double 0.000000e+00
define void @set_d_eps(i32 %0, double %1) {
%3 = alloca i32
%4 = alloca double
%5 = alloca double
%6 = bitcast i32* %3 to i8*
%7 = bitcast double* %4 to i8*
%8 = bitcast double* %5 to i8*
%9 = load double, double* @DBL_MAX
%10 = fneg double %9
%11 = fcmp ole double %10, %1
%12 = fcmp oge double %9, %1
%13 = and i1 %11, %12
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call double @llvm.floor.f64(double %1)
%17 = fcmp oeq double %16, %1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = tail call i32 @mpq_set_d(i32 %0, double %1)
br label %66
20: ; preds = %2
%21 = fcmp ogt double %1, 0.000000e+00
%22 = fcmp ueq double %1, 0.000000e+00
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @mpq_set_si(i32 %0, i32 0, i32 1)
br label %66
25: ; preds = %20
%26 = tail call double @llvm.fabs.f64(double %1)
%27 = call double @frexp(double %26, i32* %3)
%28 = call i32 @fp2rat(double %27, double 0x3DDB7CDFD9D7BDBC, double* %4, double* %5)
%29 = call i32 @mpq_init(i32 undef)
%30 = load double, double* %4
%31 = call i32 @mpq_set_d(i32 %0, double %30)
%32 = load double, double* %5
%33 = call i32 @mpq_set_d(i32 undef, double %32)
%34 = call i32 @mpq_div(i32 %0, i32 %0, i32 undef)
%35 = call i32 @mpq_set_si(i32 undef, i32 1, i32 1)
%36 = load i32, i32* %3
%37 = icmp eq i32 %36, 0
br i1 %37, label %53, label %38
38: ; preds = %25
%39 = call i32 @llvm.abs.i32(i32 %36, i1 true)
br label %40
40: ; preds = %38, %40
%41 = phi i32 [ %43, %40 ], [ 1, %38 ]
%42 = call i32 @mpq_add(i32 undef, i32 undef, i32 undef)
%43 = add nuw i32 %41, 1
%44 = icmp eq i32 %41, %39
br i1 %44, label %45, label %40
45: ; preds = %40
%46 = icmp sgt i32 %36, 0
br i1 %46, label %47, label %49
47: ; preds = %45
%48 = call i32 @mpq_mul(i32 %0, i32 %0, i32 undef)
br label %53
49: ; preds = %45
%50 = icmp slt i32 %36, 0
br i1 %50, label %51, label %53
51: ; preds = %49
%52 = call i32 @mpq_div(i32 %0, i32 %0, i32 undef)
br label %53
53: ; preds = %25, %49, %51, %47
%54 = call i32 @mpq_clear(i32 undef)
br i1 %21, label %57, label %55
55: ; preds = %53
%56 = call i32 @mpq_neg(i32 %0, i32 %0)
br label %57
57: ; preds = %55, %53
%58 = call double @mpq_get_d(i32 %0)
%59 = fsub double %1, %58
%60 = call double @llvm.fabs.f64(double %59)
%61 = fadd double %26, 1.000000e+00
%62 = fmul double %61, 1.000000e-09
%63 = fcmp ole double %60, %62
%64 = zext i1 %63 to i32
%65 = call i32 @assert(i32 %64)
br label %66
66: ; preds = %57, %23, %18
ret void
}