// File: /workspace/github_c_makefile_star2_O1/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
movl $0x32, %edi
callq 0x1160
movq %rax, %r12
movl $0x32, %edi
callq 0x1160
movq %rax, %rbp
movl $0x32, %edi
callq 0x1160
movq %rax, %r15
movl $0x32, %edi
callq 0x1160
movq %rax, %rbx
movl $0x32, %edi
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 0x20e2
testl %eax, %eax
js 0x1558
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x20e2
testl %eax, %eax
js 0x1558
movq %rbp, 0x20(%rsp)
movq %r13, 0x10(%rsp)
leaq 0x1d1b(%rip), %rdi      # 0x310e
callq 0x1070
movl $0x3e8, 0xc(%rsp)       # imm = 0x3E8
leaq 0x1c16(%rip), %rbp      # 0x301d
leaq 0x1c12(%rip), %rbx      # 0x3020
leaq 0x1c2d(%rip), %r12      # 0x3042
leaq 0x1c29(%rip), %r13      # 0x3045
leaq 0x1be8(%rip), %rdi      # 0x300b
xorl %eax, %eax
callq 0x10c0
movq %rbp, %rdi
leaq 0x4(%rsp), %rsi
xorl %eax, %eax
callq 0x1200
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10c0
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1200
movq %r14, %rdi
callq 0x10b0
movq %rax, %r15
movq %r13, %rdi
movq %r14, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x10c0
cmpl $0x1, 0x4(%rsp)
jne 0x1482
movslq %r15d, %rdx
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x236d
cmpl $0x2, 0x4(%rsp)
jne 0x141c
movslq %r15d, %rdx
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x236d
movl 0x4(%rsp), %edi
callq 0x2858
leaq 0x1bba(%rip), %rdi      # 0x3063
movl %eax, %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x1bbf(%rip), %rdi      # 0x3078
xorl %eax, %eax
callq 0x10c0
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x1200
movl 0x8(%rsp), %r15d
cmpl $-0x1, %r15d
je 0x156b
movl 0x4(%rsp), %edi
callq 0x2858
cmpl %eax, %r15d
jg 0x141c
movl 0xc(%rsp), %r15d
cmpl %r15d, 0x8(%rsp)
jl 0x1527
movq 0x10(%rsp), %rdi
callq 0x1030
movslq %r15d, %rdi
addq %rdi, %rdi
callq 0x1160
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x15f4
addl %r15d, %r15d
movl %r15d, 0xc(%rsp)
movl 0x4(%rsp), %edi
movl 0x8(%rsp), %edx
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x2725
movl 0x8(%rsp), %edx
leaq 0x1b78(%rip), %rdi      # 0x30bf
movl %edx, %esi
movq %r15, %rcx
xorl %eax, %eax
callq 0x10c0
jmp 0x141c
leaq 0x1bed(%rip), %rdi      # 0x314c
callq 0x1070
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x15e3
leaq 0x1bbf(%rip), %rdi      # 0x3131
callq 0x1070
movl $0x1, %edi
callq 0x2858
leaq 0x1b4e(%rip), %rdi      # 0x30d6
xorl %ebx, %ebx
movl %eax, %esi
xorl %eax, %eax
callq 0x10c0
movl $0x1, %edi
callq 0x28a2
leaq 0x1b47(%rip), %rdi      # 0x30eb
movl %eax, %esi
xorl %eax, %eax
callq 0x10c0
movl $0x1, %edi
callq 0x28d9
leaq 0x1b3d(%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 0x1aa2(%rip), %rdi      # 0x309d
leaq 0x1aa9(%rip), %rsi      # 0x30ab
leaq 0x1aab(%rip), %rcx      # 0x30b4
movl $0x43, %edx
callq 0x10d0
nop