// File: /workspace/github_c_makefile_star2_O0/umbrant[P]Non-blocking-Buffers/nbb.c
// Function: nbb_read_bytes
int nbb_read_bytes(int slot, char* buf, int size)
{
  assert(slot >= 0 && buf != NULL && size >= 0);

  delay_buffer_t* delay_buffer = &(delay_buffers[slot]);
  printf("***NBB***: Delay buffer %d: %d/%d\n",
         slot, delay_buffer->len, delay_buffer->capacity);
  assert(delay_buffer->capacity >= delay_buffer->len);

  // Attempt to read 0 bytes or buffer has nothing to read
  if (size == 0 || delay_buffer->content == NULL || delay_buffer->len == 0) {
    return 0;
  }

  assert(delay_buffer->content != NULL && delay_buffer->len > 0);

  // Read minimum of the requested length and available data
  if(size > delay_buffer->len) {
    size = delay_buffer->len;
  }

  // Read |size| bytes into |buf| and update statistics
  memcpy(buf, delay_buffer->content, size);
  channel_list[slot].read_count += size;

  // Move remaining data (if any) into the front of buffer
  int new_len = delay_buffer->len - size;
  if (new_len > 0) {
    memmove(delay_buffer->content, delay_buffer->content + size, new_len);
  }
  delay_buffer->len = new_len;

  return size;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
cmpl $0x0, -0x8(%rbp)
jl 0x30f7
cmpq $0x0, -0x10(%rbp)
je 0x30f7
cmpl $0x0, -0x14(%rbp)
jl 0x30f7
jmp 0x3116
leaq 0x1708(%rip), %rdi      # 0x4806
leaq 0x10a4(%rip), %rsi      # 0x41a9
movl $0x206, %edx            # imm = 0x206
leaq 0x171b(%rip), %rcx      # 0x482c
callq 0x10c0
movslq -0x8(%rbp), %rcx
leaq 0x464f(%rip), %rax      # 0x7770
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movl -0x8(%rbp), %esi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %ecx
leaq 0x170d(%rip), %rdi      # 0x4851
movb $0x0, %al
callq 0x10b0
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jl 0x315d
jmp 0x317c
leaq 0x1710(%rip), %rdi      # 0x4874
leaq 0x103e(%rip), %rsi      # 0x41a9
movl $0x20b, %edx            # imm = 0x20B
leaq 0x16b5(%rip), %rcx      # 0x482c
callq 0x10c0
cmpl $0x0, -0x14(%rbp)
je 0x3196
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
je 0x3196
movq -0x20(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x31a2
movl $0x0, -0x4(%rbp)
jmp 0x325d
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
je 0x31b8
movq -0x20(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jle 0x31b8
jmp 0x31d7
leaq 0x16e1(%rip), %rdi      # 0x48a0
leaq 0xfe3(%rip), %rsi       # 0x41a9
movl $0x212, %edx            # imm = 0x212
leaq 0x165a(%rip), %rcx      # 0x482c
callq 0x10c0
movl -0x14(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jle 0x31ed
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
movslq -0x14(%rbp), %rdx
callq 0x1130
movl -0x14(%rbp), %ecx
movslq -0x8(%rbp), %rdx
leaq 0x3f31(%rip), %rax      # 0x7140
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
addl 0x14(%rax), %ecx
movl %ecx, 0x14(%rax)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x324d
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
movslq -0x14(%rbp), %rax
addq %rax, %rsi
movslq -0x24(%rbp), %rdx
callq 0x1180
movl -0x24(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)