// File: /workspace/github_c_makefile_star2_O0/melynx[P]peekaboo/read_trace.c
// Function: append2pattern_list
int append2pattern_list(cache_linked_list_t *pattern_ptr, const uint8_t *buffer, const unsigned int buffer_size)
{
    // Empty buffer, directly return
    if (buffer_size == 0) return 0;

    insn_rawbyte_node_t *new_node = malloc(sizeof(insn_rawbyte_node_t));
    if (!new_node) PEEKABOO_DIE("Failed to malloc.");
    new_node->succ = NULL;
    
    if (pattern_ptr->head == NULL)
    {
        pattern_ptr->head = new_node;
    }

    // Update tail and length
    insn_rawbyte_node_t *curr_tail_node = pattern_ptr->tail;
    if (curr_tail_node != NULL)
    {
        curr_tail_node->succ = new_node;
    }
    new_node->prec = curr_tail_node;
    pattern_ptr->tail = new_node;
    pattern_ptr->length += 1;

    new_node->bytes = malloc((buffer_size/2+1)*sizeof(uint16_t));
    if(!new_node->bytes) PEEKABOO_DIE("Failed to malloc.");
    new_node->size = hex_string_to_uint16_arrary(new_node->bytes, buffer);
    if (new_node->size <= 0) return -1; 

    // Find if abitrary
    size_t idx;
    new_node->is_arbitrary = false;
    for (idx=0; idx<new_node->size; idx++)
    {
        if (new_node->bytes[idx] == 0x100*16+0x100) // Matched "**"
        {
            new_node->is_arbitrary = true;
            new_node->size = 0;
            free(new_node->bytes);
            new_node->bytes = NULL;
            break;
        }
    }
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x4f75
movl $0x0, -0x4(%rbp)
jmp 0x5108
movl $0x28, %edi
callq 0x4190
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x4fac
movq 0x8067(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x3281(%rip), %rsi      # 0x821c
movb $0x0, %al
callq 0x4130
movl $0x1, %edi
callq 0x4200
movq -0x28(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x4fcd
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x4fec
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movl -0x1c(%rbp), %eax
shrl %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
shlq %rdi
callq 0x4190
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x5060
movq 0x7fb3(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x31cd(%rip), %rsi      # 0x821c
movb $0x0, %al
callq 0x4130
movl $0x1, %edi
callq 0x4200
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x4520
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jg 0x508d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5108
movq -0x28(%rbp), %rax
movb $0x0, (%rax)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq 0x10(%rcx), %rcx
cmpq %rcx, %rax
jae 0x5101
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x38(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
cmpl $0x1100, %eax           # imm = 0x1100
jne 0x50f1
movq -0x28(%rbp), %rax
movb $0x1, (%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4030
movq -0x28(%rbp), %rax
movq $0x0, 0x8(%rax)
jmp 0x5101
jmp 0x50f3
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x509c
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)