// File: /workspace/github_c_makefile_star2_O2/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, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq %rdi, 0x8(%rsp)
andq $0x0, 0x10(%rdi)
leaq 0x26e9(%rip), %rsi      # 0x6200
movq %r14, %rdi
callq 0x31f0
testq %rax, %rax
je 0x3c1b
movq %rax, %r15
xorl %r14d, %r14d
leaq 0x7(%rsp), %r12
pushq $0x1
popq %r13
xorl %ebp, %ebp
xorl %ebx, %ebx
movq %r12, %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x3090
cmpq $0x1, %rax
jne 0x3be1
movzbl 0x7(%rsp), %eax
cmpl $0x23, %eax
je 0x3b8e
cmpl $0xa, %eax
je 0x3ba1
testb $0x1, %bl
jne 0x3b3b
leal -0x3a(%rax), %ecx
cmpb $-0xb, %cl
ja 0x3b92
movl %eax, %ecx
addl $-0x2a, %ecx
cmpl $0x3c, %ecx
ja 0x3b3b
movabsq $0x1f8000001fa00001, %rdx # imm = 0x1F8000001FA00001
btq %rcx, %rdx
jae 0x3b3b
jmp 0x3b92
movb $0x1, %bl
jmp 0x3b3b
movb %al, 0x10(%rsp,%rbp)
incq %rbp
cmpq $0x22, %rbp
jne 0x3b39
jmp 0x3bf7
incl %r14d
movl %ebp, %eax
movb $0x0, 0x10(%rsp,%rax)
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl %ebp, %edx
callq 0x3a00
testl %eax, %eax
jns 0x3b37
movq 0x742d(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x2647(%rip), %rsi      # 0x621c
movl %r14d, %edx
xorl %eax, %eax
callq 0x3160
jmp 0x3c13
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x30a0
movq 0x73fa(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x263b(%rip), %rdi      # 0x6243
pushq $0x33
popq %rsi
pushq $0x1
popq %rdx
callq 0x3230
pushq $0x1
popq %rdi
callq 0x3220
movq 0x73d6(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x25d7(%rip), %rsi      # 0x6203
movq %r14, %rdx
xorl %eax, %eax
callq 0x3160
jmp 0x3c13