// File: /workspace/github_c_makefile_star2_O3/BernardTatin[P]s7sh/s7.c
// Function: g_rationalize
static s7_pointer g_rationalize(s7_scheme *sc, s7_pointer args)
    {
#define H_rationalize "(rationalize x err) returns the ratio with smallest denominator within err of x"
#define Q_rationalize s7_make_signature(sc, 3, sc->is_rational_symbol, sc->is_real_symbol, sc->is_real_symbol)
        /* I can't find a case where this returns a non-rational result */

        s7_double err;
        s7_pointer x;

        x = car(args);
        if (!s7_is_real(x))
            return(method_or_bust(sc, x, sc->rationalize_symbol, args, T_REAL, 1));

        if (is_not_null(cdr(args)))
        {
            s7_pointer ex;
            ex = cadr(args);
            if (!s7_is_real(ex))
                return(method_or_bust(sc, ex, sc->rationalize_symbol, args, T_REAL, 2));

            err = real_to_double(sc, ex, "rationalize");
            if (is_NaN(err))
                return(out_of_range(sc, sc->rationalize_symbol, small_int(2), cadr(args), its_nan_string));
            if (err < 0.0) err = -err;
        }
        else err = sc->default_rationalize_error;

        switch (type(x))
        {
            case T_INTEGER:
                {
                    s7_int a, b, pa;
                    if (err < 1.0) return(x);
                    a = s7_integer(x);
                    if (a < 0) pa = -a; else pa = a;
                    if (err >= pa) return(small_int(0));
                    b = (s7_int)err;
                    pa -= b;
                    if (a < 0)
                        return(make_integer(sc, -pa));
                    return(make_integer(sc, pa));
                }

            case T_RATIO:
                if (err == 0.0)
                    return(x);

            case T_REAL:
                {
                    s7_double rat;
                    s7_int numer = 0, denom = 1;

                    rat = s7_real(x); /* possible fall through from above */
                    if ((is_NaN(rat)) || (is_inf(rat)))
                        return(out_of_range(sc, sc->rationalize_symbol, small_int(1), x, a_normal_real_string));

                    if (err >= fabs(rat))
                        return(small_int(0));

                    if ((rat > 9.2233720368548e+18) || (rat < -9.2233720368548e+18))
                        return(out_of_range(sc, sc->rationalize_symbol, small_int(1), x, its_too_large_string));

                    if ((fabs(rat) + fabs(err)) < 1.0e-18)
                        err = 1.0e-18;
                    /* (/ 1.0 most-positive-fixnum) is 1.0842021e-19, so if we let err be less than that,
                     * (rationalize 1e-19 1e-20) hangs, but this only affects the initial ceiling, I believe.
                     */

                    if (fabs(rat) < fabs(err))
                        return(small_int(0));

                    if (c_rationalize(rat, err, &numer, &denom))
                        return(s7_make_ratio(sc, numer, denom));
                    return(sc->F);
                }
        }
        return(sc->F); /* make compiler happy */
    }

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x20, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movq 0x8(%rsi), %r14
movzbl (%r14), %eax
leaq 0xe4b3a(%rip), %rcx     # 0x14a9b0
cmpb $0x0, (%rax,%rcx)
je 0x65ea1
movq 0x10(%r15), %rdx
cmpq 0x110(%rbx), %rdx
je 0x65ebf
movq 0x8(%rdx), %rsi
movzbl (%rsi), %eax
cmpb $0x0, (%rax,%rcx)
je 0x65ec9
cmpb $0xd, %al
jne 0x65ef0
movsd 0x8(%rsi), %xmm0
jmp 0x65eff
movq 0x1438(%rbx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
movl $0xd, %r8d
movl $0x1, %r9d
jmp 0x65ee2
movsd 0x258(%rbx), %xmm6
jmp 0x65f1c
movq 0x1438(%rbx), %rdx
movq %rbx, %rdi
movq %r15, %rcx
movl $0xd, %r8d
movl $0x2, %r9d
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0xe42d
leaq 0xa878c(%rip), %rdx     # 0x10e683
movq %rbx, %rdi
callq 0xd498
ucomisd %xmm0, %xmm0
jp 0x6616c
movapd 0xa3f9f(%rip), %xmm6    # 0x109eb0
xorpd %xmm0, %xmm6
maxsd %xmm0, %xmm6
movb (%r14), %al
cmpb $0xd, %al
je 0x65fd9
movzbl %al, %eax
cmpl $0xc, %eax
je 0x65f94
cmpl $0xb, %eax
jne 0x66067
movsd 0xa00f3(%rip), %xmm0    # 0x106030
ucomisd %xmm6, %xmm0
ja 0x6615f
movq 0x8(%r14), %rax
movq %rax, %r15
negq %r15
cmovsq %rax, %r15
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
ucomisd %xmm0, %xmm6
jae 0x660ef
cvttsd2si %xmm6, %rcx
subq %rcx, %r15
testq %rax, %rax
jns 0x65f77
negq %r15
cmpq $0x7ff, %r15            # imm = 0x7FF
ja 0x660f8
leaq 0xe09c5(%rip), %rax     # 0x146950
movq (%rax,%r15,8), %r14
jmp 0x6615f
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm6
jne 0x65fa4
jnp 0x6615f
movq $0x0, 0x8(%rsp)
movq $0x1, (%rsp)
movq 0xe4a74(%rip), %rdi     # 0x14aa30
leaq 0xa40b5(%rip), %rdx     # 0x10a078
movq %r14, %rsi
movapd %xmm6, 0x10(%rsp)
callq 0xd498
movapd 0x10(%rsp), %xmm6
jmp 0x65ff0
movq $0x0, 0x8(%rsp)
movq $0x1, (%rsp)
movsd 0x8(%r14), %xmm0
movq %xmm0, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
jl 0x66073
movq 0x1438(%rbx), %rax
movq 0x8(%rax), %rax
movq 0xe093d(%rip), %rcx     # 0x146958
movq 0xe4a16(%rip), %rdx     # 0x14aa38
movq 0x1ee8(%rbx), %rsi
movq 0x10(%rsi), %rsi
movq %rax, 0x8(%rsi)
movq 0x10(%rsi), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%rax), %rax
movq %r14, 0x8(%rax)
movq 0x10(%rax), %rax
movq %rdx, 0x8(%rax)
movq 0x1948(%rbx), %r14
movq 0x1ee8(%rbx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x38dfa
jmp 0x6615f
movq 0x120(%rbx), %r14
jmp 0x6615f
movapd 0xa3e45(%rip), %xmm2    # 0x109ec0
andpd %xmm0, %xmm2
ucomisd %xmm2, %xmm6
jae 0x660ef
ucomisd 0x9ffb3(%rip), %xmm2    # 0x106040
jbe 0x660b8
movq 0x1438(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0xe08b7(%rip), %rdx     # 0x146958
movq 0xe4998(%rip), %r8      # 0x14aa40
movq %rbx, %rdi
movq %r14, %rcx
callq 0xe1fe
jmp 0x6615c
movapd 0xa3e00(%rip), %xmm4    # 0x109ec0
andpd %xmm6, %xmm4
movapd %xmm4, %xmm1
addsd %xmm2, %xmm1
movsd 0x9ffbc(%rip), %xmm3    # 0x106090
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm3
movapd %xmm1, %xmm5
andnpd %xmm4, %xmm5
orpd %xmm3, %xmm5
ucomisd %xmm2, %xmm5
jbe 0x66133
movq 0xe085a(%rip), %r14     # 0x146950
jmp 0x6615f
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x66117
movq %rbx, %rdi
callq 0xa550
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r14
movq $0xb, (%r14)
movq %r15, 0x8(%r14)
jmp 0x6615f
andnpd %xmm6, %xmm1
orpd %xmm3, %xmm1
leaq 0x8(%rsp), %r14
movq %rsp, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xcc81
movq (%r14), %rsi
movq (%r15), %rdx
movq %rbx, %rdi
callq 0xcfa8
movq %rax, %r14
movq %r14, %rax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq 0x1438(%rbx), %rax
movq 0x8(%rax), %rax
movq 0xe07e2(%rip), %rcx     # 0x146960
movq 0x10(%r15), %rdx
movq 0x8(%rdx), %rdx
movq 0xef4b3(%rip), %rsi     # 0x155640
movq 0x1ee8(%rbx), %rdi
movq 0x10(%rdi), %rdi
movq %rax, 0x8(%rdi)
movq 0x10(%rdi), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%rax), %rax
movq %rdx, 0x8(%rax)
movq 0x10(%rax), %rax
movq %rsi, 0x8(%rax)
jmp 0x66049