// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
cmpl $0xfa0, -0x14(%rbp)     # imm = 0xFA0
jne 0x17c0
cmpl $0xfa1, -0x18(%rbp)     # imm = 0xFA1
jne 0x17c0
movl $0x0, -0x2c(%rbp)
jmp 0x17c8
callq 0x3080
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
jne 0x17e8
leaq 0x2fba(%rip), %rdi      # 0x478f
movb $0x0, %al
callq 0x10b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1ac8
movl -0x14(%rbp), %edi
movl -0x1c(%rbp), %edx
orl $0x1b6, %edx            # imm = 0x1B6
movl $0x2000, %esi           # imm = 0x2000
callq 0x11e0
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
jge 0x181e
leaq 0x2fa6(%rip), %rdi      # 0x47b3
callq 0x11b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1ac8
movl -0x20(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x11d0
movq %rax, -0x28(%rbp)
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x1854
leaq 0x2f77(%rip), %rdi      # 0x47ba
callq 0x11b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1ac8
movq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x2000, %edx           # imm = 0x2000
callq 0x10d0
movq -0x28(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
leaq 0x58cd(%rip), %rax      # 0x7140
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movslq -0x2c(%rbp), %rcx
leaq 0x58b8(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movw $0x1000, 0xc(%rax)      # imm = 0x1000
movslq -0x2c(%rbp), %rcx
leaq 0x589d(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movw $0x1000, 0xa(%rax)      # imm = 0x1000
movq -0x28(%rbp), %rcx
addq $0x1000, %rcx           # imm = 0x1000
movslq -0x2c(%rbp), %rdx
leaq 0x5877(%rip), %rax      # 0x7140
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl -0x14(%rbp), %ecx
movslq -0x2c(%rbp), %rdx
leaq 0x585e(%rip), %rax      # 0x7140
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
movslq -0x2c(%rbp), %rcx
leaq 0x5849(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x14(%rax)
movl $0xffffffff, -0x20(%rbp) # imm = 0xFFFFFFFF
movq $-0x1, %rax
movq %rax, -0x28(%rbp)
movl -0x18(%rbp), %edi
movl -0x1c(%rbp), %edx
orl $0x1b6, %edx            # imm = 0x1B6
movl $0x2000, %esi           # imm = 0x2000
callq 0x11e0
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
jge 0x194d
leaq 0x2e77(%rip), %rdi      # 0x47b3
callq 0x11b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1ac8
movl -0x20(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x11d0
movq %rax, -0x28(%rbp)
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x1983
leaq 0x2e48(%rip), %rdi      # 0x47ba
callq 0x11b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1ac8
movq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x2000, %edx           # imm = 0x2000
callq 0x10d0
movq -0x28(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
leaq 0x579e(%rip), %rax      # 0x7140
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x18(%rax)
movslq -0x2c(%rbp), %rcx
leaq 0x5788(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movw $0x1000, 0xc(%rax)      # imm = 0x1000
movslq -0x2c(%rbp), %rcx
leaq 0x576c(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movw $0x1000, 0xa(%rax)      # imm = 0x1000
movq -0x28(%rbp), %rcx
addq $0x1000, %rcx           # imm = 0x1000
movslq -0x2c(%rbp), %rdx
leaq 0x5745(%rip), %rax      # 0x7140
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x20(%rax)
movl -0x18(%rbp), %ecx
movslq -0x2c(%rbp), %rdx
leaq 0x572c(%rip), %rax      # 0x7140
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x28(%rax)
movslq -0x2c(%rbp), %rcx
leaq 0x5717(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x2c(%rax)
movslq -0x2c(%rbp), %rcx
leaq 0x56fe(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl $0x1, 0x50(%rax)
cmpq $0x0, -0x10(%rbp)
je 0x1aa4
movq -0x10(%rbp), %rdi
callq 0x10a0
movq %rax, %rdi
addq $0x1, %rdi
callq 0x1160
movq %rax, %rcx
movslq -0x2c(%rbp), %rdx
leaq 0x56c6(%rip), %rax      # 0x7140
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x30(%rax)
movslq -0x2c(%rbp), %rcx
leaq 0x56b0(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x30(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x1040
movslq -0x2c(%rbp), %rax
leaq 0x5cc1(%rip), %rdi      # 0x7770
imulq $0x18, %rax, %rax
addq %rax, %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x10d0
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)