// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x8(%rbp), %rax
movq $0x0, (%rax)
movq -0x8(%rbp), %rax
movq $0x0, 0x8(%rax)
movl $0x0, -0x44(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x30c9(%rip), %rsi      # 0x822e
callq 0x41d0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x519b
movq 0x7e7c(%rip), %rax      # 0xcff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x30a7(%rip), %rsi      # 0x8231
movb $0x0, %al
callq 0x4130
movl $0x1, %edi
callq 0x4200
movl $0x0, -0x58(%rbp)
movb $0x0, -0x59(%rbp)
movq -0x50(%rbp), %rcx
leaq -0x51(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x4070
cmpq $0x1, %rax
jne 0x52d6
movzbl -0x51(%rbp), %eax
cmpl $0x23, %eax
jne 0x51d4
movb $0x1, -0x59(%rbp)
jmp 0x51a6
movzbl -0x51(%rbp), %eax
cmpl $0xa, %eax
jne 0x5238
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movb $0x0, -0x59(%rbp)
movl -0x44(%rbp), %eax
movb $0x0, -0x40(%rbp,%rax)
movq -0x8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movl -0x44(%rbp), %edx
callq 0x4f50
cmpl $0x0, %eax
jge 0x522c
movq 0x7dea(%rip), %rax      # 0xcff8
movq (%rax), %rdi
movl -0x58(%rbp), %edx
leaq 0x302f(%rip), %rsi      # 0x824a
movb $0x0, %al
callq 0x4130
movl $0x1, %edi
callq 0x4200
movl $0x0, -0x44(%rbp)
jmp 0x51a6
testb $0x1, -0x59(%rbp)
je 0x5243
jmp 0x51a6
movzbl -0x51(%rbp), %eax
cmpl $0x30, %eax
jl 0x5255
movzbl -0x51(%rbp), %eax
cmpl $0x39, %eax
jle 0x5296
movzbl -0x51(%rbp), %eax
cmpl $0x61, %eax
jl 0x5267
movzbl -0x51(%rbp), %eax
cmpl $0x66, %eax
jle 0x5294
movzbl -0x51(%rbp), %eax
cmpl $0x41, %eax
jl 0x5279
movzbl -0x51(%rbp), %eax
cmpl $0x46, %eax
jle 0x5292
movzbl -0x51(%rbp), %eax
cmpl $0x2a, %eax
je 0x5290
movzbl -0x51(%rbp), %eax
cmpl $0x3f, %eax
je 0x5290
jmp 0x51a6
jmp 0x5292
jmp 0x5294
jmp 0x5296
movb -0x51(%rbp), %cl
movl -0x44(%rbp), %eax
movb %cl, -0x40(%rbp,%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
cmpl $0x21, -0x44(%rbp)
jbe 0x52d1
movq 0x7d42(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x2fb1(%rip), %rsi      # 0x8271
movb $0x0, %al
callq 0x4130
movl $0x1, %edi
callq 0x4200
jmp 0x51a6
movq -0x50(%rbp), %rdi
callq 0x4080
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)