// File: /workspace/github_c_makefile_star2_O1/BernardTatin[P]s7sh/s7.c
// Function: g_sqrt
static s7_pointer g_sqrt(s7_scheme *sc, s7_pointer args)
    {
#define H_sqrt "(sqrt z) returns the square root of z"
#define Q_sqrt sc->pl_nn

        s7_pointer n;
        s7_double sqx;

        n = car(args);
        switch (type(n))
        {
            case T_INTEGER:
                if (integer(n) >= 0)
                {
                    s7_int ix;
                    sqx = sqrt((s7_double)integer(n));
                    ix = (s7_int)sqx;
                    if ((ix * ix) == integer(n))
                        return(make_integer(sc, ix));
                    return(make_real(sc, sqx));
                    /* Mark Weaver notes that
                     *     (zero? (- (sqrt 9007199136250226) 94906265.0)) -> #t
                     * but (* 94906265 94906265) -> 9007199136250225 -- oops
                     * at least we return a real here, not an incorrect integer and
                     *     (sqrt 9007199136250225) -> 94906265
                     */
                }
                sqx = (s7_double)integer(n); /* we're trying to protect against (sqrt -9223372036854775808) where we can't negate the integer argument */
                return(s7_make_complex(sc, 0.0, sqrt((s7_double)(-sqx))));

            case T_RATIO:
                sqx = (s7_double)fraction(n);
                if (sqx > 0.0) /* else it's complex, so it can't be a ratio */
                {
                    s7_int nm = 0, dn = 1;
                    if (c_rationalize(sqx, 1.0e-16, &nm, &dn)) /* 1e-16 so that (sqrt 1/1099511627776) returns 1/1048576 */
                    {
#if HAVE_OVERFLOW_CHECKS
                        s7_int nm2, dn2;
                        if ((multiply_overflow(nm, nm, &nm2)) ||
                                (multiply_overflow(dn, dn, &dn2)))
                            return(make_real(sc, sqrt(sqx)));
                        if ((nm2 == numerator(n)) &&
                                (dn2 == denominator(n)))
                            return(s7_make_ratio(sc, nm, dn));
#else
                        if ((nm * nm == numerator(n)) &&
                                (dn * dn == denominator(n)))
                            return(s7_make_ratio(sc, nm, dn));
#endif
                    }
                    return(make_real(sc, sqrt(sqx)));
                }
                return(s7_make_complex(sc, 0.0, sqrt(-sqx)));

            case T_REAL:
                if (is_NaN(real(n)))
                    return(real_NaN);
                if (real(n) >= 0.0)
                    return(make_real(sc, sqrt(real(n))));
                return(s7_make_complex(sc, 0.0, sqrt(-real(n))));

            case T_COMPLEX:
                /* (* inf.0 (sqrt -1)) -> -nan+infi, but (sqrt -inf.0) -> 0+infi */
#if HAVE_COMPLEX_NUMBERS
                return(s7_from_c_complex(sc, csqrt(as_c_complex(n))));
#else
                return(out_of_range(sc, sc->sqrt_symbol, small_int(1), n, no_complex_numbers_string));
#endif

            default:
                return(method_or_bust_with_type_one_arg(sc, n, sc->sqrt_symbol, args, a_number_string));
        }
    }

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq 0x8(%rsi), %r14
movzbl (%r14), %eax
addl $-0xb, %eax
cmpl $0x3, %eax
ja 0x6d0fb
leaq 0xa3a00(%rip), %rcx     # 0x1109c4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%r14), %rax
testq %rax, %rax
js 0x6d14d
cvtsi2sd %rax, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x6d175
sqrtsd %xmm0, %xmm0
jmp 0x6d17a
movsd 0x8(%r14), %xmm0
ucomisd %xmm0, %xmm0
jp 0x6d2fa
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jae 0x6d13e
movapd 0xa5da4(%rip), %xmm1    # 0x112dc0
xorpd %xmm0, %xmm1
movsd 0xa20a0(%rip), %xmm2    # 0x10f0c8
ucomisd %xmm0, %xmm2
jb 0x6d1ad
sqrtsd %xmm1, %xmm1
jmp 0x6d1ba
movsd 0x10(%r14), %xmm1
xorpd %xmm0, %xmm0
mulsd %xmm1, %xmm0
addsd 0x8(%r14), %xmm0
callq 0x9560
jmp 0x6d1be
movq 0x8(%r14), %r12
movq 0x10(%r14), %r15
movq %r12, 0x30(%rsp)
fildll 0x30(%rsp)
movq %r15, 0x28(%rsp)
fildll 0x28(%rsp)
fdivrp %st, %st(1)
fstpl 0x20(%rsp)
movsd 0x20(%rsp), %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jbe 0x6d122
leaq 0x40(%rsp), %r14
movq $0x0, (%r14)
leaq 0x38(%rsp), %rsi
movq $0x1, (%rsi)
movsd 0xa2023(%rip), %xmm1    # 0x10f0d0
movapd %xmm2, (%rsp)
movapd %xmm2, %xmm0
movq %r14, %rdi
callq 0xda3e
movq (%r14), %rsi
movq %rsi, %rax
imulq %rsi, %rax
jo 0x6d0dc
movq 0x38(%rsp), %rdx
movq %rdx, %rcx
imulq %rdx, %rcx
jno 0x6d26c
xorpd %xmm0, %xmm0
movapd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x6d1d1
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x6d1df
movq %rsi, %rcx
movq 0x14f0(%rbx), %rdx
movq 0xf0294(%rip), %r8      # 0x15d3a0
movq %rbx, %rdi
movq %r14, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x103001
movapd 0xa5c96(%rip), %xmm0    # 0x112dc0
xorpd %xmm2, %xmm0
movsd 0xa1f92(%rip), %xmm1    # 0x10f0c8
ucomisd %xmm2, %xmm1
jae 0x6d16c
jmp 0x6d1b1
jb 0x6d2a5
sqrtsd %xmm0, %xmm0
jmp 0x6d2aa
cvtsi2sd %rax, %xmm1
movapd 0xa5c66(%rip), %xmm0    # 0x112dc0
xorpd %xmm1, %xmm0
movsd 0xa1f62(%rip), %xmm2    # 0x10f0c8
ucomisd %xmm1, %xmm2
jb 0x6d1b1
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x6d1ba
callq 0x9690
cvttsd2si %xmm0, %r15
movq %r15, %rax
imulq %r15, %rax
cmpq 0x8(%r14), %rax
jne 0x6d2aa
cmpq $0x7ff, %r15            # imm = 0x7FF
ja 0x6d22e
leaq 0xe17ac(%rip), %rax     # 0x14e950
movq (%rax,%r15,8), %rax
jmp 0x6d2ee
movapd %xmm1, %xmm0
callq 0x9690
movapd %xmm0, %xmm1
xorpd %xmm0, %xmm0
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xe101
movapd %xmm1, %xmm0
callq 0x9690
movapd (%rsp), %xmm1
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6d208
movq %rbx, %rdi
movsd %xmm0, 0x18(%rsp)
callq 0xb389
movsd 0x18(%rsp), %xmm0
movapd (%rsp), %xmm1
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movq $0xd, (%rax)
movsd %xmm0, 0x8(%rax)
xorl %ecx, %ecx
jmp 0x6d28a
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6d246
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 %r15, 0x8(%rax)
jmp 0x6d2ee
xorq %r12, %rax
xorq %r15, %rcx
orq %rax, %rcx
jne 0x6d283
movq %rbx, %rdi
callq 0xdd7f
xorl %ecx, %ecx
jmp 0x6d285
movb $0x1, %cl
movapd (%rsp), %xmm1
testb %cl, %cl
je 0x6d2ee
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x6d2a1
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x6d2aa
movapd %xmm1, %xmm0
callq 0x9690
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6d2cc
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), %rax
movq $0xd, (%rax)
movsd %xmm0, 0x8(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0xe8b97(%rip), %rax     # 0x155e98
jmp 0x6d2ee