// File: /workspace/github_c_makefile_star2_O3/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 0x6eede
movq 0x8(%rax), %rsi
cmpb $0x24, (%rsi)
je 0x6eee5
movq 0x1430(%rbx), %rdx
movq 0xdbb80(%rip), %r8      # 0x14aa50
movq %rbx, %rdi
movl $0x2, %r9d
jmp 0x6f104
movq 0xaa0(%rbx), %rsi
movq 0x8(%rcx), %r14
movzbl (%r14), %eax
addl $-0xb, %eax
cmpl $0x3, %eax
ja 0x6f0ea
leaq 0x98c44(%rip), %rcx     # 0x107b44
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 0x97112(%rip), %xmm1    # 0x106048
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %r14
cmpq $0x7ff, %r14            # imm = 0x7FF
ja 0x6f114
leaq 0xd79fd(%rip), %rax     # 0x146950
movq (%rax,%r14,8), %rax
jmp 0x6f20c
imulq $0x7f13ac69, 0x8(%rsi), %rax # imm = 0x7F13AC69
addq 0x10(%rsi), %rax
movl %eax, %ecx
cvtsi2sd %rcx, %xmm0
divsd 0x970d1(%rip), %xmm0    # 0x106048
mulsd 0x8(%r14), %xmm0
movq %rcx, 0x8(%rsi)
shrq $0x20, %rax
movq %rax, 0x10(%rsi)
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6efb4
movq %rbx, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0xa550
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 0x6f20c
imulq $0x7f13ac69, 0x8(%rsi), %rax # imm = 0x7F13AC69
addq 0x10(%rsi), %rax
movl %eax, %ecx
cvtsi2sd %rcx, %xmm0
movsd 0x97059(%rip), %xmm2    # 0x106048
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 0xd387
movq $0x0, 0x30(%rsp)
movq $0x1, 0x28(%rsp)
movq 0x8(%r14), %r15
movq 0x10(%r14), %r12
movq %r15, 0x20(%rsp)
fildll 0x20(%rsp)
movq %r12, 0x18(%rsp)
fildll 0x18(%rsp)
fdivrp %st, %st(1)
fstpl 0x10(%rsp)
movsd 0x10(%rsp), %xmm2
movapd 0x9ae41(%rip), %xmm0    # 0x109ec0
andpd %xmm2, %xmm0
movsd 0x97065(%rip), %xmm1    # 0x1060f0
ucomisd %xmm0, %xmm1
jbe 0x6f152
cmpq $-0xb, %r15
jg 0x6f1ba
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 0x96f74(%rip), %xmm0    # 0x106048
mulsd %xmm1, %xmm0
callq 0x85f0
cvttsd2si %xmm0, %rsi
negq %rsi
jmp 0x6f200
movq 0x1430(%rbx), %rdx
movq 0xe62a8(%rip), %r8      # 0x1553a0
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %r9d
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xe659
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6f133
movq %rbx, %rdi
callq 0xa550
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movq $0xb, (%rax)
movq %r14, 0x8(%rax)
jmp 0x6f20c
movsd 0x96f1e(%rip), %xmm1    # 0x106078
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
leaq 0x9ae16(%rip), %rcx     # 0x109f80
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 0x96eaf(%rip), %xmm0    # 0x106048
mulsd %xmm2, %xmm0
leaq 0x30(%rsp), %r14
leaq 0x28(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xcc81
movq (%r14), %rsi
movq (%r15), %rdx
jmp 0x6f204
cmpq $0xb, %r15
jl 0x6f218
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 0x96e56(%rip), %xmm0    # 0x106048
mulsd %xmm1, %xmm0
callq 0x85f0
cvttsd2si %xmm0, %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xcfa8
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
xorl %ecx, %ecx
cmpb $0x0, 0xdb9e7(%rip)     # 0x14ac08
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
cmoveq %rcx, %rax
subq %r12, %rax
cmpq $0x63, %rax
jle 0x6f27b
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 0x96dde(%rip), %xmm0    # 0x106048
mulsd %xmm1, %xmm0
callq 0x85f0
cvttsd2si %xmm0, %rax
addq %rax, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
jmp 0x6f207