// File: /workspace/github_cpp_makefile_star2_O1/dguglielmi[P]dlan-linux-software/common/packetinterface-linux.cpp
// Function: CPacketInterfaceLinux::ReceivePackets(IPacketReceiver&, unsigned long)
bool CPacketInterfaceLinux::ReceivePackets(IPacketReceiver& receiver, unsigned long ulTimeout)
{
  size_t buffersize = 3000;
  UnknownPacket packet(buffersize);

  bool infinite_timeout = (ulTimeout == (unsigned long)-1);
  struct timeval start, now, overall_timeout, select_timeout;

  if(!infinite_timeout)
  {
    overall_timeout.tv_sec = (long)(ulTimeout / 1000);
    overall_timeout.tv_usec = (long)(ulTimeout % 1000) * 1000;

    gettimeofday(&start, NULL);
    now = start;
  }
  bool stop_receiving = false;
  bool packet_indication = false;
  do
  {
    fd_set recset;
    int nfds;
    buildfdset(recset, nfds);

    if(!infinite_timeout)
      select_timeout = overall_timeout - (now - start);

    if(select(nfds, &recset, NULL, NULL, infinite_timeout ? 0 : &select_timeout) > 0)
    {
      std::map<CMACAddress,Interface>::const_iterator itIfc;
      if((itIfc = nextifcfromfdset(recset)) != m_ifcs.end())
      {
        struct sockaddr_ll source = { 0 };
        socklen_t sourcelen = sizeof(source);
        size_t recvbytes = (size_t) recvfrom(itIfc->second.fdsocket,
          (void*)packet.Data(), buffersize, 0, SOCKADDR_STAR(&source), &sourcelen);
        packet.SetSize(recvbytes);

        packet_indication = receiver.PacketIndication(itIfc->first, packet);
      }
      else if(FD_ISSET(m_interruptPipe[0], &recset))
      {
        char buffer[10];
        read(m_interruptPipe[0], buffer, sizeof(buffer));
        stop_receiving = true;
      }
    }

    if(!infinite_timeout)
      gettimeofday(&now, NULL);
  }
  while(!stop_receiving && !packet_indication && (infinite_timeout || ((now - start) < overall_timeout))); 

  return packet_indication;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdx, %rbp
movq %rsi, 0x60(%rsp)
movq %rdi, %r15
leaq 0x97bd(%rip), %rax      # 0x1dd80
movq %rax, 0x28(%rsp)
movl $0xbb8, %edi            # imm = 0xBB8
callq 0x4300
movq %rax, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
cmpq $-0x1, %rbp
je 0x14639
movq %rbp, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
shrq $0x4, %rdx
movq %rdx, 0x18(%rsp)
imulq $0x3e8, %rdx, %rax      # imm = 0x3E8
movq %rbp, %rcx
subq %rax, %rcx
imulq $0x3e8, %rcx, %rax      # imm = 0x3E8
movq %rax, 0x58(%rsp)
leaq 0x98(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x43a0
movups (%r14), %xmm0
movaps %xmm0, 0x40(%rsp)
jmp 0x14639
movq 0x98(%rsp), %r14
movq 0xa0(%rsp), %r13
leaq 0x70(%r15), %rax
movq %rax, 0x68(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r12d, %r12d
movq %r13, 0x8(%rsp)
movq %r15, %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x24(%rsp), %rdx
callq 0x14886
cmpq $-0x1, %rbp
je 0x146e5
movq 0x48(%rsp), %rax
movq %rax, %rcx
subq %r13, %rcx
movl $0xf4240, %esi          # imm = 0xF4240
addq %rsi, %rcx
xorl %edx, %edx
subq %r13, %rax
setl %dl
cmovlq %rcx, %rax
movq 0x58(%rsp), %rdi
movq %rdi, %rcx
subq %rax, %rcx
addq %rsi, %rcx
xorl %esi, %esi
subq %rax, %rdi
setl %sil
cmovlq %rcx, %rdi
movq 0x18(%rsp), %rax
subq 0x40(%rsp), %rax
addq %r14, %rax
addq %rdx, %rax
subq %rsi, %rax
movq %rax, 0x88(%rsp)
movq %rdi, 0x90(%rsp)
leaq 0x88(%rsp), %r8
jmp 0x146e8
xorl %r8d, %r8d
movl 0x24(%rsp), %edi
leaq 0xa8(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4240
testl %eax, %eax
jle 0x147c5
movq %r12, %rbx
movq %r14, %r12
movq %r15, %r13
movq %r15, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x14950
cmpq 0x68(%rsp), %rax
je 0x14789
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movl $0x0, 0x80(%rsp)
movl $0x14, 0x20(%rsp)
movl 0x4c(%rax), %edi
movq 0x30(%rsp), %rsi
movl $0xbb8, %edx            # imm = 0xBB8
xorl %ecx, %ecx
leaq 0x70(%rsp), %r8
leaq 0x20(%rsp), %r9
callq 0x41f0
addq $0x20, %r14
movq %rax, 0x38(%rsp)
movq 0x60(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
leaq 0x28(%rsp), %rdx
callq *(%rax)
movq %rax, 0x10(%rsp)
movq %r12, %r14
movq %rbx, %r12
jmp 0x147c0
movl 0x98(%r13), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0xa8(%rsp,%rax,8), %rax
btq %rdi, %rax
jae 0x14781
movl $0xa, %edx
leaq 0x70(%rsp), %rsi
callq 0x4340
movq %r12, %r14
movb $0x1, %r12b
movq 0x8(%rsp), %r13
cmpq $-0x1, %rbp
je 0x147d7
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x43a0
testb $0x1, %r12b
jne 0x14831
testb $0x1, 0x10(%rsp)
jne 0x14831
cmpq $-0x1, %rbp
je 0x14663
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
xorl %edx, %edx
cmpq %r13, %rcx
setl %dl
subq %rdx, %rax
subq %r14, %rax
cmpq 0x18(%rsp), %rax
jl 0x14663
subq %r13, %rcx
leaq 0xf4240(%rcx), %rdx
cmovgeq %rcx, %rdx
cmpq 0x18(%rsp), %rax
jne 0x14831
cmpq 0x58(%rsp), %rdx
jl 0x14663
leaq 0x9548(%rip), %rax      # 0x1dd80
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rdi
callq 0x42e0
movq 0x10(%rsp), %rax
andb $0x1, %al
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x14864
jmp 0x14864
movq %rax, %rbx
leaq 0x9512(%rip), %rax      # 0x1dd80
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rdi
callq 0x42e0
movq %rbx, %rdi
callq 0x43e0
nop