// File: Thomas1995[P]Rentz/src/./server.h
// Function: Server::Server()
Server() {
    //returns server's fd

    struct addrinfo hints, *rez;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    getaddrinfo(NULL, PORT, &hints, &rez);

    int yes = 1;

    int server = -1;

    for(addrinfo *p = rez; p != NULL; p = p->ai_next) {
      server = socket(p->ai_family, p->ai_socktype, p->ai_protocol);

      if(server == -1) {
        perror("Socket() failed: ");
        continue;
      }

      if(setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
        perror("Setsockopt() failed: ");
        exit(1);
      }

      if(bind(server, p->ai_addr, p->ai_addrlen) == -1) {
        perror("Bind() failed: ");
        exit(1);
      }

      if(listen(server, BACKLOG) == -1) {
        perror("Listen() failed: ");
        exit(1);
      }

      break;
    }

    freeaddrinfo(rez);

    if(server == -1) {
      printf("Could not set up server\n");
      exit(1);
    }

    fd = server;

    epfd = epoll_create1(0);

    if(epfd == -1) {
      perror("Epoll_create() failed: ");
      exit(1);
    }

    addEpollEvent(server, EPOLLIN);

    printf("Server up and listening for connections\n");
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x3190
movl $0x0, -0x34(%rbp)
movl $0x1, -0x30(%rbp)
movl $0x1, -0x38(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0xfac4(%rip), %rsi      # 0x161e8
leaq -0x38(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0x33d0
movl $0x1, -0x44(%rbp)
movl $0xffffffff, -0x48(%rbp) # imm = 0xFFFFFFFF
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x682a
movq -0x50(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x50(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x50(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x30a0
movl %eax, -0x48(%rbp)
cmpl $-0x1, -0x48(%rbp)
jne 0x6786
leaq 0xf981(%rip), %rdi      # 0x160fd
callq 0x3280
jmp 0x6819
movl -0x48(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x44(%rbp), %rcx
movl $0x4, %r8d
callq 0x34f0
cmpl $-0x1, %eax
jne 0x67bd
leaq 0xf961(%rip), %rdi      # 0x1610f
callq 0x3280
movl $0x1, %edi
callq 0x33a0
movl -0x48(%rbp), %edi
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x50(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x3080
cmpl $-0x1, %eax
jne 0x67ef
leaq 0xf945(%rip), %rdi      # 0x16125
callq 0x3280
movl $0x1, %edi
callq 0x33a0
movl -0x48(%rbp), %edi
movl $0x10, %esi
callq 0x3360
cmpl $-0x1, %eax
jne 0x6817
leaq 0xf92d(%rip), %rdi      # 0x16135
callq 0x3280
movl $0x1, %edi
callq 0x33a0
jmp 0x682a
movq -0x50(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x6747
movq -0x40(%rbp), %rdi
callq 0x3410
cmpl $-0x1, -0x48(%rbp)
jne 0x6851
leaq 0xf907(%rip), %rdi      # 0x16147
movb $0x0, %al
callq 0x3060
movl $0x1, %edi
callq 0x33a0
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
movl %ecx, (%rax)
xorl %edi, %edi
callq 0x3420
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x4(%rax)
cmpl $-0x1, 0x4(%rax)
jne 0x6886
leaq 0xf8e9(%rip), %rdi      # 0x16160
callq 0x3280
movl $0x1, %edi
callq 0x33a0
movq -0x58(%rbp), %rdi
movl -0x48(%rbp), %esi
movl $0x1, %edx
callq 0x6ce0
leaq 0xf8da(%rip), %rdi      # 0x16178
movb $0x0, %al
callq 0x3060
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)