// File: /workspace/github_cpp_makefile_star2_O3/prakhar2808[P]DBEngine/serverCode/server.cc
// Function: main
int main(int argc, char** argv) {

  int server_socket, client_socket, addr_size;
  SA_IN server_addr, client_addr;

  for(int i = 0; i < THREAD_POOL_SIZE; i++) {
    pthread_create(&thread_pool[i], NULL, thread_function, NULL);
  }

  check((server_socket = socket(AF_INET, SOCK_STREAM, 0)), 
        "Failed to create socket");
  
  // Initializing address struct
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = INADDR_ANY;
  server_addr.sin_port = htons(SERVERPORT);

  check(bind(server_socket, (SA*)&server_addr, sizeof(server_addr)),
        "Bind failed!");
  check(listen(server_socket, SERVER_BACKLOG),
        "Listen failed!");

  // Create database object reference
  dbObjectRef = new database();

  while(true) {

    printf("Waiting for connections...\n");
    addr_size = sizeof(SA_IN);

    check(client_socket = 
        accept(server_socket, (SA*)&client_addr, (socklen_t*)&addr_size),
        "accept failed");
    printf("Connected!\n");
    
    // Put the connection somewhere so that an available thread can find it.
    int* pclient = (int*)malloc(sizeof(int));
    *pclient = client_socket;
    pthread_mutex_lock(&mutex);
    conn_queue.push(pclient);
    // Signal that a job has arrived in the queue.
    pthread_cond_signal(&condition_var);
    pthread_mutex_unlock(&mutex);
  }
  delete dbObjectRef;
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
xorl %r14d, %r14d
leaq 0xc4fc(%rip), %r15      # 0x26820
leaq 0x1af(%rip), %rbx       # 0x1a4da
leaq (%r15,%r14), %rdi
xorl %esi, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x56b0
addq $0x8, %r14
cmpq $0x50, %r14
jne 0x1a32b
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x50c0
cmpl $-0x1, %eax
jne 0x1a367
leaq 0x2266(%rip), %rdi      # 0x1c5c8
jmp 0x1a4b3
movl %eax, %ebx
leaq 0x28(%rsp), %rsi
movq $0x39300002, (%rsi)     # imm = 0x39300002
movl %eax, %edi
movl $0x10, %edx
callq 0x5090
cmpl $-0x1, %eax
jne 0x1a392
leaq 0x2253(%rip), %rdi      # 0x1c5e0
jmp 0x1a4b3
movl %ebx, %edi
movl $0x64, %esi
callq 0x53f0
cmpl $-0x1, %eax
jne 0x1a3af
leaq 0x2243(%rip), %rdi      # 0x1c5ed
jmp 0x1a4b3
movl $0x10, %edi
callq 0x5380
movq %rax, %r14
movq %rax, %rdi
callq 0x6e3c
movq %r14, 0xc54d(%rip)      # 0x26918
leaq 0x23b1(%rip), %rdi      # 0x1c783
callq 0x55a0
leaq 0xc(%rsp), %rdx
movl $0x10, (%rdx)
leaq 0x18(%rsp), %rsi
movl %ebx, %edi
callq 0x5120
cmpl $-0x1, %eax
je 0x1a4ac
movl %eax, %ebp
leaq 0xc4c0(%rip), %r15      # 0x268c0
leaq 0x237c(%rip), %r12      # 0x1c783
leaq 0x18(%rsp), %r13
leaq 0xc(%rsp), %r14
leaq 0x2386(%rip), %rdi      # 0x1c79e
callq 0x55a0
movl $0x4, %edi
callq 0x54d0
movq %rax, 0x10(%rsp)
movl %ebp, (%rax)
movq %r15, %rdi
callq 0x5550
movq 0xc463(%rip), %rax      # 0x268a0
movq 0xc46c(%rip), %rcx      # 0x268b0
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x1a45f
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0xc443(%rip)      # 0x268a0
jmp 0x1a470
leaq 0xc40a(%rip), %rdi      # 0x26870
leaq 0x10(%rsp), %rsi
callq 0x1ad26
leaq 0xc471(%rip), %rdi      # 0x268e8
callq 0x5030
movq %r15, %rdi
callq 0x52c0
movq %r12, %rdi
callq 0x55a0
movl $0x10, 0xc(%rsp)
movl %ebx, %edi
movq %r13, %rsi
movq %r14, %rdx
callq 0x5120
movl %eax, %ebp
cmpl $-0x1, %eax
jne 0x1a411
leaq 0x2149(%rip), %rdi      # 0x1c5fc
callq 0x52f0
movl $0x1, %edi
callq 0x5470
movq %rax, %rbx
movl $0x10, %esi
movq %r14, %rdi
callq 0x5390
movq %rbx, %rdi
callq 0x5670