// File: /workspace/github_c_makefile_star2_O3/mahrr[P]raven/src/compiler.c
// Function: match
static void match(Parser *parser) {
    Debug_Log(parser);

    expression(parser); // Value
    consume(parser, TOKEN_DO, "expect 'do' after match value");

    int cases_exit[MATCH_LIMIT];
    int cases_count = 0;

    do {
        if (cases_count == MATCH_LIMIT) {
            error_limit(parser, "match cases", MATCH_LIMIT);
            break;
        }

        begin_scope(parser);

        Pattern_Context context = {0};
        pattern(parser, &context, false);
        consume(parser, TOKEN_ARROW, "expect '->' after pattern");

        // Match succeeded.
        expression(parser);

        // Save the expression value in X and rewind the stack.
        emit_byte(parser, OP_POP_X);
        unwind_stack(parser, parser->context->scope_depth);
        emit_byte(parser, OP_PUSH_X);
        cases_exit[cases_count++] = emit_jump(parser, OP_JMP);

        // Match failed.
        for (int i = 0; i < context.cases_count; ++i) {
            patch_jump(parser, context.cases_next[i]);
        }

        // Discard the case scope.
        parser->context->scope_depth--;
    } while (consume_if(parser, TOKEN_COMMA));

    // If all patterns didn't match.
    emit_bytes(parser, OP_POP, OP_PUSH_NIL);

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

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

    Debug_Exit(parser);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8b8, %rsp            # imm = 0x8B8
movq %rdi, %r15
movl $0x1, %esi
callq 0x679e
leaq 0x18(%r15), %r14
cmpl $0x22, 0x18(%r15)
jne 0x6e93
movq 0x10(%r14), %rax
movq %rax, 0x40(%r15)
movups (%r14), %xmm0
movups %xmm0, 0x30(%r15)
leaq 0xb0(%rsp), %rbx
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x941f
movq 0xc0(%rsp), %rax
movq %rax, 0x10(%r14)
movups 0xb0(%rsp), %xmm0
movups %xmm0, (%r14)
cmpl $0x35, 0x18(%r15)
je 0x6e67
jmp 0x6ea5
leaq 0x4b88(%rip), %rdx      # 0xba22
movq %r15, %rdi
movq %r14, %rsi
callq 0x61ef
leaq 0x30(%r15), %rbx
xorl %r12d, %r12d
leaq 0x28(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq %rbx, 0x8(%rsp)
movq 0x8(%r15), %rax
incl 0x221c(%rax)
movl $0x88, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x2100
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x8541
cmpl $0x24, 0x18(%r15)
jne 0x6f1d
movq 0x10(%r14), %rax
movq %rax, 0x10(%rbx)
movups (%r14), %xmm0
movups %xmm0, (%rbx)
movq (%r15), %rsi
movq %rbp, %rdi
callq 0x941f
movq 0x20(%rsp), %rax
movq %rax, 0x10(%r14)
movups 0x10(%rsp), %xmm0
movups %xmm0, (%r14)
cmpl $0x35, 0x18(%r15)
je 0x6ef7
jmp 0x6f2f
movq %r15, %rdi
movq %r14, %rsi
leaq 0x4b16(%rip), %rdx      # 0xba40
callq 0x61ef
movq %r15, %rdi
movl $0x1, %esi
callq 0x679e
movq 0x8(%r15), %rax
movl 0x44(%r15), %edx
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl $0x5, %esi
callq 0x4af1
movq 0x8(%r15), %rax
movl 0x221c(%rax), %esi
movq %r15, %rdi
callq 0x66bb
movq 0x8(%r15), %rax
movl 0x44(%r15), %edx
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl $0x4, %esi
callq 0x4af1
movq 0x8(%r15), %rax
movl 0x44(%r15), %edx
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl $0x1e, %esi
callq 0x4af1
movq 0x8(%r15), %rax
movl 0x44(%r15), %edx
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl $0xff, %esi
callq 0x4af1
movq 0x8(%r15), %rax
movl 0x44(%r15), %edx
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl $0xff, %esi
callq 0x4af1
movq 0x8(%r15), %rax
movq 0x8(%rax), %rcx
movl 0x20(%rcx), %ecx
addl $-0x2, %ecx
movl %ecx, 0xb0(%rsp,%r12,4)
cmpl $0x0, 0xa8(%rsp)
jle 0x7060
movq %r12, (%rsp)
xorl %r13d, %r13d
movslq 0x28(%rsp,%r13,4), %rbp
movq 0x8(%r15), %rax
movq 0x8(%rax), %r12
movl 0x20(%r12), %ebx
subl %ebp, %ebx
cmpl $0x10002, %ebx          # imm = 0x10002
jl 0x7025
movq %r15, %rdi
movq %r14, %rsi
leaq 0x4999(%rip), %rdx      # 0xb9b9
callq 0x61ef
addl $-0x2, %ebx
movq 0x28(%r12), %rax
movb %bh, (%rax,%rbp)
movq 0x28(%r12), %rax
movb %bl, 0x1(%rax,%rbp)
incq %r13
movslq 0xa8(%rsp), %rax
cmpq %rax, %r13
jl 0x6ff7
movq 0x8(%r15), %rax
movq 0x8(%rsp), %rbx
movq (%rsp), %r12
leaq 0x28(%rsp), %r13
leaq 0x10(%rsp), %rbp
decl 0x221c(%rax)
incq %r12
cmpl $0x25, (%r14)
jne 0x70ce
movq 0x10(%r14), %rax
movq %rax, 0x10(%rbx)
movups (%r14), %xmm0
movups %xmm0, (%rbx)
movq (%r15), %rsi
movq %r13, %rdi
callq 0x941f
movq 0x38(%rsp), %rax
movq %rax, 0x10(%r14)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%r14)
cmpl $0x35, 0x18(%r15)
je 0x707e
cmpq $0x200, %r12            # imm = 0x200
jne 0x6ebb
leaq 0x49b7(%rip), %rsi      # 0xba6d
movq %r15, %rdi
movl $0x200, %edx            # imm = 0x200
callq 0x621b
movq 0x8(%r15), %rax
movl $0x200, %ecx            # imm = 0x200
jmp 0x70d1
movl %r12d, %ecx
movq %rcx, (%rsp)
movl 0x44(%r15), %edx
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl $0x7, %esi
callq 0x4af1
movq 0x8(%r15), %rax
movl 0x44(%r15), %edx
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl $0x2, %esi
callq 0x4af1
xorl %r13d, %r13d
movslq 0xb0(%rsp,%r13,4), %rbp
movq 0x8(%r15), %rax
movq 0x8(%rax), %r12
movl 0x20(%r12), %ebx
subl %ebp, %ebx
cmpl $0x10002, %ebx          # imm = 0x10002
jl 0x7139
movq %r15, %rdi
movq %r14, %rsi
leaq 0x4885(%rip), %rdx      # 0xb9b9
callq 0x61ef
addl $-0x2, %ebx
movq 0x28(%r12), %rax
movb %bh, (%rax,%rbp)
movq 0x28(%r12), %rax
movb %bl, 0x1(%rax,%rbp)
incq %r13
cmpq %r13, (%rsp)
jne 0x7108
cmpl $0x23, (%r14)
jne 0x719b
movq 0x10(%r14), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups (%r14), %xmm0
movups %xmm0, (%rcx)
leaq 0x28(%rsp), %rbx
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x941f
movq 0x38(%rsp), %rax
movq %rax, 0x10(%r14)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%r14)
cmpl $0x35, 0x18(%r15)
je 0x7175
jmp 0x71ad
leaq 0x48b8(%rip), %rdx      # 0xba5a
movq %r15, %rdi
movq %r14, %rsi
callq 0x61ef
addq $0x8b8, %rsp            # imm = 0x8B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq