// File: /workspace/github_cpp_makefile_star2_O1/tsoding[P]wcc/src/./wcc_wat_compiler.cpp
// Function: Wat_Compiler::compile_func_body(Block*)
S_Expr *Wat_Compiler::compile_func_body(Block *block)
{
    S_Expr *local_var_def_section = nullptr;
    S_Expr *local_var_def_init = nullptr;

    while (block && block->statement.kind == Statement_Kind::Local_Var_Def) {
        auto var_ident = wat_ident(block->statement.local_var_def.def.name);

        local_var_def_section = append(
            local_var_def_section,
            list(list(atom("local"_sv),
                      var_ident,
                      wat_name_of_type(block->statement.local_var_def.def.type))));

        local_var_def_init = append(
            local_var_def_init,
            list(list(atom("set_local"_sv),
                      var_ident,
                      compile_expression(block->statement.local_var_def.value))));

        block = block->next;
    }

    return append(local_var_def_section, local_var_def_init, compile_block(block));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
testq %rsi, %rsi
je 0x674d
movq $0x0, -0x30(%rbp)
xorl %r15d, %r15d
cmpl $0x0, (%r14)
jne 0x6758
movq 0x10(%r14), %rcx
movq 0x18(%r14), %r8
movl $0x1, %esi
movq %rbx, %rdi
leaq 0x4540(%rip), %rdx      # 0xab24
callq 0x7df0
movq %r15, -0x40(%rbp)
movq %rax, %r15
movq %rdx, %r12
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r13
movl $0x1, %eax
movl %eax, (%r13)
movq %r15, 0x8(%r13)
movq %r12, 0x10(%r13)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r15
movl $0x1, %eax
movl %eax, (%r15)
movq $0x5, 0x8(%r15)
leaq 0x4640(%rip), %rax      # 0xac7b
movq %rax, 0x10(%r15)
movl 0x20(%r14), %esi
movq %rbx, %rdi
callq 0x4c88
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x7e62
movq %rax, -0x38(%rbp)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r12
xorl %eax, %eax
movl %eax, (%r12)
movq %r15, 0x8(%r12)
movq -0x38(%rbp), %rax
movq %rax, 0x10(%r12)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
xorl %ecx, %ecx
movl %ecx, (%rax)
movq %r12, 0x8(%rax)
movq %rcx, 0x10(%rax)
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %rax, %rdx
callq 0x7fb8
movq %rax, -0x30(%rbp)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r15
movl $0x1, %eax
movl %eax, (%r15)
movq $0x9, 0x8(%r15)
leaq 0x52ce(%rip), %rax      # 0xb9a2
movq %rax, 0x10(%r15)
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0x51b2
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x7e62
movq %rax, %r12
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r13
xorl %eax, %eax
movl %eax, (%r13)
movq %r15, 0x8(%r13)
movq %r12, 0x10(%r13)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
xorl %ecx, %ecx
movl %ecx, (%rax)
movq %r13, 0x8(%rax)
movq %rcx, 0x10(%rax)
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
movq %rax, %rdx
callq 0x7fb8
movq %rax, %r15
movq 0x30(%r14), %r14
testq %r14, %r14
jne 0x65c3
jmp 0x6758
xorl %r15d, %r15d
movq $0x0, -0x30(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6508
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %r15, %rdx
movq %rax, %rcx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8040
nop