// File: /workspace/github_cpp_makefile_star2_O1/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 0x5f1c
movq 0x30(%rsp), %rbp
testq %rbp, %rbp
sete %r13b
je 0x5baa
movq 0x44e7(%rip), %rbx      # 0x9fd0
leaq 0x1738(%rip), %r15      # 0x7228
leaq 0x173c(%rip), %r12      # 0x7233
movl 0x4(%rbp), %edi
movl 0x8(%rbp), %esi
movl 0xc(%rbp), %edx
callq 0x3080
cmpl $-0x1, %eax
movl %eax, 0xc(%rsp)
je 0x5b83
movq 0x18(%rbp), %rsi
movl 0x10(%rbp), %edx
movl %eax, %r14d
movl %eax, %edi
callq 0x3210
cmpl $-0x1, %eax
jne 0x5baa
movl %r14d, %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 0x5b92
movl $0xd, %edx
movq %rbx, %rdi
leaq 0x1688(%rip), %rsi      # 0x721a
callq 0x32b0
movq 0x28(%rbp), %rbp
testq %rbp, %rbp
sete %r13b
jne 0x5af7
jmp 0x5baa
testb $0x1, %r13b
jne 0x5f50
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), %r15
movl $0x3e7, %edx            # imm = 0x3E7
movl 0xc(%rsp), %edi
leaq 0x4d0(%rsp), %rsi
xorl %ecx, %ecx
callq 0x30a0
cmpl $0x2, %eax
jl 0x5dbb
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rsp,%rax), %rbx
addq $0x4d0, %rbx            # imm = 0x4D0
movb $0x0, (%rbx)
leaq 0x4d0(%rsp), %rbp
movq %rbp, %rdi
movl $0xa, %esi
callq 0x30d0
movq %rbx, 0x58(%rsp)
testq %rax, %rax
je 0x5d8a
movq %r15, %rbx
movq %r13, %r15
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 %r15, %r13
movq %r15, %rdi
callq 0x55de
movq %rbx, %r15
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4cc1
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x5ce5
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
leaq 0x45c2(%rip), %rbx      # 0xa2b8
leaq 0x45e3(%rip), %r14      # 0xa2e0
je 0x5eee
movq %rbx, %rdi
callq 0x3370
movq %r14, %rdi
movq %r15, %rsi
callq 0x6230
movq %rax, %rdi
movq %r14, %rsi
callq 0x3060
incq 0x45c8(%rip)            # 0xa2f0
movq %rbx, %rdi
callq 0x31b0
leaq 0x4551(%rip), %rdi      # 0xa288
callq 0x3030
movq 0x60(%rsp), %rax
leaq 0x1(%rax), %rbp
movq 0x58(%rsp), %rbx
cmpq %rbx, %rbp
je 0x5d5c
movq %rbp, %rdi
movl $0xa, %esi
callq 0x30d0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5d79
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
movq %rax, %r14
callq 0x3280
movq %r14, %rax
leaq 0xe0(%rsp), %r14
cmpq %rbx, %rbp
jne 0x5c46
cmpq %rbx, %rbp
je 0x5bf8
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3240
leaq 0xe0(%rsp), %rdi
callq 0x30f0
leaq (%rsp,%rax), %r14
addq $0xe0, %r14
jmp 0x5bf8
testl %eax, %eax
je 0x5de0
cmpl $-0x1, %eax
jne 0x5e90
movq 0x4201(%rip), %rdi      # 0x9fd0
leaq 0x14ac(%rip), %rsi      # 0x7282
callq 0x3260
jmp 0x5f12
leaq 0x44d1(%rip), %rdi      # 0xa2b8
callq 0x3370
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
leaq 0xa8(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x10(%rsp), %rdi
leaq 0xb(%rsp), %rcx
movl $0x1, %edx
callq 0x61a6
movl $0x28, %edi
callq 0x3270
movq 0x10(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x18(%rsp), %rcx
movq %rcx, 0x18(%rax)
movq 0x20(%rsp), %rcx
movq %rcx, 0x20(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x448e(%rip), %rsi      # 0xa2e0
movq %rax, %rdi
callq 0x3060
incq 0x448f(%rip)            # 0xa2f0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5e78
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x4439(%rip), %rdi      # 0xa2b8
callq 0x31b0
leaq 0x43fd(%rip), %rdi      # 0xa288
callq 0x3030
movl 0xc(%rsp), %edi
callq 0x34b0
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x5eb5
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x5eda
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 0x40db(%rip), %rdi      # 0x9fd0
leaq 0x1361(%rip), %rsi      # 0x725d
movl $0x24, %edx
callq 0x32b0
movq 0x40c3(%rip), %rdi      # 0x9fd0
callq 0x3100
movl $0x1, %edi
callq 0x32e0
movl %eax, %ebx
movq 0x40ab(%rip), %rdi      # 0x9fd0
leaq 0x12d8(%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 0x5f12
movq 0x4079(%rip), %rdi      # 0x9fd0
leaq 0x12ec(%rip), %rsi      # 0x724a
callq 0x3260
movl $0x2, %edi
callq 0x32e0
jmp 0x5f90
jmp 0x5fac
jmp 0x5fac
jmp 0x5fac
jmp 0x5f90
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x5faf
movq 0x78(%rsp), %rsi
incq %rsi
jmp 0x5fa5
jmp 0x5fac
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5faf
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
jmp 0x5faf
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x5fcb
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x5ff0
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x3460