// File: /workspace/github_cpp_makefile_star2_O2/mortrevere[P]ninjat/main.cpp
// Function: main
int main() {
  int error = init("0.0.2"); //will be used to handle all networking errors
  int sock;
  int port = PORT;
  struct sockaddr_in sin;
  sin.sin_addr.s_addr = INADDR_ANY;
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  std::string tmp, key, value;
  tmp = std::to_string(port);

  int csock;
  struct sockaddr_in csin;
  socklen_t tempo = sizeof(csin);

  std::vector<std::string> lines;
  std::ofstream fout;

  std::vector<std::thread> threads;
  std::vector<int> clients;
  std::vector<std::string> nicks;

  if(error != 0)
    die("Couldn't initialize network.");

  sock = socket(AF_INET, SOCK_STREAM, 0);
  if(sock == INVALID_SOCKET)
    die("Couldn't create socket.");
  else
    info("Main socket created.");

  //allows binding on FIN_WAIT1 hanging ports
  int reuse = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)) < 0)
    info("setsockopt(SO_REUSEADDR) failed");

  while(bind(sock, (sockaddr *)&sin, sizeof(sin)) != 0) {
    err("Can't listen to port " + std::to_string(port) + " ... check if the port is busy.");
    port++;
    info("Retrying on port " + std::to_string(port) + " ...");
    sin.sin_port = htons(port);
  }
  info("bind() successful on port " + std::to_string(port));

  listen(sock, 99);
  info("Listening on port " + std::to_string(port));

  while(1) {
    info("Waiting for peers ...");
    csock = accept(sock,(struct sockaddr*)&csin,&tempo);
    clients.push_back(csock);
    nicks.push_back("");
    threads.push_back(std::thread(clientCare,csock,std::ref(clients),std::ref(nicks)));
    sAll("Someone (#" + std::to_string(csock) + ") joined !\n", clients);

    s("/nick               : get a name and talk to people.\n", csock);
    s("/list               : list people on the chat\n", csock);
    s("/p <dest> <message> : pm someone based on nickname\n", csock);
    s("/p . <message>      : pm the last person you pm'd\n", csock);
    s("/quit               : well, you guessed it\n", csock);

    if(PRIVATE) {
      s("THIS SERVER IS PASSWORD-PROTECTED : use /auth <pw> to log in\n", csock);
    }
    info("NCC !");
  }

  for (auto& th : threads) th.join();
  info("End.");
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
leaq 0x18aa(%rip), %rsi      # 0x528c
leaq 0x208(%rsp), %rdi
leaq 0xf8(%rsp), %rdx
callq 0x40c8
leaq 0x208(%rsp), %rdi
callq 0x24fc
leaq 0x208(%rsp), %rdi
callq 0x2100
leaq 0x108(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x1e8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq $0x39050002, 0x90(%rsp) # imm = 0x39050002
movb %cl, (%rax)
leaq 0x1c8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x208(%rsp), %rdi
movl $0x539, %esi            # imm = 0x539
callq 0x436b
leaq 0xf8(%rsp), %rdi
leaq 0x208(%rsp), %rbx
movq %rbx, %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x2100
movl $0x10, 0x4c(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xe0(%rsp)
andq $0x0, 0xf0(%rsp)
leaq 0x208(%rsp), %rdi
callq 0x2330
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
andq $0x0, 0xd0(%rsp)
movaps %xmm0, 0x50(%rsp)
andq $0x0, 0x60(%rsp)
movaps %xmm0, 0xa0(%rsp)
andq $0x0, 0xb0(%rsp)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x2050
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
jne 0x3b19
leaq 0x1792(%rip), %rsi      # 0x5292
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x40c8
leaq 0x8(%rsp), %rdi
callq 0x2545
leaq 0x178a(%rip), %rsi      # 0x52aa
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x40c8
leaq 0x8(%rsp), %rdi
callq 0x2577
leaq 0x8(%rsp), %rdi
callq 0x2100
leaq 0x6c(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x4
popq %r8
movl 0x4(%rsp), %edi
callq 0x2350
testl %eax, %eax
jns 0x3b8f
leaq 0x1753(%rip), %rsi      # 0x52bf
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x40c8
leaq 0x8(%rsp), %rdi
callq 0x2577
leaq 0x8(%rsp), %rdi
callq 0x2100
movl $0x53a, %r14d           # imm = 0x53A
leaq 0x70(%rsp), %r12
leaq 0x28(%rsp), %rbx
leaq 0x8(%rsp), %rbp
leaq 0x1508(%rip), %r15      # 0x50b3
leal -0x1(%r14), %r13d
movl 0x4(%rsp), %edi
leaq 0x90(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x2040
testl %eax, %eax
je 0x3c7c
movq %r12, %rdi
movl %r13d, %esi
callq 0x436b
movq %rbx, %rdi
leaq 0x16ff(%rip), %rsi      # 0x52df
movq %r12, %rdx
callq 0x4228
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0x1700(%rip), %rdx      # 0x52f5
callq 0x41c7
movq %rbp, %rdi
callq 0x25a2
movq %rbp, %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x2100
movq %r12, %rdi
callq 0x2100
movq %r12, %rdi
movl %r14d, %esi
callq 0x436b
movq %rbx, %rdi
leaq 0x16e6(%rip), %rsi      # 0x5315
movq %r12, %rdx
callq 0x4228
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x41c7
movq %rbp, %rdi
callq 0x2577
movq %rbp, %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x2100
movq %r12, %rdi
callq 0x2100
movl %r14d, %eax
rolw $0x8, %ax
movw %ax, 0x92(%rsp)
incl %r14d
jmp 0x3bab
leaq 0x28(%rsp), %rdi
movl %r13d, %esi
callq 0x436b
leaq 0x1697(%rip), %rsi      # 0x5327
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x4228
leaq 0x8(%rsp), %rdi
callq 0x2577
leaq 0x8(%rsp), %rdi
callq 0x2100
leaq 0x28(%rsp), %rdi
callq 0x2100
pushq $0x63
popq %rsi
movl 0x4(%rsp), %edi
callq 0x2230
leaq 0x28(%rsp), %rdi
movl %r13d, %esi
callq 0x436b
leaq 0x1665(%rip), %rsi      # 0x5342
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x4228
leaq 0x8(%rsp), %rdi
callq 0x2577
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x2100
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x2100
leaq 0x70(%rsp), %r13
pushq $0x1
popq %r12
leaq 0x118(%rsp), %rbp
movq %r14, %rdi
leaq 0x162a(%rip), %rsi      # 0x5355
movq %r15, %rdx
callq 0x40c8
movq %r14, %rdi
callq 0x2577
movq %r14, %rdi
callq 0x2100
movl 0x4(%rsp), %edi
leaq 0x1f8(%rsp), %rsi
leaq 0x4c(%rsp), %rdx
callq 0x20a0
movl %eax, (%rsp)
leaq 0x50(%rsp), %rdi
movq %rsp, %rsi
callq 0x43ee
movq %r14, %rdi
leaq 0x132f(%rip), %rsi      # 0x50a2
movq %r15, %rdx
callq 0x40c8
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4b3a
movq %r14, %rdi
callq 0x2100
leaq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rbx, 0x70(%rsp)
movq %r14, %rdi
leaq -0x11ab(%rip), %rsi     # 0x2c04
movq %rsp, %rdx
movq %r15, %rcx
movq %r13, %r8
callq 0x440c
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0x4c62
movq %r14, %rdi
callq 0x44a2
movl (%rsp), %esi
movq %r13, %rdi
callq 0x436b
movq %r15, %rdi
leaq 0x1581(%rip), %rsi      # 0x536b
movq %r13, %rdx
callq 0x4228
movq %r14, %rdi
movq %r15, %rsi
leaq 0x1577(%rip), %rdx      # 0x5376
callq 0x41c7
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
callq 0x286d
movq %r14, %rdi
callq 0x2100
movq %r15, %rdi
callq 0x2100
movq %r13, %rdi
callq 0x2100
leaq 0x198(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1547(%rip), %rsi      # 0x5382
movq %r14, %rdx
callq 0x40c8
movl (%rsp), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x280c
movq %rbx, %rdi
callq 0x2100
leaq 0x178(%rsp), %rbx
movq %rbx, %rdi
leaq 0x154d(%rip), %rsi      # 0x53b8
movq %r14, %rdx
callq 0x40c8
movl (%rsp), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x280c
movq %rbx, %rdi
callq 0x2100
leaq 0x158(%rsp), %rbx
movq %rbx, %rdi
leaq 0x154c(%rip), %rsi      # 0x53e7
movq %r14, %rdx
callq 0x40c8
movl (%rsp), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x280c
movq %rbx, %rdi
callq 0x2100
leaq 0x138(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1550(%rip), %rsi      # 0x541b
movq %r14, %rdx
callq 0x40c8
movl (%rsp), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x280c
movq %rbx, %rdi
callq 0x2100
movq %rbp, %rdi
leaq 0x155b(%rip), %rsi      # 0x544e
movq %r14, %rdx
callq 0x40c8
movl (%rsp), %esi
movq %rbp, %rdi
movl %r12d, %edx
callq 0x280c
movq %rbp, %rdi
callq 0x2100
movq %r14, %rdi
leaq 0x155f(%rip), %rsi      # 0x547a
movq %r15, %rdx
callq 0x40c8
movq %r14, %rdi
callq 0x2577
movq %r14, %rdi
callq 0x2100
jmp 0x3d21
jmp 0x402a
jmp 0x403b
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
jmp 0x4032
jmp 0x403b
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
jmp 0x4032
jmp 0x403b
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
jmp 0x4032
jmp 0x403b
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
jmp 0x4032
jmp 0x403b
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
jmp 0x4032
jmp 0x403b
jmp 0x3ffa
jmp 0x4009
jmp 0x4018
jmp 0x403b
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x44a2
jmp 0x403e
jmp 0x403b
jmp 0x402a
jmp 0x403b
jmp 0x402a
jmp 0x403b
jmp 0x403b
jmp 0x3fd7
jmp 0x3fe6
jmp 0x403b
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x2100
jmp 0x3fe9
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x4032
jmp 0x403b
jmp 0x3ffa
jmp 0x4009
jmp 0x4018
jmp 0x403b
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x2100
jmp 0x400c
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x2100
jmp 0x401b
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x4032
jmp 0x403b
jmp 0x402a
jmp 0x403b
jmp 0x402a
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x2100
jmp 0x403e
jmp 0x403b
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x4130
leaq 0x50(%rsp), %rdi
callq 0x49c4
leaq 0xc0(%rsp), %rdi
callq 0x44b0
leaq 0x208(%rsp), %rdi
callq 0x22b0
jmp 0x4074
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x4130
jmp 0x4086
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
callq 0x2100
leaq 0x1d8(%rsp), %rdi
callq 0x2100
leaq 0xf8(%rsp), %rdi
jmp 0x40b5
movq %rax, %rbx
leaq 0x208(%rsp), %rdi
callq 0x2100
jmp 0x40bf
movq %rax, %rbx
movq %rbx, %rdi
callq 0x2340
nop