// File: yahoo[P]UDPing/src/serversession.cc
// Function: ServerSessionManager::readNextPacket(int)
int ServerSessionManager::readNextPacket (int fd) {
    char buf[MAXBUF];
    struct sockaddr_in remote;
    struct timespec tv;
    socklen_t len = sizeof(remote);
    int n=recvfrom(fd,buf,MAXBUF,0,(struct sockaddr *)&remote,&len);
    packet* ph = (packet*) buf;
    if (n < 0) {
        warn("Error receiving data");
    } else if (n < sizeof(packet)) {
        warn("Malformed packet - size %d is less than header size %ld", n, sizeof(packet));
    } else if (n != ph->size) {
        warn("Malformed packet - expected %d, got %d", ph->size, n);
    } else if (ph->protoVersion != PROTOVERSION) {
        warn("Incorrect protocol version.  Expected %d, got %d", PROTOVERSION, ph->protoVersion);
    } else {
        clock_gettime(CLOCK_REALTIME, &tv);
        dumpBuffer(buf);
        string hostName = hostMap[remote.sin_addr.s_addr];
        if (hostName.empty()) {
            struct hostent* remoteName = gethostbyaddr(&(remote.sin_addr), sizeof(struct in_addr), AF_INET);
            if (remoteName) {
                hostName = hostMap[remote.sin_addr.s_addr] = remoteName->h_name;
            } else {
                hostName = hostMap[remote.sin_addr.s_addr] =  inet_ntoa(remote.sin_addr);
            }
        }
        receivePing (ph, &tv, hostName, ntohs(remote.sin_port));
    }
    return n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x8d0, %rsp            # imm = 0x8D0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x890(%rbp)
movl $0x10, -0x804(%rbp)
movl -0xc(%rbp), %edi
leaq -0x7e0(%rbp), %rsi
movl $0x7d0, %edx            # imm = 0x7D0
xorl %ecx, %ecx
leaq -0x7f0(%rbp), %r8
leaq -0x804(%rbp), %r9
callq 0x2230
movl %eax, -0x808(%rbp)
leaq -0x7e0(%rbp), %rax
movq %rax, -0x810(%rbp)
cmpl $0x0, -0x808(%rbp)
jge 0x574b
leaq 0x5d1b(%rip), %rdi      # 0xb45a
movb $0x0, %al
callq 0x2110
jmp 0x5a26
movslq -0x808(%rbp), %rax
cmpq $0x48, %rax
jae 0x5776
movl -0x808(%rbp), %esi
leaq 0x5d0a(%rip), %rdi      # 0xb46f
movl $0x48, %edx
movb $0x0, %al
callq 0x2110
jmp 0x5a24
movl -0x808(%rbp), %eax
movq -0x810(%rbp), %rcx
cmpl 0x40(%rcx), %eax
je 0x57ab
movq -0x810(%rbp), %rax
movl 0x40(%rax), %esi
movl -0x808(%rbp), %edx
leaq 0x5d08(%rip), %rdi      # 0xb4a7
movb $0x0, %al
callq 0x2110
jmp 0x5a22
movq -0x810(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
je 0x57dc
movq -0x810(%rbp), %rax
movzbl (%rax), %edx
leaq 0x5d03(%rip), %rdi      # 0xb4ce
movl $0x1, %esi
movb $0x0, %al
callq 0x2110
jmp 0x5a20
xorl %edi, %edi
leaq -0x800(%rbp), %rsi
callq 0x22c0
leaq -0x7e0(%rbp), %rdi
callq 0x4270
movq -0x890(%rbp), %rdi
addq $0x60, %rdi
movl -0x7ec(%rbp), %eax
movq %rax, -0x838(%rbp)
leaq -0x838(%rbp), %rsi
callq 0x5f70
movq %rax, %rsi
leaq -0x830(%rbp), %rdi
callq 0x2160
leaq -0x830(%rbp), %rdi
callq 0x2490
testb $0x1, %al
jne 0x583e
jmp 0x5984
leaq -0x7ec(%rbp), %rdi
movl $0x4, %esi
movl $0x2, %edx
callq 0x23d0
movq %rax, -0x898(%rbp)
jmp 0x585d
movq -0x898(%rbp), %rax
movq %rax, -0x840(%rbp)
cmpq $0x0, -0x840(%rbp)
je 0x5904
movq -0x890(%rbp), %rdi
movq -0x840(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x8a8(%rbp)
addq $0x60, %rdi
movl -0x7ec(%rbp), %eax
movq %rax, -0x858(%rbp)
leaq -0x858(%rbp), %rsi
callq 0x5f70
movq %rax, -0x8a0(%rbp)
jmp 0x58b7
movq -0x8a8(%rbp), %rsi
movq -0x8a0(%rbp), %rdi
callq 0x23c0
movq %rax, -0x8b0(%rbp)
jmp 0x58d3
movq -0x8b0(%rbp), %rsi
leaq -0x830(%rbp), %rdi
callq 0x2050
jmp 0x58e8
jmp 0x5982
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x848(%rbp)
movl %eax, -0x84c(%rbp)
jmp 0x5a12
movl -0x7ec(%rbp), %eax
movl %eax, -0x85c(%rbp)
movl -0x85c(%rbp), %edi
callq 0x2180
movq -0x890(%rbp), %rdi
movq %rax, -0x8c0(%rbp)
addq $0x60, %rdi
movl -0x7ec(%rbp), %eax
movq %rax, -0x868(%rbp)
leaq -0x868(%rbp), %rsi
callq 0x5f70
movq %rax, -0x8b8(%rbp)
jmp 0x594f
movq -0x8c0(%rbp), %rsi
movq -0x8b8(%rbp), %rdi
callq 0x23c0
movq %rax, -0x8c8(%rbp)
jmp 0x596b
movq -0x8c8(%rbp), %rsi
leaq -0x830(%rbp), %rdi
callq 0x2050
jmp 0x5980
jmp 0x5982
jmp 0x5984
movq -0x810(%rbp), %rax
movq %rax, -0x8d0(%rbp)
leaq -0x888(%rbp), %rdi
leaq -0x830(%rbp), %rsi
callq 0x2160
jmp 0x59a7
movzwl -0x7ee(%rbp), %edi
callq 0x24e0
movq -0x890(%rbp), %rdi
movq -0x8d0(%rbp), %rsi
movzwl %ax, %r8d
leaq -0x800(%rbp), %rdx
leaq -0x888(%rbp), %rcx
callq 0x5590
jmp 0x59da
leaq -0x888(%rbp), %rdi
callq 0x21e0
leaq -0x830(%rbp), %rdi
callq 0x21e0
jmp 0x5a20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x848(%rbp)
movl %eax, -0x84c(%rbp)
leaq -0x888(%rbp), %rdi
callq 0x21e0
leaq -0x830(%rbp), %rdi
callq 0x21e0
jmp 0x5a35
jmp 0x5a22
jmp 0x5a24
jmp 0x5a26
movl -0x808(%rbp), %eax
addq $0x8d0, %rsp            # imm = 0x8D0
popq %rbp
retq
movq -0x848(%rbp), %rdi
callq 0x24b0
nopw %cs:(%rax,%rax)