// File: /workspace/github_cpp_makefile_star2_O2/brunokeymolen[P]ssdp/src/ssdp.cpp
// Function: SSDP::ReadLoop()
int SSDP::ReadLoop(){
    int ret = 0;
    mReadLoop = 1;

    struct timeval timeout;
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;

    const int bufsize = 4096;
    u8 buf[bufsize];

    struct sockaddr_in sender;
    socklen_t senderlen = sizeof(struct sockaddr);

    int maxsock = mMulticastSocket > mUnicastSocket ? mMulticastSocket : mUnicastSocket;

    //Read UDP answers
    while (mReadLoop) {
        memset(buf, 0, bufsize);

        //(Re)set file descriptor
        FD_ZERO(&mReadFDS);
        FD_ZERO(&mWriteFDS);
        FD_ZERO(&mExceptionFDS);
        if (!mMulticastSocket) {
            printf("Multicast socket failed!\n");
            break;
        }

        FD_SET(mMulticastSocket, &mReadFDS);
        FD_SET(mMulticastSocket, &mWriteFDS);
        FD_SET(mMulticastSocket, &mExceptionFDS);

        if (!mUnicastSocket) {
            printf("Multicast socket failed!\n");
            break;
        }
        FD_SET(mUnicastSocket, &mReadFDS);
        FD_SET(mUnicastSocket, &mWriteFDS);
        FD_SET(mUnicastSocket, &mExceptionFDS);

        timeout.tv_sec = 5;
        timeout.tv_usec = 0;

        ret = select(maxsock+1, &mReadFDS, 0, &mExceptionFDS, &timeout);
        if (ret == SOCKET_ERROR) {
            printf("Socket error!\n");
            break;
        }
        else if (ret != 0) {
            //Multicast
            if (FD_ISSET(mMulticastSocket, &mExceptionFDS)) {
                printf("Error on Multicast socket, continue\n");
            }
            if (FD_ISSET(mMulticastSocket, &mReadFDS)) {
                //Data
                //printf("Data\n");
                ret = (int)recvfrom(mMulticastSocket, buf, bufsize, MSG_WAITALL, (struct sockaddr*)&sender, &senderlen);
                if(ret != SOCKET_ERROR){
                    //Be sure to only deliver full messages (!)
                    IncommingMessage((struct sockaddr*)&sender, buf, ret);
                }
            }
            //Unicast
            if (FD_ISSET(mUnicastSocket, &mExceptionFDS)) {
                printf("Error on Unicast socket, continue\n");
            }
            if (FD_ISSET(mUnicastSocket, &mReadFDS)) {
                //Data
                //printf("Data\n");
                ret = (int)recvfrom(mUnicastSocket, buf, bufsize, MSG_WAITALL, (struct sockaddr*)&sender, &senderlen);
                if(ret != SOCKET_ERROR){
                    //Be sure to only deliver full messages (!)
                    IncommingMessage((struct sockaddr*)&sender, buf, ret);
                }
            }

        }
    }
//EXIT:
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1038, %rsp           # imm = 0x1038
movq %rdi, %rbx
movb $0x1, 0x68(%rdi)
leaq 0x10(%rsp), %rax
movq $0x5, (%rax)
andq $0x0, 0x8(%rax)
movq %rsp, %rax
movl $0x10, (%rax)
movl 0x8(%rdi), %eax
movl 0xc(%rdi), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
leaq 0xf8(%rdi), %r13
leaq 0x78(%rdi), %rax
movq %rax, 0x8(%rsp)
incl %ecx
movl %ecx, 0x4(%rsp)
xorl %r15d, %r15d
movb $0x1, %al
pushq $0x40
popq %r12
pushq $0x1
popq %rbp
testb %al, %al
je 0x4679
movl $0x1000, %edx           # imm = 0x1000
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x3130
pushq $0x1f
popq %rax
cmpq $0x2f, %rax
je 0x448d
andq $0x0, (%rbx,%rax,8)
incq %rax
jmp 0x447d
pushq $0x2f
popq %rax
cmpq $0x3f, %rax
je 0x44a0
andq $0x0, (%rbx,%rax,8)
incq %rax
jmp 0x4490
pushq $0xf
popq %rax
cmpq $0x1f, %rax
je 0x44b3
andq $0x0, (%rbx,%rax,8)
incq %rax
jmp 0x44a3
movl 0x8(%rbx), %ecx
testl %ecx, %ecx
je 0x466d
movl %ecx, %eax
cltd
idivl %r12d
movslq %eax, %rdx
shll $0x6, %eax
subl %eax, %ecx
movq %rbp, %rax
shlq %cl, %rax
orq %rax, 0xf8(%rbx,%rdx,8)
orq %rax, 0x178(%rbx,%rdx,8)
orq %rax, 0x78(%rbx,%rdx,8)
movl 0xc(%rbx), %ecx
testl %ecx, %ecx
je 0x466d
movl %ecx, %eax
cltd
idivl %r12d
movslq %eax, %rdx
shll $0x6, %eax
subl %eax, %ecx
movq %rbp, %rax
shlq %cl, %rax
orq %rax, 0xf8(%rbx,%rdx,8)
orq %rax, 0x178(%rbx,%rdx,8)
orq %rax, 0x78(%rbx,%rdx,8)
movq $0x5, 0x10(%rsp)
andq $0x0, 0x18(%rsp)
movl 0x4(%rsp), %edi
movq %r13, %rsi
xorl %edx, %edx
movq 0x8(%rsp), %rcx
leaq 0x10(%rsp), %r8
callq 0x3230
movl %eax, %r15d
testl %eax, %eax
je 0x4665
cmpl $-0x1, %r15d
je 0x468e
movl 0x8(%rbx), %edi
movl %edi, %eax
cltd
idivl %r12d
movl %edx, %ecx
cltq
movq 0x78(%rbx,%rax,8), %rsi
movq %rbp, %rdx
shlq %cl, %rdx
btq %rcx, %rsi
jae 0x4594
leaq 0x3e61(%rip), %rdi      # 0x83dd
callq 0x3370
movl 0x8(%rbx), %edi
movl %edi, %eax
cltd
idivl %r12d
movl %edx, %ecx
cltq
movq %rbp, %rdx
shlq %cl, %rdx
testq %rdx, (%r13,%rax,8)
je 0x45de
movl $0x1000, %edx           # imm = 0x1000
leaq 0x30(%rsp), %rsi
movl $0x100, %ecx            # imm = 0x100
leaq 0x20(%rsp), %r8
movq %rsp, %r9
callq 0x31c0
movq %rax, %r14
pushq $-0x1
popq %rax
movl %eax, %r15d
cmpl $-0x1, %r14d
je 0x45de
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movl %r14d, %ecx
callq 0x4698
movl %r14d, %r15d
movl 0xc(%rbx), %edi
movl %edi, %eax
cltd
idivl %r12d
movl %edx, %ecx
cltq
movq 0x78(%rbx,%rax,8), %rsi
movq %rbp, %rdx
shlq %cl, %rdx
btq %rcx, %rsi
jae 0x461b
leaq 0x3dfe(%rip), %rdi      # 0x8401
callq 0x3370
movl 0xc(%rbx), %edi
movl %edi, %eax
cltd
idivl %r12d
movl %edx, %ecx
cltq
movq %rbp, %rdx
shlq %cl, %rdx
testq %rdx, (%r13,%rax,8)
je 0x4665
movl $0x1000, %edx           # imm = 0x1000
leaq 0x30(%rsp), %rsi
movl $0x100, %ecx            # imm = 0x100
leaq 0x20(%rsp), %r8
movq %rsp, %r9
callq 0x31c0
movq %rax, %r14
pushq $-0x1
popq %rax
movl %eax, %r15d
cmpl $-0x1, %r14d
je 0x4665
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movl %r14d, %ecx
callq 0x4698
movl %r14d, %r15d
movb 0x68(%rbx), %al
jmp 0x4461
leaq 0x3d50(%rip), %rdi      # 0x83c4
callq 0x3370
movl %r15d, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3d8e(%rip), %rdi      # 0x8423
jmp 0x4674
nop