// File: /workspace/github_c_makefile_star2_O0/melynx[P]peekaboo/read_trace.c
// Function: update_raw_byte_buffer
void update_raw_byte_buffer(cache_linked_list_t *instr_buffer, 
                            char const *cur_insn_rawbytes, 
                            const uint32_t instr_size,
                            const size_t target_length)
{
    // Remove instr from head if buffer is full
    if (instr_buffer->length > target_length)
    {
        assert(instr_buffer->head != NULL);
        assert(instr_buffer->head != instr_buffer->tail);
        insn_rawbyte_node_t *new_head = instr_buffer->head->succ;
        assert(new_head != NULL);
        free_insn_rawbyte_node(instr_buffer->head);
        instr_buffer->head = new_head;
        instr_buffer->length -= 1;
    }

    // New instr
    insn_rawbyte_node_t *new_node = malloc(sizeof(insn_rawbyte_node_t));
    if (!new_node) PEEKABOO_DIE("Failed to malloc!");
    new_node->is_arbitrary = false;
    new_node->prec = instr_buffer->tail;
    new_node->succ = NULL;
    new_node->size = instr_size;
    new_node->bytes = malloc(sizeof(uint16_t) * instr_size);
    uint32_t idx;
    for (idx=0; idx<instr_size; idx++)
    {
        new_node->bytes[idx] = cur_insn_rawbytes[idx] & 0xFF;
    }

    // Update buffer
    if (instr_buffer->head == NULL) 
    {
        instr_buffer->head = new_node;
    }
    if (instr_buffer->tail != NULL) 
    {
        instr_buffer->tail->succ = new_node;
    }
    instr_buffer->tail = new_node;
    instr_buffer->length += 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
cmpq -0x20(%rbp), %rax
jbe 0x4764
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x46b5
jmp 0x46d4
leaq 0x39d8(%rip), %rdi      # 0x8094
leaq 0x39ec(%rip), %rsi      # 0x80af
movl $0xfd, %edx
leaq 0x39ed(%rip), %rcx      # 0x80bc
callq 0x40f0
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
cmpq 0x8(%rcx), %rax
je 0x46e7
jmp 0x4706
leaq 0x3a2d(%rip), %rdi      # 0x811b
leaq 0x39ba(%rip), %rsi      # 0x80af
movl $0xfe, %edx
leaq 0x39bb(%rip), %rcx      # 0x80bc
callq 0x40f0
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x471e
jmp 0x473d
leaq 0x3a1f(%rip), %rdi      # 0x8144
leaq 0x3983(%rip), %rsi      # 0x80af
movl $0x100, %edx            # imm = 0x100
leaq 0x3984(%rip), %rcx      # 0x80bc
callq 0x40f0
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x4640
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
subq $0x1, %rcx
movq %rcx, 0x10(%rax)
movl $0x28, %edi
callq 0x4190
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x479b
movq 0x8878(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x39cb(%rip), %rsi      # 0x8155
movb $0x0, %al
callq 0x4130
movl $0x1, %edi
callq 0x4200
movq -0x30(%rbp), %rax
movb $0x0, (%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x20(%rax)
movl -0x14(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x14(%rbp), %eax
movl %eax, %edi
shlq %rdi
callq 0x4190
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x481c
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movsbl (%rax,%rcx), %eax
andl $0xff, %eax
movw %ax, %dx
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
movw %dx, (%rax,%rcx,2)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x47e7
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x4831
movq -0x30(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x484c
movq -0x30(%rbp), %rcx
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
addq $0x40, %rsp
popq %rbp
retq
nop