// File: /workspace/github_c_makefile_star2_O2/umbrant[P]Non-blocking-Buffers/nbb.c
// Function: nbb_open_channel
int nbb_open_channel(const char* owner, int shm_read_id, int shm_write_id, int is_ipc_create)
{
	int shmid;
	unsigned char * shm;
  int free_slot;

  if(shm_read_id == NAMESERVER_WRITE && shm_write_id == NAMESERVER_READ) {
    free_slot = 0;
  }
  else {
    free_slot = nbb_free_channel_slot();
  }

  if(free_slot == -1) {
	  printf("! nbb_open_channel(): no free_slot\n");
    return -1;
  }

	// Allocate 4 pages, 1 meta + 1 data for each buffer
	// Read buffer
	// note that we use SERVICE_TEST_WRITE, not READ, since the service's
	// read is the client's write
	if((shmid = shmget(shm_read_id, PAGE_SIZE*2, is_ipc_create | 0666)) < 0) {
		perror("shmget");
		return -1;
	}
	if((shm = (unsigned char *) shmat(shmid, NULL, 0)) == (unsigned char*) -1) {
		perror("shmat");
		return -1;
	}
	// Make sure the memory is zero'd out
	memset(shm, 0, PAGE_SIZE*2);

	channel_list[free_slot].read = (struct buffer*) shm;
	channel_list[free_slot].read->data_size = PAGE_SIZE;
	channel_list[free_slot].read->data_offset = PAGE_SIZE;
	channel_list[free_slot].read_data = (unsigned char*) shm+PAGE_SIZE;
  channel_list[free_slot].read_id = shm_read_id;
  channel_list[free_slot].read_count = 0;

	// Write buffer. Same note as above about swapping read/write
	shmid = -1;
	shm = (unsigned char*) -1;
	if((shmid = shmget(shm_write_id, PAGE_SIZE*2, is_ipc_create | 0666)) < 0) {
		perror("shmget");
		return -1;
	}
	if((shm = (unsigned char *) shmat(shmid, NULL, 0)) == (unsigned char*) -1) {
		perror("shmat");
		return -1;
	}
	// Make sure the memory is zero'd out
	memset(shm, 0, PAGE_SIZE*2);

	channel_list[free_slot].write = (struct buffer*) (shm);
	channel_list[free_slot].write->data_size = PAGE_SIZE;
	channel_list[free_slot].write->data_offset = PAGE_SIZE;
	channel_list[free_slot].write_data = (unsigned char*) shm+PAGE_SIZE;
  channel_list[free_slot].write_id = shm_write_id;
  channel_list[free_slot].write_count = 0;

  channel_list[free_slot].in_use = 1;

  if(owner) {
    channel_list[free_slot].owner = (char*) malloc(strlen(owner) + 1);
    strcpy(channel_list[free_slot].owner, owner);
  }

  memset(&delay_buffers[free_slot], 0, sizeof(struct delay_buffer));

  return free_slot;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %r15d
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, %r14
movl %esi, %eax
xorl $0xfa0, %eax            # imm = 0xFA0
movl %edx, %ecx
xorl $0xfa1, %ecx            # imm = 0xFA1
xorl %ebx, %ebx
orl %eax, %ecx
je 0x1768
callq 0x268a
movl %eax, %ebx
cmpl $-0x1, %eax
je 0x18a0
orl $0x1b6, %r15d           # imm = 0x1B6
movl $0x2000, %esi           # imm = 0x2000
movl %r12d, %edi
movl %r15d, %edx
callq 0x11f0
testl %eax, %eax
js 0x18ae
movq %r14, 0x10(%rsp)
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11e0
cmpq $-0x1, %rax
je 0x18b7
movq %rax, %r13
movl $0x2000, %edx           # imm = 0x2000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10e0
movl %ebx, 0x4(%rsp)
movslq %ebx, %rax
movq %rax, 0x8(%rsp)
imulq $0x58, %rax, %rbx
leaq 0x4976(%rip), %r14      # 0x6140
movq %r13, (%rbx,%r14)
movl $0x10001000, 0xa(%r13)  # imm = 0x10001000
addq $0x1000, %r13           # imm = 0x1000
movq %r13, 0x8(%rbx,%r14)
movl %r12d, 0x10(%rbx,%r14)
andl $0x0, 0x14(%rbx,%r14)
movl $0x2000, %esi           # imm = 0x2000
movl %ebp, %edi
movl %r15d, %edx
callq 0x11f0
testl %eax, %eax
js 0x18ae
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11e0
cmpq $-0x1, %rax
je 0x18b7
movq %rax, %r15
addq %rbx, %r14
movl $0x2000, %edx           # imm = 0x2000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10e0
movq %r15, 0x18(%r14)
movl $0x10001000, 0xa(%r15)  # imm = 0x10001000
addq $0x1000, %r15           # imm = 0x1000
movq %r15, 0x20(%r14)
movl %ebp, 0x28(%r14)
andl $0x0, 0x2c(%r14)
movl $0x1, 0x50(%r14)
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x1880
movq %rbx, %rdi
callq 0x10b0
leaq 0x1(%rax), %rdi
callq 0x1160
movq %rax, 0x30(%r14)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1040
imulq $0x18, 0x8(%rsp), %rax
leaq 0x4ee3(%rip), %rcx      # 0x6770
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rcx)
andq $0x0, 0x10(%rax,%rcx)
movl 0x4(%rsp), %eax
jmp 0x18c6
leaq 0x2086(%rip), %rdi      # 0x392d
callq 0x1070
jmp 0x18c3
leaq 0x1d8d(%rip), %rdi      # 0x3642
jmp 0x18be
leaq 0x1d8b(%rip), %rdi      # 0x3649
callq 0x11c0
pushq $-0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq