// File: /workspace/github_cpp_makefile_star2_O1/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 0x64e9
movq %rdi, %rbx
leaq 0x10(%rbp), %r13
leaq 0x43fc(%rip), %rcx      # 0xa328
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, -0x38(%rbp)
movl $0x1, %ecx
movl %ecx, (%rax)
movl $0x1, %r12d
movl $0x5, %r14d
movq %r14, 0x8(%rax)
leaq 0x5a22(%rip), %rcx      # 0xb986
movq %rcx, 0x10(%rax)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r15
movl %r12d, (%rax)
movq $0x4, 0x8(%rax)
leaq 0x5a02(%rip), %rax      # 0xb98c
movq %rax, 0x10(%r15)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r12
movl $0x1, %eax
movl %eax, (%r12)
movq %r14, 0x8(%r12)
leaq 0x59de(%rip), %rax      # 0xb991
movq %rax, 0x10(%r12)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x7ec2
movq %rax, -0x30(%rbp)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movq $0x7, 0x8(%r14)
leaq 0x59a7(%rip), %rax      # 0xb997
movq %rax, 0x10(%r14)
movq 0x10(%r13), %rsi
movq %rbx, %rdi
callq 0x51b2
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7e62
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %rax, %rdx
callq 0x7e62
movq %rax, %r14
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
xorl %r13d, %r13d
movl %r13d, (%rax)
movq %r12, 0x8(%rax)
movq %r14, 0x10(%rax)
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x7e62
movq %rax, %r15
leaq 0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq %rbx, %rdi
callq 0x6508
movq %rax, %r12
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movq $0x2, 0x8(%r14)
leaq 0x5919(%rip), %rax      # 0xb99f
movq %rax, 0x10(%r14)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7ec2
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7e62
movq %rax, %r14
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movl %r13d, (%rax)
movq %r14, 0x8(%rax)
movq $0x0, 0x10(%rax)
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0x8040
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
jmp 0x6151
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r14
movl $0x1, (%rax)
movq $0x6, 0x8(%rax)
leaq 0x4829(%rip), %rax      # 0xa929
jmp 0x613b
movq 0x18(%r13), %r12
movq 0x20(%r13), %r15
movq %r12, %rax
orq %r15, %rax
jne 0x6239
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r14
movl $0x1, (%rax)
movq $0x4, 0x8(%rax)
leaq 0x57a6(%rip), %rax      # 0xb8e1
movq %rax, 0x10(%r14)
movq 0x10(%r13), %rsi
movq %rbx, %rdi
callq 0x51b2
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 0x7e62
movq 0x10(%r13), %rsi
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x51b2
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movq $0x9, 0x8(%r14)
leaq 0x57fa(%rip), %rax      # 0xb9a2
movq %rax, 0x10(%r14)
movq 0x10(%r13), %rcx
movq 0x18(%r13), %r8
leaq 0x4969(%rip), %rdx      # 0xab24
movl $0x1, %esi
movq %rbx, %rdi
callq 0x7df0
movq %rax, %r15
movq %rdx, %r12
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
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 0x51b2
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x7e62
movq %rax, %r15
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
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 0x85c6
movl $0x1, (%rax)
movq $0x2, 0x8(%rax)
leaq 0x5735(%rip), %rcx      # 0xb994
movq %rax, -0x38(%rbp)
movq %rcx, 0x10(%rax)
cmpl $0x1, %r13d
jne 0x6372
leaq 0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x51b2
movq %rax, -0x30(%rbp)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r13
movl $0x1, %eax
movl %eax, (%r13)
movl $0x4, %eax
movq %rax, 0x8(%r13)
leaq 0x56d3(%rip), %rax      # 0xb981
movq %rax, 0x10(%r13)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r14
xorl %eax, %eax
movl %eax, (%r14)
movq %r13, 0x8(%r14)
movq %rax, 0x10(%r14)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6508
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7fb8
movq %rax, %r12
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movl $0x4, %eax
movq %rax, 0x8(%r14)
leaq 0x4611(%rip), %rax      # 0xa924
movq %rax, 0x10(%r14)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r13
xorl %eax, %eax
movl %eax, (%r13)
movq %r14, 0x8(%r13)
movq %rax, 0x10(%r13)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6508
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x7fb8
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 0x7f4c
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movq $0x6, 0x8(%r14)
leaq 0x48cd(%rip), %rax      # 0xac66
movq %rax, 0x10(%r14)
movq %rbx, %rdi
movl %r13d, %esi
callq 0x4c88
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7e62
movq %rax, -0x30(%rbp)
leaq 0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x51b2
movq %rax, -0x40(%rbp)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movl $0x4, %eax
movq %rax, 0x8(%r14)
leaq 0x558b(%rip), %rax      # 0xb981
movq %rax, 0x10(%r14)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r13
xorl %eax, %eax
movl %eax, (%r13)
movq %r14, 0x8(%r13)
movq %rax, 0x10(%r13)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6508
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x7fb8
movq %rax, %r12
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r14
movl $0x1, %eax
movl %eax, (%r14)
movl $0x4, %eax
movq %rax, 0x8(%r14)
leaq 0x44c8(%rip), %rax      # 0xa924
movq %rax, 0x10(%r14)
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
movq %rax, %r13
xorl %eax, %eax
movl %eax, (%r13)
movq %r14, 0x8(%r13)
movq %rax, 0x10(%r13)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6508
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x7fb8
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x7f4c
movq %rax, %r14
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x85c6
xorl %ecx, %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq %r14, 0x10(%rax)
jmp 0x622a
addq $0x8, %rbx
movq 0x8(%r13), %rsi
leaq 0x544d(%rip), %rdx      # 0xb92e
movq %rbx, %rdi
callq 0x7f2e
leaq 0x4907(%rip), %rdi      # 0xadf7
leaq 0x46c3(%rip), %rsi      # 0xabba
leaq 0x54ae(%rip), %rcx      # 0xb9ac
movl $0x1ed, %edx            # imm = 0x1ED
callq 0x1090