// File: /workspace/github_c_makefile_star2_O2/BernardTatin[P]s7sh/s7.c
// Function: g_map
static s7_pointer g_map(s7_scheme *sc, s7_pointer args)
                {
#define H_map "(map proc object . objects) applies proc to a list made up of the next element of each of its arguments, returning \
                    a list of the results.  Its arguments can be lists, vectors, strings, hash-tables, or any applicable objects."
#define Q_map s7_make_circular_signature(sc, 2, 3, sc->is_proper_list_symbol, sc->is_procedure_symbol, sc->is_sequence_symbol)

                    s7_pointer p, f;
                    s7_int len;
                    bool got_nil = false;

                    f = car(args);                                /* the function */
                    for (len = 0, p = cdr(args); is_not_null(p); p = cdr(p), len++)
                    {
                        if (!is_mappable(car(p)))
                        {
                            if (is_null(car(p)))
                                got_nil = true;
                            else return(simple_wrong_type_argument_with_type(sc, sc->map_symbol, car(p), a_sequence_string));
                        }
                    }

                    switch (type(f))
                    {
                        case T_C_FUNCTION:
                        case T_C_RST_ARGS_FUNCTION:
                            if ((c_function_required_args(f) > len) ||
                                    (c_function_all_args(f) < len))
                                return(s7_error(sc, sc->wrong_number_of_args_symbol,
                                            set_elist_3(sc, wrap_string(sc, "map ~A: ~A args?", 16), f, small_int(len))));

                        case T_C_OPT_ARGS_FUNCTION:
                        case T_C_ANY_ARGS_FUNCTION:
                            /* if function is safe c func, do the map locally */
                            if (got_nil) return(sc->nil);
                            if (is_safe_procedure(f))
                            {
                                s7_function func;
                                func = c_function_call(f);
                                if ((is_pair(cadr(args))) &&
                                        (len == 1))
                                {
                                    s7_pointer f_args, val, fast, slow;
                                    f_args = list_1(sc, sc->F);
                                    val = list_1(sc, sc->nil);
                                    push_stack_no_let(sc, OP_GC_PROTECT, f_args, val);
                                    for (fast = cadr(args), slow = cadr(args); is_pair(fast); fast = cdr(fast), slow = cdr(slow))
                                    {
                                        s7_pointer z;
                                        set_car(f_args, car(fast));
                                        z = func(sc, f_args);
                                        if (z != sc->no_value)
                                            set_car(val, cons(sc, z, car(val)));
                                        if (is_pair(cdr(fast)))
                                        {
                                            fast = cdr(fast);
                                            if (fast == slow)
                                                break;
                                            set_car(f_args, car(fast));
                                            z = func(sc, f_args);
                                            if (z != sc->no_value)
                                                set_car(val, cons(sc, z, car(val)));
                                        }
                                    }
                                    unstack(sc);
                                    return(safe_reverse_in_place(sc, car(val)));
                                }
                                else
                                {
                                    s7_pointer val, val1, old_args, iter_list;
                                    sc->z = make_iterators(sc, args);
                                    val1 = cons(sc, sc->z, make_list(sc, len, sc->nil));
                                    iter_list = sc->z;
                                    old_args = sc->args;
                                    func = c_function_call(f);
                                    push_stack_no_let(sc, OP_GC_PROTECT, val1, val = cons(sc, sc->nil, sc->code)); /* temporary GC protection: need to protect val1, iter_list, val */
                                    sc->z = sc->nil;
                                    while (true)
                                    {
                                        s7_pointer x, y, z;
                                        for (x = iter_list, y = cdr(val1); is_pair(x); x = cdr(x), y = cdr(y))
                                        {
                                            set_car(y, s7_iterate(sc, car(x)));
                                            if (iterator_is_at_end(car(x)))
                                            {
                                                unstack(sc);
                                                /* free_cell(sc, car(x)); */ /* 16-Jan-19 iterator in circular list -- see s7test */
                                                sc->args = T_Pos(old_args);
                                                return(safe_reverse_in_place(sc, car(val)));
                                            }
                                        }
                                        z = func(sc, cdr(val1)); /* can this contain multiple-values? */
                                        if (z != sc->no_value)
                                            set_car(val, cons(sc, z, car(val)));
                                    }
                                }
                            }
                            else /* not safe procedure */
                            {
                                /* to mimic map values handling elsewhere:
                                 *   ((lambda args (format *stderr* "~A~%" (map values args))) (values)):   ()
                                 *   ((lambda args (format *stderr* "~A~%" (map values args))) (values #<unspecified>)): #<unspecified> etc
                                 */
                                if ((f == slot_value(global_slot(sc->values_symbol))) &&
                                        (len == 1) &&
                                        (!has_methods(cadr(args)))) /* iterator should be ok here -- object_to_list can handle it */
                                {
                                    p = object_to_list(sc, cadr(args));
                                    if (p != cadr(args))
                                        return(p);
                                }
                            }
                            break;

                        case T_CLOSURE:
                            {
                                int32_t fargs;
                                fargs = closure_arity_to_int(sc, f);
                                /* if closure call is straightforward, use OP_MAP_1 */
                                if ((len == 1) &&
                                        (fargs == 1) &&
                                        (!is_constant_symbol(sc, car(closure_args(f)))))
                                {
                                    /* g_map_closure here if not s7_tree_memq 'map takes more time than it saves */
                                    if (got_nil) return(sc->nil);
                                    /* don't go to OP_MAP_2 here! It assumes no recursion */
                                    sc->z = (!is_iterator(cadr(args))) ? s7_make_iterator(sc, cadr(args)) : cadr(args);
                                    push_stack(sc, OP_MAP_1, make_counter(sc, sc->z), f);
                                    sc->z = sc->nil;
                                    return(sc->nil);
                                }
                                if ((fargs > len) ||
                                        ((fargs < len) &&
                                         ((fargs >= 0) ||
                                          (abs(fargs) > len))))
                                    return(s7_error(sc, sc->wrong_number_of_args_symbol,
                                                set_elist_3(sc, wrap_string(sc, "map ~A: ~A args?", 16), f, small_int(len))));
                                if (got_nil) return(sc->nil);
                            }
                            break;

                        default:
                            if (!is_applicable(f))
                                return(method_or_bust_with_type(sc, f, sc->map_symbol, args, something_applicable_string, 1));

                            if ((!is_pair(f)) &&
                                    (!s7_is_aritable(sc, f, len)))
                                return(s7_error(sc, sc->wrong_number_of_args_symbol,
                                            set_elist_3(sc, wrap_string(sc, "map ~A: ~A args?", 16), f, small_int(len))));
                            if (got_nil) return(sc->nil);
                            break;
                    }

                    set_current_code(sc, sc->code);
                    sc->z = make_iterators(sc, args);
                    push_stack(sc, OP_MAP, make_counter(sc, sc->z), f);
                    sc->z = sc->nil;
                    return(sc->nil);
                }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movq 0x8(%rsi), %rsi
leaq 0x10(%r12), %r8
movq 0x110(%rdi), %r15
leaq 0xb0add(%rip), %r14     # 0x115950
xorl %ebp, %ebp
leaq 0xbf5b4(%rip), %rax     # 0x124430
xorl %r13d, %r13d
movq %r8, %rcx
movq (%rcx), %rcx
cmpq %r15, %rcx
je 0x64eb0
movq 0x8(%rcx), %rdx
movzbl (%rdx), %edi
cmpb $0x0, (%rdi,%rax)
jne 0x64ea3
movb $0x1, %bpl
cmpq %r15, %rdx
jne 0x64f31
addq $0x10, %rcx
incq %r13
addq $0x8, %r14
jmp 0x64e82
movzbl (%rsi), %eax
leal -0x28(%rax), %ecx
cmpl $0xb, %ecx
ja 0x65089
leaq 0x6edba(%rip), %rdx     # 0xd3c80
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpq %r13, 0x18(%rsi)
jg 0x64edf
cmpq %r13, 0x28(%rsi)
jge 0x64f60
movq 0x1900(%rbx), %r15
movq 0x820(%rbx), %rax
movslq 0x7dc(%rbx), %rcx
movq (%rax,%rcx,8), %rax
incl %ecx
andl $0x7, %ecx
movl %ecx, 0x7dc(%rbx)
leaq 0x82ef2(%rip), %rcx     # 0xe7dfc
movq %rcx, 0x10(%rax)
movq $0x10, 0x8(%rax)
movq (%r14), %rcx
movq 0x6e8(%rbx), %rdx
movq %rax, 0x8(%rdx)
movq 0x10(%rdx), %rax
movq %rsi, 0x8(%rax)
jmp 0x6506a
movq 0x1320(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0x140(%rbx), %rcx
movq 0xb4cc6(%rip), %r8      # 0x119c10
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd456
testb $0x1, %bpl
jne 0x655a9
testb $0x20, 0x2(%rsi)
jne 0x650c1
xorq $0x1, %r13
movq 0x1638(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
movq %rsi, (%rsp)
xorq %rsi, %rax
orq %r13, %rax
jne 0x65301
movq (%r8), %rax
movq 0x8(%rax), %rsi
testb $0x40, 0x3(%rsi)
jne 0x65301
movq %rbx, %rdi
movq %r8, %r14
callq 0x79f0b
movq %rax, %r15
movq (%r14), %rax
cmpq 0x8(%rax), %r15
jne 0x655a9
jmp 0x65301
movq %r8, 0x8(%rsp)
movq %rbx, %rdi
movq %rsi, (%rsp)
callq 0xa2581
movl %eax, %r15d
cmpq $0x1, %r13
sete %al
cmpl $0x1, %r15d
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x6500d
movq (%rsp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x72bfa
testb %al, %al
je 0x6559c
movslq %r15d, %rax
cmpq %rax, %r13
jge 0x652db
movq 0x1900(%rbx), %r15
movq 0x820(%rbx), %rax
movslq 0x7dc(%rbx), %rcx
movq (%rax,%rcx,8), %rax
incl %ecx
andl $0x7, %ecx
movl %ecx, 0x7dc(%rbx)
leaq 0x82db8(%rip), %rcx     # 0xe7dfc
movq %rcx, 0x10(%rax)
movq $0x10, 0x8(%rax)
movq (%r14), %rcx
movq 0x6e8(%rbx), %rdx
movq %rax, 0x8(%rdx)
movq 0x10(%rdx), %rax
movq (%rsp), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x6e8(%rbx), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2cdc0
jmp 0x655a9
leaq 0xbf320(%rip), %rcx     # 0x1243b0
cmpb $0x0, (%rax,%rcx)
je 0x652b0
cmpl $0x1, %eax
je 0x650b8
movq %rbx, %rdi
movq %r13, %rdx
movq %rsi, %r15
callq 0x32378
movq %r15, %rsi
testb %al, %al
je 0x64edf
movq %rsi, (%rsp)
jmp 0x652f7
cmpq $0x1, %r13
sete %al
movq (%r8), %rcx
movq 0x8(%rcx), %rcx
cmpb $0x1, (%rcx)
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x653bc
movq 0x10(%rsi), %rax
movq %rax, 0x8(%rsp)
movq 0x90(%rbx), %rax
movq 0x120(%rbx), %r12
cmpq 0x98(%rbx), %rax
ja 0x65115
movq %rbx, %rdi
movq %r8, %r14
callq 0x9c27
movq %r14, %r8
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r14
movq $0x200001, (%r14)       # imm = 0x200001
movq %r12, 0x8(%r14)
movq %r15, 0x10(%r14)
movq 0x90(%rbx), %rax
movq 0x110(%rbx), %r15
cmpq 0x98(%rbx), %rax
ja 0x6515f
movq %rbx, %rdi
movq %r8, %r12
callq 0x9c27
movq %r12, %r8
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rcx
movq $0x200001, (%rcx)       # imm = 0x200001
movq %r15, 0x8(%rcx)
movq %r15, 0x10(%rcx)
movq 0x50(%rbx), %rax
movq %rcx, (%rsp)
movq %rcx, (%rax)
movq 0x50(%rbx), %rax
movq %r14, 0x10(%rax)
movq 0x50(%rbx), %rax
movq $0x1, 0x18(%rax)
addq $0x20, 0x50(%rbx)
movq (%r8), %rax
movq 0x8(%rax), %r12
movq %r12, %rbp
cmpb $0x1, (%rbp)
jne 0x65569
movq 0x8(%rbp), %rax
movq %rax, 0x8(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq *0x8(%rsp)
cmpq 0x138(%rbx), %rax
je 0x65220
movq %rax, %r15
movq (%rsp), %rax
movq 0x8(%rax), %r13
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x651fa
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movq $0x200001, (%rax)       # imm = 0x200001
movq %r15, 0x8(%rax)
movq %r13, 0x10(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movq 0x10(%rbp), %r13
cmpb $0x1, (%r13)
jne 0x652a2
cmpq %r12, %r13
je 0x65569
movq 0x8(%r13), %rax
movq %rax, 0x8(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq *0x8(%rsp)
cmpq 0x138(%rbx), %rax
je 0x6529f
movq %rax, %r15
movq (%rsp), %rax
movq 0x8(%rax), %rbp
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x65279
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movq $0x200001, (%rax)       # imm = 0x200001
movq %r15, 0x8(%rax)
movq %rbp, 0x10(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movq %r13, %rbp
movq 0x10(%rbp), %rbp
movq 0x10(%r12), %r12
jmp 0x651ab
movq 0x1320(%rbx), %rdx
movq 0xbf362(%rip), %r8      # 0x124620
pushq $0x1
popq %r9
movq %rbx, %rdi
movq %r12, %rcx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xcf9a
jle 0x652f7
testl %r15d, %r15d
setns %al
negl %r15d
cmpq %r15, %r13
setb %cl
orb %al, %cl
cmpb $0x1, %cl
je 0x65019
testb $0x1, %bpl
jne 0x655a2
movq (%rbx), %rax
movq %rax, 0x28(%rbx)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xcc76d
movq %rax, 0x4f0(%rbx)
movq 0x50(%rbx), %rax
movq (%rsp), %rcx
movq %rcx, (%rax)
movq 0x8(%rbx), %rax
movq 0x50(%rbx), %rcx
movq %rax, 0x8(%rcx)
movq 0x4f0(%rbx), %r14
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x65357
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movq $0x1f, (%rax)
movq 0x110(%rbx), %rcx
movq %rcx, 0x8(%rax)
movq %r14, 0x10(%rax)
andq $0x0, 0x28(%rax)
movq 0x110(%rbx), %rcx
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq 0x50(%rbx), %rcx
movq %rax, 0x10(%rcx)
movq 0x50(%rbx), %rax
movq $0x1fc, 0x18(%rax)      # imm = 0x1FC
addq $0x20, 0x50(%rbx)
movq 0x110(%rbx), %r15
movq %r15, 0x4f0(%rbx)
jmp 0x655a9
movq %rsi, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xcc76d
movq %rax, %r14
movq %rax, 0x4f0(%rbx)
movq 0x110(%rbx), %rdx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x2ddf5
movq %rax, %r12
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x65409
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r15
movq $0x200001, (%r15)       # imm = 0x200001
movq %r14, 0x8(%r15)
movq %r12, 0x10(%r15)
movq 0x4f0(%rbx), %rbp
movq (%rbx), %r14
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x110(%rbx), %r12
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6546d
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rcx
movq $0x200001, (%rcx)       # imm = 0x200001
movq %r12, 0x8(%rcx)
movq %r14, 0x10(%rcx)
movq 0x50(%rbx), %rax
movq %rcx, (%rsp)
movq %rcx, (%rax)
movq 0x50(%rbx), %rax
movq %r15, 0x10(%rax)
addq $0x10, %r15
movq 0x50(%rbx), %rax
movq $0x1, 0x18(%rax)
addq $0x20, 0x50(%rbx)
movq 0x110(%rbx), %rax
movq %rax, 0x4f0(%rbx)
movq %rbp, %r12
movq %r15, %r13
cmpb $0x1, (%r12)
jne 0x654fb
movq (%r13), %r13
movq 0x8(%r12), %rsi
movq %rbx, %rdi
callq *0x28(%rsi)
movq %rax, 0x8(%r13)
movq 0x8(%r12), %rax
testb $-0x80, 0x3(%rax)
je 0x65570
movq 0x10(%r12), %r12
addq $0x10, %r13
jmp 0x654c7
movq (%r15), %rsi
movq %rbx, %rdi
callq *0x8(%rsp)
movq %rbp, %r12
movq %r15, %r13
cmpq 0x138(%rbx), %rax
je 0x654c7
movq %rax, %r14
movq (%rsp), %rax
movq 0x8(%rax), %r12
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6553e
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movq $0x200001, (%rax)       # imm = 0x200001
movq %r14, 0x8(%rax)
movq %r12, 0x10(%rax)
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
jmp 0x654c1
addq $-0x20, 0x50(%rbx)
jmp 0x6557e
addq $-0x20, 0x50(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x10(%rbx)
movq (%rsp), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa833
testb $0x1, %bpl
je 0x655bb
movq 0x110(%rbx), %r15
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
cmpb $0x1d, (%rax)
je 0x655d7
movq %rbx, %rdi
movq %rax, %rsi
callq 0xa3c7
movq (%rsp), %rcx
movq %rax, 0x4f0(%rbx)
movq 0x50(%rbx), %rax
movq %rcx, (%rax)
movq 0x8(%rbx), %rax
movq 0x50(%rbx), %rcx
movq %rax, 0x8(%rcx)
movq 0x4f0(%rbx), %r14
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x6561b
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movq $0x1f, (%rax)
movq 0x110(%rbx), %rcx
movq %rcx, 0x8(%rax)
movq %r14, 0x10(%rax)
andq $0x0, 0x28(%rax)
movq 0x110(%rbx), %rcx
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq 0x50(%rbx), %rcx
movq %rax, 0x10(%rcx)
movq 0x50(%rbx), %rax
movq $0x1fd, 0x18(%rax)      # imm = 0x1FD
jmp 0x653a4