// File: /workspace/github_c_makefile_star2_O3/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 0x20ce(%rip), %rsi      # 0x6200
movq %r15, %rdi
callq 0x31f0
testq %rax, %rax
je 0x4236
movq %rax, %r14
xorl %r15d, %r15d
leaq 0x7(%rsp), %r12
movabsq $0x1f8000001fa00001, %r13 # imm = 0x1F8000001FA00001
xorl %ebp, %ebp
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x3090
cmpq $0x1, %rax
jne 0x421b
xorl %ebx, %ebx
movzbl 0x7(%rsp), %eax
cmpl $0x23, %eax
je 0x41c4
cmpl $0xa, %eax
je 0x41f0
testb $0x1, %bl
jne 0x41a7
leal -0x3a(%rax), %ecx
cmpb $-0xb, %cl
ja 0x41dd
movl %eax, %ecx
addl $-0x2a, %ecx
cmpl $0x3c, %ecx
ja 0x41a7
btq %rcx, %r13
jb 0x41dd
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x3090
cmpq $0x1, %rax
je 0x417b
jmp 0x421b
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x3090
movb $0x1, %bl
jmp 0x41bc
movb %al, 0x10(%rsp,%rbp)
incq %rbp
cmpq $0x22, %rbp
jne 0x415a
jmp 0x4231
incl %r15d
movl %ebp, %eax
movb $0x0, 0x10(%rsp,%rax)
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl %ebp, %edx
callq 0x4021
testl %eax, %eax
jns 0x4158
movl %r15d, %edi
callq 0x33ca
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x30a0
callq 0x33ed
movq %r15, %rdi
callq 0x3412