// File: brianbbsu[P]TCP-Ping/src/server.cpp
// Function: setup(unsigned short)
int setup(uint16_t port){
    int fd, enable = 1;
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)quit("Failed to creat socket!");
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
    if(bind(fd, (sockaddr*)&addr, sizeof(addr)) < 0)quit("Failed to bind socket on port " + std::to_string(port) + "!");
    if(listen(fd, 10) < 0)quit("Failed to start listening for connection!");
    
    logger::log_info("Listing on port " + std::to_string(port) + " ...");
    
    server_socket_fd = fd;
    struct sigaction sa;
    sa.sa_handler = sigint_handler;
    sa.sa_flags = 0;
    sigemptyset(&sa.sa_mask);
    sigaction(SIGINT, &sa, NULL);

    return fd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movw %di, %ax
movw %ax, -0x2(%rbp)
movl $0x1, -0xc(%rbp)
movw $0x2, -0x1c(%rbp)
movzwl -0x2(%rbp), %edi
callq 0x6060
movw %ax, -0x1a(%rbp)
xorl %edi, %edi
callq 0x62a0
movl %eax, -0x18(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x60f0
movl %eax, -0x8(%rbp)
cmpl $0x0, %eax
jge 0x7058
leaq -0x41(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x6670
movq -0x1e0(%rbp), %rdx
leaq 0xe043(%rip), %rsi      # 0x15040
leaq -0x40(%rbp), %rdi
callq 0x7d50
jmp 0x7008
leaq -0x40(%rbp), %rdi
callq 0x14b00
jmp 0x7013
leaq -0x40(%rbp), %rdi
callq 0x6768
leaq -0x41(%rbp), %rdi
callq 0x63c0
jmp 0x7058
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x704a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x6768
leaq -0x41(%rbp), %rdi
callq 0x63c0
jmp 0x731e
movl -0x8(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0xc(%rbp), %rcx
movl $0x4, %r8d
callq 0x6680
movl -0x8(%rbp), %edi
leaq -0x1c(%rbp), %rsi
movl $0x10, %edx
callq 0x6090
cmpl $0x0, %eax
jge 0x7156
movzwl -0x2(%rbp), %esi
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x7ea0
movq -0x1e8(%rbp), %rdx
leaq 0xdfa5(%rip), %rsi      # 0x15058
leaq -0x98(%rbp), %rdi
callq 0x7e50
jmp 0x70c1
leaq 0xdfd7(%rip), %rdx      # 0x1509f
leaq -0x78(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x7e00
jmp 0x70da
leaq -0x78(%rbp), %rdi
callq 0x14b00
jmp 0x70e5
leaq -0x78(%rbp), %rdi
callq 0x6768
leaq -0x98(%rbp), %rdi
callq 0x6768
leaq -0xb8(%rbp), %rdi
callq 0x6768
jmp 0x7156
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x7145
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x7139
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x6768
leaq -0x98(%rbp), %rdi
callq 0x6768
leaq -0xb8(%rbp), %rdi
callq 0x6768
jmp 0x731e
movl -0x8(%rbp), %edi
movl $0xa, %esi
callq 0x6420
cmpl $0x0, %eax
jge 0x71fa
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x6670
movq -0x1f0(%rbp), %rdx
leaq 0xdeea(%rip), %rsi      # 0x15077
leaq -0xd8(%rbp), %rdi
callq 0x7d50
jmp 0x719b
leaq -0xd8(%rbp), %rdi
callq 0x14b00
jmp 0x71a9
leaq -0xd8(%rbp), %rdi
callq 0x6768
leaq -0xd9(%rbp), %rdi
callq 0x63c0
jmp 0x71fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x71e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x6768
leaq -0xd9(%rbp), %rdi
callq 0x63c0
jmp 0x731e
movzwl -0x2(%rbp), %esi
leaq -0x140(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x7ea0
movq -0x1f8(%rbp), %rdx
leaq 0xde82(%rip), %rsi      # 0x150a1
leaq -0x120(%rbp), %rdi
callq 0x7e50
jmp 0x722d
leaq 0xde07(%rip), %rdx      # 0x1503b
leaq -0x100(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x7e00
jmp 0x7249
leaq -0x100(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x14860
jmp 0x725c
leaq -0x100(%rbp), %rdi
callq 0x6768
leaq -0x120(%rbp), %rdi
callq 0x6768
leaq -0x140(%rbp), %rdi
callq 0x6768
movl -0x8(%rbp), %eax
movl %eax, 0x19237(%rip)     # 0x204c0
leaq -0x3b0(%rip), %rax      # 0x6ee0
movq %rax, -0x1d8(%rbp)
movl $0x0, -0x150(%rbp)
leaq -0x1d8(%rbp), %rdi
addq $0x8, %rdi
callq 0x6480
movl $0x2, %edi
leaq -0x1d8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x6490
movl -0x8(%rbp), %eax
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x7312
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x7306
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x6768
leaq -0x120(%rbp), %rdi
callq 0x6768
leaq -0x140(%rbp), %rdi
callq 0x6768
movq -0x50(%rbp), %rdi
callq 0x6650
nopw (%rax,%rax)