// File: /workspace/github_c_makefile_star2_O2/BernardTatin[P]s7sh/s7.c
// Function: g_modulo
static s7_pointer g_modulo(s7_scheme *sc, s7_pointer args)
    {
#define H_modulo "(modulo x1 x2) returns x1 mod x2; (modulo 4 3) = 1.  The arguments can be real numbers."
#define Q_modulo sc->pcl_r
        /* (define (mod x1 x2) (- x1 (* x2 (floor (/ x1 x2))))) from slib
         * (mod x 0) = x according to "Concrete Mathematics"
         */
        s7_pointer x, y;
        s7_double a, b;
        s7_int n1, n2, d1, d2;

        x = car(args);
        y = cadr(args);

        switch (type(x))
        {
            case T_INTEGER:
                switch (type(y))
                {
                    case T_INTEGER:
                        return(make_integer(sc, c_mod(integer(x), integer(y))));

                    case T_RATIO:
                        n1 = integer(x);
                        d1 = 1;
                        n2 = numerator(y);
                        d2 = denominator(y);
                        goto RATIO_MOD_RATIO;

                    case T_REAL:
                        {
                            double c;
                            b = real(y);
                            if (b == 0.0) return(x);
                            if (is_NaN(b)) return(y);
                            if (is_inf(b)) return(real_NaN);
                            a = (s7_double)integer(x);
                            c = a / b;
                            if ((c > 1e19) || (c < -1e19))
                                return(simple_out_of_range(sc, sc->modulo_symbol, y, wrap_string(sc, "intermediate (a/b) is too large", 31)));
                            return(make_real(sc, a - b * (s7_int)floor(c)));
                        }

                    default:
                        return(method_or_bust(sc, y, sc->modulo_symbol, args, T_REAL, 2));
                }

            case T_RATIO:
                switch (type(y))
                {
                    case T_INTEGER:
                        if (integer(y) == 0) return(x);
                        n1 = numerator(x);
                        d1 = denominator(x);
                        n2 = integer(y);

                        if ((n2 > 0) && (n1 > 0) && (n2 > n1)) return(x);
                        if ((n2 < 0) && (n1 < 0) && (n2 < n1)) return(x);

                        if (n2 == s7_int_min)
                            return(simple_out_of_range(sc, sc->modulo_symbol, y, wrap_string(sc, "intermediate (a/b) is too large", 31)));
                        /* the problem here is that (modulo 3/2 most-negative-fixnum)
                         * will segfault with signal SIGFPE, Arithmetic exception, so try to trap it.
                         */

                        d2 = 1;
                        goto RATIO_MOD_RATIO;

                    case T_RATIO:
                        n1 = numerator(x);
                        d1 = denominator(x);
                        n2 = numerator(y); /* can't be 0 */
                        d2 = denominator(y);
                        if (d1 == d2)
                            return(s7_make_ratio(sc, c_mod(n1, n2), d1));

RATIO_MOD_RATIO:

                        if ((n1 == n2) &&
                                (d1 > d2))
                            return(x);                 /* signs match so this should be ok */
#if HAVE_OVERFLOW_CHECKS
                        {
                            s7_int n2d1, n1d2, d1d2, fl;
                            if (!multiply_overflow(n2, d1, &n2d1))
                            {
                                if (n2d1 == 1)
                                    return(small_int(0));

                                if (!multiply_overflow(n1, d2, &n1d2))
                                {
                                    /* can't use "floor" here (int->float ruins everything) */
                                    fl = (s7_int)(n1d2 / n2d1);
                                    if (((n1 < 0) && (n2 > 0)) ||
                                            ((n1 > 0) && (n2 < 0)))
                                        fl -= 1;

                                    if (fl == 0)
                                        return(x);

                                    if ((!multiply_overflow(d1, d2, &d1d2)) &&
                                            (!multiply_overflow(fl, n2d1, &fl)) &&
                                            (!subtract_overflow(n1d2, fl, &fl)))
                                        return(s7_make_ratio(sc, fl, d1d2));
                                }
                            }
                        }
#else
                        {
                            s7_int n1d2, n2d1, fl;
                            n1d2 = n1 * d2;
                            n2d1 = n2 * d1;

                            if (n2d1 == 1)
                                return(small_int(0));

                            /* can't use "floor" here (int->float ruins everything) */
                            fl = (s7_int)(n1d2 / n2d1);
                            if (((n1 < 0) && (n2 > 0)) ||
                                    ((n1 > 0) && (n2 < 0)))
                                fl -= 1;

                            if (fl == 0)
                                return(x);

                            return(s7_make_ratio(sc, n1d2 - (n2d1 * fl), d1 * d2));
                        }
#endif

                        /* there are cases here we might want to catch:
                         *    (modulo 9223372036 1/9223372036) -> error, not 0?
                         *    (modulo 1 1/9223372036854775807) -> error, not 0?
                         */
                        return(simple_out_of_range(sc, sc->modulo_symbol, x, wrap_string(sc, "intermediate (a/b) is too large", 31)));

                    case T_REAL:
                        b = real(y);
                        if (b == 0.0) return(x);
                        if (is_NaN(b)) return(y);
                        if (is_inf(b)) return(real_NaN);
                        a = fraction(x);
                        return(make_real(sc, a - b * (s7_int)floor(a / b)));

                    default:
                        return(method_or_bust(sc, y, sc->modulo_symbol, args, T_REAL, 2));
                }

            case T_REAL:
                a = real(x);

                switch (type(y))
                {
                    case T_INTEGER:
                        if (is_NaN(a)) return(x);
                        if (is_inf(a)) return(real_NaN);
                        if (integer(y) == 0) return(x);
                        b = (s7_double)integer(y);
                        return(make_real(sc, a - b * (s7_int)floor(a / b)));

                    case T_RATIO:
                        if (is_NaN(a)) return(x);
                        if (is_inf(a)) return(real_NaN);
                        b = fraction(y);
                        return(make_real(sc, a - b * (s7_int)floor(a / b)));

                    case T_REAL:
                        {
                            s7_double c;
                            if (is_NaN(a)) return(x);
                            if (is_inf(a)) return(real_NaN);
                            b = real(y);
                            if (b == 0.0) return(x);
                            if (is_NaN(b)) return(y);
                            if (is_inf(b)) return(real_NaN);
                            c = a / b;
                            if ((c > 1e19) || (c < -1e19))
                                return(simple_out_of_range(sc, sc->modulo_symbol, y, wrap_string(sc, "intermediate (a/b) is too large", 31)));
                            return(make_real(sc, a - b * (s7_int)floor(c)));
                        }

                    default:
                        return(method_or_bust(sc, y, sc->modulo_symbol, args, T_REAL, 2));
                }

            default:
                return(method_or_bust(sc, x, sc->modulo_symbol, args, T_REAL, 1));
        }
    }

// Assembly:
pushq %r14
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rax
movq %rdi, %rbx
movq 0x8(%rsi), %r11
movq 0x10(%rsi), %rcx
movq 0x8(%rcx), %r10
movzbl (%r11), %ecx
cmpl $0xd, %ecx
je 0x572a1
cmpl $0xc, %ecx
je 0x5722f
cmpl $0xb, %ecx
jne 0x57303
movzbl (%r10), %ecx
cmpl $0xd, %ecx
je 0x575f3
cmpl $0xc, %ecx
je 0x57523
cmpl $0xb, %ecx
jne 0x5731a
movq 0x8(%r11), %rdi
movq 0x8(%r10), %rsi
callq 0xc92c4
movq %rax, %r14
cmpq $0x7ff, %rax            # imm = 0x7FF
ja 0x5760a
leaq 0xbe72a(%rip), %rax     # 0x115950
movq (%rax,%r14,8), %rax
jmp 0x5767e
movzbl (%r10), %ecx
cmpl $0xd, %ecx
je 0x57371
cmpl $0xc, %ecx
je 0x5733e
cmpl $0xb, %ecx
jne 0x5731a
movq 0x8(%r10), %rsi
testq %rsi, %rsi
je 0x57605
movq 0x8(%r11), %rdi
movq 0x10(%r11), %r14
jle 0x57273
testq %rdi, %rdi
jle 0x57273
cmpq %rdi, %rsi
jg 0x57605
testq %rsi, %rsi
jns 0x57286
testq %rdi, %rdi
jns 0x57286
cmpq %rdi, %rsi
jl 0x57605
movl 0xc279c(%rip), %eax     # 0x119a28
shlq $0x3f, %rax
cmpq %rax, %rsi
je 0x57707
pushq $0x1
popq %rcx
jmp 0x57533
movsd 0x8(%r11), %xmm2
movzbl (%r10), %ecx
cmpl $0xd, %ecx
je 0x57595
cmpl $0xc, %ecx
je 0x57455
cmpl $0xb, %ecx
jne 0x5731a
ucomisd %xmm2, %xmm2
jp 0x57605
movq %xmm2, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
je 0x57677
movq 0x8(%r10), %rcx
movq %r11, %rax
testq %rcx, %rcx
je 0x5767e
cvtsi2sd %rcx, %xmm1
jmp 0x574a5
movq 0x1350(%rbx), %rdx
pushq $0xd
popq %r8
pushq $0x1
popq %r9
movq %rbx, %rdi
movq %r11, %rsi
jmp 0x5732f
movq 0x1350(%rbx), %rdx
pushq $0xd
popq %r8
pushq $0x2
popq %r9
movq %rbx, %rdi
movq %r10, %rsi
movq %rax, %rcx
addq $0x48, %rsp
popq %rbx
popq %r14
jmp 0xcd70
movq 0x8(%r11), %rdi
movq 0x10(%r11), %r14
movq 0x8(%r10), %rsi
movq 0x10(%r10), %rcx
cmpq %rcx, %r14
jne 0x57533
callq 0xc92c4
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
addq $0x48, %rsp
popq %rbx
popq %r14
jmp 0xbd87
movsd 0x8(%r10), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x57387
jnp 0x57605
ucomisd %xmm1, %xmm1
movq %r10, %rax
jp 0x5767e
movq %xmm1, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
je 0x57677
movq 0x8(%r11), %rax
movq 0x10(%r11), %rcx
movq %rax, 0x28(%rsp)
fildll 0x28(%rsp)
movq %rcx, 0x20(%rsp)
fildll 0x20(%rsp)
fdivrp %st, %st(1)
fstpl 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
divsd %xmm1, %xmm0
movsd %xmm1, 0x10(%rsp)
callq 0x85f0
cvttsd2si %xmm0, %rax
cvtsi2sd %rax, %xmm0
mulsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x57435
movq %rbx, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x9c27
movsd 0x8(%rsp), %xmm0
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movq $0xd, (%rax)
movsd %xmm0, 0x8(%rax)
jmp 0x5767e
ucomisd %xmm2, %xmm2
movq %r11, %rax
jp 0x5767e
movq %xmm2, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
je 0x57677
movq 0x8(%r10), %rax
movq 0x10(%r10), %rcx
movq %rax, 0x40(%rsp)
fildll 0x40(%rsp)
movq %rcx, 0x38(%rsp)
fildll 0x38(%rsp)
fdivrp %st, %st(1)
fstpl 0x30(%rsp)
movsd 0x30(%rsp), %xmm1
movsd %xmm1, 0x10(%rsp)
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
movsd %xmm2, 0x8(%rsp)
callq 0x85f0
movsd 0x8(%rsp), %xmm1
cvttsd2si %xmm0, %rax
cvtsi2sd %rax, %xmm0
mulsd 0x10(%rsp), %xmm0
subsd %xmm0, %xmm1
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x57503
movq %rbx, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x9c27
movsd 0x8(%rsp), %xmm1
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movq $0xd, (%rax)
movsd %xmm1, 0x8(%rax)
jmp 0x5767e
movq 0x8(%r11), %rdi
movq 0x8(%r10), %rsi
movq 0x10(%r10), %rcx
pushq $0x1
popq %r14
cmpq %rsi, %rdi
jne 0x57541
cmpq %rcx, %r14
jg 0x57605
movq %rsi, %r9
imulq %r14, %r9
jno 0x57645
movq 0x1350(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0x820(%rbx), %rax
movslq 0x7dc(%rbx), %rdx
movq (%rax,%rdx,8), %rcx
leal 0x1(%rdx), %eax
andl $0x7, %eax
movl %eax, 0x7dc(%rbx)
leaq 0x9014a(%rip), %rax     # 0xe76c8
movq %rax, 0x10(%rcx)
movq $0x1f, 0x8(%rcx)
movq %rbx, %rdi
movq %r11, %rdx
jmp 0x57749
ucomisd %xmm2, %xmm2
jp 0x57605
movq %xmm2, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
je 0x57677
movsd 0x8(%r10), %xmm3
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm3
movq %r11, %rax
jne 0x575d1
jnp 0x5767e
ucomisd %xmm3, %xmm3
jp 0x57789
movq %xmm3, %rax
btrq $0x3f, %rax
cmpq %rcx, %rax
je 0x57677
jmp 0x576e9
movsd 0x8(%r10), %xmm3
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm3
jne 0x57654
jp 0x57654
movq %r11, %rax
jmp 0x5767e
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x57629
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movq $0xb, (%rax)
movq %r14, 0x8(%rax)
jmp 0x5767e
cmpq $0x1, %r9
jne 0x57686
movq 0xbe2fe(%rip), %rax     # 0x115950
jmp 0x5767e
ucomisd %xmm3, %xmm3
jp 0x57789
movq %xmm3, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
jne 0x576e3
movq 0xc581a(%rip), %rax     # 0x11ce98
addq $0x48, %rsp
popq %rbx
popq %r14
retq
movq %rdi, %r8
imulq %rcx, %r8
jo 0x5754e
movq %r8, %rax
cqto
idivq %r9
testq %rsi, %rsi
jle 0x576a5
testq %rdi, %rdi
js 0x576af
testq %rsi, %rsi
jns 0x576b2
testq %rdi, %rdi
jle 0x576b2
decq %rax
testq %rax, %rax
je 0x57605
imulq %rcx, %r14
jo 0x5754e
imulq %r9, %rax
jo 0x5754e
subq %rax, %r8
jo 0x5754e
movq %rbx, %rdi
movq %r8, %rsi
jmp 0x57362
cvtsi2sdq 0x8(%r11), %xmm2
movapd %xmm2, %xmm0
divsd %xmm3, %xmm0
movapd 0x7e7c7(%rip), %xmm1    # 0xd5ec0
andpd %xmm0, %xmm1
ucomisd 0x7a9db(%rip), %xmm1    # 0xd20e0
jbe 0x57755
movq 0x1350(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0x820(%rbx), %rax
movslq 0x7dc(%rbx), %rdx
movq (%rax,%rdx,8), %rcx
leal 0x1(%rdx), %eax
andl $0x7, %eax
movl %eax, 0x7dc(%rbx)
leaq 0x8ff91(%rip), %rax     # 0xe76c8
movq %rax, 0x10(%rcx)
movq $0x1f, 0x8(%rcx)
movq %rbx, %rdi
movq %r10, %rdx
addq $0x48, %rsp
popq %rbx
popq %r14
jmp 0x35b47
movsd %xmm2, 0x8(%rsp)
movsd %xmm3, 0x10(%rsp)
callq 0x85f0
movsd 0x8(%rsp), %xmm1
cvttsd2si %xmm0, %rax
cvtsi2sd %rax, %xmm0
movsd 0x10(%rsp), %xmm2
mulsd %xmm0, %xmm2
subsd %xmm2, %xmm1
jmp 0x574d8
movq %r10, %rax
jmp 0x5767e