// File: /workspace/github_c_makefile_star2_O0/BernardTatin[P]s7sh/s7.c
// Function: g_call_with_exit
static s7_pointer g_call_with_exit(s7_scheme *sc, s7_pointer args)
    {
#define H_call_with_exit "(call-with-exit (lambda (exiter) ...)) is call/cc without the ability to jump back into a previous computation."
#define Q_call_with_exit s7_make_signature(sc, 2, sc->values_symbol, sc->is_procedure_symbol)

        s7_pointer p, x;
        /* (call-with-exit (lambda (return) ...)) */

        p = car(args);
        if (!is_t_procedure(p))                  /* this includes continuations */
            return(method_or_bust_with_type_one_arg(sc, p, sc->call_with_exit_symbol, args, a_procedure_string));

        x = make_goto(sc);
        push_stack(sc, OP_DEACTIVATE_GOTO, x, p); /* this means call-with-exit is not tail-recursive */
        push_stack(sc, OP_APPLY, cons_unchecked(sc, x, sc->nil), p);

        /* if the lambda body calls the argument as a function,
         *   it is applied to its arguments, apply notices that it is a goto, and...
         *
         *      (conceptually...) sc->stack_top = call_exit_goto_loc(sc->code);
         *      s_pop(sc, (is_not_null(sc->args)) ? car(sc->args) : sc->nil);
         *
         *   which jumps to the point of the goto returning car(args).
         *
         * There is one gotcha: we can't jump back in from outside, so if the caller saves the goto
         *   and tries to invoke it outside the call-with-exit block, we have to
         *   make sure it triggers an error.  So, if the escape is called, it then
         *   deactivates itself.  Otherwise the block returns, we pop to OP_DEACTIVATE_GOTO,
         *   and it finds the goto in sc->args.
         * Even worse:
         (let ((cc #f))
         (call-with-exit
         (lambda (c3)
         (call/cc (lambda (ret) (set! cc ret)))
         (c3)))
         (cc))
         * where we jump back into a call-with-exit body via call/cc, the goto has to be re-established.
         *
         * I think call-with-exit could be based on catch, but it's a simpler notion,
         *   and certainly at the source level it is easier to read.
         */
        return(sc->nil);
    }

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0x17ed73(%rip), %rax    # 0x21be60
testb $0x1, (%rax,%rcx)
jne 0x9d124
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rax
movq 0xcf8(%rax), %rdx
movq 0x10(%rsp), %rcx
movq 0x18809b(%rip), %r8     # 0x2251b0
callq 0x1ba7e0
movq %rax, 0x20(%rsp)
jmp 0x9d214
movq 0x18(%rsp), %rdi
callq 0x15cbd0
movq %rax, (%rsp)
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rax
movq %rcx, (%rax)
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x8(%rax)
movq (%rsp), %rcx
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x10(%rax)
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rax
movl $0x204, %ecx            # imm = 0x204
movq %rcx, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rcx
addq $0x20, %rcx
movq %rcx, 0x50(%rax)
jmp 0x9d18f
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rax
movq %rcx, (%rax)
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x18(%rsp), %rdi
movq (%rsp), %rsi
movq 0x18(%rsp), %rax
movq 0x110(%rax), %rdx
callq 0xd9f0
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x10(%rax)
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rax
movl $0x198, %ecx            # imm = 0x198
movq %rcx, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rcx
addq $0x20, %rcx
movq %rcx, 0x50(%rax)
movq 0x18(%rsp), %rax
movq 0x110(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
addq $0x28, %rsp
retq
nop