// File: /workspace/github_c_makefile_star2_O2/mahrr[P]raven/src/compiler.c
// Function: cond
static void cond(Parser *parser) {
    // Cond Control Flow:
    //
    // Condition (1)
    // OP_JMP_POP_FALSE  ---.
    //                      |
    // Expression (1)       |
    // OP_JMP            --------.
    //                      |    |
    // [Condition (2)]   <---    |
    // OP_JMP_POP_FALSE  ----    |
    //                      |    |
    // [Expression (2)]     |    |
    // OP_JMP            --------|
    //                      |    |
    // [Condition (3)]   <---    |
    // OP_JMP_POP_FALSE  ---.    |
    //                      |    |
    // [Expression (3)]     |    |
    // OP_JMP            --------|
    //                      |    |
    // ....                 .    .
    // ....                 .    .
    //                      |    |
    // [Condition (n)]   <---    |
    // OP_JMP_POP_FALSE  ---.    |
    //                      |    |
    // [Expression (n)]     |    |
    // OP_JMP            --------|
    //                      |    |
    // OP_PUSH_NIL <---------    |
    // ....        <--------------

    Debug_Log(parser);

    int cases_exit[COND_LIMIT];
    int cases_count = 0;

    do {
        if (cases_count == COND_LIMIT) {
            error_limit(parser, "cond cases", COND_LIMIT);
            break;
        }

        // Condition
        expression(parser);
        consume(parser, TOKEN_ARROW, "expect '->' after expression");
        int next_case = emit_jump(parser, OP_JMP_POP_FALSE);

        // Expression
        expression(parser);
        cases_exit[cases_count++] = emit_jump(parser, OP_JMP);

        patch_jump(parser, next_case);
    } while (consume_if(parser, TOKEN_COMMA));

    // If all conditions evaluate to false.
    emit_byte(parser, OP_PUSH_NIL);

    for (int i = 0; i < cases_count; i++) {
        patch_jump(parser, cases_exit[i]);
    }

    consume(parser, TOKEN_END, "expect 'end' after cond cases");

    Debug_Exit(parser);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rdi, %rbx
xorl %ebp, %ebp
pushq $0x21
popq %r15
pushq $0x1e
popq %r12
pushq $0x25
popq %r13
cmpq $0x100, %rbp            # imm = 0x100
je 0x6192
movq %rbx, %rdi
callq 0x5a96
movq %rbx, %rdi
pushq $0x24
popq %rsi
leaq 0x3834(%rip), %rdx      # 0x997e
callq 0x5bf2
movq %rbx, %rdi
movl %r15d, %esi
callq 0x6b8f
movl %eax, %r14d
movq %rbx, %rdi
callq 0x5a96
movq %rbx, %rdi
movl %r12d, %esi
callq 0x6b8f
movl %eax, (%rsp,%rbp,4)
incq %rbp
movq %rbx, %rdi
movl %r14d, %esi
callq 0x6bba
movq %rbx, %rdi
movl %r13d, %esi
callq 0x5876
testb %al, %al
jne 0x612c
jmp 0x61ab
leaq 0x3815(%rip), %rsi      # 0x99ae
movq %rbx, %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x5d22
movl $0x100, %ebp            # imm = 0x100
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x5a9e
xorl %r14d, %r14d
cmpq %r14, %rbp
je 0x61cf
movl (%rsp,%r14,4), %esi
movq %rbx, %rdi
callq 0x6bba
incq %r14
jmp 0x61b9
leaq 0x37c5(%rip), %rdx      # 0x999b
pushq $0x23
popq %rsi
movq %rbx, %rdi
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5bf2