// File: /workspace/github_c_makefile_star2_O1/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 0x1780
leaq 0x4a29(%rip), %rax      # 0x6190
xorl %ebx, %ebx
cmpl $0x0, (%rax)
je 0x1780
incq %rbx
addq $0x58, %rax
cmpq $0xf, %rbx
jne 0x1769
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $-0x1, %ebx
je 0x18d0
orl $0x1b6, %r15d           # imm = 0x1B6
movl $0x2000, %esi           # imm = 0x2000
movl %r12d, %edi
movl %r15d, %edx
callq 0x11e0
testl %eax, %eax
js 0x18de
movl %ebp, 0x4(%rsp)
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11d0
cmpq $-0x1, %rax
je 0x18e7
movq %rax, %r13
movq %r14, 0x10(%rsp)
movl $0x2000, %edx           # imm = 0x2000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10e0
movslq %ebx, %rcx
movq %rcx, 0x8(%rsp)
imulq $0x58, %rcx, %rbp
leaq 0x4955(%rip), %r14      # 0x6140
movq %r13, (%rbp,%r14)
movl $0x10001000, 0xa(%r13)  # imm = 0x10001000
addq $0x1000, %r13           # imm = 0x1000
movq %r13, 0x8(%rbp,%r14)
movl %r12d, 0x10(%rbp,%r14)
movl $0x0, 0x14(%rbp,%r14)
movl $0x2000, %esi           # imm = 0x2000
movl 0x4(%rsp), %r12d
movl %r12d, %edi
movl %r15d, %edx
callq 0x11e0
testl %eax, %eax
js 0x18de
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11d0
cmpq $-0x1, %rax
je 0x18e7
movq %rax, %r15
addq %rbp, %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 %r12d, 0x28(%r14)
movl $0x0, 0x2c(%r14)
movl $0x1, 0x50(%r14)
movq 0x10(%rsp), %r15
testq %r15, %r15
je 0x18ae
movq %r15, %rdi
callq 0x10b0
leaq 0x1(%rax), %rdi
callq 0x1160
movq %rax, 0x30(%r14)
movq %rax, %rdi
movq %r15, %rsi
callq 0x1040
movq 0x8(%rsp), %rax
leaq (%rax,%rax,2), %rax
leaq 0x4eb2(%rip), %rcx      # 0x6770
xorps %xmm0, %xmm0
movups %xmm0, (%rcx,%rax,8)
movq $0x0, 0x10(%rcx,%rax,8)
jmp 0x18f8
leaq 0x2056(%rip), %rdi      # 0x392d
callq 0x1070
jmp 0x18f3
leaq 0x1d5d(%rip), %rdi      # 0x3642
jmp 0x18ee
leaq 0x1d5b(%rip), %rdi      # 0x3649
callq 0x11b0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq