// File: /workspace/github_c_makefile_star2_O2/BernardTatin[P]s7sh/s7.c
// Function: g_random
static s7_pointer g_random(s7_scheme *sc, s7_pointer args)
    {
#define H_random "(random num (state #f)) returns a random number between 0 and num (0 if num=0)."
#define Q_random s7_make_signature(sc, 3, sc->is_number_symbol, sc->is_number_symbol, sc->is_random_state_symbol)
        s7_pointer r, num;

        if (is_not_null(cdr(args)))
        {
            r = cadr(args);
            if (!is_random_state(r))
                return(method_or_bust_with_type(sc, r, sc->random_symbol, args, a_random_state_object_string, 2));
        }
        else r = sc->default_rng;
        num = car(args);

        switch (type(num))
        {
            case T_INTEGER:
                return(make_integer(sc, (s7_int)(integer(num) * next_random(r))));

            case T_RATIO:
                {
                    s7_double x, error;
                    s7_int numer = 0, denom = 1;
                    /* the error here needs to take the size of the fraction into account.  Otherwise, if
                     *    error is (say) 1e-6 and the fraction is (say) 9000000/9223372036854775807,
                     *    c_rationalize will always return 0.  But even that isn't foolproof:
                     *    (random 1/562949953421312) -> 1/376367230475000
                     */
                    x = fraction(num);
                    if ((x < 1.0e-10) && (x > -1.0e-10))
                    {
                        /* 1e-12 is not tight enough:
                         *    (random 1/2251799813685248) -> 1/2250240579436280
                         *    (random -1/4503599627370496) -> -1/4492889778435526
                         *    (random 1/140737488355328) -> 1/140730223985746
                         *    (random -1/35184372088832) -> -1/35183145492420
                         *    (random -1/70368744177664) -> -1/70366866392738
                         *    (random 1/4398046511104) -> 1/4398033095756
                         *    (random 1/137438953472) -> 1/137438941127
                         */
                        if (numerator(num) < -10)
                            numer = -(s7_int)(floor(-numerator(num) * next_random(r)));
                        else
                        {
                            if (numerator(num) > 10)
                                numer = (s7_int)floor(numerator(num) * next_random(r));
                            else
                            {
                                int64_t diff;
                                numer = numerator(num);
                                diff = s7_int_max - denominator(num);
                                if (diff < 100)
                                    return(s7_make_ratio(sc, numer, denominator(num)));
                                denom = denominator(num) + (s7_int)floor(diff * next_random(r));
                                return(s7_make_ratio(sc, numer, denom));
                            }
                        }
                        return(s7_make_ratio(sc, numer, denominator(num)));
                    }
                    if ((x < 1e-6) && (x > -1e-6))
                        error = 1e-18;
                    else error = 1e-12;
                    c_rationalize(x * next_random(r), error, &numer, &denom);
                    return(s7_make_ratio(sc, numer, denom));
                }

            case T_REAL:
                return(make_real(sc, real(num) * next_random(r)));

            case T_COMPLEX:
                return(s7_make_complex(sc, real_part(num) * next_random(r), imag_part(num) * next_random(r)));

            default:
                return(method_or_bust_with_type(sc, num, sc->random_symbol, args, a_number_string, 1));
        }
        return(sc->F);
    }

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x50, %rsp
movq %rsi, %rcx
movq %rdi, %rbx
movq 0x10(%rsi), %rax
cmpq 0x110(%rdi), %rax
je 0x59aa4
movq 0x8(%rax), %r14
cmpb $0x24, (%r14)
je 0x59aab
movq 0x1430(%rbx), %rdx
movq 0xbffbb(%rip), %r8      # 0x119a50
pushq $0x2
popq %r9
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x59c6c
movq 0xaa0(%rbx), %r14
movq 0x8(%rcx), %r15
movzbl (%r15), %eax
addl $-0xb, %eax
cmpl $0x3, %eax
ja 0x59c54
leaq 0x7a05e(%rip), %rcx     # 0xd3b24
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cvtsi2sdq 0x8(%r15), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0xd0c9
mulsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %r14
cmpq $0x7ff, %r14            # imm = 0x7FF
ja 0x59c7e
leaq 0xbbe4e(%rip), %rax     # 0x115950
movq (%rax,%r14,8), %rax
jmp 0x59d4b
movsd 0x8(%r15), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0xd0c9
mulsd 0x8(%rsp), %xmm0
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x59b50
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 0x59d4b
movsd 0x8(%r15), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0xd0c9
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x10(%r15), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
callq 0xd0c9
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xc152
andq $0x0, 0x48(%rsp)
movq $0x1, 0x40(%rsp)
movq 0x8(%r15), %r12
movq 0x10(%r15), %r13
movq %r12, 0x38(%rsp)
fildll 0x38(%rsp)
movq %r13, 0x30(%rsp)
fildll 0x30(%rsp)
fdivrp %st, %st(1)
fstpl 0x28(%rsp)
movsd 0x28(%rsp), %xmm2
movapd 0x7c2ba(%rip), %xmm0    # 0xd5ec0
andpd %xmm2, %xmm0
movsd 0x784d6(%rip), %xmm1    # 0xd20e8
ucomisd %xmm0, %xmm1
jbe 0x59cbc
cmpq $-0xb, %r12
jg 0x59d16
negq %r12
cvtsi2sd %r12, %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0xd0c9
mulsd 0x8(%rsp), %xmm0
callq 0x85f0
cvttsd2si %xmm0, %rsi
negq %rsi
jmp 0x59d3f
movq 0x1430(%rbx), %rdx
movq 0xca73e(%rip), %r8      # 0x1243a0
pushq $0x1
popq %r9
movq %rbx, %rdi
movq %r15, %rsi
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xcf9a
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x59c9d
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 0x59d4b
movsd 0x783a4(%rip), %xmm1    # 0xd2068
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
leaq 0x7c24c(%rip), %rcx     # 0xd5f20
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
movapd %xmm2, 0x10(%rsp)
callq 0xd0c9
mulsd 0x10(%rsp), %xmm0
leaq 0x48(%rsp), %r14
leaq 0x40(%rsp), %r15
movsd 0x8(%rsp), %xmm1
movq %r14, %rdi
movq %r15, %rsi
callq 0xba8a
movq (%r14), %rsi
movq (%r15), %rdx
jmp 0x59d43
cmpq $0xb, %r12
jl 0x59d59
cvtsi2sd %r12, %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0xd0c9
mulsd 0x8(%rsp), %xmm0
callq 0x85f0
cvttsd2si %xmm0, %rsi
movq 0x10(%r15), %rdx
movq %rbx, %rdi
callq 0xbd87
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
xorl %ecx, %ecx
cmpb $0x0, 0xbfea6(%rip)     # 0x119c08
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
cmoveq %rcx, %rax
subq %r13, %rax
cmpq $0x63, %rax
jle 0x59d9f
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0xd0c9
mulsd 0x8(%rsp), %xmm0
callq 0x85f0
cvttsd2si %xmm0, %rax
addq %rax, %r13
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
jmp 0x59d46