// File: /workspace/github_c_makefile_star2_O3/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 0xffd87
testb %al, %al
je 0x7e385
movq 0x10(%r14), %rax
movq 0x8(%rax), %rsi
cmpb $0x26, (%rsi)
jb 0x7e3a8
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3f86f
movq 0x10(%r14), %rcx
testb %al, %al
je 0x7e3a4
movq 0x10(%rcx), %rax
movq 0x8(%rax), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xffd87
testb %al, %al
je 0x7e3d2
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x7e20b
movq %rbx, %rdi
callq 0xa550
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r12
movq $0x1a, (%r12)
movq 0x8(%r14), %r15
cmpb $0x28, (%r15)
jne 0x7e256
movq 0x10(%r15), %rax
movq 0x10(%rax), %rcx
cmpb $0x1, (%rcx)
je 0x7e256
movq 0x8(%rax), %rax
cmpb $0x1, (%rax)
jne 0x7e24f
movq 0x8(%rax), %rax
cmpq 0x17b0(%rbx), %rax
jne 0x7e256
movq 0x120(%rbx), %r15
movq %r15, 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
cmpb $0x28, (%rsi)
jne 0x7e2a3
movq 0x10(%rsi), %rax
movq 0x10(%rax), %rcx
cmpb $0x1, (%rcx)
je 0x7e2a3
movq 0x8(%rax), %rax
cmpb $0x1, (%rax)
jne 0x7e29c
movq 0x8(%rax), %rax
cmpq 0x17b0(%rbx), %rax
jne 0x7e2a3
movq 0x120(%rbx), %rsi
movq %rsi, 0x10(%r12)
movzbl (%r15), %eax
leaq 0xd492d(%rip), %r14     # 0x152be0
cmpb $0x1, (%rax,%r14)
jne 0x7e2d6
testb $0x10, 0x1(%r15)
jne 0x7e2d6
movq %rbx, %rdi
movq %r15, %rsi
callq 0xffdc7
movq %rax, 0x8(%r12)
movq 0x10(%r12), %rsi
movzbl (%rsi), %eax
cmpb $0x1, (%rax,%r14)
jne 0x7e2f3
testb $0x10, 0x1(%rsi)
jne 0x7e2f3
movq %rbx, %rdi
callq 0xffdc7
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 0xd3f9d(%rip), %r8      # 0x152330
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rcx
movl $0x1, %r9d
jmp 0x7e3c2
movq 0x8(%rcx), %rsi
movq 0xe98(%rbx), %rdx
movq 0xd3f7a(%rip), %r8      # 0x152330
movq %rbx, %rdi
movq %r14, %rcx
movl $0x2, %r9d
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xe659
movq 0xe98(%rbx), %rdx
movq 0xd3f50(%rip), %r8      # 0x152330
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rcx
movl $0x3, %r9d
jmp 0x7e3c2