// File: /workspace/github_c_makefile_star2_O0/agl[P]obstcp/libobstcp.c
// Function: obstcp_accum_commit
ssize_t EXPORTED
obstcp_accum_commit(struct obstcp_accum *ac, ssize_t bytes) {
  struct obstcp_accum_buffer *ab = NULL;
  uint8_t c, next;
  ssize_t used = 0;

  if (bytes < 0) {
    errno = EINVAL;
    return -1;
  }

  for (c = ac->data_head; bytes && c != 0xff; c = next) {
    ab = &ac->buffers[c];

    if (ab->prefix_used < ab->prefix_len) {
      size_t a = ab->prefix_len - ab->prefix_used;
      if (a > bytes) a = bytes;
      ab->prefix_used += a;
      bytes -= a;
    }

    size_t a = ab->payload_len - ab->payload_used;
    if (a > bytes) a = bytes;
    ab->payload_used += a;
    bytes -= a;
    used += a;

    next = ab->next;

    if (ab->payload_len == ab->payload_used &&
        ab->prefix_len == ab->prefix_used) {

      ac->data_head = ab->next;
      if (ac->data_head == 0xff) ac->data_tail = 0xff;

      ab->next = ac->free_head;
      ac->free_head = c;
    }
  }

  // The user has claimed to have written more bytes than we've ever given
  // them.
  if (bytes) {
    errno = EINVAL;
    return -1;
  }

  return used;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
cmpq $0x0, -0x18(%rbp)
jge 0x423f
callq 0x1050
movl $0x16, (%rax)
movq $-0x1, -0x8(%rbp)
jmp 0x43ee
movq -0x10(%rbp), %rax
movb 0x1bb(%rax), %al
movb %al, -0x21(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x18(%rbp)
movb %al, -0x41(%rbp)
je 0x4267
movzbl -0x21(%rbp), %eax
cmpl $0xff, %eax
setne %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x4273
jmp 0x43ca
movq -0x10(%rbp), %rax
addq $0xa, %rax
movzbl -0x21(%rbp), %ecx
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movzbl 0x5(%rax), %eax
movq -0x20(%rbp), %rcx
movzbl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jge 0x42e9
movq -0x20(%rbp), %rax
movzbl 0x4(%rax), %eax
movq -0x20(%rbp), %rcx
movzbl 0x5(%rcx), %ecx
subl %ecx, %eax
cltq
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x42c8
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rax
movzbl 0x5(%rax), %ecx
addq %rdx, %rcx
movb %cl, 0x5(%rax)
movq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
movq -0x20(%rbp), %rcx
movzwl 0x2(%rcx), %ecx
subl %ecx, %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x4312
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdx
movq -0x20(%rbp), %rax
movzwl 0x2(%rax), %ecx
addq %rdx, %rcx
movw %cx, 0x2(%rax)
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x40(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movb 0x6(%rax), %al
movb %al, -0x22(%rbp)
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
movq -0x20(%rbp), %rcx
movzwl 0x2(%rcx), %ecx
cmpl %ecx, %eax
jne 0x43bd
movq -0x20(%rbp), %rax
movzbl 0x4(%rax), %eax
movq -0x20(%rbp), %rcx
movzbl 0x5(%rcx), %ecx
cmpl %ecx, %eax
jne 0x43bd
movq -0x20(%rbp), %rax
movb 0x6(%rax), %cl
movq -0x10(%rbp), %rax
movb %cl, 0x1bb(%rax)
movq -0x10(%rbp), %rax
movzbl 0x1bb(%rax), %eax
cmpl $0xff, %eax
jne 0x439f
movq -0x10(%rbp), %rax
movb $-0x1, 0x1bc(%rax)
movq -0x10(%rbp), %rax
movb 0x1ba(%rax), %cl
movq -0x20(%rbp), %rax
movb %cl, 0x6(%rax)
movb -0x21(%rbp), %cl
movq -0x10(%rbp), %rax
movb %cl, 0x1ba(%rax)
jmp 0x43bf
movb -0x22(%rbp), %al
movb %al, -0x21(%rbp)
jmp 0x424c
cmpq $0x0, -0x18(%rbp)
je 0x43e6
callq 0x1050
movl $0x16, (%rax)
movq $-0x1, -0x8(%rbp)
jmp 0x43ee
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)