// File: /workspace/github_c_makefile_star2_O2/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 0xc810b(%rip), %rcx     # 0x1199b0
cmpb $0x0, (%rax,%rcx)
je 0x518d0
movq 0x10(%r15), %rdx
cmpq 0x110(%rbx), %rdx
je 0x518e7
movq 0x8(%rdx), %rsi
movzbl (%rsi), %eax
cmpb $0x0, (%rax,%rcx)
je 0x518f1
cmpb $0xd, %al
jne 0x51914
movsd 0x8(%rsi), %xmm0
jmp 0x51923
movq 0x1438(%rbx), %rdx
pushq $0xd
popq %r8
pushq $0x1
popq %r9
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x51903
movsd 0x258(%rbx), %xmm1
jmp 0x51940
movq 0x1438(%rbx), %rdx
pushq $0xd
popq %r8
pushq $0x2
popq %r9
movq %rbx, %rdi
movq %r15, %rcx
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0xcd70
leaq 0x88d08(%rip), %rdx     # 0xda623
movq %rbx, %rdi
callq 0xc263
ucomisd %xmm0, %xmm0
jp 0x51b1c
movapd 0x8457b(%rip), %xmm1    # 0xd5eb0
xorpd %xmm0, %xmm1
maxsd %xmm0, %xmm1
movb (%r14), %al
cmpb $0xd, %al
je 0x519c1
movzbl %al, %eax
cmpl $0xc, %eax
je 0x519b1
cmpl $0xb, %eax
jne 0x51a22
movsd 0x806d3(%rip), %xmm0    # 0xd2030
ucomisd %xmm1, %xmm0
ja 0x51aa6
movq 0x8(%r14), %rax
movq %rax, %r15
negq %r15
cmovsq %rax, %r15
cvtsi2sd %r15, %xmm0
ucomisd %xmm0, %xmm1
jae 0x51a9f
cvttsd2si %xmm1, %rcx
subq %rcx, %r15
testq %rax, %rax
jns 0x51994
negq %r15
cmpq $0x7ff, %r15            # imm = 0x7FF
ja 0x51ab3
leaq 0xc3fa8(%rip), %rax     # 0x115950
movq (%rax,%r15,8), %r14
jmp 0x51aa6
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x519c1
jnp 0x51aa6
movapd %xmm1, 0x10(%rsp)
andq $0x0, 0x8(%rsp)
movq $0x1, (%rsp)
movq %r14, %rdi
callq 0xc42a
movq %xmm0, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
jl 0x51a2b
movq 0x1438(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0xc3f50(%rip), %rdx     # 0x115958
movq 0xc8029(%rip), %r8      # 0x119a38
movq %rbx, %rdi
movq %r14, %rcx
callq 0xcb8f
movq %rax, %r14
jmp 0x51aa6
movq 0x120(%rbx), %r14
jmp 0x51aa6
movapd 0x8448d(%rip), %xmm2    # 0xd5ec0
andpd %xmm0, %xmm2
movapd 0x10(%rsp), %xmm6
ucomisd %xmm2, %xmm6
jae 0x51a9f
ucomisd 0x805f5(%rip), %xmm2    # 0xd2040
jbe 0x51a68
movq 0x1438(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0xc3ef9(%rip), %rdx     # 0x115958
movq 0xc7fda(%rip), %r8      # 0x119a40
jmp 0x51a0f
movapd 0x84450(%rip), %xmm4    # 0xd5ec0
andpd %xmm6, %xmm4
movapd %xmm4, %xmm1
addsd %xmm2, %xmm1
movsd 0x805fc(%rip), %xmm3    # 0xd2080
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm3
movapd %xmm1, %xmm5
andnpd %xmm4, %xmm5
orpd %xmm3, %xmm5
ucomisd %xmm2, %xmm5
jbe 0x51aee
movq 0xc3eaa(%rip), %r14     # 0x115950
movq %r14, %rax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x51ad2
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r14
movq $0xb, (%r14)
movq %r15, 0x8(%r14)
jmp 0x51aa6
andnpd %xmm6, %xmm1
orpd %xmm3, %xmm1
leaq 0x8(%rsp), %r14
movq %rsp, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xba8a
movq (%r14), %rsi
movq (%r15), %rdx
movq %rbx, %rdi
callq 0xbd87
jmp 0x51a1a
movq 0x1438(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0xc3e32(%rip), %rdx     # 0x115960
movq 0x10(%r15), %rax
movq 0x8(%rax), %rcx
movq 0xd2b03(%rip), %r8      # 0x124640
movq %rbx, %rdi
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0xcb8f