// File: /workspace/github_c_makefile_star2_O1/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), %ecx
leaq 0xe7b18(%rip), %rax     # 0x1529b0
cmpb $0x0, (%rcx,%rax)
je 0x6aec4
movq 0x10(%r15), %rcx
cmpq 0x110(%rbx), %rcx
je 0x6aeee
movq 0x8(%rcx), %rsi
movzbl (%rsi), %ecx
cmpb $0x0, (%rcx,%rax)
je 0x6aef8
cmpb $0xd, %cl
jne 0x6af1d
movsd 0x8(%rsi), %xmm0
jmp 0x6af2c
movq 0x1438(%rbx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
movl $0xd, %r8d
movl $0x1, %r9d
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0xf1ae
movsd 0x258(%rbx), %xmm6
jmp 0x6af54
movq 0x1438(%rbx), %rdx
movq %rbx, %rdi
movq %r15, %rcx
movl $0xd, %r8d
movl $0x2, %r9d
callq 0xf1ae
movq %rax, %r15
xorl %eax, %eax
jmp 0x6af4c
leaq 0xac64f(%rip), %rdx     # 0x117573
movq %rbx, %rdi
callq 0xe212
ucomisd %xmm0, %xmm0
jp 0x6b19d
movapd 0xa7e82(%rip), %xmm1    # 0x112dc0
xorpd %xmm0, %xmm1
maxsd %xmm0, %xmm1
movb $0x1, %al
movapd %xmm1, %xmm6
testb %al, %al
je 0x6b190
movzbl (%r14), %eax
cmpl $0xd, %eax
je 0x6af9a
cmpl $0xc, %eax
je 0x6af86
cmpl $0xb, %eax
jne 0x6afb8
movsd 0xa40c1(%rip), %xmm0    # 0x10f030
ucomisd %xmm6, %xmm0
ja 0x6af92
cmpb $0xb, %al
jne 0x6b0a4
movq 0x8(%r14), %rax
jmp 0x6b0a6
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm6
jne 0x6af9a
jp 0x6af9a
movq %r14, %r15
jmp 0x6b190
movq $0x0, 0x18(%rsp)
movq $0x1, 0x10(%rsp)
cmpb $0xd, %al
jne 0x6afc4
movsd 0x8(%r14), %xmm0
jmp 0x6afe4
movq 0x120(%rbx), %r15
jmp 0x6b190
movq 0xe7a65(%rip), %rdi     # 0x152a30
leaq 0xa7f96(%rip), %rdx     # 0x112f68
movq %r14, %rsi
movapd %xmm6, (%rsp)
callq 0xe212
movapd (%rsp), %xmm6
movq %xmm0, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
jl 0x6b05b
movq 0x1438(%rbx), %rax
movq 0x8(%rax), %rax
movq 0xe3949(%rip), %rcx     # 0x14e958
movq 0xe7a22(%rip), %rdx     # 0x152a38
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), %r15
movq 0x1ee8(%rbx), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3b366
jmp 0x6b190
movapd 0xa7d6d(%rip), %xmm2    # 0x112dd0
andpd %xmm0, %xmm2
ucomisd %xmm2, %xmm6
jae 0x6b11e
ucomisd 0xa3fc7(%rip), %xmm2    # 0x10f040
jbe 0x6b0e7
movq 0x1438(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0xe38cb(%rip), %rdx     # 0x14e958
movq 0xe79ac(%rip), %r8      # 0x152a40
movq %rbx, %rdi
movq %r14, %rcx
callq 0xef7f
jmp 0x6b18d
xorl %eax, %eax
movq %rax, %r14
negq %r14
cmovsq %rax, %r14
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
ucomisd %xmm0, %xmm6
jae 0x6b11e
cvttsd2si %xmm6, %rcx
subq %rcx, %r14
testq %rax, %rax
jns 0x6b0ce
negq %r14
cmpq $0x7ff, %r14            # imm = 0x7FF
ja 0x6b127
leaq 0xe3872(%rip), %rax     # 0x14e950
movq (%rax,%r14,8), %r15
jmp 0x6b190
movapd 0xa7ce1(%rip), %xmm4    # 0x112dd0
andpd %xmm6, %xmm4
movapd %xmm4, %xmm1
addsd %xmm2, %xmm1
movsd 0xa3f8d(%rip), %xmm3    # 0x10f090
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm3
movapd %xmm1, %xmm5
andnpd %xmm4, %xmm5
orpd %xmm3, %xmm5
ucomisd %xmm2, %xmm5
jbe 0x6b162
movq 0xe382b(%rip), %r15     # 0x14e950
jmp 0x6b190
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6b13f
movq %rbx, %rdi
callq 0xb389
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r15
movq $0xb, (%r15)
movq %r14, 0x8(%r15)
jmp 0x6b190
andnpd %xmm6, %xmm1
orpd %xmm3, %xmm1
leaq 0x18(%rsp), %r14
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xda3e
movq (%r14), %rsi
movq (%r15), %rdx
movq %rbx, %rdi
callq 0xdd7f
movq %rax, %r15
movq %r15, %rax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq 0x1438(%rbx), %rax
movq 0x8(%rax), %rax
movq 0xe37b1(%rip), %rcx     # 0x14e960
movq 0x10(%r15), %rdx
movq 0x8(%rdx), %rdx
movq 0xf2482(%rip), %rsi     # 0x15d640
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)
movq 0x1948(%rbx), %r15
movq 0x1ee8(%rbx), %rdx
movq %rbx, %rdi
movq %r15, %rsi
movapd %xmm0, (%rsp)
callq 0x3b366
movapd (%rsp), %xmm6
xorl %eax, %eax
jmp 0x6af4c