// File: /workspace/github_c_makefile_star2_O0/umbrant[P]Non-blocking-Buffers/client.c
// Function: main
int main() 
{
  char* service_name = (char*)malloc(sizeof(char)*50);
  char* service_name_other = (char*)malloc(sizeof(char)*50);

  char* client_name = (char*)malloc(sizeof(char)*50);
  char* client_name_other = (char*)malloc(sizeof(char)*50);

	char* msg = (char*)malloc(50*sizeof(char));
  int msg_len;
  int size;
  int option;
  char* array = (char*)malloc(sizeof(char) * MAX_MSG_LEN);
  int array_size = MAX_MSG_LEN;

  strcpy(service_name, GUI);
  strcpy(service_name_other, "GUI2");

  strcpy(client_name, "Client");
  strcpy(client_name_other, "Client2");

	if(nbb_connect_service(client_name, service_name) < 0) {
		printf("Error getting channel!\n");
		return -1;
	}

  if(nbb_connect_service(client_name_other, service_name_other) < 0) {
		printf("Error getting channel!\n");
		return -1;
	}

  printf("Available server: (1) GUI (2) GUI2\n");

  while(1) {
    printf("Choose a server: ");
    scanf("%d", &option);

    printf("Enter message to send to server: ");
    scanf("%s", msg);

    msg_len = strlen(msg);
    printf("User entered '%s' (%d bytes)\n", msg, msg_len);

    if(option == 1) nbb_send(service_name, msg, msg_len); 
    if(option == 2) nbb_send(service_name_other, msg, msg_len);
    else continue;

    printf("bytes available: %d\n", nbb_bytes_available(option));
    printf("How many bytes do you want to read: ");
    scanf("%d", &size);

    if(size == -1) {
      break;
    }

    if(size > nbb_bytes_available(option)) {
      continue;
    }

    if (size >= array_size) {
      // Grow the array size and ignore old contents (no need for realloc)
      free(array);
      array = (char *) malloc(sizeof(char) * 2 * array_size);
      assert(array != NULL);
      array_size = 2 * array_size;
    }

    nbb_read_bytes(option, array, size);

    // array might be not null-terminated
    printf("read %d bytes: '%.*s'\n", size, size, array);
  }

  printf("\n********Statistic********\n");
  printf("bytes_available: %d\n", nbb_bytes_available(1));
  printf("bytes_read: %d\n", nbb_bytes_read(1));
  printf("bytes_written: %d\n", nbb_bytes_written(1));

  free(array);
  free(msg);
  free(service_name);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl $0x0, -0x4(%rbp)
movl $0x32, %edi
callq 0x1160
movq %rax, -0x10(%rbp)
movl $0x32, %edi
callq 0x1160
movq %rax, -0x18(%rbp)
movl $0x32, %edi
callq 0x1160
movq %rax, -0x20(%rbp)
movl $0x32, %edi
callq 0x1160
movq %rax, -0x28(%rbp)
movl $0x32, %edi
callq 0x1160
movq %rax, -0x30(%rbp)
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x1160
movq %rax, -0x48(%rbp)
movl $0x3e8, -0x4c(%rbp)     # imm = 0x3E8
movq -0x10(%rbp), %rdi
leaq 0x2c5f(%rip), %rsi      # 0x4004
callq 0x1040
movq -0x18(%rbp), %rdi
leaq 0x2c53(%rip), %rsi      # 0x4008
callq 0x1040
movq -0x20(%rbp), %rdi
leaq 0x2c48(%rip), %rsi      # 0x400d
callq 0x1040
movq -0x28(%rbp), %rdi
leaq 0x2c3f(%rip), %rsi      # 0x4014
callq 0x1040
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x2760
cmpl $0x0, %eax
jge 0x1406
leaq 0x2c29(%rip), %rdi      # 0x401c
movb $0x0, %al
callq 0x10b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1629
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2760
cmpl $0x0, %eax
jge 0x1432
leaq 0x2bfd(%rip), %rdi      # 0x401c
movb $0x0, %al
callq 0x10b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1629
leaq 0x2bfb(%rip), %rdi      # 0x4034
movb $0x0, %al
callq 0x10b0
leaq 0x2c11(%rip), %rdi      # 0x4058
movb $0x0, %al
callq 0x10b0
leaq 0x2c15(%rip), %rdi      # 0x406a
leaq -0x3c(%rbp), %rsi
movb $0x0, %al
callq 0x1200
leaq 0x2c06(%rip), %rdi      # 0x406d
movb $0x0, %al
callq 0x10b0
movq -0x30(%rbp), %rsi
leaq 0x2c16(%rip), %rdi      # 0x408f
movb $0x0, %al
callq 0x1200
movq -0x30(%rbp), %rdi
callq 0x10a0
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rsi
movl -0x34(%rbp), %edx
leaq 0x2bf8(%rip), %rdi      # 0x4092
movb $0x0, %al
callq 0x10b0
cmpl $0x1, -0x3c(%rbp)
jne 0x14b8
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movslq -0x34(%rbp), %rdx
callq 0x2b00
cmpl $0x2, -0x3c(%rbp)
jne 0x14d1
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
movslq -0x34(%rbp), %rdx
callq 0x2b00
jmp 0x14d6
jmp 0x1440
movl -0x3c(%rbp), %edi
callq 0x3270
movl %eax, %esi
leaq 0x2bc9(%rip), %rdi      # 0x40b0
movb $0x0, %al
callq 0x10b0
leaq 0x2bd0(%rip), %rdi      # 0x40c5
movb $0x0, %al
callq 0x10b0
leaq 0x2b67(%rip), %rdi      # 0x406a
leaq -0x38(%rbp), %rsi
movb $0x0, %al
callq 0x1200
cmpl $-0x1, -0x38(%rbp)
jne 0x1519
jmp 0x15b2
movl -0x38(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x3c(%rbp), %edi
callq 0x3270
movl %eax, %ecx
movl -0x50(%rbp), %eax
cmpl %ecx, %eax
jle 0x1535
jmp 0x1440
movl -0x38(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jl 0x1586
movq -0x48(%rbp), %rdi
callq 0x1030
movslq -0x4c(%rbp), %rdi
shlq %rdi
callq 0x1160
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x155f
jmp 0x157e
leaq 0x2b84(%rip), %rdi      # 0x40ea
leaq 0x2b8b(%rip), %rsi      # 0x40f8
movl $0x43, %edx
leaq 0x2b88(%rip), %rcx      # 0x4101
callq 0x10c0
movl -0x4c(%rbp), %eax
shll %eax
movl %eax, -0x4c(%rbp)
movl -0x3c(%rbp), %edi
movq -0x48(%rbp), %rsi
movl -0x38(%rbp), %edx
callq 0x30d0
movl -0x38(%rbp), %esi
movl -0x38(%rbp), %edx
movq -0x48(%rbp), %rcx
leaq 0x2b66(%rip), %rdi      # 0x410c
movb $0x0, %al
callq 0x10b0
jmp 0x1440
leaq 0x2b6a(%rip), %rdi      # 0x4123
movb $0x0, %al
callq 0x10b0
movl $0x1, %edi
callq 0x3270
movl %eax, %esi
leaq 0x2b6c(%rip), %rdi      # 0x413f
movb $0x0, %al
callq 0x10b0
movl $0x1, %edi
callq 0x32e0
movl %eax, %esi
leaq 0x2b67(%rip), %rdi      # 0x4154
movb $0x0, %al
callq 0x10b0
movl $0x1, %edi
callq 0x3340
movl %eax, %esi
leaq 0x2b5d(%rip), %rdi      # 0x4164
movb $0x0, %al
callq 0x10b0
movq -0x48(%rbp), %rdi
callq 0x1030
movq -0x30(%rbp), %rdi
callq 0x1030
movq -0x10(%rbp), %rdi
callq 0x1030
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)