// File: /workspace/github_c_makefile_star2_O0/creationix[P]color-control/src/libs/reader.c
// Function: reader_push
reader_result_t reader_push(reader_t* reader, uint8_t* data, uint16_t len) {

  // Read the header in an interruptible way if we don't have it all yet.
  while (!reader->body) {
    if (!len--) return READER_NEED_MORE;
    uint8_t byte = *data++;
    uint8_t offset = reader->offset++;
    if (offset == 0) {
      if (byte == '(') continue;
    }
    if (offset == 1) {
      if (byte == ']') continue;
    }
    if (offset == 2) {
      reader->size = byte << 8;
      continue;
    }
    if (offset == 3) {
      reader->size = (reader->size | byte) - 6;
      reader->offset = 0;
      reader->body = malloc(reader->size);
      if (reader->size) break;
    }
    return READER_INVALID_START_BYTES;
  }

  uint16_t bytes_left = reader->size - reader->offset;

  if (len >= bytes_left) {
    memcpy(reader->body + reader->offset, data, bytes_left);
    if (len > bytes_left + 1) {
      if (data[bytes_left] != '[' || data[bytes_left + 1] != ')') {
        return READER_INVALID_END_BYTES;
      }
    }
    // TODO: verify trailing bytes when they are broken to a new packet?
    return READER_DONE;
  }

  memcpy(reader->body + reader->offset, data, len);
  reader->offset += len;
  return READER_NEED_MORE;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movw %dx, %ax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movw %ax, -0x1a(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1f2e
jmp 0x2027
movw -0x1a(%rbp), %ax
movw %ax, %cx
addw $-0x1, %cx
movw %cx, -0x1a(%rbp)
cmpw $0x0, %ax
jne 0x1f4f
movl $0x1, -0x4(%rbp)
jmp 0x20f4
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movb (%rax), %al
movb %al, -0x1b(%rbp)
movq -0x10(%rbp), %rcx
movw 0x2(%rcx), %ax
movw %ax, %dx
addw $0x1, %dx
movw %dx, 0x2(%rcx)
movb %al, -0x1c(%rbp)
movzbl -0x1c(%rbp), %eax
cmpl $0x0, %eax
jne 0x1f8f
movzbl -0x1b(%rbp), %eax
cmpl $0x28, %eax
jne 0x1f8d
jmp 0x1f17
jmp 0x1f8f
movzbl -0x1c(%rbp), %eax
cmpl $0x1, %eax
jne 0x1fa8
movzbl -0x1b(%rbp), %eax
cmpl $0x5d, %eax
jne 0x1fa6
jmp 0x1f17
jmp 0x1fa8
movzbl -0x1c(%rbp), %eax
cmpl $0x2, %eax
jne 0x1fc7
movzbl -0x1b(%rbp), %eax
shll $0x8, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, (%rax)
jmp 0x1f17
movzbl -0x1c(%rbp), %eax
cmpl $0x3, %eax
jne 0x201b
movq -0x10(%rbp), %rax
movzwl (%rax), %eax
movzbl -0x1b(%rbp), %ecx
orl %ecx, %eax
subl $0x6, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, (%rax)
movq -0x10(%rbp), %rax
movw $0x0, 0x2(%rax)
movq -0x10(%rbp), %rax
movzwl (%rax), %eax
movl %eax, %edi
callq 0x10a0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
cmpw $0x0, (%rax)
je 0x2019
jmp 0x2027
jmp 0x201b
movl $0x2, -0x4(%rbp)
jmp 0x20f4
movq -0x10(%rbp), %rax
movzwl (%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x2(%rcx), %ecx
subl %ecx, %eax
movw %ax, -0x1e(%rbp)
movzwl -0x1a(%rbp), %eax
movzwl -0x1e(%rbp), %ecx
cmpl %ecx, %eax
jl 0x20b7
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x2(%rax), %eax
cltq
addq %rax, %rdi
movq -0x18(%rbp), %rsi
movzwl -0x1e(%rbp), %eax
movl %eax, %edx
callq 0x1080
movzwl -0x1a(%rbp), %eax
movzwl -0x1e(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0x20ae
movq -0x18(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x5b, %eax
jne 0x20a3
movq -0x18(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x29, %eax
je 0x20ac
movl $0x3, -0x4(%rbp)
jmp 0x20f4
jmp 0x20ae
movl $0x0, -0x4(%rbp)
jmp 0x20f4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x2(%rax), %eax
cltq
addq %rax, %rdi
movq -0x18(%rbp), %rsi
movzwl -0x1a(%rbp), %eax
movl %eax, %edx
callq 0x1080
movzwl -0x1a(%rbp), %edx
movq -0x10(%rbp), %rax
movzwl 0x2(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x2(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)