// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
pushq $0x32
popq %r14
movq %r14, %rdi
callq 0x1160
movq %rax, %r12
movq %r14, %rdi
callq 0x1160
movq %rax, %rbp
movq %r14, %rdi
callq 0x1160
movq %rax, %r15
movq %r14, %rdi
callq 0x1160
movq %rax, %rbx
movq %r14, %rdi
callq 0x1160
movq %rax, %r14
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x1160
movq %rax, %r13
movl $0x495547, (%r12)       # imm = 0x495547
movl $0x32495547, (%rbp)     # imm = 0x32495547
movb $0x0, 0x4(%rbp)
movl $0x746e65, 0x3(%r15)    # imm = 0x746E65
movl $0x65696c43, (%r15)     # imm = 0x65696C43
movabsq $0x32746e65696c43, %rax # imm = 0x32746E65696C43
movq %rax, (%rbx)
movq %r15, %rdi
movq %r12, 0x18(%rsp)
movq %r12, %rsi
callq 0x2077
testl %eax, %eax
js 0x1568
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2077
testl %eax, %eax
js 0x1568
movq %rbp, 0x20(%rsp)
movq %r13, 0x10(%rsp)
leaq 0x1d11(%rip), %rdi      # 0x310e
callq 0x1070
movl $0x3e8, 0xc(%rsp)       # imm = 0x3E8
leaq 0x1c0c(%rip), %rbp      # 0x301d
leaq 0x1c08(%rip), %rbx      # 0x3020
leaq 0x1c23(%rip), %r12      # 0x3042
leaq 0x1c1f(%rip), %r13      # 0x3045
leaq 0x1bde(%rip), %rdi      # 0x300b
xorl %eax, %eax
callq 0x10c0
movq %rbp, %rdi
leaq 0x4(%rsp), %rsi
xorl %eax, %eax
callq 0x1210
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10c0
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1210
movq %r14, %rdi
callq 0x10b0
movq %rax, %r15
movq %r13, %rdi
movq %r14, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x10c0
movl 0x4(%rsp), %eax
cmpl $0x1, %eax
jne 0x1492
movslq %r15d, %rdx
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x22fa
movl 0x4(%rsp), %eax
cmpl $0x2, %eax
jne 0x1426
movslq %r15d, %rdx
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x22fa
movl 0x4(%rsp), %edi
callq 0x27d4
leaq 0x1bac(%rip), %rdi      # 0x3063
movl %eax, %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x1bb1(%rip), %rdi      # 0x3078
xorl %eax, %eax
callq 0x10c0
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x1210
movl 0x8(%rsp), %r15d
cmpl $-0x1, %r15d
je 0x1579
movl 0x4(%rsp), %edi
callq 0x27d4
cmpl %eax, %r15d
jg 0x1426
movl 0x8(%rsp), %edx
movl 0xc(%rsp), %r15d
cmpl %r15d, %edx
jl 0x153b
movq 0x10(%rsp), %rdi
callq 0x1030
movslq %r15d, %rdi
addq %rdi, %rdi
callq 0x1160
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x15fc
addl %r15d, %r15d
movl %r15d, 0xc(%rsp)
movl 0x8(%rsp), %edx
movl 0x4(%rsp), %edi
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x26ad
movl 0x8(%rsp), %edx
leaq 0x1b68(%rip), %rdi      # 0x30bf
movl %edx, %esi
movq %r15, %rcx
xorl %eax, %eax
callq 0x10c0
jmp 0x1426
leaq 0x1bdd(%rip), %rdi      # 0x314c
callq 0x1070
pushq $-0x1
popq %rbx
jmp 0x15eb
leaq 0x1bb1(%rip), %rdi      # 0x3131
callq 0x1070
pushq $0x1
popq %rbp
movl %ebp, %edi
callq 0x27d4
leaq 0x1b40(%rip), %rdi      # 0x30d6
xorl %ebx, %ebx
movl %eax, %esi
xorl %eax, %eax
callq 0x10c0
movl %ebp, %edi
callq 0x281e
leaq 0x1b3c(%rip), %rdi      # 0x30eb
movl %eax, %esi
xorl %eax, %eax
callq 0x10c0
movl %ebp, %edi
callq 0x2855
leaq 0x1b35(%rip), %rdi      # 0x30fb
movl %eax, %esi
xorl %eax, %eax
callq 0x10c0
movq 0x10(%rsp), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
callq 0x1030
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a9a(%rip), %rdi      # 0x309d
leaq 0x1aa1(%rip), %rsi      # 0x30ab
leaq 0x1aa3(%rip), %rcx      # 0x30b4
pushq $0x43
popq %rdx
callq 0x10d0
nopl (%rax)