// File: /workspace/github_c_makefile_star2_O3/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 %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
movq 0x8(%rsi), %rax
movq (%rax), %rdx
btl $0x18, %edx
jb 0xdb775
btq $0x30, %rdx
jae 0xdb77a
movq 0x8(%r15), %rsi
movq 0x18(%rsi), %r9
movq 0x20(%rax), %rdi
movl $0x18, %edx
cmpq %rdi, %r9
je 0xdb763
jle 0xdb75a
movq 0x10(%rsi), %rsi
movq 0x18(%rsi), %r9
cmpq %r9, %rdi
jl 0xdb74d
cmpq %rdi, %r9
jne 0xdb947
movq (%rax,%rdx), %rdi
cmpb $0x20, (%rdi)
jne 0xdb77a
movq (%rdi), %rdx
btl $0x18, %edx
jae 0xdb77a
movl $0x1, %ecx
movb (%r14), %dl
movl %edx, %esi
andb $-0x2, %sil
cmpb $0x28, %sil
jne 0xdb7d4
movzwl (%r14), %r13d
movq (%r14), %rax
movq 0x8(%r14), %rdx
btl $0x18, %eax
movl $0x1, %r12d
cmovael %ecx, %r12d
cmpq 0x110(%r15), %rdx
je 0xdb893
cmpb $0xa, (%rdx)
jne 0xdb8bc
movq %r14, 0x18(%rbx)
orq $0x800800, (%rbx)       # imm = 0x800800
addl $0xa2, %r12d
movw %r12w, 0x4(%rbx)
jmp 0xdb992
xorl %ebp, %ebp
cmpb $0x30, %dl
jb 0xdb867
cmpq $0x0, 0x18(%r14)
jne 0xdb994
movw $0x3, %dx
testl %ecx, %ecx
jne 0xdb7fd
xorl %edx, %edx
cmpq $0x1, 0x20(%rax)
adcw $0x2, %dx
testb $0x20, 0x2(%r14)
jne 0xdb815
leaq -0x65281(%rip), %rax    # 0x7658a
cmpq %rax, 0x10(%r14)
jne 0xdb994
orb $0x8, 0x1(%rbx)
movw %dx, 0x4(%rbx)
movq 0x8(%r14), %rax
movl $0x1, %ebp
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rbx, %rcx
movl $0x1, %r8d
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 0xdb994
cmpb $0x2f, %dl
jne 0xdb994
orb $0x8, 0x1(%rbx)
addl $0x94, %ecx
movw %cx, 0x4(%rbx)
movq %r14, 0x18(%rbx)
movq 0x10(%r14), %rax
movq %rax, 0x20(%rbx)
orb $0x4, 0x6(%rbx)
jmp 0xdb940
orb $0x8, 0x1(%rbx)
movq 0x10(%r14), %rbp
movq 0x10(%rbp), %rax
cmpq 0x110(%r15), %rax
je 0xdb8e8
btl $0xc, %r13d
movl $0x9e, %ecx
movl $0xa4, %eax
jmp 0xdb983
xorl %ebp, %ebp
cmpb $0x29, %al
jne 0xdb994
movq %r14, 0x18(%rbx)
orb $0x8, 0x1(%rbx)
shrl $0x9, %r13d
andl $0x8, %r13d
leal (%r12,%r13), %eax
addl $0x11a, %eax            # imm = 0x11A
movw %ax, 0x4(%rbx)
jmp 0xdb994
movq %r8, (%rsp)
movq 0x8(%rbp), %rsi
movq %r15, %rdi
callq 0xcd0e8
testb %al, %al
je 0xdb974
movq (%rsp), %rdx
movb (%rdx), %al
decb %al
cmpb $0x2, %al
leaq -0xe7b1(%rip), %rax     # 0xcd15c
leaq -0x2a7bf(%rip), %rcx    # 0xb1155
cmovbq %rax, %rcx
movq %r15, %rdi
movq %rbp, %rsi
callq 0xaf535
movq %rax, 0x20(%rbp)
orb $0x4, 0x6(%rbp)
addl $0xa8, %r12d
movw %r12w, 0x4(%rbx)
orb $0x5, 0x1(%r14)
movq %r14, 0x18(%rbx)
movl $0x1, %ebp
jmp 0xdb994
movl $0x10, %edx
cmpb $0x1c, (%rsi)
jne 0xdb763
movq 0x8(%rsi), %rdi
testq %rdi, %rdi
je 0xdb96e
cmpq %rax, 0x8(%rdi)
je 0xdb767
movq 0x18(%rdi), %rdi
jmp 0xdb959
movq 0x10(%rsi), %rsi
jmp 0xdb94c
btl $0xc, %r13d
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