// File: /workspace/github_c_makefile_star2_O1/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), %r14
movq 0x10(%rsi), %rax
movq 0x8(%rax), %rsi
movzbl (%r14), %eax
cmpl $0xd, %eax
je 0x71981
cmpl $0xc, %eax
je 0x7190d
cmpl $0xb, %eax
jne 0x719e2
movzbl (%rsi), %eax
cmpl $0xd, %eax
je 0x71cce
cmpl $0xc, %eax
je 0x71c0c
cmpl $0xb, %eax
jne 0x719fd
movq 0x8(%rsi), %rcx
cmpq $-0x1, %rcx
je 0x71905
movq 0x8(%r14), %r14
testq %rcx, %rcx
je 0x71db0
cmpq $0x1, %rcx
jne 0x71d8e
xorl %r14d, %r14d
jmp 0x71db0
movzbl (%rsi), %eax
cmpl $0xd, %eax
je 0x71a5b
cmpl $0xc, %eax
je 0x71a21
cmpl $0xb, %eax
jne 0x719fd
movq 0x8(%rsi), %rdi
testq %rdi, %rdi
je 0x71cdf
movq 0x8(%r14), %r8
movq 0x10(%r14), %rcx
jle 0x71950
testq %r8, %r8
jle 0x71950
cmpq %r8, %rdi
jg 0x71cdf
testq %rdi, %rdi
jns 0x71963
testq %r8, %r8
jns 0x71963
cmpq %r8, %rdi
jl 0x71cdf
movl 0xe10bf(%rip), %eax     # 0x152a28
shlq $0x3f, %rax
movl $0x1, %r10d
cmpq %rax, %rdi
jne 0x71c1d
jmp 0x71e94
movsd 0x8(%r14), %xmm2
movzbl (%rsi), %eax
cmpl $0xd, %eax
je 0x71c40
cmpl $0xc, %eax
je 0x71b3e
cmpl $0xb, %eax
jne 0x719fd
ucomisd %xmm2, %xmm2
jp 0x71cdf
movq %xmm2, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
je 0x71d82
movq 0x8(%rsi), %rax
movq %r14, %r15
testq %rax, %rax
je 0x71f07
cvtsi2sd %rax, %xmm1
jmp 0x71b8e
movq 0x1350(%rbx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
movl $0xd, %r8d
movl $0x1, %r9d
jmp 0x71a13
movq 0x1350(%rbx), %rdx
movq %rbx, %rdi
movl $0xd, %r8d
movl $0x2, %r9d
addq $0x40, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0xf1ae
movq 0x8(%r14), %r8
movq 0x10(%r14), %rcx
movq 0x8(%rsi), %rdi
movq 0x10(%rsi), %r10
cmpq %r10, %rcx
jne 0x71c1d
cmpq $-0x1, %rdi
je 0x71a53
testq %rdi, %rdi
je 0x71f36
cmpq $0x1, %rdi
jne 0x71f14
xorl %r8d, %r8d
jmp 0x71f36
movsd 0x8(%rsi), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x71a70
jnp 0x71cdf
ucomisd %xmm1, %xmm1
movq %rsi, %r15
jp 0x71f07
movq %xmm1, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
je 0x71d82
movq 0x8(%r14), %rax
movq 0x10(%r14), %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 0x95f0
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 0x71b16
movq %rbx, %rdi
movsd %xmm0, (%rsp)
callq 0xb389
movsd (%rsp), %xmm0
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r15
movq $0xd, (%r15)
movsd %xmm0, 0x8(%r15)
jmp 0x71f07
ucomisd %xmm2, %xmm2
movq %r14, %r15
jp 0x71f07
movq %xmm2, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
je 0x71d82
movq 0x8(%rsi), %rax
movq 0x10(%rsi), %rcx
movq %rax, 0x38(%rsp)
fildll 0x38(%rsp)
movq %rcx, 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 0x95f0
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 0x71be4
movq %rbx, %rdi
movsd %xmm1, (%rsp)
callq 0xb389
movsd (%rsp), %xmm1
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r15
movq $0xd, (%r15)
movsd %xmm1, 0x8(%r15)
jmp 0x71f07
movq 0x8(%r14), %r8
movq 0x8(%rsi), %rdi
movq 0x10(%rsi), %r10
movl $0x1, %ecx
cmpq %rdi, %r8
jne 0x71c2b
cmpq %r10, %rcx
jg 0x71cdf
movq %rdi, %r11
imulq %rcx, %r11
movb $0x1, %r9b
jno 0x71ce7
jmp 0x71cfb
ucomisd %xmm2, %xmm2
jp 0x71cdf
movq %xmm2, %rcx
btrq $0x3f, %rcx
movabsq $0x7ff0000000000000, %rax # imm = 0x7FF0000000000000
cmpq %rax, %rcx
je 0x71d82
movsd 0x8(%rsi), %xmm3
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm3
movq %r14, %r15
jne 0x71c7f
jnp 0x71f07
ucomisd %xmm3, %xmm3
jp 0x71f8d
movq %xmm3, %rcx
btrq $0x3f, %rcx
cmpq %rax, %rcx
je 0x71d82
movapd %xmm2, %xmm0
divsd %xmm3, %xmm0
movapd 0xa1124(%rip), %xmm1    # 0x112dd0
andpd %xmm0, %xmm1
ucomisd 0x9d430(%rip), %xmm1    # 0x10f0e8
ja 0x71e94
movsd %xmm2, (%rsp)
movsd %xmm3, 0x8(%rsp)
jmp 0x71f58
movsd 0x8(%rsi), %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x71d5b
jp 0x71d5b
movq %r14, %r15
jmp 0x71f07
cmpq $0x1, %r11
jne 0x71e07
movq 0xdcc58(%rip), %r15     # 0x14e950
xorl %r9d, %r9d
testb %r9b, %r9b
je 0x71f07
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 0xb28e5(%rip), %rdx     # 0x124618
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 %r14, 0x8(%rax)
jmp 0x71ee6
ucomisd %xmm2, %xmm2
jp 0x71f8d
movq %xmm2, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
jne 0x71e6c
movq 0xe410f(%rip), %r15     # 0x155e98
jmp 0x71f07
movq %r14, %rax
cqto
idivq %rcx
movq %rdx, %r14
testq %rcx, %rcx
jns 0x71da3
testq %r14, %r14
jg 0x71dad
testq %rcx, %rcx
jle 0x71db0
testq %r14, %r14
jns 0x71db0
addq %rcx, %r14
cmpq $0x7ff, %r14            # imm = 0x7FF
ja 0x71dc9
leaq 0xdcb90(%rip), %rax     # 0x14e950
movq (%rax,%r14,8), %r15
jmp 0x71f07
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x71de1
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 0x71f07
movq %r8, %rsi
imulq %r10, %rsi
jo 0x71c3b
movq %rsi, %rax
cqto
idivq %r11
testq %rdi, %rdi
jle 0x71e26
testq %r8, %r8
js 0x71e30
testq %rdi, %rdi
jns 0x71e33
testq %r8, %r8
jle 0x71e33
decq %rax
testq %rax, %rax
je 0x71f82
imulq %r10, %rcx
jo 0x71c3b
imulq %r11, %rax
jo 0x71c3b
subq %rax, %rsi
jo 0x71cfb
movq %rbx, %rdi
movq %rcx, %rdx
callq 0xdd7f
movq %rax, %r15
jmp 0x71cf8
cvtsi2sdq 0x8(%r14), %xmm3
movapd %xmm3, %xmm0
divsd %xmm2, %xmm0
movapd 0xa0f4e(%rip), %xmm1    # 0x112dd0
andpd %xmm0, %xmm1
ucomisd 0x9d25a(%rip), %xmm1    # 0x10f0e8
jbe 0x71f4d
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 0xb2755(%rip), %rdx     # 0x124618
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)
movq 0x1948(%rbx), %r15
movq 0x1ef8(%rbx), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3b366
movq %r15, %rax
addq $0x40, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq %r8, %rax
cqto
idivq %rdi
movq %rdx, %r8
testq %rdi, %rdi
jns 0x71f29
testq %r8, %r8
jg 0x71f33
testq %rdi, %rdi
jle 0x71f36
testq %r8, %r8
jns 0x71f36
addq %rdi, %r8
movq %rbx, %rdi
movq %r8, %rsi
movq %rcx, %rdx
addq $0x40, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0xdd7f
movsd %xmm2, 0x8(%rsp)
movsd %xmm3, (%rsp)
callq 0x95f0
movsd (%rsp), %xmm1
cvttsd2si %xmm0, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x8(%rsp), %xmm2
mulsd %xmm0, %xmm2
subsd %xmm2, %xmm1
jmp 0x71bc2
xorl %r9d, %r9d
movq %r14, %r15
jmp 0x71cfb
movq %rsi, %r15
jmp 0x71f07