// File: /workspace/github_c_makefile_star2_O2/BernardTatin[P]s7sh/s7.c
// Function: g_log
static s7_pointer g_log(s7_scheme *sc, s7_pointer args)
    {
#define H_log "(log z1 (z2 e)) returns log(z1) / log(z2) where z2 (the base) defaults to e: (log 8 2) = 3"
#define Q_log sc->pcl_n

        s7_pointer x;
        x = car(args);
        if (!s7_is_number(x))
            return(method_or_bust_with_type(sc, x, sc->log_symbol, args, a_number_string, 1));

        if (is_pair(cdr(args)))
        {
            s7_pointer y;

            y = cadr(args);
            if (!(s7_is_number(y)))
                return(method_or_bust_with_type(sc, y, sc->log_symbol, args, a_number_string, 2));

            if (y == small_int(2))
            {
                /* (define (2^n? x) (and (not (zero? x)) (zero? (logand x (- x 1))))) */
                if (is_t_integer(x))
                {
                    s7_int ix;
                    ix = s7_integer(x);
                    if (ix > 0)
                    {
                        s7_double fx;
#if (__ANDROID__) || (MS_WINDOWS) || ((__GNUC__) && ((__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ <= 4))))
                        /* just a guess -- log2 gets a warning in gcc 4.3.2, but not in 4.4.4 */
                        fx = log((double)ix) / log(2.0);
#else
                        fx = log2((double)ix);
#endif
                        /* (s7_int)fx rounds (log 8 2) to 2 in FreeBSD! */
#if ((__GNUC__) && ((__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 4))))
                        return(make_real(sc, fx));
#else
                        if ((ix & (ix - 1)) == 0)
                            return(make_integer(sc, (s7_int)s7_round(fx)));
                        return(make_real(sc, fx));
#endif
                    }
                }
                if ((s7_is_real(x)) &&
                        (s7_is_positive(x)))
                    return(make_real(sc, log(s7_real(x)) * LOG_2));
                return(s7_from_c_complex(sc, clog(s7_to_c_complex(x)) * LOG_2));
            }

            if ((x == small_int(1)) && (y == small_int(1)))  /* (log 1 1) -> 0 (this is NaN in the bignum case) */
                return(small_int(0));

            /* (log 1 0) must be 0 since everyone says (expt 0 0) is 1 */
            if (s7_is_zero(y))
            {
                if ((y == small_int(0)) &&
                        (x == small_int(1)))
                    return(y);
                return(out_of_range(sc, sc->log_symbol, small_int(2), y, wrap_string(sc, "can't be 0", 10)));
            }

            if (s7_is_one(y))          /* this used to raise an error, but the bignum case is simpler if we return inf */
            {
                if (s7_is_one(x))      /* but (log 1.0 1.0) -> 0.0 */
                    return(real_zero);
                return(real_infinity); /* currently (log 1/0 1) is inf? */
            }

            if ((s7_is_real(x)) &&
                    (s7_is_real(y)) &&
                    (s7_is_positive(x)) &&
                    (s7_is_positive(y)))
            {
                if ((s7_is_rational(x)) &&
                        (s7_is_rational(y)))
                {
                    s7_double res;
                    s7_int ires;
                    res = log(rational_to_double(sc, x)) / log(rational_to_double(sc, y));
                    ires = (s7_int)res;
                    if (res - ires == 0.0)
                        return(make_integer(sc, ires));   /* (log 8 2) -> 3 or (log 1/8 2) -> -3 */
                    return(make_real(sc, res));         /* perhaps use rationalize here? (log 2 8) -> 1/3 */
                }
                return(make_real(sc, log(s7_real(x)) / log(s7_real(y))));
            }
            return(s7_from_c_complex(sc, clog(s7_to_c_complex(x)) / clog(s7_to_c_complex(y))));
        }

        if (s7_is_real(x))
        {
            if (s7_is_positive(x))
                return(make_real(sc, log(s7_real(x))));
            return(s7_make_complex(sc, log(-s7_real(x)), M_PI));
        }
        return(s7_from_c_complex(sc, clog(s7_to_c_complex(x))));
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %rcx
movq %rdi, %rbx
movq 0x8(%rsi), %r15
movzbl (%r15), %r13d
leaq 0xc7ae7(%rip), %rax     # 0x119970
cmpb $0x0, (%r13,%rax)
je 0x51ee5
movq 0x10(%rcx), %rdx
cmpb $0x1, (%rdx)
jne 0x51f10
movq 0x8(%rdx), %r14
movzbl (%r14), %ebp
cmpb $0x0, (%rbp,%rax)
je 0x51f9c
movq 0xc3aac(%rip), %rdx     # 0x115960
cmpq %rdx, %r14
je 0x51fe4
movq 0xc3a94(%rip), %r12     # 0x115958
movq %r15, %rax
xorq %r12, %rax
movq %r14, %rcx
xorq %r12, %rcx
orq %rax, %rcx
jne 0x52012
movq 0xc3a70(%rip), %r14     # 0x115950
jmp 0x51f8a
movq 0x1290(%rbx), %rdx
movq 0xd24ad(%rip), %r8      # 0x1243a0
pushq $0x1
popq %r9
movq %rbx, %rdi
movq %r15, %rsi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xcf9a
leaq 0xc7a99(%rip), %rax     # 0x1199b0
cmpb $0x1, (%r13,%rax)
jne 0x51fb9
movq %r15, %rdi
callq 0xc7778
movl %eax, %ebp
movq %r15, %rdi
callq 0xc42a
testb %bpl, %bpl
je 0x51fcb
callq 0x82b0
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x51f6e
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), %r14
movq $0xd, (%r14)
movsd %xmm0, 0x8(%r14)
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1290(%rbx), %rdx
movq 0xd23f6(%rip), %r8      # 0x1243a0
pushq $0x2
popq %r9
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x51efd
movq %r15, %rdi
callq 0xc779f
callq 0x82a0
jmp 0x521e1
xorps 0x83ede(%rip), %xmm0    # 0xd5eb0
callq 0x82b0
movsd 0x800a9(%rip), %xmm1    # 0xd2088
jmp 0x521e1
cmpl $0xb, %r13d
jne 0x52093
movq 0x8(%r15), %rax
testq %rax, %rax
jle 0x52093
cvtsi2sd %rax, %xmm0
callq 0x82b0
divsd 0x80083(%rip), %xmm0    # 0xd2090
jmp 0x51f43
movq %rdx, 0x8(%rsp)
movq %r14, %rdi
callq 0xc77c4
testb %al, %al
je 0x52120
cmpq %r12, %r15
jne 0x52039
cmpq 0xc391d(%rip), %r14     # 0x115950
je 0x51f8a
movq 0x1290(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0x820(%rbx), %rax
movslq 0x7dc(%rbx), %rcx
movq (%rax,%rcx,8), %r8
leal 0x1(%rcx), %eax
andl $0x7, %eax
movl %eax, 0x7dc(%rbx)
leaq 0x9562e(%rip), %rax     # 0xe7697
movq %rax, 0x10(%r8)
movq $0xa, 0x8(%r8)
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
movq %r14, %rcx
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xcb8f
leaq 0xc7916(%rip), %rax     # 0x1199b0
cmpb $0x1, (%r13,%rax)
jne 0x520da
movq %r15, %rdi
callq 0xc7778
testb %al, %al
je 0x520da
movq %r15, %rdi
callq 0xc42a
callq 0x82b0
movsd %xmm0, 0x20(%rsp)
fldt 0x83e49(%rip)           # 0xd5f10
fmull 0x20(%rsp)
fstpl 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
jmp 0x51f43
movq %r15, %rdi
callq 0xc779f
callq 0x82a0
movsd %xmm0, 0x40(%rsp)
movsd %xmm1, 0x38(%rsp)
fldt 0x83e17(%rip)           # 0xd5f10
fld %st(0)
fmull 0x40(%rsp)
fxch %st(1)
fmull 0x38(%rsp)
fxch %st(1)
fstpl 0x30(%rsp)
movsd 0x30(%rsp), %xmm0
fstpl 0x28(%rsp)
movsd 0x28(%rsp), %xmm1
jmp 0x521e1
movq %r14, %rdi
callq 0x79edb
testb %al, %al
je 0x52148
movq %r15, %rdi
callq 0x79edb
testb %al, %al
je 0x521f7
movq 0xc7b15(%rip), %r14     # 0x119c58
jmp 0x51f8a
leaq 0xc7861(%rip), %rax     # 0x1199b0
cmpb $0x1, (%r13,%rax)
jne 0x521a4
cmpb $0x1, (%rbp,%rax)
jne 0x521a4
movq %r15, %rdi
callq 0xc7778
testb %al, %al
je 0x521a4
movq %r14, %rdi
callq 0xc7778
testb %al, %al
je 0x521a4
leaq 0xc7873(%rip), %rax     # 0x1199f0
cmpb $0x1, (%r13,%rax)
jne 0x52203
cmpb $0x1, (%rbp,%rax)
jne 0x52203
movq 0x8(%r15), %rax
cmpl $0xb, %r13d
jne 0x52273
cvtsi2sd %rax, %xmm0
jmp 0x52295
movq %r15, %rdi
callq 0xc779f
callq 0x82a0
movsd %xmm0, 0x8(%rsp)
movsd %xmm1, 0x10(%rsp)
movq %r14, %rdi
callq 0xc779f
callq 0x82a0
movaps %xmm0, %xmm2
movaps %xmm1, %xmm3
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0xd1910
movq %rbx, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc152
movq 0xcaca2(%rip), %r14     # 0x11cea0
jmp 0x51f8a
movq %r15, %rdi
callq 0xc42a
callq 0x82b0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0xc42a
callq 0x82b0
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x52252
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r14
movq $0xd, (%r14)
movsd 0x8(%rsp), %xmm0
jmp 0x51f84
movq %rax, 0x68(%rsp)
fildll 0x68(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x70(%rsp)
fildll 0x70(%rsp)
fdivrp %st, %st(1)
fstpl 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
callq 0x82b0
cmpb $0xb, (%r14)
movq 0x8(%r14), %rax
movsd %xmm0, 0x8(%rsp)
jne 0x522b1
cvtsi2sd %rax, %xmm0
jmp 0x522d3
movq %rax, 0x50(%rsp)
fildll 0x50(%rsp)
movq 0x10(%r14), %rax
movq %rax, 0x58(%rsp)
fildll 0x58(%rsp)
fdivrp %st, %st(1)
fstpl 0x48(%rsp)
movsd 0x48(%rsp), %xmm0
callq 0x82b0
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %r15
cvtsi2sd %r15, %xmm0
ucomisd %xmm0, %xmm1
jne 0x5222d
jp 0x5222d
cmpq $0x7ff, %r15            # imm = 0x7FF
ja 0x52315
leaq 0xc3644(%rip), %rax     # 0x115950
movq (%rax,%r15,8), %r14
jmp 0x51f8a
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x52334
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r14
movq $0xb, (%r14)
movq %r15, 0x8(%r14)
jmp 0x51f8a