// File: /workspace/github_c_makefile_star2_O2/BernardTatin[P]s7sh/s7.c
// Function: copy_stack
static s7_pointer copy_stack(s7_scheme *sc, s7_pointer old_v, int64_t top)
    {
#define CC_INITIAL_STACK_SIZE 256 /* 128 is too small here */
        int64_t i, len;
        s7_pointer new_v;
        s7_pointer *nv, *ov;

        /* stacks can grow temporarily, so sc->stack_size grows, but we don't normally need all that
         *   leftover space here, so choose the original stack size if it's smaller.
         */
        len = vector_length(old_v);
        if (len > CC_INITIAL_STACK_SIZE)
        {
            if (top < CC_INITIAL_STACK_SIZE / 4)
                len = CC_INITIAL_STACK_SIZE;
        }
        else
        {
            if (len < CC_INITIAL_STACK_SIZE)
                len = CC_INITIAL_STACK_SIZE;
        }

        if ((int64_t)(sc->free_heap_top - sc->free_heap) < (int64_t)(sc->heap_size / 8))
        {
            int64_t freed_heap;
#if S7_DEBUGGING
            freed_heap = gc(sc, __func__, __LINE__);
#else
            freed_heap = gc(sc);
#endif
            if (freed_heap < (int64_t)(sc->heap_size / 8))
                resize_heap(sc);
        }

        new_v = make_simple_vector(sc, len);
        set_type(new_v, T_STACK);
        temp_stack_top(new_v) = top;
        nv = stack_elements(new_v);
        ov = stack_elements(old_v);
        if (len > 0)
            memcpy((void *)nv, (void *)ov, ((len > vector_length(old_v)) ? vector_length(old_v) : len) * sizeof(s7_pointer));

        s7_gc_on(sc, false);

        for (i = 2; i < top; i += 4)
        {
            s7_pointer p;
            p = ov[i];                               /* args */
            if (is_pair(p))                          /* args need not be a list (it can be a port or #f, etc) */
            {
                if (is_null(cdr(p)))
                    nv[i] = list_1(sc, car(p));
                else
                {
                    if ((is_pair(cdr(p))) && (is_null(cddr(p))))
                        nv[i] = list_2(sc, car(p), cadr(p));
                    else nv[i] = protected_list_copy(sc, p);  /* args (copy is needed -- see s7test.scm) */
                }
                set_type(nv[i], (typeflag(p) & (~T_COLLECTED))); /* carry over T_IMMUTABLE */
            }
            /* lst can be dotted or circular here.  The circular list only happens in a case like:
             *    (dynamic-wind (lambda () (eq? (let ((lst (cons 1 2))) (set-cdr! lst lst) lst) (call/cc (lambda (k) k)))) (lambda () #f) (lambda () #f))
             */
            else
            {
                if (is_counter(p))                   /* these can only occur in this context */
                    nv[i] = copy_counter(sc, p);
            }
        }
        s7_gc_on(sc, true);
        return(new_v);
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r13
movq 0x8(%rsi), %rax
movq %rdx, 0x8(%rsp)
cmpq $0x40, %rdx
movl $0x100, %ecx            # imm = 0x100
movq %rax, %rbp
cmovlq %rcx, %rbp
cmpq $0x101, %rax            # imm = 0x101
cmovlq %rcx, %rbp
movq %rdi, %r14
movq 0x90(%rdi), %rcx
subq 0x88(%rdi), %rcx
movq 0xa8(%rdi), %rax
sarq $0x3, %rcx
pushq $0x8
popq %rbx
cqto
idivq %rbx
cmpq %rax, %rcx
jge 0xb734
movq %r14, %rdi
callq 0x714ee
movq %rax, %rcx
movq 0xa8(%r14), %rax
cqto
idivq %rbx
cmpq %rax, %rcx
jge 0xb734
movq %r14, %rdi
xorl %esi, %esi
callq 0x70da3
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2f202
movq $0x1e, (%rax)
movq 0x8(%rsp), %rbx
movq %rbx, 0x20(%rax)
movq %rax, 0x18(%rsp)
movq 0x10(%rax), %r12
movq 0x8(%r13), %rdx
movq 0x10(%r13), %r13
cmpq %rdx, %rbp
cmovlq %rbp, %rdx
shlq $0x3, %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x83e0
movb $0x1, 0x218(%r14)
pushq $0x2
popq %r15
movq %r13, 0x20(%rsp)
cmpq %rbx, %r15
jge 0xb923
movq (%r13,%r15,8), %rbp
movzbl (%rbp), %eax
cmpl $0x1f, %eax
je 0xb7dd
cmpl $0x1, %eax
jne 0xb91a
movq %r12, %r13
movq 0x10(%rbp), %rax
movq 0x110(%r14), %r12
cmpq %r12, %rax
je 0xb843
cmpb $0x1, (%rax)
jne 0xb7cd
cmpq %r12, 0x10(%rax)
je 0xb886
movq %r14, %rdi
movq %rbp, %rsi
callq 0x72e87
jmp 0xb8fc
movq 0x90(%r14), %rax
cmpq 0x98(%r14), %rax
ja 0xb7fc
movq %r14, %rdi
callq 0x9c27
movq 0x90(%r14), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%r14)
movq -0x8(%rax), %rax
movq $0x1f, (%rax)
movq 0x8(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq 0x28(%rbp), %rcx
movq %rcx, 0x28(%rax)
movq 0x18(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq 0x20(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq %rax, (%r12,%r15,8)
jmp 0xb91a
movq 0x8(%rbp), %rbx
movq 0x90(%r14), %rax
cmpq 0x98(%r14), %rax
ja 0xb866
movq %r14, %rdi
callq 0x9c27
movq 0x90(%r14), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%r14)
movq -0x8(%rax), %rax
movq $0x200001, (%rax)       # imm = 0x200001
movq %rbx, 0x8(%rax)
movq %r12, 0x10(%rax)
jmp 0xb8f7
movq 0x8(%rbp), %rdi
movq 0x8(%rax), %rbx
movq 0x90(%r14), %rax
cmpq 0x98(%r14), %rax
ja 0xb8b7
movq %rdi, 0x10(%rsp)
movq %r14, %rdi
callq 0x9c27
movq 0x10(%rsp), %rdi
movq 0x90(%r14), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%r14)
movq -0x8(%rax), %rcx
movl $0x200001, %esi         # imm = 0x200001
movq %rsi, (%rcx)
movq %rbx, 0x8(%rcx)
movq %r12, 0x10(%rcx)
movq 0x90(%r14), %rax
leaq -0x8(%rax), %rdx
movq %rdx, 0x90(%r14)
movq -0x8(%rax), %rax
movq %rsi, (%rax)
movq %rdi, 0x8(%rax)
movq %rcx, 0x10(%rax)
movq 0x8(%rsp), %rbx
movq %r13, %r12
movq %rax, (%r13,%r15,8)
movq (%rbp), %rcx
movq $-0x20001, %rdx         # imm = 0xFFFDFFFF
andq %rdx, %rcx
movq %rcx, (%rax)
movq 0x20(%rsp), %r13
addq $0x4, %r15
jmp 0xb787
movb $0x0, 0x218(%r14)
movq 0x18(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq