// File: /workspace/github_cpp_makefile_star2_O1/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 0xc504(%rip), %r15      # 0x26820
leaq 0x1af(%rip), %rbx       # 0x1a4d2
leaq (%r15,%r14), %rdi
xorl %esi, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x56b0
addq $0x8, %r14
cmpq $0x50, %r14
jne 0x1a323
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x50c0
cmpl $-0x1, %eax
jne 0x1a35f
leaq 0x226e(%rip), %rdi      # 0x1c5c8
jmp 0x1a4ab
movl %eax, %ebx
leaq 0x28(%rsp), %rsi
movq $0x39300002, (%rsi)     # imm = 0x39300002
movl %eax, %edi
movl $0x10, %edx
callq 0x5090
cmpl $-0x1, %eax
jne 0x1a38a
leaq 0x225b(%rip), %rdi      # 0x1c5e0
jmp 0x1a4ab
movl %ebx, %edi
movl $0x64, %esi
callq 0x53f0
cmpl $-0x1, %eax
jne 0x1a3a7
leaq 0x224b(%rip), %rdi      # 0x1c5ed
jmp 0x1a4ab
movl $0x10, %edi
callq 0x5380
movq %rax, %r14
movq %rax, %rdi
callq 0x6df0
movq %r14, 0xc555(%rip)      # 0x26918
leaq 0x23b9(%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 0x1a4a4
movl %eax, %ebp
leaq 0xc4c8(%rip), %r15      # 0x268c0
leaq 0x2384(%rip), %r12      # 0x1c783
leaq 0x18(%rsp), %r13
leaq 0xc(%rsp), %r14
leaq 0x238e(%rip), %rdi      # 0x1c79e
callq 0x55a0
movl $0x4, %edi
callq 0x54d0
movq %rax, 0x10(%rsp)
movl %ebp, (%rax)
movq %r15, %rdi
callq 0x5550
movq 0xc46b(%rip), %rax      # 0x268a0
movq 0xc474(%rip), %rcx      # 0x268b0
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x1a457
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0xc44b(%rip)      # 0x268a0
jmp 0x1a468
leaq 0xc412(%rip), %rdi      # 0x26870
leaq 0x10(%rsp), %rsi
callq 0x1ad52
leaq 0xc479(%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 0x1a409
leaq 0x2151(%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