// File: /workspace/github_c_makefile_star2_O1/melynx[P]peekaboo/read_trace.c
// Function: load_pattern
void load_pattern(cache_linked_list_t *pattern_ptr, const char* pattern_file_path)
{
    // Init pattern
    pattern_ptr->length = 0;
    pattern_ptr->head = NULL;
    pattern_ptr->tail = NULL;

    // Load pattern, if given.
    uint8_t buffer[33];
    unsigned int buffer_size = 0;
    FILE* file = fopen(pattern_file_path, "rb");
    if (!file) PEEKABOO_DIE("No such pattern file %s\n", pattern_file_path);
    uint8_t c;
    uint32_t line_num = 0;
    bool line_is_commented = false;
    while (fread(&c, 1, 1, file) == 1) 
    {
        // Check if this is comment
        if (c == '#') 
        {
            line_is_commented = true;
            continue;
        }

        // Check if this is '\n'
        if (c == '\n')
        {   
            // Update line number 
            line_num++;

            // Reset commented
            line_is_commented = false;

            // Parse buffer
            buffer[buffer_size] = 0x0;
            if (append2pattern_list(pattern_ptr, buffer, buffer_size) < 0) PEEKABOO_DIE("Fail to parse input pattern at line %u", line_num);

            // Reset buffer to load next instruction
            buffer_size = 0;
            continue;
        } 

        // Check if commented
        if (line_is_commented) continue;

        // Parse this char
        if (c < '0' || c > '9')
            if (c < 'a' || c > 'f')
                if (c < 'A' || c > 'F')
                    if (c != '*' && c != '?') // Arbitrary matching
                    {
                        // this is not a hex char, or an arbitrary matching char
                        continue;
                    }
        buffer[buffer_size] = c;
        buffer_size++;
        if (buffer_size > 33) PEEKABOO_DIE("Pattern: Rawbytes are too long for one instruction!");
    }
    fclose(file);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq %rdi, 0x8(%rsp)
movq $0x0, 0x10(%rdi)
leaq 0x20e1(%rip), %rsi      # 0x6200
movq %r15, %rdi
callq 0x31e0
testq %rax, %rax
je 0x4203
movq %rax, %r14
xorl %ebp, %ebp
leaq 0x7(%rsp), %r15
movabsq $0x1f8000001fa00001, %r12 # imm = 0x1F8000001FA00001
xorl %r13d, %r13d
xorl %ebx, %ebx
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x3090
cmpq $0x1, %rax
jne 0x41e8
movzbl 0x7(%rsp), %eax
cmpl $0x23, %eax
jne 0x418b
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x3090
movb $0x1, %bl
jmp 0x415e
cmpl $0xa, %eax
je 0x41bd
testb $0x1, %bl
jne 0x4149
leal -0x3a(%rax), %ecx
cmpb $-0xb, %cl
ja 0x41ad
movl %eax, %ecx
addl $-0x2a, %ecx
cmpl $0x3c, %ecx
ja 0x4149
btq %rcx, %r12
jae 0x4149
movb %al, 0x10(%rsp,%r13)
incq %r13
cmpq $0x22, %r13
jne 0x4149
jmp 0x41fe
incl %ebp
movl %r13d, %eax
movb $0x0, 0x10(%rsp,%rax)
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl %r13d, %edx
callq 0x400e
testl %eax, %eax
jns 0x4144
movl %ebp, %edi
callq 0x33ba
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x30a0
callq 0x33dd
movq %r15, %rdi
callq 0x3402