// File: /workspace/github_c_makefile_star2_O2/umbrant[P]Non-blocking-Buffers/nbb.c
// Function: nbb_insert_item
int nbb_insert_item(int channel_id, const void* ptr_to_item, size_t size)
{
  assert(channel_id >= 0 && channel_id < SERVICE_MAX_CHANNELS);
  assert(ptr_to_item != NULL && size >= 0);

	struct buffer *buf = channel_list[channel_id].write;
	unsigned char *data_buf = channel_list[channel_id].write_data;

  unsigned short temp_ac = buf->ack_counter;

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

  if (buf->last_update_counter - temp_ac == 2 * BUFFER_SIZE) {
    return BUFFER_FULL;
  }

  if (buf->last_update_counter - temp_ac == (2 * BUFFER_SIZE) - 1) {
    return BUFFER_FULL_CONSUMER_READING;
  }

  // Check if there is space in the data region for new item
  // This works by checking how far the previous item extends,
  // and then if our size will fit in the remaining space
  // 
  // If it doesn't fit at the end, check at the head of the list too.
  struct channel_item* prev_item = 
				&(buf->items[(((buf->last_update_counter/2)-1)%BUFFER_SIZE)]);

	int item_offset;

	if(buf->last_update_counter == 0) {
		item_offset = 0;
	}
	else if((prev_item->offset+prev_item->size+size) <  buf->data_size) {
		item_offset = prev_item->offset + prev_item->size;
	}
	// Check if there's space at the head of the list for our item instead
	// This is done by checking the offset of the 
	// oldest unread item (at the ack counter)
	else if(buf->items[((buf->last_ack_counter)/2)%BUFFER_SIZE].offset > size) {
		item_offset = 0;
	} 
	// Couldn't fit at the end or the beginning. Sad.
	else {
		printf("else...\n");
		printf("poff: %d psize: %d size: %zu bsize: %d\n", prev_item->offset,
					prev_item->size, size, buf->data_size);
		return BUFFER_FULL;
	}

	// Update our new item in items[], say that we're writing
  buf->update_counter = buf->last_update_counter + 1;

	// Copy the item into the buffer's shm data region at offset
	memcpy(data_buf+item_offset, ptr_to_item, size);

	// Set the offset based on our above calculations
	buf->items[((buf->last_update_counter/2)%BUFFER_SIZE)].offset = item_offset;
	buf->items[((buf->last_update_counter/2)%BUFFER_SIZE)].size = size;

  // Done writing
  buf->update_counter = buf->last_update_counter + 2;

  // Also increment the recycle counter if it's in step w/ update_counter
  //if(recycle_counter == last_update_counter) {
  //	recycle_counter = update_counter;
  //}

  buf->last_update_counter = buf->update_counter;
 
  if(memcmp(NEW_CONN_NOTIFY_MSG, ptr_to_item, sizeof(NEW_CONN_NOTIFY_MSG))) {
    channel_list[channel_id].write_count += (size - 1); // Excluding '\0'
  }

  return OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0xf, %edi
jae 0x1a44
movq %rsi, %r14
testq %rsi, %rsi
je 0x1a63
movq %rdx, %r9
movl %edi, %eax
imulq $0x58, %rax, %r13
leaq 0x483b(%rip), %r15      # 0x6140
movq 0x18(%r13,%r15), %r12
movzwl (%r12), %edx
movzwl 0x6(%r12), %ecx
movl %ecx, %eax
subl %edx, %eax
pushq $0x1
popq %rbp
cmpl $0x200, %eax            # imm = 0x200
je 0x19fc
cmpl $0x1ff, %eax            # imm = 0x1FF
jne 0x1936
pushq $0x2
popq %rbp
jmp 0x19fc
movq 0x20(%r13,%r15), %rdi
movzwl %cx, %esi
movl %esi, %eax
shrl %eax
decl %eax
movw $0x100, %r8w            # imm = 0x100
cwtd
idivw %r8w
movl $0x0, %ebx
testw %si, %si
je 0x1995
movswq %dx, %rsi
movzwl 0xe(%r12,%rsi,4), %eax
movzwl 0x10(%r12,%rsi,4), %ebx
addl %eax, %ebx
leaq (%rbx,%r9), %rax
movzwl 0xc(%r12), %edx
cmpq %rdx, %rax
jb 0x1995
movzwl 0x2(%r12), %eax
andl $0x1fe, %eax            # imm = 0x1FE
movzwl 0xe(%r12,%rax,2), %eax
movl $0x0, %ebx
cmpq %r9, %rax
jbe 0x1a0d
incl %ecx
movw %cx, 0x4(%r12)
movl %ebx, %eax
addq %rax, %rdi
movq %r14, %rsi
movq %r9, %rdx
movq %r9, %rbp
callq 0x1130
movq %rbp, %rdx
movzwl 0x6(%r12), %eax
movl %eax, %ecx
andl $0x1fe, %ecx            # imm = 0x1FE
movw %bx, 0xe(%r12,%rcx,2)
movw %dx, 0x10(%r12,%rcx,2)
addl $0x2, %eax
movw %ax, 0x4(%r12)
movw %ax, 0x6(%r12)
movabsq $0x2a2a515f512a2a, %rax # imm = 0x2A2A515F512A2A
cmpq %rax, (%r14)
je 0x19fa
addq %r13, %r15
movl 0x2c(%r15), %eax
addl %edx, %eax
decl %eax
movl %eax, 0x2c(%r15)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f3c(%rip), %rdi      # 0x3950
movq %r9, %rbx
movq %rsi, %r14
callq 0x1070
movzwl 0xe(%r12,%r14,4), %esi
movzwl 0x10(%r12,%r14,4), %edx
movzwl 0xc(%r12), %r8d
leaq 0x1e7f(%rip), %rdi      # 0x38b7
movq %rbx, %rcx
xorl %eax, %eax
callq 0x10c0
jmp 0x19fc
leaq 0x1de7(%rip), %rdi      # 0x3832
leaq 0x1743(%rip), %rsi      # 0x3195
leaq 0x1e0e(%rip), %rcx      # 0x3867
movl $0x259, %edx            # imm = 0x259
callq 0x10d0
leaq 0x1e2c(%rip), %rdi      # 0x3896
leaq 0x1724(%rip), %rsi      # 0x3195
leaq 0x1def(%rip), %rcx      # 0x3867
movl $0x25a, %edx            # imm = 0x25A
callq 0x10d0