// File: /workspace/github_c_makefile_star2_O1/BernardTatin[P]s7sh/s7.c
// Function: optimize_thunk
static opt_t optimize_thunk(s7_scheme *sc, s7_pointer expr, s7_pointer func, int32_t hop, s7_pointer e)
                {
#if OPTIMIZE_PRINT
                    fprintf(stderr, "%s: %s %d\n", __func__, DISPLAY(expr), hop);
#endif

                    if (is_constant_symbol(sc, car(expr))) hop = 1;

                    if ((is_closure(func)) || (is_closure_star(func)))
                    {
                        bool safe_case;
                        safe_case = is_safe_closure(func);
                        if (is_immutable(func)) hop = 1;
                        if (is_null(closure_args(func)))               /* no rest arg funny business */
                        {
                            s7_pointer body;
                            set_optimized(expr);
                            body = closure_body(func);
                            if (is_null(cdr(body)))
                            {
                                if (is_fxable(sc, car(body)))          /* fx stuff is not set yet */
                                {
                                    annotate_arg(sc, body, e);
                                    set_optimize_op(expr, hop + OP_SAFE_THUNK_A);
                                    set_closure_has_fx(func);
                                    set_opt1_lambda(expr, func);
                                    return(OPT_T);
                                }
                                set_optimize_op(expr, hop + ((safe_case) ? OP_SAFE_THUNK_P : OP_THUNK_P));
                            }
                            else set_optimize_op(expr, hop + ((safe_case) ? OP_SAFE_THUNK : OP_THUNK));
                            set_opt1_lambda(expr, func);
                            return(OPT_F);
                        }
                        if (is_symbol(closure_args(func))) /* (define* (f1 . a) ...) called (f1) -- called a closure (not closure*) in define_unchecked */
                        {
                            set_opt1_lambda(expr, func);
                            set_unsafe_optimize_op(expr, hop + OP_THUNK_NIL);
                            return(OPT_F);
                        }
                        if (is_closure_star(func))
                        {
                            set_opt1_lambda(expr, func);
                            set_safe_optimize_op(expr, hop + ((safe_case) ? OP_SAFE_CLOSURE_STAR_FX_0 : OP_CLOSURE_STAR_FX));
                        }
                        return(OPT_F);
                    }

                    if (is_c_function(func))
                    {
                        if (c_function_required_args(func) != 0)
                            return(OPT_F);
                        if ((hop == 0) && (symbol_id(car(expr)) == 0)) hop = 1;

                        if ((is_safe_procedure(func)) ||
                                (c_function_call(func) == g_list))          /* (list) is safe, (values) is not (in this context -- possibly used as list-values arg) */
                        {
                            set_safe_optimize_op(expr, hop + OP_SAFE_C_D);
                            choose_c_function(sc, expr, func, 0);
                            return(OPT_T);
                        }
                        return(OPT_F);
                    }

                    if (is_c_function_star(func))
                    {
                        set_safe_optimize_op(expr, hop + OP_SAFE_C_FUNCTION_STAR);
                        set_c_function(expr, func);
                        return(OPT_T);
                    }

                    return(OPT_F);
                }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r8, %rbp
movq %rdx, %r14
movq %rsi, %rbx
movq 0x8(%rsi), %rax
movl $0x1, %edx
testb $0x1, 0x3(%rax)
jne 0xe416e
testb $0x1, 0x6(%rax)
je 0xe416c
movq 0x8(%rdi), %rsi
movq 0x18(%rsi), %r8
movq 0x20(%rax), %r9
movl $0x18, %edx
cmpq %r9, %r8
je 0xe4153
jle 0xe4117
movq 0x10(%rsi), %rsi
movq 0x18(%rsi), %r8
cmpq %r8, %r9
jl 0xe410a
cmpq %r9, %r8
je 0xe4153
movl $0x10, %edx
cmpb $0x1c, (%rsi)
jne 0xe4153
movq 0x8(%rsi), %r8
testq %r8, %r8
sete %r9b
je 0xe4145
cmpq %rax, 0x8(%r8)
je 0xe413f
movq 0x18(%r8), %r8
jmp 0xe412a
xorl %r9d, %r9d
movq %r8, %r10
testb %r9b, %r9b
je 0xe4157
movq 0x10(%rsi), %rsi
cmpb $0x1c, (%rsi)
je 0xe4126
movq (%rax,%rdx), %r10
cmpb $0x20, (%r10)
jne 0xe416c
testb $0x1, 0x3(%r10)
movl $0x1, %edx
cmovel %ecx, %edx
jmp 0xe416e
movl %ecx, %edx
movb (%r14), %cl
movl %ecx, %esi
andb $-0x2, %sil
cmpb $0x28, %sil
jne 0xe41c6
testb $0x1, 0x3(%r14)
movl $0x1, %r12d
cmovel %edx, %r12d
movzwl (%r14), %r15d
movq 0x8(%r14), %rax
cmpq 0x110(%rdi), %rax
je 0xe4285
cmpb $0xa, (%rax)
jne 0xe42ae
movq %r14, 0x18(%rbx)
orq $0x800800, (%rbx)       # imm = 0x800800
addl $0xa2, %r12d
movw %r12w, 0x4(%rbx)
jmp 0xe4355
xorl %ebp, %ebp
cmpb $0x30, %cl
jb 0xe4259
cmpq $0x0, 0x18(%r14)
jne 0xe4357
movw $0x3, %cx
testl %edx, %edx
jne 0xe41ef
xorl %ecx, %ecx
cmpq $0x1, 0x20(%rax)
adcw $0x2, %cx
testb $0x20, 0x2(%r14)
jne 0xe4207
leaq -0x67fe7(%rip), %rax    # 0x7c216
cmpq %rax, 0x10(%r14)
jne 0xe4357
orb $0x8, 0x1(%rbx)
movw %cx, 0x4(%rbx)
movq 0x8(%r14), %rax
movl $0x1, %ebp
movq %r14, %rsi
xorl %edx, %edx
movq %rbx, %rcx
movl $0x1, %r8d
movq %rdi, %r15
callq *0x48(%rax)
movq %rax, 0x18(%rbx)
movq 0x8(%r14), %rax
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rbx, %rcx
movl $0x1, %r8d
callq *0x48(%rax)
movq 0x10(%rax), %rax
movq %rax, 0x20(%rbx)
orb $0x4, 0x6(%rbx)
jmp 0xe4357
cmpb $0x2f, %cl
jne 0xe4357
orb $0x8, 0x1(%rbx)
addl $0x94, %edx
movw %dx, 0x4(%rbx)
movq %r14, 0x18(%rbx)
movq 0x10(%r14), %rax
movq %rax, 0x20(%rbx)
orb $0x4, 0x6(%rbx)
jmp 0xe4330
orb $0x8, 0x1(%rbx)
movq 0x10(%r14), %r13
movq 0x10(%r13), %rax
cmpq 0x110(%rdi), %rax
je 0xe42d9
btl $0xc, %r15d
movl $0x9e, %ecx
movl $0xa4, %eax
jmp 0xe4346
xorl %ebp, %ebp
cmpb $0x29, (%r14)
jne 0xe4357
movq %r14, 0x18(%rbx)
orb $0x8, 0x1(%rbx)
shrl $0x9, %r15d
andl $0x8, %r15d
leal (%r15,%r12), %eax
addl $0x11a, %eax            # imm = 0x11A
movw %ax, 0x4(%rbx)
jmp 0xe4357
movq 0x8(%r13), %rsi
movq %rdi, (%rsp)
callq 0xd5a05
testb %al, %al
je 0xe4337
movb (%rbp), %al
decb %al
cmpb $0x2, %al
leaq -0xe87a(%rip), %rax     # 0xd5a7e
leaq -0x2c177(%rip), %rcx    # 0xb8188
cmovbq %rax, %rcx
movq (%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0xb69b9
movq %rax, 0x20(%r13)
orb $0x4, 0x6(%r13)
addl $0xa8, %r12d
movw %r12w, 0x4(%rbx)
orb $0x5, 0x1(%r14)
movq %r14, 0x18(%rbx)
movl $0x1, %ebp
jmp 0xe4357
btl $0xc, %r15d
movl $0xa0, %ecx
movl $0xa6, %eax
cmovael %ecx, %eax
addl %eax, %r12d
movw %r12w, 0x4(%rbx)
movq %r14, 0x18(%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq