// File: brianbbsu[P]TCP-Ping/src/server.cpp
// Function: main
int main(int argc, char *argv[])
{
    uint16_t port;
    po::options_description desc, desc_all;
    desc.add_options()
    ("help,h", "Print help messages");
    desc_all.add(desc);
    desc_all.add_options()
    ("port", po::value<uint16_t>(&port)->required(), "Port to listen on");
    
    po::positional_options_description pos_desc;
    pos_desc.add("port", 1);

    po::variables_map vmap;
    try{
        po::store(po::command_line_parser(argc, argv).options(desc_all).positional(pos_desc).run(), vmap);
        if(vmap.count("help"))print_help(desc);
        po::notify(vmap);
    }catch(const std::exception &ex){
        logger::log_error("Failed to parse arguments: " + std::string(ex.what()) + "\n");
        print_help(desc);
    }
    
    logger::log_info("== TCP Ping - Server - Made by Po-Hsuan Su (b08902107) ==");
    int fd = setup(port);
    
    while(1)
    {
        sockaddr_in conn_addr;
        socklen_t conn_addr_len = sizeof(conn_addr);
        int conn_fd = accept(fd, (struct sockaddr*)&conn_addr, &conn_addr_len);
        if(conn_fd < 0)
        {
            logger::log_error("Failed to establish new connection!");
            continue;
        }
        char conn_ip[INET_ADDRSTRLEN + 2];
        memset(conn_ip, 0, sizeof(conn_ip));
        inet_ntop(AF_INET, &conn_addr.sin_addr, conn_ip, INET_ADDRSTRLEN);
        logger::log_info("recv from " + std::string(conn_ip) + ":" + std::to_string(ntohs(conn_addr.sin_port)));
        shutdown(conn_fd, SHUT_RDWR);
        close(conn_fd);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5f0, %rsp            # imm = 0x5F0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0x18bb0(%rip), %rax     # 0x1ff90
movq %rax, -0x570(%rbp)
movl (%rax), %esi
movl %esi, %edx
shrl %edx
leaq -0x98(%rbp), %rdi
callq 0x6740
movq -0x570(%rbp), %rax
movl (%rax), %esi
movl %esi, %edx
shrl %edx
leaq -0x118(%rbp), %rdi
callq 0x6740
jmp 0x7414
leaq -0x98(%rbp), %rdi
callq 0x6440
movq %rax, -0x578(%rbp)
jmp 0x7429
movq -0x578(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq 0xdd0f(%rip), %rsi      # 0x1514d
leaq 0xdd0f(%rip), %rdx      # 0x15154
leaq -0x130(%rbp), %rdi
callq 0x66c0
jmp 0x7453
leaq -0x118(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x6370
jmp 0x7468
leaq -0x118(%rbp), %rdi
callq 0x6440
movq %rax, -0x580(%rbp)
jmp 0x747d
movq -0x580(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq -0x12(%rbp), %rdi
callq 0x7fc0
movq %rax, -0x588(%rbp)
jmp 0x749d
movq -0x588(%rbp), %rdi
callq 0x8020
movq %rax, -0x590(%rbp)
jmp 0x74b2
movq -0x590(%rbp), %rdx
leaq 0xdca8(%rip), %rsi      # 0x15168
leaq 0xdca6(%rip), %rcx      # 0x1516d
leaq -0x138(%rbp), %rdi
callq 0x66f0
jmp 0x74d5
leaq -0x170(%rbp), %rdi
callq 0x64e0
jmp 0x74e3
leaq 0xdc7e(%rip), %rsi      # 0x15168
leaq -0x170(%rbp), %rdi
movl $0x1, %edx
callq 0x63d0
jmp 0x74fd
leaq -0x210(%rbp), %rdi
callq 0x6160
jmp 0x750b
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rdx
leaq -0x2b0(%rbp), %rdi
callq 0x8040
jmp 0x7520
leaq -0x2b0(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x8160
movq %rax, -0x598(%rbp)
jmp 0x753c
movq -0x598(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x81a0
movq %rax, -0x5a0(%rbp)
jmp 0x7558
movq -0x5a0(%rbp), %rsi
leaq -0x238(%rbp), %rdi
callq 0x81d0
jmp 0x756d
leaq -0x238(%rbp), %rdi
leaq -0x210(%rbp), %rsi
xorl %edx, %edx
callq 0x6640
jmp 0x7584
leaq -0x238(%rbp), %rdi
callq 0x8290
leaq -0x2b0(%rbp), %rdi
callq 0x82b0
leaq -0x200(%rbp), %rax
movq %rax, -0x5b0(%rbp)
leaq -0x2d1(%rbp), %rdi
movq %rdi, -0x5a8(%rbp)
callq 0x6670
movq -0x5a8(%rbp), %rdx
leaq 0xdbb4(%rip), %rsi      # 0x1517f
leaq -0x2d0(%rbp), %rdi
callq 0x7d50
jmp 0x75d9
movq -0x5b0(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
callq 0x82d0
movq %rax, -0x5b8(%rbp)
jmp 0x75f5
leaq -0x2d0(%rbp), %rdi
callq 0x6768
leaq -0x2d1(%rbp), %rdi
callq 0x63c0
movq -0x5b8(%rbp), %rax
cmpq $0x0, %rax
je 0x7987
leaq -0x358(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x8330
jmp 0x7633
leaq -0x358(%rbp), %rdi
callq 0x7330
jmp 0x7641
leaq -0x358(%rbp), %rdi
callq 0x8410
jmp 0x7987
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7d30
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7d24
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7d18
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x774c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x76e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x8290
leaq -0x2b0(%rbp), %rdi
callq 0x82b0
jmp 0x774c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7720
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x2d0(%rbp), %rdi
callq 0x6768
leaq -0x2d1(%rbp), %rdi
callq 0x63c0
jmp 0x774c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x358(%rbp), %rdi
callq 0x8410
movl -0x124(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x7d0c
movq -0x120(%rbp), %rdi
callq 0x6150
movq %rax, -0x360(%rbp)
movq -0x360(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, -0x5c8(%rbp)
leaq -0x3c1(%rbp), %rdi
movq %rdi, -0x5c0(%rbp)
callq 0x6670
movq -0x5c8(%rbp), %rsi
movq -0x5c0(%rbp), %rdx
leaq -0x3c0(%rbp), %rdi
callq 0x7d50
jmp 0x77b8
leaq 0xd9c5(%rip), %rsi      # 0x15184
leaq -0x3a0(%rbp), %rdi
leaq -0x3c0(%rbp), %rdx
callq 0x7e50
jmp 0x77d4
leaq 0xd92b(%rip), %rdx      # 0x15106
leaq -0x380(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
callq 0x7e00
jmp 0x77f0
leaq -0x380(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x149b0
jmp 0x7803
leaq -0x380(%rbp), %rdi
callq 0x6768
leaq -0x3a0(%rbp), %rdi
callq 0x6768
leaq -0x3c0(%rbp), %rdi
callq 0x6768
leaq -0x3c1(%rbp), %rdi
callq 0x63c0
leaq -0x448(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x8330
jmp 0x7848
leaq -0x448(%rbp), %rdi
callq 0x7330
jmp 0x7856
leaq -0x448(%rbp), %rdi
callq 0x8410
callq 0x65c0
jmp 0x7869
jmp 0x786b
leaq -0x469(%rbp), %rdi
movq %rdi, -0x5d0(%rbp)
callq 0x6670
movq -0x5d0(%rbp), %rdx
leaq 0xd914(%rip), %rsi      # 0x151a0
leaq -0x468(%rbp), %rdi
callq 0x7d50
jmp 0x789a
leaq -0x468(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x14860
jmp 0x78ad
leaq -0x468(%rbp), %rdi
callq 0x6768
leaq -0x469(%rbp), %rdi
callq 0x63c0
movzwl -0x12(%rbp), %edi
callq 0x6f90
movl %eax, -0x5d4(%rbp)
jmp 0x78d6
movl -0x5d4(%rbp), %eax
movl %eax, -0x470(%rbp)
jmp 0x78e4
movl $0x10, -0x484(%rbp)
movl -0x470(%rbp), %edi
leaq -0x480(%rbp), %rsi
leaq -0x484(%rbp), %rdx
callq 0x6120
movl %eax, -0x5d8(%rbp)
jmp 0x790f
movl -0x5d8(%rbp), %eax
movl %eax, -0x488(%rbp)
cmpl $0x0, -0x488(%rbp)
jge 0x7af7
leaq -0x4a9(%rbp), %rdi
movq %rdi, -0x5e0(%rbp)
callq 0x6670
movq -0x5e0(%rbp), %rdx
leaq 0xd891(%rip), %rsi      # 0x151da
leaq -0x4a8(%rbp), %rdi
callq 0x7d50
jmp 0x7957
leaq -0x4a8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x149b0
jmp 0x796a
leaq -0x4a8(%rbp), %rdi
callq 0x6768
leaq -0x4a9(%rbp), %rdi
callq 0x63c0
jmp 0x78e2
leaq -0x210(%rbp), %rdi
callq 0x61d0
jmp 0x7995
jmp 0x786b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7a0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7a00
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x79f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x380(%rbp), %rdi
callq 0x6768
leaq -0x3a0(%rbp), %rdi
callq 0x6768
leaq -0x3c0(%rbp), %rdi
callq 0x6768
leaq -0x3c1(%rbp), %rdi
callq 0x63c0
jmp 0x7a65
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7a65
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x8410
jmp 0x7a65
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7d0c
callq 0x65c0
jmp 0x7a6c
jmp 0x7d0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7aa3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x468(%rbp), %rdi
callq 0x6768
leaq -0x469(%rbp), %rdi
callq 0x63c0
jmp 0x7d0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7ae6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x4a8(%rbp), %rdi
callq 0x6768
leaq -0x4a9(%rbp), %rdi
callq 0x63c0
jmp 0x7d0c
xorps %xmm0, %xmm0
movaps %xmm0, -0x4c0(%rbp)
movw $0x0, -0x4b0(%rbp)
leaq -0x47c(%rbp), %rsi
movl $0x2, %edi
leaq -0x4c0(%rbp), %rdx
movq %rdx, -0x5f0(%rbp)
movl $0x10, %ecx
callq 0x63b0
leaq -0x541(%rbp), %rdi
movq %rdi, -0x5e8(%rbp)
callq 0x6670
movq -0x5f0(%rbp), %rsi
movq -0x5e8(%rbp), %rdx
leaq -0x540(%rbp), %rdi
callq 0x7d50
jmp 0x7b5d
leaq 0xd69a(%rip), %rsi      # 0x151fe
leaq -0x520(%rbp), %rdi
leaq -0x540(%rbp), %rdx
callq 0x7e50
jmp 0x7b79
leaq 0xd689(%rip), %rdx      # 0x15209
leaq -0x500(%rbp), %rdi
leaq -0x520(%rbp), %rsi
callq 0x7e00
jmp 0x7b95
movzwl -0x47e(%rbp), %edi
callq 0x66b0
movzwl %ax, %esi
leaq -0x568(%rbp), %rdi
callq 0x7ea0
jmp 0x7bb2
leaq -0x4e0(%rbp), %rdi
leaq -0x500(%rbp), %rsi
leaq -0x568(%rbp), %rdx
callq 0x8470
jmp 0x7bce
leaq -0x4e0(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x14860
jmp 0x7be1
leaq -0x4e0(%rbp), %rdi
callq 0x6768
leaq -0x568(%rbp), %rdi
callq 0x6768
leaq -0x500(%rbp), %rdi
callq 0x6768
leaq -0x520(%rbp), %rdi
callq 0x6768
leaq -0x540(%rbp), %rdi
callq 0x6768
leaq -0x541(%rbp), %rdi
callq 0x63c0
movl -0x488(%rbp), %edi
movl $0x2, %esi
callq 0x6500
movl -0x488(%rbp), %edi
callq 0x6710
jmp 0x7c46
jmp 0x78e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7d00
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7cf4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7ce8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7cdc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x7cd0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x4e0(%rbp), %rdi
callq 0x6768
leaq -0x568(%rbp), %rdi
callq 0x6768
leaq -0x500(%rbp), %rdi
callq 0x6768
leaq -0x520(%rbp), %rdi
callq 0x6768
leaq -0x540(%rbp), %rdi
callq 0x6768
leaq -0x541(%rbp), %rdi
callq 0x63c0
leaq -0x210(%rbp), %rdi
callq 0x8540
leaq -0x170(%rbp), %rdi
callq 0x85a0
leaq -0x118(%rbp), %rdi
callq 0x8410
leaq -0x98(%rbp), %rdi
callq 0x8410
movq -0x120(%rbp), %rdi
callq 0x6650
movq %rax, %rdi
callq 0x8460