// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x3357d(%rip), %r13     # 0x111988
movzbl (%r15), %eax
leal -0x13(%rax), %ecx
cmpl $0xa, %ecx
ja 0xde445
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq *0x18(%r15)
movq 0x20(%rax), %r15
movq 0x10(%r14), %r14
cmpb $0x1, (%r14)
movq %r15, %r12
je 0xde40b
jmp 0xde6dd
cmpl $0x1, %eax
jne 0xde47f
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded9d
jmp 0xde46a
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xc3ac
movq %rax, %r15
movq 0x10(%r14), %r14
cmpb $0x1, (%r14)
movq %rax, %r12
je 0xde40b
jmp 0xde6dd
leaq 0x7ef2a(%rip), %rcx     # 0x15d3b0
cmpb $0x1, (%rax,%rcx)
jne 0xde522
cmpb $0x30, %al
jb 0xde67d
testb $0x20, 0x2(%r15)
je 0xde67d
cmpb $0x1, (%r14)
jne 0xde658
xorl %eax, %eax
movq %r14, %rcx
incq %rax
movq 0x10(%rcx), %rcx
cmpb $0x1, (%rcx)
je 0xde4b2
jmp 0xde65a
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xddf3e
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 0xde5ef
movq 0x10(%r14), %rax
cmpq 0x110(%rbx), %rax
je 0xde606
movq 0x1900(%rbx), %r12
movq 0x7cb68(%rip), %rax     # 0x15b098
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 0x3b366
jmp 0xde6dd
movq 0x560(%rbx), %rax
movq %r15, 0x8(%rax)
movq 0x560(%rbx), %rax
movq %r14, 0x10(%rax)
movq 0x560(%rbx), %rsi
movq 0xc30(%rbx), %rdx
movq %rbx, %rdi
movl $0x18, %ecx
jmp 0xde5ea
movq 0x560(%rbx), %rax
movq %r15, 0x8(%rax)
movq 0x560(%rbx), %rax
movq %r14, 0x10(%rax)
movq 0x560(%rbx), %rsi
movq 0xfa8(%rbx), %rdx
movq %rbx, %rdi
movl $0x16, %ecx
jmp 0xde5ea
movq 0x560(%rbx), %rax
movq %r15, 0x8(%rax)
movq 0x560(%rbx), %rax
movq %r14, 0x10(%rax)
movq 0x560(%rbx), %rsi
movq 0xef0(%rbx), %rdx
movq %rbx, %rdi
movl $0x17, %ecx
callq 0xde964
movq 0x120(%rbx), %rcx
movq 0x560(%rbx), %rdx
movq %rcx, 0x8(%rdx)
jmp 0xde6e0
movq 0x8(%r14), %rcx
cmpb $0xb, (%rcx)
jne 0xde627
movq 0x8(%rcx), %rdx
movq %rbx, %rdi
movq %r15, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x88fab
movq 0x1548(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0x70327(%rip), %rdx     # 0x14e960
movq 0x140(%rbx), %r8
movq 0x74471(%rip), %r9      # 0x152ab8
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xb31d
xorl %eax, %eax
movb $0x1, %cl
cmpq %rax, 0x18(%r15)
jle 0xde664
jmp 0xde679
cmpq %rax, 0x28(%r15)
jl 0xde679
movq %rbx, %rdi
movq %r14, %rsi
callq *0x10(%r15)
movq %rax, %r12
xorl %ecx, %ecx
testb %cl, %cl
je 0xde6dd
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 0xde6c8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x45a8d
movq %rax, %r14
movq %r14, 0x10(%rbx)
movl $0x198, %esi            # imm = 0x198
movq %rbx, %rdi
callq 0x10b83
movq 0x20(%rbx), %r12
movq %r12, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq