// File: /workspace/github_cpp_makefile_star2_O2/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 $0x8a8, %rsp            # imm = 0x8A8
movq (%rdi), %rax
movq 0x8(%rdi), %rsi
xorps %xmm0, %xmm0
leaq 0xa0(%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), %rbx
movq %rax, %rdi
movq %rbx, %rcx
callq 0x22c0
testl %eax, %eax
jne 0x4ac5
leaq 0xa36(%rip), %r12       # 0x51ea
movq 0x480d(%rip), %r14      # 0x8fc8
leaq 0xa36(%rip), %r15       # 0x51f8
leaq 0xa3a(%rip), %r13       # 0x5203
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x4aaa
movl 0x4(%rbx), %edi
movl 0x8(%rbx), %esi
movl 0xc(%rbx), %edx
callq 0x2080
movq %r12, %rsi
cmpl $-0x1, %eax
je 0x4829
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movl %eax, %ebp
movl %eax, %edi
callq 0x21f0
cmpl $-0x1, %eax
jne 0x4837
movl %ebp, %edi
callq 0x2440
movq %r14, %rdi
movq %r15, %rsi
callq 0x2230
movq 0x18(%rbx), %rsi
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x2100
movq %r13, %rsi
movq %r14, %rdi
callq 0x2230
addq $0x28, %rbx
jmp 0x47c9
movl %ebp, 0x4(%rsp)
movq 0x30(%rsp), %rdi
callq 0x22f0
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
leaq 0x50(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq 0xd0(%rsp), %rbp
leaq 0x80(%rsp), %r13
leaq 0x8(%rsp), %r12
leaq 0x4a05(%rip), %r14      # 0x9288
movl $0x3e7, %edx            # imm = 0x3E7
movl 0x4(%rsp), %edi
leaq 0x4c0(%rsp), %rsi
xorl %ecx, %ecx
callq 0x20a0
cmpl $0x2, %eax
jl 0x49d6
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rsp,%rax), %r15
addq $0x4c0, %r15            # imm = 0x4C0
movb $0x0, (%r15)
leaq 0x4c0(%rsp), %rbx
movq %rbx, %rdi
pushq $0xa
popq %rsi
callq 0x20c0
movq %r15, 0x20(%rsp)
testq %rax, %rax
je 0x49a5
movq %rax, 0x28(%rsp)
movq %rbp, %r15
movq %rax, %rbp
subq %rbx, %rbp
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x2120
movb $0x0, (%r15,%rbp)
leaq 0xd0(%rsp), %rsi
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x2430
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2300
movq %r12, %rdi
movq %r13, %rsi
callq 0x3c08
movq %r13, %rdi
callq 0x20f0
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0x4a8b
movq %r14, %rdi
callq 0x2340
leaq 0x4964(%rip), %rdi      # 0x92b0
movq %r12, %rsi
callq 0x4c8a
movq %r14, %rdi
callq 0x21b0
leaq 0x48f5(%rip), %rdi      # 0x9258
callq 0x2030
movq 0x28(%rsp), %rbp
leaq 0x1(%rbp), %rbx
movq 0x20(%rsp), %r15
cmpq %r15, %rbx
je 0x4989
movq %rbx, %rdi
pushq $0xa
popq %rsi
callq 0x20c0
movq %rax, %rbp
movq %r12, %rdi
callq 0x2b4c
movq %rbp, %rax
leaq 0xd0(%rsp), %rbp
cmpq %r15, %rbx
jne 0x48d0
cmpq %r15, %rbx
je 0x4883
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2220
leaq 0xd0(%rsp), %rdi
callq 0x20e0
leaq (%rsp,%rax), %rbp
addq $0xd0, %rbp
jmp 0x4883
testl %eax, %eax
je 0x49f7
cmpl $-0x1, %eax
jne 0x4a5a
movq 0x45e2(%rip), %rdi      # 0x8fc8
leaq 0x865(%rip), %rsi       # 0x5252
callq 0x2230
jmp 0x4aa6
leaq 0x488a(%rip), %rdi      # 0x9288
callq 0x2340
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
leaq 0x38(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x8(%rsp), %rdi
pushq $0x1
popq %rdx
leaq 0x3(%rsp), %rcx
callq 0x4ca2
leaq 0x4882(%rip), %rdi      # 0x92b0
leaq 0x8(%rsp), %rsi
callq 0x4c96
leaq 0x8(%rsp), %rdi
callq 0x2b4c
leaq 0x483f(%rip), %rdi      # 0x9288
callq 0x21b0
leaq 0x4803(%rip), %rdi      # 0x9258
callq 0x2030
movl 0x4(%rsp), %edi
callq 0x2440
leaq 0x40(%rsp), %rdi
callq 0x20f0
leaq 0x60(%rsp), %rdi
callq 0x20f0
xorl %eax, %eax
addq $0x8a8, %rsp            # imm = 0x8A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4536(%rip), %rdi      # 0x8fc8
leaq 0x794(%rip), %rsi       # 0x522d
callq 0x2230
movq %rax, %rdi
callq 0x2100
pushq $0x1
jmp 0x4abf
movq 0x4517(%rip), %rdi      # 0x8fc8
leaq 0x762(%rip), %rsi       # 0x521a
callq 0x2230
pushq $0x2
popq %rdi
callq 0x22a0
movl %eax, %ebp
movq 0x44fa(%rip), %rdi      # 0x8fc8
leaq 0x6ff(%rip), %rsi       # 0x51d4
callq 0x2230
movq %rax, %rbx
movl %ebp, %edi
callq 0x2310
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2230
movq %rax, %rdi
callq 0x2100
jmp 0x4aa6
jmp 0x4b17
jmp 0x4b26
jmp 0x4b26
jmp 0x4b26
jmp 0x4b17
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x20f0
jmp 0x4b29
jmp 0x4b26
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x2b4c
jmp 0x4b29
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x20f0
leaq 0x60(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x23f0