// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movl %ecx, 0x24(%rsp)
movq %r8, 0x18(%rsp)
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rsi
callq 0xc57c0
testb $0x1, %al
jne 0x178a95
jmp 0x178a9d
movl $0x1, 0x24(%rsp)
movq 0x28(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x28, %eax
je 0x178abb
movq 0x28(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x29, %eax
jne 0x178ce3
movq 0x28(%rsp), %rax
movzwl (%rax), %eax
andl $0x1000, %eax           # imm = 0x1000
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, 0x17(%rsp)
movq 0x28(%rsp), %rax
movq (%rax), %rax
andq $0x1000000, %rax        # imm = 0x1000000
cmpq $0x0, %rax
je 0x178af0
movl $0x1, 0x24(%rsp)
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x38(%rsp), %rcx
cmpq 0x110(%rcx), %rax
jne 0x178c21
movq 0x30(%rsp), %rax
movzwl (%rax), %ecx
orl $0x800, %ecx            # imm = 0x800
movw %cx, (%rax)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x38(%rsp), %rcx
cmpq 0x110(%rcx), %rax
jne 0x178bde
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rsi
callq 0x15cfb0
testb $0x1, %al
jne 0x178b5e
jmp 0x178bb4
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x15d020
movl 0x24(%rsp), %eax
addl $0xa8, %eax
movw %ax, %cx
movq 0x30(%rsp), %rax
movw %cx, 0x4(%rax)
movq 0x28(%rsp), %rax
movq (%rax), %rcx
orq $0x500, %rcx            # imm = 0x500
movq %rcx, (%rax)
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x18(%rax)
movl $0x1, 0x44(%rsp)
jmp 0x178e87
movl 0x24(%rsp), %eax
movb 0x17(%rsp), %sil
movl $0xa0, %ecx
movl $0xa6, %edx
testb $0x1, %sil
cmovnel %edx, %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x30(%rsp), %rax
movw %cx, 0x4(%rax)
jmp 0x178c06
movl 0x24(%rsp), %eax
movb 0x17(%rsp), %sil
movl $0x9e, %ecx
movl $0xa4, %edx
testb $0x1, %sil
cmovnel %edx, %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x30(%rsp), %rax
movw %cx, 0x4(%rax)
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x18(%rax)
movl $0x0, 0x44(%rsp)
jmp 0x178e87
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movzbl (%rax), %eax
cmpl $0xa, %eax
jne 0x178c80
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rcx
orq $0x800000, %rcx         # imm = 0x800000
orq $0x800, %rcx            # imm = 0x800
movq 0x30(%rsp), %rax
movq %rcx, (%rax)
movl 0x24(%rsp), %eax
addl $0xa2, %eax
movw %ax, %cx
movq 0x30(%rsp), %rax
movw %cx, 0x4(%rax)
movl $0x0, 0x44(%rsp)
jmp 0x178e87
movq 0x28(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x29, %eax
jne 0x178cd6
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x30(%rsp), %rax
movzwl (%rax), %ecx
orl $0x800, %ecx            # imm = 0x800
movw %cx, (%rax)
movl 0x24(%rsp), %eax
movb 0x17(%rsp), %sil
movl $0x11a, %ecx            # imm = 0x11A
movl $0x122, %edx            # imm = 0x122
testb $0x1, %sil
cmovnel %edx, %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x30(%rsp), %rax
movw %cx, 0x4(%rax)
jmp 0x178cd6
movl $0x0, 0x44(%rsp)
jmp 0x178e87
movq 0x28(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x30, %eax
jl 0x178e0c
movq 0x28(%rsp), %rax
cmpq $0x0, 0x18(%rax)
je 0x178d0d
movl $0x0, 0x44(%rsp)
jmp 0x178e87
cmpl $0x0, 0x24(%rsp)
jne 0x178d2c
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
cmpq $0x0, 0x20(%rax)
jne 0x178d2c
movl $0x1, 0x24(%rsp)
movq 0x28(%rsp), %rax
movq (%rax), %rax
andq $0x200000, %rax         # imm = 0x200000
cmpq $0x0, %rax
jne 0x178d56
movq 0x28(%rsp), %rax
leaq -0xe574c(%rip), %rcx    # 0x93600
cmpq %rcx, 0x10(%rax)
jne 0x178e02
jmp 0x178d58
movq 0x30(%rsp), %rax
movzwl (%rax), %ecx
orl $0x800, %ecx            # imm = 0x800
movw %cx, (%rax)
movl 0x24(%rsp), %eax
addl $0x2, %eax
movw %ax, %cx
movq 0x30(%rsp), %rax
movw %cx, 0x4(%rax)
jmp 0x178d7e
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x48(%rax), %rax
movq 0x38(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rcx
xorl %edx, %edx
movl $0x1, %r8d
callq *%rax
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x48(%rax), %rax
movq 0x38(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rcx
xorl %edx, %edx
movl $0x1, %r8d
callq *%rax
movq 0x10(%rax), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x30(%rsp), %rax
movzwl 0x6(%rax), %ecx
orl $0x4, %ecx
movw %cx, 0x6(%rax)
jmp 0x178df5
movl $0x1, 0x44(%rsp)
jmp 0x178e87
movl $0x0, 0x44(%rsp)
jmp 0x178e87
movq 0x28(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x178e7f
jmp 0x178e1b
movq 0x30(%rsp), %rax
movzwl (%rax), %ecx
orl $0x800, %ecx            # imm = 0x800
movw %cx, (%rax)
movl 0x24(%rsp), %eax
addl $0x94, %eax
movw %ax, %cx
movq 0x30(%rsp), %rax
movw %cx, 0x4(%rax)
jmp 0x178e43
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x30(%rsp), %rax
movzwl 0x6(%rax), %ecx
orl $0x4, %ecx
movw %cx, 0x6(%rax)
jmp 0x178e75
movl $0x1, 0x44(%rsp)
jmp 0x178e87
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq