// File: /workspace/github_cpp_makefile_star2_O3/ChristalC[P]Online-Outlier-Detection/thread_org.cpp
// Function: socket_thread_func(void*)
void* socket_thread_func(void* argv) {
  Socket_arg* sa = (Socket_arg*)argv;
  const char* host = sa->h;
  const char* port = sa->p;

  // Get ready for connecting to server
  int sockfd, numbytes;
  struct addrinfo hints, *servinfo, *p;
  int rv;

  hints = {};
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;

  if ((rv = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
    cout << "Error: getaddrinfor: " << gai_strerror(rv) << endl;
    exit(1);
  }

  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      cout << "socket error\n";
      continue;
    }

    if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sockfd);
      cout << "ai_addr = " << p->ai_addr << endl;
      cout << "client: connect error\n";
      continue;
    }

    break;
  }

  if (p == NULL) {
    cout << "Failed to connect\n";
    exit(2);
  }

  freeaddrinfo(servinfo);

  char recv_buffer[MAX_BUF_LENGTH];
  char buffer[MAX_BUF_LENGTH];
  string recv_str;
  string data_str;
  int buffer_start = 0;
  char* recv_start_pos = recv_buffer;
  char* recv_newline_pos;
  char* recv_end_pos;
  char* buffer_start_pos = buffer;
  char* buffer_end_pos;
  while ((numbytes = recv(sockfd, recv_buffer, MAX_BUF_LENGTH - 1, 0)) > 1) {
    recv_end_pos = recv_buffer + numbytes;
    *recv_end_pos = '\0';

    // Find '\n' in recv_buffer
    recv_start_pos = recv_buffer;
    recv_newline_pos = strchr(recv_buffer, '\n');
    while (recv_newline_pos != NULL) {
      strncpy(buffer_start_pos, recv_start_pos, recv_newline_pos - recv_start_pos);
      buffer_end_pos = buffer_start_pos + (recv_newline_pos - recv_start_pos);
      *buffer_end_pos = '\0';
      data_str = buffer;
      
      // Split data_str into a vector of doubles, push the vector to shared_data list
      vector<double> read_point = splitCsv(data_str);
      if (read_point.size() == 0) {
        cout << "read invalid point! End the program!" << endl;
        exit(1);
      }
      pthread_mutex_lock(&lock);
      shared_data.push_back(read_point);
      pthread_mutex_unlock(&lock);

      // Signal the has_new condition
      pthread_cond_signal(&has_new);

      buffer_start_pos = buffer;
      recv_start_pos = recv_newline_pos + 1;

      // If the previous found recv_newline_pos is already the last char of recv_buffer
      // Jump out of the loop
      if (recv_start_pos == recv_end_pos) {
        break;
      }
      recv_newline_pos = strchr(recv_start_pos, '\n');
    }
    if (recv_start_pos == recv_end_pos) {
      continue;
    }
    strcpy(buffer_start_pos, recv_start_pos);
    buffer_start_pos = strchr(buffer, '\0');
  }
  if (numbytes == -1) {
    cout << "recv error\n";
    exit(1);
  }
  if (numbytes == 0) {
    pthread_mutex_lock(&lock);
    shared_data.push_back({{-1}});
    pthread_mutex_unlock(&lock);
    pthread_cond_signal(&has_new);
  }

  close(sockfd);
  return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8b8, %rsp            # imm = 0x8B8
movq (%rdi), %rax
movq 0x8(%rdi), %rsi
xorps %xmm0, %xmm0
leaq 0xb0(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, 0x4(%rdx)
leaq 0x30(%rsp), %rcx
movq %rax, %rdi
callq 0x3300
testl %eax, %eax
jne 0x5e9b
movq 0x30(%rsp), %rbp
testq %rbp, %rbp
je 0x5b13
movq 0x4577(%rip), %rbx      # 0x9fd0
leaq 0x17ba(%rip), %r14      # 0x721a
leaq 0x17c1(%rip), %r15      # 0x7228
leaq 0x17c5(%rip), %r12      # 0x7233
movl 0x4(%rbp), %edi
movl 0x8(%rbp), %esi
movl 0xc(%rbp), %edx
callq 0x3080
cmpl $-0x1, %eax
je 0x5af6
movl %eax, %r13d
movq 0x18(%rbp), %rsi
movl 0x10(%rbp), %edx
movl %eax, %edi
callq 0x3210
cmpl $-0x1, %eax
jne 0x5b30
movl %r13d, %edi
callq 0x34b0
movl $0xa, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x32b0
movq 0x18(%rbp), %rsi
movq %rbx, %rdi
callq 0x31c0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3250
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
movl $0x16, %edx
movq %rbx, %rdi
movq %r12, %rsi
jmp 0x5b01
movl $0xd, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x32b0
movq 0x28(%rbp), %rbp
testq %rbp, %rbp
jne 0x5a6e
movq 0x44b6(%rip), %rdi      # 0x9fd0
leaq 0x1729(%rip), %rsi      # 0x724a
callq 0x3260
movl $0x2, %edi
callq 0x32e0
movl %r13d, 0x8(%rsp)
movq 0x30(%rsp), %rdi
callq 0x3320
leaq 0x98(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x48(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0xe0(%rsp), %r14
leaq 0x78(%rsp), %r12
leaq 0x68(%rsp), %r13
leaq 0x10(%rsp), %rbx
movl $0x3e7, %edx            # imm = 0x3E7
movl 0x8(%rsp), %edi
leaq 0x4d0(%rsp), %rsi
xorl %ecx, %ecx
callq 0x30a0
cmpl $0x2, %eax
jl 0x5d3d
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rsp,%rax), %r15
addq $0x4d0, %r15            # imm = 0x4D0
movb $0x0, (%r15)
leaq 0x4d0(%rsp), %rbp
movq %rbp, %rdi
movl $0xa, %esi
callq 0x30d0
movq %r15, 0x58(%rsp)
testq %rax, %rax
je 0x5d0c
movq %rbx, %r15
movq %r13, %rbx
movq %r12, %r13
movq %rax, 0x60(%rsp)
movq %rax, %r12
subq %rbp, %r12
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x3110
movb $0x0, (%r14,%r12)
movq 0x40(%rsp), %r14
leaq 0xe0(%rsp), %r12
movq %r12, %rdi
callq 0x30f0
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x33c0
movq %r13, %r12
movq %r13, 0x68(%rsp)
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
addq %rsi, %rdx
movq %rbx, %r13
movq %rbx, %rdi
callq 0x5584
movq %r15, %rbx
movq %r15, %rdi
movq %r13, %rsi
callq 0x4c83
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x5c67
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
leaq 0x4640(%rip), %r14      # 0xa2b8
leaq 0x4661(%rip), %r15      # 0xa2e0
je 0x5e6d
movq %r14, %rdi
callq 0x3370
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6192
movq %rax, %rdi
movq %r15, %rsi
callq 0x3060
incq 0x4646(%rip)            # 0xa2f0
movq %r14, %rdi
callq 0x31b0
leaq 0x45cf(%rip), %rdi      # 0xa288
callq 0x3030
movq 0x60(%rsp), %rax
leaq 0x1(%rax), %rbp
movq 0x58(%rsp), %r15
cmpq %r15, %rbp
je 0x5cde
movq %rbp, %rdi
movl $0xa, %esi
callq 0x30d0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5cfb
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
movq %rax, %r14
callq 0x3280
movq %r14, %rax
leaq 0xe0(%rsp), %r14
cmpq %r15, %rbp
jne 0x5bc8
cmpq %r15, %rbp
je 0x5b79
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3240
leaq 0xe0(%rsp), %rdi
callq 0x30f0
leaq (%rsp,%rax), %r14
addq $0xe0, %r14
jmp 0x5b79
testl %eax, %eax
je 0x5d66
cmpl $-0x1, %eax
movl 0x8(%rsp), %ebx
jne 0x5e11
movq 0x427b(%rip), %rdi      # 0x9fd0
leaq 0x1526(%rip), %rsi      # 0x7282
callq 0x3260
jmp 0x5e91
leaq 0x454b(%rip), %rdi      # 0xa2b8
callq 0x3370
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
leaq 0xa8(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rcx
movl $0x1, %edx
callq 0x6108
movl 0x8(%rsp), %ebx
movl $0x28, %edi
callq 0x3270
movaps 0x10(%rsp), %xmm0
movups %xmm0, 0x10(%rax)
movq 0x20(%rsp), %rcx
movq %rcx, 0x20(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x450d(%rip), %rsi      # 0xa2e0
movq %rax, %rdi
callq 0x3060
incq 0x450e(%rip)            # 0xa2f0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5df9
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x44b8(%rip), %rdi      # 0xa2b8
callq 0x31b0
leaq 0x447c(%rip), %rdi      # 0xa288
callq 0x3030
movl %ebx, %edi
callq 0x34b0
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x5e34
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x5e59
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3280
xorl %eax, %eax
addq $0x8b8, %rsp            # imm = 0x8B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x415c(%rip), %rdi      # 0x9fd0
leaq 0x13e2(%rip), %rsi      # 0x725d
movl $0x24, %edx
callq 0x32b0
movq 0x4144(%rip), %rdi      # 0x9fd0
callq 0x3100
movl $0x1, %edi
callq 0x32e0
movl %eax, %ebx
movq 0x412c(%rip), %rdi      # 0x9fd0
leaq 0x1359(%rip), %rsi      # 0x7204
callq 0x3260
movq %rax, %r14
movl %ebx, %edi
callq 0x3330
movq %r14, %rdi
movq %rax, %rsi
callq 0x3260
movq %rax, %rdi
callq 0x3100
jmp 0x5e91
jmp 0x5ef2
jmp 0x5f0e
jmp 0x5f0e
jmp 0x5f0e
jmp 0x5ef2
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x5f11
movq 0x78(%rsp), %rsi
incq %rsi
jmp 0x5f07
jmp 0x5f0e
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5f11
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
jmp 0x5f11
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x5f2d
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x5f52
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x3460