// File: /workspace/github_cpp_makefile_star2_O3/tsoding[P]wcc/src/./wcc_wat_compiler.cpp
// Function: Wat_Compiler::compile_statement(Statement)
S_Expr *Wat_Compiler::compile_statement(Statement statement)
{
    switch (statement.kind) {
    case Statement_Kind::Local_Var_Def:
        reporter.fail(statement.offset, "TODO: Local variable definitions are only allowed at the beginning of the function");
        break;
    case Statement_Kind::If:
        if (statement.iph.then == nullptr && statement.iph.elze == nullptr) {
            return list(atom("drop"_sv), compile_expression(statement.iph.condition));
        } else {
            //  (if (result <type>)
            //    (then <then-branch>)
            //    (else (else-branch)))
            if (statement.type == Type::U0) {
                return list(
                    atom("if"_sv),
                    compile_expression(statement.iph.condition),
                    append(list(atom("then"_sv)), compile_block(statement.iph.then)),
                    append(list(atom("else"_sv)), compile_block(statement.iph.elze)));
            } else {
                return list(
                    atom("if"_sv), list(atom("result"_sv), wat_name_of_type(statement.type)),
                    compile_expression(statement.iph.condition),
                    append(list(atom("then"_sv)), compile_block(statement.iph.then)),
                    append(list(atom("else"_sv)), compile_block(statement.iph.elze)));
            }
        }
    case Statement_Kind::While:
        // (block
        //  (loop
        //   (br_if 1 (i32.eqz <condition>))
        //   <body>
        //   (br 0)))
        return list(
            atom("block"_sv),
            append(
                list(atom("loop"_sv),
                     list(atom("br_if"_sv), atom(1),
                          list(atom("i32.eqz"_sv), compile_expression(statement.hwile.condition)))),
                compile_block(statement.hwile.body),
                list(list(atom("br"_sv), atom(0)))));

    case Statement_Kind::Assignment:
        return list(atom("set_local"_sv),
                    wat_ident(statement.assignment.var_name),
                    compile_expression(statement.assignment.value));

    case Statement_Kind::Expression:
        return compile_expression(statement.expression);

    case Statement_Kind::Return:
        return list(atom("return"_sv), compile_expression(statement.reeturn.value));
    }

    assert(0 && "Memory corruption?");
    return nullptr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0x10(%rbp), %eax
cmpq $0x5, %rax
ja 0x62f9
movq %rdi, %rbx
leaq 0x10(%rbp), %r13
leaq 0x3600(%rip), %rcx      # 0x933c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, -0x38(%rbp)
movl $0x1, %ecx
movl %ecx, (%rax)
movl $0x1, %r12d
movl $0x5, %r14d
movq %r14, 0x8(%rax)
leaq 0x4c32(%rip), %rcx      # 0xa9a6
movq %rcx, 0x10(%rax)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r15
movl %r12d, (%rax)
movq $0x4, 0x8(%rax)
leaq 0x4c12(%rip), %rax      # 0xa9ac
movq %rax, 0x10(%r15)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r12
movl $0x1, %eax
movl %eax, (%r12)
movq %r14, 0x8(%r12)
leaq 0x4bee(%rip), %rax      # 0xa9b1
movq %rax, 0x10(%r12)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x7c1e
movq %rax, -0x30(%rbp)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movq $0x7, 0x8(%r14)
leaq 0x4bb7(%rip), %rax      # 0xa9b7
movq %rax, 0x10(%r14)
movq 0x10(%r13), %rsi
movq %rbx, %rdi
callq 0x4f8e
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7bbe
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %rax, %rdx
callq 0x7bbe
movq %rax, %r14
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
xorl %r13d, %r13d
movl %r13d, (%rax)
movq %r12, 0x8(%rax)
movq %r14, 0x10(%rax)
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x7bbe
movq %rax, %r15
leaq 0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq %rbx, %rdi
callq 0x6318
movq %rax, %r12
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movq $0x2, 0x8(%r14)
leaq 0x4b29(%rip), %rax      # 0xa9bf
movq %rax, 0x10(%r14)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7c1e
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7bbe
movq %rax, %r14
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movl %r13d, (%rax)
movq %r14, 0x8(%rax)
movq $0x0, 0x10(%rax)
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0x7d70
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
jmp 0x5f61
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r14
movl $0x1, (%rax)
movq $0x6, 0x8(%rax)
leaq 0x3a39(%rip), %rax      # 0x9949
jmp 0x5f4b
movq 0x18(%r13), %r12
movq 0x20(%r13), %r15
movq %r12, %rax
orq %r15, %rax
jne 0x6049
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r14
movl $0x1, (%rax)
movq $0x4, 0x8(%rax)
leaq 0x49b6(%rip), %rax      # 0xa901
movq %rax, 0x10(%r14)
movq 0x10(%r13), %rsi
movq %rbx, %rdi
callq 0x4f8e
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7bbe
movq 0x10(%r13), %rsi
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4f8e
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movq $0x9, 0x8(%r14)
leaq 0x4a0a(%rip), %rax      # 0xa9c2
movq %rax, 0x10(%r14)
movq 0x10(%r13), %rcx
movq 0x18(%r13), %r8
leaq 0x3b79(%rip), %rdx      # 0x9b44
movl $0x1, %esi
movq %rbx, %rdi
callq 0x7b54
movq %rax, %r15
movq %rdx, %r12
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %r13, %rcx
movq %rax, %r13
movl $0x1, %eax
movl %eax, (%r13)
movq %r15, 0x8(%r13)
movq %r12, 0x10(%r13)
movq 0x20(%rcx), %rsi
movq %rbx, %rdi
callq 0x4f8e
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x7bbe
movq %rax, %r15
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movl $0x0, (%rax)
movq %r14, 0x8(%rax)
movq %r15, 0x10(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%r13), %r13d
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movl $0x1, (%rax)
movq $0x2, 0x8(%rax)
leaq 0x4945(%rip), %rcx      # 0xa9b4
movq %rax, -0x38(%rbp)
movq %rcx, 0x10(%rax)
cmpl $0x1, %r13d
jne 0x6182
leaq 0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x4f8e
movq %rax, -0x30(%rbp)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r13
movl $0x1, %eax
movl %eax, (%r13)
movl $0x4, %eax
movq %rax, 0x8(%r13)
leaq 0x48e3(%rip), %rax      # 0xa9a1
movq %rax, 0x10(%r13)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r14
xorl %eax, %eax
movl %eax, (%r14)
movq %r13, 0x8(%r14)
movq %rax, 0x10(%r14)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6318
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7d14
movq %rax, %r12
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movl $0x4, %eax
movq %rax, 0x8(%r14)
leaq 0x3821(%rip), %rax      # 0x9944
movq %rax, 0x10(%r14)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r13
xorl %eax, %eax
movl %eax, (%r13)
movq %r14, 0x8(%r13)
movq %rax, 0x10(%r13)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6318
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x7d14
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq %r12, %rcx
movq %rax, %r8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7ca8
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movq $0x6, 0x8(%r14)
leaq 0x3add(%rip), %rax      # 0x9c86
movq %rax, 0x10(%r14)
movq %rbx, %rdi
movl %r13d, %esi
callq 0x4a64
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7bbe
movq %rax, -0x30(%rbp)
leaq 0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x4f8e
movq %rax, -0x40(%rbp)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movl $0x4, %eax
movq %rax, 0x8(%r14)
leaq 0x479b(%rip), %rax      # 0xa9a1
movq %rax, 0x10(%r14)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r13
xorl %eax, %eax
movl %eax, (%r13)
movq %r14, 0x8(%r13)
movq %rax, 0x10(%r13)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6318
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x7d14
movq %rax, %r12
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movl $0x4, %eax
movq %rax, 0x8(%r14)
leaq 0x36d8(%rip), %rax      # 0x9944
movq %rax, 0x10(%r14)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
movq %rax, %r13
xorl %eax, %eax
movl %eax, (%r13)
movq %r14, 0x8(%r13)
movq %rax, 0x10(%r13)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6318
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x7d14
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x7ca8
movq %rax, %r14
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x8296
xorl %ecx, %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq %r14, 0x10(%rax)
jmp 0x603a
addq $0x8, %rbx
movq 0x8(%r13), %rsi
leaq 0x465d(%rip), %rdx      # 0xa94e
movq %rbx, %rdi
callq 0x7c8a
leaq 0x3b17(%rip), %rdi      # 0x9e17
leaq 0x38d3(%rip), %rsi      # 0x9bda
leaq 0x46be(%rip), %rcx      # 0xa9cc
movl $0x1ed, %edx            # imm = 0x1ED
callq 0x1090