// File: /workspace/github_c_makefile_star2_O1/BernardTatin[P]s7sh/s7.c
// Function: g_dynamic_wind
static s7_pointer g_dynamic_wind(s7_scheme *sc, s7_pointer args)
                {
#define H_dynamic_wind "(dynamic-wind init body finish) calls init, then body, then finish, \
                    each a function of no arguments, guaranteeing that finish is called even if body is exited"
#define Q_dynamic_wind s7_make_circular_signature(sc, 1, 2, sc->values_symbol, sc->is_procedure_symbol)

                    s7_pointer p, inp, outp;

                    if (!is_dwind_thunk(sc, car(args)))
                        return(method_or_bust_with_type(sc, car(args), sc->dynamic_wind_symbol, args, a_thunk_string, 1));
                    if (!is_thunk(sc, cadr(args)))
                        return(method_or_bust_with_type(sc, cadr(args), sc->dynamic_wind_symbol, args, a_thunk_string, 2));
                    if (!is_dwind_thunk(sc, caddr(args)))
                        return(method_or_bust_with_type(sc, caddr(args), sc->dynamic_wind_symbol, args, a_thunk_string, 3));

                    /* this won't work:
                       (let ((final (lambda (a b c) (list a b c))))
                       (dynamic-wind
                       (lambda () #f)
                       (lambda () (set! final (lambda () (display "in final"))))
                       final))
                     * but why not?  'final' is a thunk by the time it is evaluated. catch (the error handler) is similar.
                     * It can't work here because we set up the dynamic_wind_out slot below and
                     *   even if the thunk check was removed, we'd still be trying to apply the original function.
                     */
                    new_cell(sc, p, T_DYNAMIC_WIND);                          /* don't mark car/cdr, don't copy */
                    dynamic_wind_in(p) = closure_or_f(sc, car(args));
                    dynamic_wind_body(p) = cadr(args);
                    dynamic_wind_out(p) = closure_or_f(sc, caddr(args));

                    inp = dynamic_wind_in(p);
                    if ((is_any_closure(inp)) && (!is_safe_closure(inp)))    /* wrap this use of inp in a with-baffle */
                        dynamic_wind_in(p) = make_baffled_closure(sc, inp);

                    outp = dynamic_wind_out(p);
                    if ((is_any_closure(outp)) && (!is_safe_closure(outp)))
                        dynamic_wind_out(p) = make_baffled_closure(sc, outp);

                    /* since we don't care about the in and out results, and they are thunks, if the body is not a pair,
                     *   or is a quoted thing, we just ignore that function.
                     */
                    push_stack(sc, OP_DYNAMIC_WIND, sc->nil, p);          /* args will be the saved result, code = s7_dynwind_t obj */
                    if (inp != sc->F)
                    {
                        dynamic_wind_state(p) = DWIND_INIT;
                        push_stack(sc, OP_APPLY, sc->nil, dynamic_wind_in(p));
                    }
                    else
                    {
                        dynamic_wind_state(p) = DWIND_BODY;
                        push_stack(sc, OP_APPLY, sc->nil, dynamic_wind_body(p));
                    }
                    return(sc->F);
                }

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rsi), %r15
movq %r15, %rsi
callq 0x1090e8
testb %al, %al
je 0x8460e
movq 0x10(%r14), %rax
movq 0x8(%rax), %rsi
cmpb $0x26, (%rsi)
jb 0x8462d
movq %rbx, %rdi
xorl %edx, %edx
callq 0x41fac
testb %al, %al
je 0x8462d
movq 0x10(%r14), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1090e8
testb %al, %al
je 0x8465f
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x844d9
movq %rbx, %rdi
callq 0xb389
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r12
movq $0x1a, (%r12)
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x109128
movq %rax, %r15
movq %rax, 0x8(%r12)
movq 0x10(%r14), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%r12)
movq 0x10(%r14), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x109128
movq %rax, 0x10(%r12)
movzbl (%r15), %eax
leaq 0xd66a4(%rip), %r14     # 0x15abe0
cmpb $0x1, (%rax,%r14)
jne 0x8455a
testb $0x10, 0x1(%r15)
jne 0x8455a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10915c
movq %rax, 0x8(%r12)
movq 0x10(%r12), %rsi
movzbl (%rsi), %eax
cmpb $0x1, (%rax,%r14)
jne 0x8457c
testb $0x10, 0x1(%rsi)
jne 0x8457c
movq %rbx, %rdi
callq 0x10915c
movq %rax, 0x10(%r12)
movq 0x50(%rbx), %rax
movq %r12, (%rax)
movq 0x8(%rbx), %rax
movq 0x50(%rbx), %rcx
movq %rax, 0x8(%rcx)
movq 0x50(%rbx), %rax
movq 0x110(%rbx), %rcx
movq %rcx, 0x10(%rax)
movq 0x50(%rbx), %rax
movq $0x1e1, 0x18(%rax)      # imm = 0x1E1
addq $0x20, 0x50(%rbx)
xorl %eax, %eax
cmpq 0x120(%rbx), %r15
sete %al
movl %eax, 0x20(%r12)
shll $0x4, %eax
movq 0x8(%r12,%rax), %rax
movq 0x50(%rbx), %rcx
movq %rax, (%rcx)
movq 0x8(%rbx), %rax
movq 0x50(%rbx), %rcx
movq %rax, 0x8(%rcx)
movq 0x50(%rbx), %rax
movq 0x110(%rbx), %rcx
movq %rcx, 0x10(%rax)
movq 0x50(%rbx), %rax
movq $0x198, 0x18(%rax)      # imm = 0x198
addq $0x20, 0x50(%rbx)
movq 0x120(%rbx), %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0xe98(%rbx), %rdx
movq 0xd5d14(%rip), %r8      # 0x15a330
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rcx
movl $0x1, %r9d
jmp 0x8464f
movq 0x10(%r14), %rax
movq 0x8(%rax), %rsi
movq 0xe98(%rbx), %rdx
movq 0xd5ced(%rip), %r8      # 0x15a330
movq %rbx, %rdi
movq %r14, %rcx
movl $0x2, %r9d
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xf3da
movq 0xe98(%rbx), %rdx
movq 0xd5cc3(%rip), %r8      # 0x15a330
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rcx
movl $0x3, %r9d
jmp 0x8464f