// File: /workspace/github_c_makefile_star2_O0/umbrant[P]Non-blocking-Buffers/nbb.c
// Function: nbb_read_item
int nbb_read_item(int channel_id, void** ptr_to_item, size_t* size)
{
	struct buffer *buf = channel_list[channel_id].read;
	unsigned char *data_buf = channel_list[channel_id].read_data;
  unsigned short temp_uc = buf->update_counter;

  assert(channel_id >= 0 && channel_id < SERVICE_MAX_CHANNELS);
  assert(ptr_to_item != NULL && size != NULL);

  *ptr_to_item = NULL;
  *size = 0;

  if(channel_id < 0) {
    printf("! nbb_read_item(): invalid channel_id %d\n", channel_id);
    return -1;
  }

  if(size < 0) {
    printf("! nbb_read_item(): invalid size %lu\n", *size);
    return -1;
  }

  if (temp_uc == buf->last_ack_counter) {
    return BUFFER_EMPTY; 
  }

  if ((temp_uc - buf->last_ack_counter) == 1) {
    return BUFFER_EMPTY_PRODUCER_INSERTING;
  }

  buf->ack_counter = buf->last_ack_counter + 1;

  // Copy out the value to malloc'd mem in our address space
  struct channel_item* tmp = 
				&(buf->items[((buf->last_ack_counter / 2) % BUFFER_SIZE)]);
  *ptr_to_item = malloc(tmp->size);
  memcpy(*ptr_to_item, data_buf+tmp->offset, tmp->size);
	*size = tmp->size;

  //printf("nbb_read_item ret pointer %x\n", ref_to_item);
  //printf("nbb_read_item ret pointer %x -> %d\n", ref_to_item, *ref_to_item);
  buf->ack_counter = buf->last_ack_counter + 2;
  buf->last_ack_counter = buf->ack_counter;

  return OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movslq -0x8(%rbp), %rcx
leaq 0x52c2(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movslq -0x8(%rbp), %rcx
leaq 0x52a9(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movw 0x4(%rax), %ax
movw %ax, -0x2a(%rbp)
cmpl $0x0, -0x8(%rbp)
jl 0x1ec0
cmpl $0xf, -0x8(%rbp)
jge 0x1ec0
jmp 0x1edf
leaq 0x2adc(%rip), %rdi      # 0x49a3
leaq 0x22db(%rip), %rsi      # 0x41a9
movl $0x2ab, %edx            # imm = 0x2AB
leaq 0x2b7f(%rip), %rcx      # 0x4a59
callq 0x10c0
cmpq $0x0, -0x10(%rbp)
je 0x1eef
cmpq $0x0, -0x18(%rbp)
je 0x1eef
jmp 0x1f0e
leaq 0x2b8d(%rip), %rdi      # 0x4a83
leaq 0x22ac(%rip), %rsi      # 0x41a9
movl $0x2ac, %edx            # imm = 0x2AC
leaq 0x2b50(%rip), %rcx      # 0x4a59
callq 0x10c0
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
cmpl $0x0, -0x8(%rbp)
jge 0x1f47
movl -0x8(%rbp), %esi
leaq 0x2b73(%rip), %rdi      # 0x4aa7
movb $0x0, %al
callq 0x10b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2070
cmpq $0x0, -0x18(%rbp)
jae 0x1f6f
movq -0x18(%rbp), %rax
movq (%rax), %rsi
leaq 0x2b75(%rip), %rdi      # 0x4ad1
movb $0x0, %al
callq 0x10b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2070
movzwl -0x2a(%rbp), %eax
movq -0x20(%rbp), %rcx
movzwl 0x2(%rcx), %ecx
cmpl %ecx, %eax
jne 0x1f8b
movl $0x3, -0x4(%rbp)
jmp 0x2070
movzwl -0x2a(%rbp), %eax
movq -0x20(%rbp), %rcx
movzwl 0x2(%rcx), %ecx
subl %ecx, %eax
cmpl $0x1, %eax
jne 0x1faa
movl $0x4, -0x4(%rbp)
jmp 0x2070
movq -0x20(%rbp), %rax
movzwl 0x2(%rax), %eax
addl $0x1, %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movq -0x20(%rbp), %rax
addq $0xe, %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movzwl 0x2(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movq -0x40(%rbp), %rax
movslq %edx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x2(%rax), %eax
movl %eax, %edi
callq 0x1160
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rax
movzwl (%rax), %eax
cltq
addq %rax, %rsi
movq -0x38(%rbp), %rax
movzwl 0x2(%rax), %eax
movl %eax, %edx
callq 0x1130
movq -0x38(%rbp), %rax
movzwl 0x2(%rax), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movzwl 0x2(%rax), %eax
addl $0x2, %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movq -0x20(%rbp), %rax
movw (%rax), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x2(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)