// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movq %rsi, 0x48(%rsp)
movq %rdi, %r15
leaq 0xc9db(%rip), %rax      # 0x1ed88
movq %rax, 0x8(%rsp)
movl $0xbb8, %edi            # imm = 0xBB8
callq 0x4310
movq %rax, 0x10(%rsp)
andq $0x0, 0x18(%rsp)
cmpq $-0x1, %rbx
je 0x12415
movl $0x3e8, %ecx            # imm = 0x3E8
movq %rbx, %rax
xorl %edx, %edx
divq %rcx
movq %rax, 0x50(%rsp)
movq %rax, 0x70(%rsp)
imulq $0x3e8, %rdx, %rax      # imm = 0x3E8
movq %rax, 0x40(%rsp)
movq %rax, 0x78(%rsp)
leaq 0x80(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x43d0
movups (%r14), %xmm0
movaps %xmm0, 0x90(%rsp)
jmp 0x12415
leaq 0x70(%r15), %rax
movq %rax, 0x58(%rsp)
xorl %ebp, %ebp
leaq 0xa8(%rsp), %r13
leaq 0x90(%rsp), %r12
movq %r15, %rdi
movq %r13, %rsi
leaq 0x4(%rsp), %rdx
callq 0x125d2
cmpq $-0x1, %rbx
je 0x12480
movq %r12, %rdi
leaq 0x80(%rsp), %rsi
callq 0x12366
movq %rax, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x12366
movq %rax, 0x60(%rsp)
movq %rdx, 0x68(%rsp)
leaq 0x60(%rsp), %r8
jmp 0x12483
xorl %r8d, %r8d
movl 0x4(%rsp), %edi
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4260
testl %eax, %eax
jle 0x124fd
movq %r15, %rdi
movq %r13, %rsi
callq 0x1268a
cmpq 0x58(%rsp), %rax
je 0x1254d
movq %rax, %r14
andl $0x0, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movl $0x14, (%rsp)
movl 0x4c(%rax), %edi
movq 0x10(%rsp), %rsi
movl $0xbb8, %edx            # imm = 0xBB8
xorl %ecx, %ecx
leaq 0x20(%rsp), %r8
movq %rsp, %r9
callq 0x4200
addq $0x20, %r14
movq %rax, 0x18(%rsp)
movq 0x48(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
leaq 0x8(%rsp), %rdx
callq *(%rax)
movl %eax, %ebp
xorl %r14d, %r14d
cmpq $-0x1, %rbx
je 0x12510
movq %r12, %rdi
xorl %esi, %esi
callq 0x43d0
orb %bpl, %r14b
testb $0x1, %r14b
jne 0x1257e
cmpq $-0x1, %rbx
je 0x12430
movq %r12, %rdi
leaq 0x80(%rsp), %rsi
callq 0x12366
cmpq 0x50(%rsp), %rax
jl 0x12430
jne 0x1257e
cmpq 0x40(%rsp), %rdx
jl 0x12430
jmp 0x1257e
movl 0x98(%r15), %edi
movl %edi, %eax
cltd
pushq $0x40
popq %rcx
idivl %ecx
cltq
movq 0xa8(%rsp,%rax,8), %rax
btq %rdx, %rax
jae 0x124fd
leaq 0x20(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x4360
movb $0x1, %r14b
jmp 0x12500
leaq 0xc803(%rip), %rax      # 0x1ed88
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0x42e0
andb $0x1, %bpl
movl %ebp, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x125b0
jmp 0x125b0
movq %rax, %rbx
leaq 0xc7ce(%rip), %rax      # 0x1ed88
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0x42e0
movq %rbx, %rdi
callq 0x4410
nop