// File: /workspace/github_c_makefile_star2_O2/BernardTatin[P]s7sh/s7.c
// Function: implicit_index
static s7_pointer implicit_index(s7_scheme *sc, s7_pointer obj, s7_pointer indices)
                {
                    s7_pointer res;
                    /* (let ((lst '("12" "34"))) (lst 0 1)) -> #\2
                     * (let ((lst (list #(1 2) #(3 4)))) (lst 0 1)) -> 2
                     *
                     * this can get tricky:
                     *   ((list (lambda (a) (+ a 1)) (lambda (b) (* b 2))) 1 2) -> 4
                     * but what if func takes rest/optional args, etc?
                     *   ((list (lambda args (car args))) 0 "hi" 0)
                     *   should this return #\h or "hi"?? currently it is "hi" which is consistent with ((lambda args (car args)) "hi" 0)
                     * but ((lambda (arg) arg) "hi" 0) is currently an error (too many arguments)
                     * maybe it should be (((lambda (arg) arg) "hi") 0) -> #\h
                     */

                    switch (type(obj))
                    {
                        case T_VECTOR:                       /* (#(#(1 2) #(3 4)) 1 1) -> 4 */
                            return(vector_ref_1(sc, obj, indices));

                        case T_FLOAT_VECTOR:
                            set_car(sc->u1_1, obj);
                            set_cdr(sc->u1_1, indices);
                            res = univect_ref(sc, sc->u1_1, sc->float_vector_ref_symbol, T_FLOAT_VECTOR);
                            set_car(sc->u1_1, sc->F);
                            return(res);

                        case T_INT_VECTOR:
                            set_car(sc->u1_1, obj);
                            set_cdr(sc->u1_1, indices);
                            res = univect_ref(sc, sc->u1_1, sc->int_vector_ref_symbol, T_INT_VECTOR);
                            set_car(sc->u1_1, sc->F);
                            return(res);

                        case T_BYTE_VECTOR:
                            set_car(sc->u1_1, obj);
                            set_cdr(sc->u1_1, indices);
                            res = univect_ref(sc, sc->u1_1, sc->byte_vector_ref_symbol, T_BYTE_VECTOR);
                            set_car(sc->u1_1, sc->F);
                            return(res);

                        case T_STRING:                       /* (#("12" "34") 0 1) -> #\2 */
                            if (is_null(cdr(indices)))
                            {
                                if (!is_t_integer(car(indices)))
                                    return(wrong_type_argument(sc, sc->string_ref_symbol, 2, car(indices), T_INTEGER));
                                return(string_ref_p_pi_direct(sc, obj, integer(car(indices))));
                            }
                            return(s7_error(sc, sc->wrong_number_of_args_symbol, set_elist_3(sc, too_many_arguments_string, obj, indices)));

                        case T_PAIR:                         /* (#((1 2) (3 4)) 1 0) -> 3, (#((1 (2 3))) 0 1 0) -> 2 */
                            obj = list_ref_1(sc, obj, car(indices));
                            if (is_pair(cdr(indices)))
                                return(implicit_index(sc, obj, cdr(indices)));
                            return(obj);

                        case T_HASH_TABLE:                   /* ((vector (hash-table '(a . 1) '(b . 2))) 0 'a) -> 1 */
                            obj = s7_hash_table_ref(sc, obj, car(indices));
                            if (is_pair(cdr(indices)))
                                return(implicit_index(sc, obj, cdr(indices)));
                            return(obj);

                        case T_C_OBJECT:
                            /* return((*(c_object_ref(sc, obj)))(sc, cons(sc, obj, indices))); */
                            set_car(sc->u1_1, obj);
                            set_cdr(sc->u1_1, indices);
                            res = (*(c_object_ref(sc, obj)))(sc, sc->u1_1);
                            set_car(sc->u1_1, sc->F);
                            return(res);

                        case T_LET:
                            obj = s7_let_ref(sc, obj, car(indices));
                            if (is_pair(cdr(indices)))
                                return(implicit_index(sc, obj, cdr(indices)));
                            return(obj);

                        case T_ITERATOR: /* indices is not nil, so this is an error */
                            return(s7_error(sc, sc->wrong_number_of_args_symbol, set_elist_3(sc, too_many_arguments_string, obj, indices)));

                        default:                             /* (#(a b c) 0 1) -> error, but ((list (lambda (x) x)) 0 "hi") -> "hi" */
                            if (is_applicable(obj))            /* (apply (list cons cons) (list 1 2)) needs the argnum check mentioned below */
                            {
                                if ((is_c_function(obj)) &&
                                        (is_safe_procedure(obj)))
                                {
                                    s7_int len;
                                    len = safe_list_length(indices);
                                    if ((c_function_required_args(obj) <= len) &&
                                            (c_function_all_args(obj) >= len))
                                        return(c_function_call(obj)(sc, indices));
                                }
                                push_stack(sc, OP_EVAL_DONE, sc->args, sc->code);
                                sc->code = obj;
                                sc->args = (needs_copied_args(obj)) ? copy_list(sc, indices) : indices;
                                eval(sc, OP_APPLY);
                                return(sc->value);
                                /* return(s7_apply_function(sc, obj, indices)); -- needs argnum check */ /* was g_apply 23-Jan-19 which assumes we're not in map */
                            }
                            return(s7_error(sc, sc->wrong_number_of_args_symbol, set_elist_3(sc, too_many_arguments_string, obj, indices)));
                    }
                }

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x2ba03(%rip), %r12     # 0xd49e8
movzbl (%r15), %eax
leal -0x13(%rax), %ecx
cmpl $0xa, %ecx
ja 0xa900e
movslq (%r12,%rcx,4), %rcx
addq %r12, %rcx
jmpq *%rcx
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq *0x18(%r15)
movq 0x20(%rax), %r15
jmp 0xa9036
cmpl $0x1, %eax
jne 0xa9045
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xa96db
jmp 0xa9033
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xa842
movq %rax, %r15
movq 0x10(%r14), %r14
cmpb $0x1, (%r14)
je 0xa8fe5
jmp 0xa918f
leaq 0x7b364(%rip), %rcx     # 0x1243b0
cmpb $0x1, (%rax,%rcx)
jne 0xa9151
cmpb $0x30, %al
jb 0xa9079
testb $0x20, 0x2(%r15)
je 0xa9079
movq %r14, %rdi
callq 0x313be
cmpq %rax, 0x18(%r15)
jg 0xa9079
cmpq %rax, 0x28(%r15)
jge 0xa927b
movq (%rbx), %rax
movq 0x50(%rbx), %rcx
movq %rax, (%rcx)
movq 0x8(%rbx), %rax
movq 0x50(%rbx), %rcx
movq %rax, 0x8(%rcx)
movq 0x10(%rbx), %rax
movq 0x50(%rbx), %rcx
movq %rax, 0x10(%rcx)
movq 0x50(%rbx), %rax
movq $0x1df, 0x18(%rax)      # imm = 0x1DF
addq $0x20, 0x50(%rbx)
movq %r15, (%rbx)
testb $0x10, 0x3(%r15)
je 0xa90c4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x35492
movq %rax, %r14
movq %r14, 0x10(%rbx)
movl $0x198, %esi            # imm = 0x198
movq %rbx, %rdi
callq 0xe1a8
movq 0x20(%rbx), %r15
jmp 0xa918f
movq 0x560(%rbx), %rax
movq %r15, 0x8(%rax)
movq 0x560(%rbx), %rax
movq %r14, 0x10(%rax)
movq 0x560(%rbx), %rsi
movq 0xc30(%rbx), %rdx
pushq $0x18
jmp 0xa91fe
movq 0x560(%rbx), %rax
movq %r15, 0x8(%rax)
movq 0x560(%rbx), %rax
movq %r14, 0x10(%rax)
movq 0x8d8(%rbx), %rax
movq 0x8(%r15), %rcx
movq (%rax,%rcx,8), %rax
movq 0x560(%rbx), %rsi
movq %rbx, %rdi
callq *0x48(%rax)
jmp 0xa9207
movq 0x10(%r14), %rax
cmpq 0x110(%rbx), %rax
je 0xa9225
movq 0x1900(%rbx), %r12
movq 0x78f39(%rip), %rax     # 0x122098
movq 0x6e8(%rbx), %rcx
movq %rax, 0x8(%rcx)
movq 0x10(%rcx), %rax
movq %r15, 0x8(%rax)
movq 0x10(%rax), %rax
movq %r14, 0x8(%rax)
movq 0x6e8(%rbx), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2cdc0
movq %r12, %r15
movq %r15, %rax
jmp 0xa9219
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xa8d11
movq 0x560(%rbx), %rax
movq %r15, 0x8(%rax)
movq 0x560(%rbx), %rax
movq %r14, 0x10(%rax)
movq 0x560(%rbx), %rsi
movq 0xfa8(%rbx), %rdx
pushq $0x16
jmp 0xa91fe
movq 0x560(%rbx), %rax
movq %r15, 0x8(%rax)
movq 0x560(%rbx), %rax
movq %r14, 0x10(%rax)
movq 0x560(%rbx), %rsi
movq 0xef0(%rbx), %rdx
pushq $0x17
popq %rcx
movq %rbx, %rdi
callq 0xa941e
movq 0x120(%rbx), %rcx
movq 0x560(%rbx), %rdx
movq %rcx, 0x8(%rdx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x8(%r14), %rcx
cmpb $0xb, (%rcx)
jne 0xa9248
movq 0x8(%rcx), %rdx
movq %rbx, %rdi
movq %r15, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x68fb8
movq 0x1548(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0x6c706(%rip), %rdx     # 0x115960
movq 0x140(%rbx), %r8
movq 0x70850(%rip), %r9      # 0x119ab8
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x9bbb
movq 0x10(%r15), %rax
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmpq *%rax