// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rcx
movq %rdi, %rbx
movq 0x10(%rsi), %rax
cmpq 0x110(%rdi), %rax
je 0x746ca
movq 0x8(%rax), %rsi
cmpb $0x24, (%rsi)
je 0x746d1
movq 0x1430(%rbx), %rdx
movq 0xde394(%rip), %r8      # 0x152a50
movq %rbx, %rdi
movl $0x2, %r9d
jmp 0x748f0
movq 0xaa0(%rbx), %rsi
movq 0x8(%rcx), %r14
movzbl (%r14), %eax
addl $-0xb, %eax
cmpl $0x3, %eax
ja 0x748d6
leaq 0x9c3d8(%rip), %rcx     # 0x110ac4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cvtsi2sdq 0x8(%r14), %xmm0
imulq $0x7f13ac69, 0x8(%rsi), %rax # imm = 0x7F13AC69
addq 0x10(%rsi), %rax
movl %eax, %ecx
movq %rcx, 0x8(%rsi)
shrq $0x20, %rax
movq %rax, 0x10(%rsi)
cvtsi2sd %rcx, %xmm1
divsd 0x9a926(%rip), %xmm1    # 0x10f048
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %r14
cmpq $0x7ff, %r14            # imm = 0x7FF
ja 0x74900
leaq 0xda211(%rip), %rax     # 0x14e950
movq (%rax,%r14,8), %rax
jmp 0x749fd
imulq $0x7f13ac69, 0x8(%rsi), %rax # imm = 0x7F13AC69
addq 0x10(%rsi), %rax
movl %eax, %ecx
cvtsi2sd %rcx, %xmm0
divsd 0x9a8e5(%rip), %xmm0    # 0x10f048
mulsd 0x8(%r14), %xmm0
movq %rcx, 0x8(%rsi)
shrq $0x20, %rax
movq %rax, 0x10(%rsi)
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x74799
movq %rbx, %rdi
movsd %xmm0, 0x18(%rsp)
callq 0xb389
movsd 0x18(%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 0x749fd
imulq $0x7f13ac69, 0x8(%rsi), %rax # imm = 0x7F13AC69
addq 0x10(%rsi), %rax
movl %eax, %ecx
cvtsi2sd %rcx, %xmm0
movsd 0x9a86d(%rip), %xmm2    # 0x10f048
divsd %xmm2, %xmm0
mulsd 0x8(%r14), %xmm0
movq %rcx, 0x8(%rsi)
shrq $0x20, %rax
movq %rax, 0x10(%rsi)
imulq $0x7f13ac69, %rcx, %rcx # imm = 0x7F13AC69
addq %rax, %rcx
movl %ecx, %eax
cvtsi2sd %rax, %xmm1
divsd %xmm2, %xmm1
mulsd 0x10(%r14), %xmm1
movq %rax, 0x8(%rsi)
shrq $0x20, %rcx
movq %rcx, 0x10(%rsi)
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xe101
movq $0x0, 0x8(%rsp)
movq $0x1, 0x10(%rsp)
movq 0x8(%r14), %r15
movq 0x10(%r14), %r12
movq %r15, 0x30(%rsp)
fildll 0x30(%rsp)
movq %r12, 0x28(%rsp)
fildll 0x28(%rsp)
fdivrp %st, %st(1)
fstpl 0x20(%rsp)
movsd 0x20(%rsp), %xmm2
movapd 0x9e565(%rip), %xmm0    # 0x112dd0
andpd %xmm2, %xmm0
movsd 0x9a879(%rip), %xmm1    # 0x10f0f0
ucomisd %xmm0, %xmm1
jbe 0x7493e
cmpq $-0xb, %r15
jg 0x749a6
negq %r15
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
imulq $0x7f13ac69, 0x8(%rsi), %rax # imm = 0x7F13AC69
addq 0x10(%rsi), %rax
movl %eax, %ecx
movq %rcx, 0x8(%rsi)
shrq $0x20, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movq %rax, 0x10(%rsi)
divsd 0x9a788(%rip), %xmm0    # 0x10f048
mulsd %xmm1, %xmm0
callq 0x95f0
cvttsd2si %xmm0, %rsi
negq %rsi
jmp 0x749ec
movq 0x1430(%rbx), %rdx
movq 0xe8abc(%rip), %r8      # 0x15d3a0
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %r9d
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xf3da
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x74918
movq %rbx, %rdi
callq 0xb389
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movq $0xb, (%rax)
movq %r14, 0x8(%rax)
jmp 0x749fd
movsd 0x9a732(%rip), %xmm1    # 0x10f078
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
leaq 0x9e53a(%rip), %rcx     # 0x112e90
imulq $0x7f13ac69, 0x8(%rsi), %rdx # imm = 0x7F13AC69
movsd (%rcx,%rax,8), %xmm1
addq 0x10(%rsi), %rdx
movl %edx, %eax
movq %rax, 0x8(%rsi)
shrq $0x20, %rdx
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movq %rdx, 0x10(%rsi)
divsd 0x9a6c3(%rip), %xmm0    # 0x10f048
mulsd %xmm2, %xmm0
leaq 0x8(%rsp), %r14
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xda3e
movq (%r14), %rsi
movq (%r15), %rdx
jmp 0x749f5
cmpq $0xb, %r15
jl 0x74a09
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
imulq $0x7f13ac69, 0x8(%rsi), %rax # imm = 0x7F13AC69
addq 0x10(%rsi), %rax
movl %eax, %ecx
movq %rcx, 0x8(%rsi)
shrq $0x20, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movq %rax, 0x10(%rsi)
divsd 0x9a66a(%rip), %xmm0    # 0x10f048
mulsd %xmm1, %xmm0
callq 0x95f0
cvttsd2si %xmm0, %rsi
movq %rsi, 0x8(%rsp)
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xdd7f
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %r15, 0x8(%rsp)
xorl %ecx, %ecx
cmpb $0x0, 0xde1f1(%rip)     # 0x152c08
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
cmoveq %rcx, %rax
subq %r12, %rax
cmpq $0x63, %rax
jle 0x74a76
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
imulq $0x7f13ac69, 0x8(%rsi), %rax # imm = 0x7F13AC69
addq 0x10(%rsi), %rax
movl %eax, %ecx
movq %rcx, 0x8(%rsi)
shrq $0x20, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movq %rax, 0x10(%rsi)
divsd 0x9a5e8(%rip), %xmm0    # 0x10f048
mulsd %xmm1, %xmm0
callq 0x95f0
cvttsd2si %xmm0, %rax
addq %rax, %r12
movq %r12, 0x10(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
jmp 0x749f8