// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %rbx
subq $0x40, %rsp
movq %rsi, %rcx
movq %rdi, %rbx
movq 0x8(%rsi), %rsi
movq 0x10(%rcx), %rax
movq 0x8(%rax), %rax
movzbl (%rsi), %edx
cmpl $0xd, %edx
je 0x6c2d4
cmpl $0xc, %edx
je 0x6c260
cmpl $0xb, %edx
jne 0x6c334
movzbl (%rax), %edx
cmpl $0xd, %edx
je 0x6c642
cmpl $0xc, %edx
je 0x6c55e
cmpl $0xb, %edx
jne 0x6c34c
movq 0x8(%rax), %rcx
xorl %eax, %eax
cmpq $-0x1, %rcx
je 0x6c250
movq 0x8(%rsi), %r15
testq %rcx, %rcx
je 0x6c240
cmpq $0x1, %rcx
je 0x6c250
movq %r15, %rax
cqto
idivq %rcx
movq %rdx, %r15
testq %rcx, %rcx
jns 0x6c233
testq %r15, %r15
jg 0x6c23d
testq %rcx, %rcx
jle 0x6c240
testq %r15, %r15
jns 0x6c240
addq %rcx, %r15
cmpq $0x7ff, %r15            # imm = 0x7FF
ja 0x6c6a0
movq %r15, %rax
leaq 0xda6f9(%rip), %rcx     # 0x146950
movq (%rcx,%rax,8), %r14
jmp 0x6c7d6
movzbl (%rax), %edx
cmpl $0xd, %edx
je 0x6c3ad
cmpl $0xc, %edx
je 0x6c373
cmpl $0xb, %edx
jne 0x6c34c
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x6c653
movq 0x8(%rsi), %r8
movq 0x10(%rsi), %rcx
jle 0x6c2a3
testq %r8, %r8
jle 0x6c2a3
cmpq %r8, %rdi
jg 0x6c653
testq %rdi, %rdi
jns 0x6c2b6
testq %r8, %r8
jns 0x6c2b6
cmpq %r8, %rdi
jl 0x6c653
movl 0xde76c(%rip), %edx     # 0x14aa28
shlq $0x3f, %rdx
movl $0x1, %r10d
cmpq %rdx, %rdi
jne 0x6c56f
jmp 0x6c763
movsd 0x8(%rsi), %xmm2
movzbl (%rax), %edx
cmpl $0xd, %edx
je 0x6c5e9
cmpl $0xc, %edx
je 0x6c490
cmpl $0xb, %edx
jne 0x6c34c
ucomisd %xmm2, %xmm2
jp 0x6c653
movq %xmm2, %rcx
btrq $0x3f, %rcx
movabsq $0x7ff0000000000000, %rdx # imm = 0x7FF0000000000000
cmpq %rdx, %rcx
je 0x6c694
movq 0x8(%rax), %rax
movq %rsi, %r14
testq %rax, %rax
je 0x6c7d6
cvtsi2sd %rax, %xmm1
jmp 0x6c4e0
movq 0x1350(%rbx), %rdx
movq %rbx, %rdi
movl $0xd, %r8d
movl $0x1, %r9d
jmp 0x6c365
movq 0x1350(%rbx), %rdx
movq %rbx, %rdi
movq %rax, %rsi
movl $0xd, %r8d
movl $0x2, %r9d
addq $0x40, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0xe42d
movq 0x8(%rsi), %r8
movq 0x10(%rsi), %rcx
movq 0x8(%rax), %rdi
movq 0x10(%rax), %r10
cmpq %r10, %rcx
jne 0x6c56f
cmpq $-0x1, %rdi
je 0x6c3a5
testq %rdi, %rdi
je 0x6c83a
cmpq $0x1, %rdi
jne 0x6c818
xorl %r8d, %r8d
jmp 0x6c83a
movsd 0x8(%rax), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x6c3c2
jnp 0x6c653
ucomisd %xmm1, %xmm1
movq %rax, %r14
jp 0x6c7d6
movq %xmm1, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
je 0x6c694
movq 0x8(%rsi), %rax
movq 0x10(%rsi), %rcx
movq %rax, 0x20(%rsp)
fildll 0x20(%rsp)
movq %rcx, 0x18(%rsp)
fildll 0x18(%rsp)
fdivrp %st, %st(1)
fstpl 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rsp)
divsd %xmm1, %xmm0
movsd %xmm1, 0x8(%rsp)
callq 0x85f0
cvttsd2si %xmm0, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x8(%rsp), %xmm0
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6c46f
movq %rbx, %rdi
movsd %xmm0, (%rsp)
callq 0xa550
movsd (%rsp), %xmm0
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r14
movq $0xd, (%r14)
movsd %xmm0, 0x8(%r14)
jmp 0x6c7d6
ucomisd %xmm2, %xmm2
movq %rsi, %r14
jp 0x6c7d6
movq %xmm2, %rcx
btrq $0x3f, %rcx
movabsq $0x7ff0000000000000, %rdx # imm = 0x7FF0000000000000
cmpq %rdx, %rcx
je 0x6c694
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rax
movq %rcx, 0x38(%rsp)
fildll 0x38(%rsp)
movq %rax, 0x30(%rsp)
fildll 0x30(%rsp)
fdivrp %st, %st(1)
fstpl 0x28(%rsp)
movsd 0x28(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
movsd %xmm2, (%rsp)
callq 0x85f0
movsd (%rsp), %xmm1
cvttsd2si %xmm0, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x8(%rsp), %xmm0
subsd %xmm0, %xmm1
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6c53d
movq %rbx, %rdi
movsd %xmm1, (%rsp)
callq 0xa550
movsd (%rsp), %xmm1
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r14
movq $0xd, (%r14)
movsd %xmm1, 0x8(%r14)
jmp 0x6c7d6
movq 0x8(%rsi), %r8
movq 0x8(%rax), %rdi
movq 0x10(%rax), %r10
movl $0x1, %ecx
cmpq %rdi, %r8
jne 0x6c57d
cmpq %r10, %rcx
jg 0x6c653
movq %rdi, %r11
imulq %rcx, %r11
jno 0x6c65b
movq 0x1350(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x820(%rbx), %rcx
movslq 0x7dc(%rbx), %rdx
movq (%rcx,%rdx,8), %rcx
incl %edx
andl $0x7, %edx
movl %edx, 0x7dc(%rbx)
leaq 0xaf16f(%rip), %rdx     # 0x11b728
movq %rdx, 0x10(%rcx)
movq $0x1f, 0x8(%rcx)
movq 0x1ef8(%rbx), %rdx
movq 0x10(%rdx), %rdx
movq %rax, 0x8(%rdx)
movq 0x10(%rdx), %rax
movq %rsi, 0x8(%rax)
movq 0x10(%rax), %rax
movq %rcx, 0x8(%rax)
jmp 0x6c7bd
ucomisd %xmm2, %xmm2
jp 0x6c653
movq %xmm2, %rdx
btrq $0x3f, %rdx
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rdx
je 0x6c694
movsd 0x8(%rax), %xmm3
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm3
movq %rsi, %r14
jne 0x6c624
jnp 0x6c7d6
ucomisd %xmm3, %xmm3
jp 0x6c851
movq %xmm3, %rdx
btrq $0x3f, %rdx
cmpq %rcx, %rdx
je 0x6c694
jmp 0x6c741
movsd 0x8(%rax), %xmm3
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm3
jne 0x6c66d
jp 0x6c66d
movq %rsi, %r14
jmp 0x6c7d6
cmpq $0x1, %r11
jne 0x6c6de
movq 0xda2e8(%rip), %r14     # 0x146950
jmp 0x6c7d6
ucomisd %xmm3, %xmm3
jp 0x6c851
movq %xmm3, %rcx
btrq $0x3f, %rcx
movabsq $0x7ff0000000000000, %rdx # imm = 0x7FF0000000000000
cmpq %rdx, %rcx
jne 0x6c73b
movq 0xe17fd(%rip), %r14     # 0x14de98
jmp 0x6c7d6
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6c6bf
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 0x6c7d6
movq %r8, %r9
imulq %r10, %r9
jo 0x6c58a
movq %r9, %rax
cqto
idivq %r11
testq %rdi, %rdi
jle 0x6c6fd
testq %r8, %r8
js 0x6c707
testq %rdi, %rdi
jns 0x6c70a
testq %r8, %r8
jle 0x6c70a
decq %rax
testq %rax, %rax
je 0x6c653
imulq %r10, %rcx
jo 0x6c58a
imulq %r11, %rax
jo 0x6c58a
subq %rax, %r9
jo 0x6c58a
movq %rbx, %rdi
movq %r9, %rsi
jmp 0x6c840
cvtsi2sdq 0x8(%rsi), %xmm2
movapd %xmm2, %xmm0
divsd %xmm3, %xmm0
movapd 0x9d76f(%rip), %xmm1    # 0x109ec0
andpd %xmm0, %xmm1
ucomisd 0x9998b(%rip), %xmm1    # 0x1060e8
jbe 0x6c7e3
movq 0x1350(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq 0x820(%rbx), %rdx
movslq 0x7dc(%rbx), %rsi
movq (%rdx,%rsi,8), %rdx
incl %esi
andl $0x7, %esi
movl %esi, 0x7dc(%rbx)
leaq 0xaef96(%rip), %rsi     # 0x11b728
movq %rsi, 0x10(%rdx)
movq $0x1f, 0x8(%rdx)
movq 0x1ef8(%rbx), %rsi
movq 0x10(%rsi), %rsi
movq %rcx, 0x8(%rsi)
movq 0x10(%rsi), %rcx
movq %rax, 0x8(%rcx)
movq 0x10(%rcx), %rax
movq %rdx, 0x8(%rax)
movq 0x1948(%rbx), %r14
movq 0x1ef8(%rbx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x38dfa
movq %r14, %rax
addq $0x40, %rsp
popq %rbx
popq %r14
popq %r15
retq
movsd %xmm2, (%rsp)
movsd %xmm3, 0x8(%rsp)
callq 0x85f0
movsd (%rsp), %xmm1
cvttsd2si %xmm0, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x8(%rsp), %xmm2
mulsd %xmm0, %xmm2
subsd %xmm2, %xmm1
jmp 0x6c514
movq %r8, %rax
cqto
idivq %rdi
movq %rdx, %r8
testq %rdi, %rdi
jns 0x6c82d
testq %r8, %r8
jg 0x6c837
testq %rdi, %rdi
jle 0x6c83a
testq %r8, %r8
jns 0x6c83a
addq %rdi, %r8
movq %rbx, %rdi
movq %r8, %rsi
movq %rcx, %rdx
addq $0x40, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0xcfa8
movq %rax, %r14
jmp 0x6c7d6