// File: /workspace/github_c_makefile_star2_O1/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 0x6ab6
leaq 0x18(%r15), %r14
cmpl $0x22, 0x18(%r15)
jne 0x71b4
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 0x97fb
movq 0xc0(%rsp), %rax
movq %rax, 0x10(%r14)
movups 0xb0(%rsp), %xmm0
movups %xmm0, (%r14)
cmpl $0x35, 0x18(%r15)
je 0x7188
jmp 0x71c6
leaq 0x4827(%rip), %rdx      # 0xb9e2
movq %r15, %rdi
movq %r14, %rsi
callq 0x64ec
leaq 0x30(%r15), %rax
movq %rax, 0x8(%rsp)
xorl %ebx, %ebx
leaq 0x28(%rsp), %r12
leaq 0x10(%rsp), %r13
cmpq $0x200, %rbx            # imm = 0x200
je 0x73e3
movq 0x8(%r15), %rax
incl 0x221c(%rax)
movl $0x88, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x2100
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x88d3
cmpl $0x24, 0x18(%r15)
jne 0x724f
movq 0x10(%r14), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups (%r14), %xmm0
movups %xmm0, (%rcx)
movq (%r15), %rsi
movq %r13, %rdi
callq 0x97fb
movq 0x20(%rsp), %rax
movq %rax, 0x10(%r14)
movups 0x10(%rsp), %xmm0
movups %xmm0, (%r14)
cmpl $0x35, 0x18(%r15)
je 0x7229
jmp 0x7261
movq %r15, %rdi
movq %r14, %rsi
leaq 0x47a4(%rip), %rdx      # 0xba00
callq 0x64ec
movq %r15, %rdi
movl $0x1, %esi
callq 0x6ab6
movq 0x8(%r15), %rax
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl 0x44(%r15), %edx
movl $0x5, %esi
callq 0x4d19
movq 0x8(%r15), %rax
movl 0x221c(%rax), %esi
movq %r15, %rdi
callq 0x69c6
movq 0x8(%r15), %rax
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl 0x44(%r15), %edx
movl $0x4, %esi
callq 0x4d19
movq 0x8(%r15), %rax
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl 0x44(%r15), %edx
movl $0x1e, %esi
callq 0x4d19
movq 0x8(%r15), %rax
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl 0x44(%r15), %edx
movl $0xff, %esi
callq 0x4d19
movq 0x8(%r15), %rax
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl 0x44(%r15), %edx
movl $0xff, %esi
callq 0x4d19
movq 0x8(%r15), %rax
movq 0x8(%rax), %rax
movl 0x20(%rax), %eax
addl $-0x2, %eax
movq %rbx, (%rsp)
movl %eax, 0xb0(%rsp,%rbx,4)
cmpl $0x0, 0xa8(%rsp)
jle 0x737a
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 0x7356
movq %r15, %rdi
movq %r14, %rsi
leaq 0x4628(%rip), %rdx      # 0xb979
callq 0x64ec
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 0x7328
movq 0x8(%r15), %rax
decl 0x221c(%rax)
movq (%rsp), %rbx
incq %rbx
movl 0x18(%r15), %ebp
cmpl $0x25, %ebp
leaq 0x28(%rsp), %r12
leaq 0x10(%rsp), %r13
jne 0x73df
movq 0x10(%r14), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups (%r14), %xmm0
movups %xmm0, (%rcx)
movq (%r15), %rsi
movq %r12, %rdi
callq 0x97fb
movq 0x38(%rsp), %rax
movq %rax, 0x10(%r14)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%r14)
cmpl $0x35, 0x18(%r15)
je 0x73b2
cmpl $0x25, %ebp
je 0x71db
movl %ebx, %eax
jmp 0x73fc
leaq 0x4643(%rip), %rsi      # 0xba2d
movq %r15, %rdi
movl $0x200, %edx            # imm = 0x200
callq 0x6518
movl $0x200, %eax            # imm = 0x200
movq %rax, (%rsp)
movq 0x8(%r15), %rax
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl 0x44(%r15), %edx
movl $0x7, %esi
callq 0x4d19
movq 0x8(%r15), %rax
movq 0x8(%rax), %rdi
addq $0x20, %rdi
movl 0x44(%r15), %edx
movl $0x2, %esi
callq 0x4d19
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 0x7468
movq %r15, %rdi
movq %r14, %rsi
leaq 0x4516(%rip), %rdx      # 0xb979
callq 0x64ec
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 0x7437
cmpl $0x23, (%r14)
jne 0x74c7
movq 0x10(%r14), %rax
movq %rax, 0x40(%r15)
movups (%r14), %xmm0
movups %xmm0, 0x30(%r15)
leaq 0x28(%rsp), %rbx
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x97fb
movq 0x38(%rsp), %rax
movq %rax, 0x10(%r14)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%r14)
cmpl $0x35, 0x18(%r15)
je 0x74a1
jmp 0x74d9
leaq 0x454c(%rip), %rdx      # 0xba1a
movq %r15, %rdi
movq %r14, %rsi
callq 0x64ec
addq $0x8b8, %rsp            # imm = 0x8B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq