// File: /workspace/github_cpp_makefile_star2_O2/yahoo[P]UDPing/src/server.cc
// Function: main
int main(int argc, char** argv) {
    if (!getuid()) {
        // I'm running as root.  Switch to nobody or die!!!
        struct passwd* pwd = getpwnam("nobody");
        if (pwd && pwd->pw_uid) {
            if (setgid(pwd->pw_gid)) {
                cerr << "Couldn't change userid to nobody (setgid failed).  Terminating server" << endl;
            }
            if (setuid(pwd->pw_uid)) {
                cerr << "Couldn't change userid to nobody (setuid failed).  Terminating server" << endl;
            }
        } else {
            cerr << "Couldn't change userid to nobody.  Terminating server" << endl;
            exit(-1);
        }
    }
    char buf[MAXBUF];
    setOptions(new Options(argc, argv, "p:k:l:m:r:s:t:vq"));

    stringstream usage;
    usage << "Usage: udping_server -l <listen hostname> -p <port number> -k <keepalive interval seconds> [-r <receive hostname>]" << endl <<
             "           [-s <statsd host:port>] [-t <tag based statsd host:port>] [-m <tag based statds metric name>] [-v] [-q]" << endl <<
             "-l: address to bind udp listener to" << endl <<
             "-p: udp port to bind listener to" << endl <<
             "-k: seconds to wait for delayed pings to show up before publishing stats" << endl <<
             "-r: address to report stats as being received by, if different from -l" << endl <<
             "-s: statsd host:port (default port 8125)" << endl <<
             "-t: tag based statsd host:port (default port 8125)" << endl <<
             "-m: tag based statsd metric name (default udping)" << endl <<
             "-v: verbose" << endl <<
             "-q: quiet" << endl;
    string listen_host = getOptions()->getStringOption('l', 1, usage.str(), "Hostname not provided\n");
    int listen_port = getOptions()->parseIntOption('p', 1, 1024, 65536, usage.str(), "Port number is out of range 1024-65532\n");
    int keepalive = getOptions()->parseIntOption('k', 1, 1, 60, usage.str(), "Keepalive interval is out of range 1-60s\n");

    char* rh = getOptions()->getStringOption('r', 0, usage.str(), "");
    string receive_host = rh == NULL ? listen_host : rh;

    /* Establish Stats Writer(s) */
    char* statsdInfo = getOptions()->getStringOption('s', 0, usage.str(), "");
    char* statsdTagInfo = getOptions()->getStringOption('t', 0, usage.str(), "");
    char* statsdTagMetric = getOptions()->getStringOption('m', 0, usage.str(), "");
    int quiet = getOptions()->getQuiet();
    StatsWriter* writer = new StatsWriter(listen_host, receive_host, listen_port, statsdInfo, statsdTagInfo, statsdTagMetric, quiet);

    /* Start Session Manager */
    ServerSessionManager sessionManager(writer, keepalive);

    /* Periodic check to see if packets are received */
    setIntervalTimer(keepalive, handleAlarm);

    int fd = makeSocket (listen_host, listen_port);
    if (fd < 0) {
        err (1, "Failed to create socket");
    }
    while (1) {
        sessionManager.readNextPacket (fd);
    }
    return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4c8, %rsp            # imm = 0x4C8
movq %rsi, %rbx
movl %edi, %ebp
callq 0x2180
testl %eax, %eax
jne 0x26ac
leaq 0x39de(%rip), %rdi      # 0x6004
callq 0x2250
testq %rax, %rax
je 0x2639
movq %rax, %r14
cmpl $0x0, 0x10(%rax)
jne 0x265c
movq 0x79b0(%rip), %rdi      # 0x9ff0
leaq 0x3a50(%rip), %rsi      # 0x6097
callq 0x22f0
movq %rax, %rdi
callq 0x2170
pushq $-0x1
popq %rdi
callq 0x2340
movl 0x14(%r14), %edi
callq 0x2480
testl %eax, %eax
je 0x2684
movq 0x7980(%rip), %rdi      # 0x9ff0
leaq 0x3994(%rip), %rsi      # 0x600b
callq 0x22f0
movq %rax, %rdi
callq 0x2170
movl 0x10(%r14), %edi
callq 0x2490
testl %eax, %eax
je 0x26ac
movq 0x7958(%rip), %rdi      # 0x9ff0
leaq 0x39b2(%rip), %rsi      # 0x6051
callq 0x22f0
movq %rax, %rdi
callq 0x2170
movl $0x800, %edi            # imm = 0x800
callq 0x2300
movq %rax, %r14
leaq 0x3a0d(%rip), %rsi      # 0x60cd
leaq 0x238(%rsp), %rdi
leaq 0x340(%rsp), %rdx
callq 0x2120
movb $0x1, %r15b
leaq 0x238(%rsp), %rcx
movq %r14, %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x3058
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x31b0
leaq 0x238(%rsp), %rdi
callq 0x2160
leaq 0x340(%rsp), %rdi
callq 0x21f0
leaq 0x350(%rsp), %rdi
leaq 0x39bd(%rip), %rsi      # 0x60de
callq 0x22f0
movq %rax, %rdi
callq 0x2170
leaq 0x3a1c(%rip), %rsi      # 0x6151
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
callq 0x2170
leaq 0x3a78(%rip), %rsi      # 0x61c4
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
callq 0x2170
leaq 0x3a85(%rip), %rsi      # 0x61e8
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
callq 0x2170
leaq 0x3a8f(%rip), %rsi      # 0x6209
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
callq 0x2170
leaq 0x3ac1(%rip), %rsi      # 0x6252
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
callq 0x2170
leaq 0x3af1(%rip), %rsi      # 0x6299
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
callq 0x2170
leaq 0x3b03(%rip), %rsi      # 0x62c2
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
callq 0x2170
leaq 0x3b1f(%rip), %rsi      # 0x62f5
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
callq 0x2170
leaq 0x3b3a(%rip), %rsi      # 0x6327
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
callq 0x2170
leaq 0x3b2f(%rip), %rsi      # 0x6333
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
callq 0x2170
callq 0x31b8
movq %rax, %rbx
leaq 0x358(%rsp), %r14
leaq 0x218(%rsp), %rdi
movq %r14, %rsi
callq 0x2410
leaq 0x3b02(%rip), %rsi      # 0x633d
leaq 0x1f8(%rsp), %rdi
leaq 0x298(%rsp), %rdx
callq 0x2120
pushq $0x6c
popq %rsi
pushq $0x1
popq %rdx
leaq 0x218(%rsp), %rcx
leaq 0x1f8(%rsp), %r8
movq %rbx, %rdi
callq 0x314c
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rdx
movq %rax, %rsi
callq 0x2120
leaq 0x1f8(%rsp), %rdi
callq 0x2160
leaq 0x218(%rsp), %rdi
callq 0x2160
callq 0x31b8
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
movq %r14, %rsi
callq 0x2410
leaq 0x3a9b(%rip), %rsi      # 0x6354
leaq 0x278(%rsp), %r15
leaq 0x298(%rsp), %rdx
movq %r15, %rdi
callq 0x2120
movq %r15, (%rsp)
pushq $0x70
popq %rsi
pushq $0x1
popq %rdx
leaq 0x1d8(%rsp), %r9
movq %rbx, %rdi
movl $0x400, %ecx            # imm = 0x400
movl $0x10000, %r8d          # imm = 0x10000
callq 0x30ea
movl %eax, %ebx
leaq 0x278(%rsp), %rdi
callq 0x2160
leaq 0x1d8(%rsp), %rdi
callq 0x2160
callq 0x31b8
movq %rax, %r15
leaq 0x1b8(%rsp), %rdi
movq %r14, %rsi
callq 0x2410
leaq 0x3a4b(%rip), %rsi      # 0x637c
leaq 0x258(%rsp), %r12
leaq 0x298(%rsp), %rdx
movq %r12, %rdi
callq 0x2120
movq %r12, (%rsp)
pushq $0x6b
popq %rsi
pushq $0x1
popq %rcx
pushq $0x3c
popq %r8
leaq 0x1b8(%rsp), %r9
movq %r15, %rdi
movl %ecx, %edx
callq 0x30ea
movl %eax, %ebp
leaq 0x258(%rsp), %rdi
callq 0x2160
leaq 0x1b8(%rsp), %rdi
callq 0x2160
callq 0x31b8
movq %rax, %r15
leaq 0x198(%rsp), %rdi
movq %r14, %rsi
callq 0x2410
leaq 0x39d7(%rip), %rsi      # 0x637b
leaq 0x178(%rsp), %rdi
leaq 0x298(%rsp), %rdx
callq 0x2120
pushq $0x72
popq %rsi
leaq 0x198(%rsp), %rcx
leaq 0x178(%rsp), %r8
movq %r15, %rdi
xorl %edx, %edx
callq 0x314c
movq %rax, %r15
leaq 0x178(%rsp), %rdi
callq 0x2160
leaq 0x198(%rsp), %rdi
callq 0x2160
testq %r15, %r15
jne 0x2a09
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x2390
jmp 0x2a1e
leaq 0x18(%rsp), %rdi
leaq 0x298(%rsp), %rdx
movq %r15, %rsi
callq 0x2120
callq 0x31b8
movq %rax, %r15
leaq 0x158(%rsp), %rdi
movq %r14, %rsi
callq 0x2410
leaq 0x393e(%rip), %rsi      # 0x637b
leaq 0x138(%rsp), %rdi
leaq 0x298(%rsp), %rdx
callq 0x2120
pushq $0x73
popq %rsi
leaq 0x158(%rsp), %rcx
leaq 0x138(%rsp), %r8
movq %r15, %rdi
xorl %edx, %edx
callq 0x314c
movq %rax, %r15
leaq 0x138(%rsp), %rdi
callq 0x2160
leaq 0x158(%rsp), %rdi
callq 0x2160
callq 0x31b8
movq %rax, %r12
leaq 0x118(%rsp), %rdi
movq %r14, %rsi
callq 0x2410
leaq 0x38d0(%rip), %rsi      # 0x637b
leaq 0xf8(%rsp), %rdi
leaq 0x298(%rsp), %rdx
callq 0x2120
pushq $0x74
popq %rsi
leaq 0x118(%rsp), %rcx
leaq 0xf8(%rsp), %r8
movq %r12, %rdi
xorl %edx, %edx
callq 0x314c
movq %rax, %r12
leaq 0xf8(%rsp), %rdi
callq 0x2160
leaq 0x118(%rsp), %rdi
callq 0x2160
callq 0x31b8
movq %rax, %r13
leaq 0xd8(%rsp), %rdi
movq %r14, %rsi
callq 0x2410
leaq 0x3862(%rip), %rsi      # 0x637b
leaq 0xb8(%rsp), %rdi
leaq 0x298(%rsp), %rdx
callq 0x2120
pushq $0x6d
popq %rsi
leaq 0xd8(%rsp), %rcx
leaq 0xb8(%rsp), %r8
movq %r13, %rdi
xorl %edx, %edx
callq 0x314c
movq %rax, 0x10(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x2160
leaq 0xd8(%rsp), %rdi
callq 0x2160
callq 0x31b8
movq %rax, %rdi
callq 0x3194
movl %eax, %r13d
pushq $0x30
popq %rdi
callq 0x2300
movq %rax, %r14
leaq 0x98(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x2390
leaq 0x78(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x2390
movl %r13d, 0x8(%rsp)
movq 0x10(%rsp), %rax
movq %rax, (%rsp)
leaq 0x98(%rsp), %rsi
leaq 0x78(%rsp), %rdx
movq %r14, %rdi
movl %ebx, %ecx
movq %r15, %r8
movq %r12, %r9
callq 0x4bbc
leaq 0x78(%rsp), %rdi
callq 0x2160
leaq 0x98(%rsp), %rdi
callq 0x2160
leaq 0x298(%rsp), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x3e18
leaq 0x12f7(%rip), %rsi      # 0x3ef8
movl %ebp, %edi
callq 0x300d
leaq 0x58(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x2390
leaq 0x58(%rsp), %rdi
movl %ebx, %esi
callq 0x31fd
movl %eax, %ebx
leaq 0x58(%rsp), %rdi
callq 0x2160
testl %ebx, %ebx
jns 0x2c44
leaq 0x376c(%rip), %rsi      # 0x63a6
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x22a0
leaq 0x298(%rsp), %r14
movq %r14, %rdi
movl %ebx, %esi
callq 0x3c20
jmp 0x2c4c
jmp 0x2c6b
jmp 0x2c6b
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x2160
jmp 0x2c6e
movq %rax, %rbx
leaq 0x298(%rsp), %rdi
callq 0x2e00
jmp 0x2d24
jmp 0x2d21
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x2160
jmp 0x2c97
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x2160
jmp 0x2ca9
movq %rax, %rbx
movq %r14, %rdi
callq 0x22e0
jmp 0x2d24
jmp 0x2d21
jmp 0x2d21
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x2160
jmp 0x2ccc
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
jmp 0x2d16
jmp 0x2d21
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x2160
jmp 0x2ced
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
jmp 0x2d16
jmp 0x2d21
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
callq 0x2160
jmp 0x2d0e
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x2160
jmp 0x2d24
jmp 0x2d91
jmp 0x2d91
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
jmp 0x2d8a
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
callq 0x2160
jmp 0x2d40
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
jmp 0x2d8a
jmp 0x2d91
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
callq 0x2160
jmp 0x2d61
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
jmp 0x2d8a
jmp 0x2d91
movq %rax, %rbx
leaq 0x278(%rsp), %rdi
callq 0x2160
jmp 0x2d82
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
callq 0x2160
jmp 0x2d94
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0x2dba
jmp 0x2d9d
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
callq 0x2160
jmp 0x2db2
movq %rax, %rbx
leaq 0x218(%rsp), %rdi
callq 0x2160
jmp 0x2dc6
jmp 0x2dc3
movq %rax, %rbx
leaq 0x340(%rsp), %rdi
callq 0x2210
jmp 0x2df7
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
callq 0x2160
testb %r15b, %r15b
jne 0x2def
jmp 0x2df7
movq %rax, %rbx
movq %r14, %rdi
callq 0x22e0
movq %rbx, %rdi
callq 0x2470
nop