// File: /workspace/github_cpp_makefile_star2_O2/Distrotech[P]stund/stun.cxx
// Function: stunOpenSocketPair(StunAddress4&, StunAddress4*, int*, int*, int, StunAddress4*, bool)
bool
stunOpenSocketPair( StunAddress4& dest, StunAddress4* mapAddr, 
                    int* fd1, int* fd2, 
                    int port, StunAddress4* srcAddr, 
                    bool verbose )
{
   assert( dest.addr!= 0 );
   assert( dest.port != 0 );
   assert( mapAddr );
   
   const int NUM=3;
	
   if ( port == 0 )
   {
      port = stunRandomPort();
   }
	
   *fd1=-1;
   *fd2=-1;
	
   char msg[STUN_MAX_MESSAGE_SIZE];
   int msgLen =sizeof(msg);
	
   StunAddress4 from;
   int fd[NUM];
   int i;
	
   unsigned int interfaceIp = 0;
   if ( srcAddr )
   {
      interfaceIp = srcAddr->addr;
   }

   for( i=0; i<NUM; i++)
   {
      fd[i] = openPort( (port == 0) ? 0 : (port + i), 
                        interfaceIp, verbose);
      if (fd[i] < 0) 
      {
         while (i > 0)
         {
            closesocket(fd[--i]);
         }
         return false;
      }
   }
	
   StunAtrString username;
   StunAtrString password;
	
   username.sizeValue = 0;
   password.sizeValue = 0;
	
#ifdef USE_TLS
   stunGetUserNameAndPassword( dest, username, password );
#endif
	
   for( i=0; i<NUM; i++)
   {
      stunSendTest(fd[i], dest, username, password, 1/*testNum*/, verbose );
   }
	
   StunAddress4 mappedAddr[NUM];
   for( i=0; i<NUM; i++)
   {
      msgLen = sizeof(msg)/sizeof(*msg);
      getMessage( fd[i],
                  msg,
                  &msgLen,
                  &from.addr,
                  &from.port ,verbose);
		
      StunMessage resp;
      memset(&resp, 0, sizeof(StunMessage));
		
      bool ok = stunParseMessage( msg, msgLen, resp, verbose );
      if (!ok) 
      {
         return false;
      }
		
      mappedAddr[i] = resp.mappedAddress.ipv4;
      StunAddress4 changedAddr = resp.changedAddress.ipv4;
   }
	
   if (verbose)
   {               
      clog << "--- stunOpenSocketPair --- " << endl;
      for( i=0; i<NUM; i++)
      {
         clog << "\t mappedAddr=" << mappedAddr[i] << endl;
      }
   }
	
   if ( mappedAddr[0].port %2 == 0 )
   {
      if (  mappedAddr[0].port+1 ==  mappedAddr[1].port )
      {
         *mapAddr = mappedAddr[0];
         *fd1 = fd[0];
         *fd2 = fd[1];
         closesocket( fd[2] );
         return true;
      }
   }
   else
   {
      if (( mappedAddr[1].port %2 == 0 )
          && (  mappedAddr[1].port+1 ==  mappedAddr[2].port ))
      {
         *mapAddr = mappedAddr[1];
         *fd1 = fd[1];
         *fd2 = fd[2];
         closesocket( fd[0] );
         return true;
      }
   }

   // something failed, close all and return error
   for( i=0; i<NUM; i++)
   {
      closesocket( fd[i] );
   }
	
   return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf28, %rsp            # imm = 0xF28
cmpl $0x0, 0x4(%rdi)
je 0x678c
movq %rdi, %r12
cmpw $0x0, (%rdi)
je 0x67ab
movq %rsi, %rbp
testq %rsi, %rsi
je 0x67ca
movq %r9, %rbx
movl %r8d, %r13d
movq %rcx, %r14
movq %rdx, %r15
testl %r8d, %r8d
jne 0x6539
callq 0x40cb
movl %eax, %r13d
orl $-0x1, (%r15)
orl $-0x1, (%r14)
movl $0x800, 0x8(%rsp)       # imm = 0x800
testq %rbx, %rbx
movq %r14, 0x18(%rsp)
movq %r15, 0x40(%rsp)
movq %rbp, 0x38(%rsp)
je 0x6562
movl 0x4(%rbx), %ebx
jmp 0x6564
xorl %ebx, %ebx
movl %r13d, %r15d
xorl %r14d, %r14d
movzbl 0xf60(%rsp), %ebp
cmpq $0x3, %r14
je 0x65c3
leal (%r15,%r14), %eax
testl %r13d, %r13d
cmovel %r13d, %eax
movzwl %ax, %edi
movl %ebx, %esi
movl %ebp, %edx
callq 0x67f4
movl %eax, 0xc(%rsp,%r14,4)
testl %eax, %eax
js 0x65aa
incq %r14
jmp 0x6572
movl 0x8(%rsp,%r14,4), %edi
decq %r14
callq 0x22c0
testq %r14, %r14
jg 0x659d
xorl %eax, %eax
addq $0xf28, %rsp            # imm = 0xF28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x156(%rsp), %rbx
andw $0x0, 0x100(%rbx)
leaq 0x54(%rsp), %r14
andw $0x0, 0x100(%r14)
xorl %r13d, %r13d
pushq $0x1
popq %r15
cmpq $0x3, %r13
je 0x660c
movl 0xc(%rsp,%r13,4), %edi
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %rcx
movl %r15d, %r8d
movl %ebp, %r9d
callq 0x5916
incq %r13
jmp 0x65e8
xorl %r12d, %r12d
leaq 0x720(%rsp), %r13
leaq 0x8(%rsp), %rbx
leaq 0x4c(%rsp), %r14
leaq 0x258(%rsp), %r15
cmpq $0x3, %r12
je 0x668c
movl $0x800, 0x8(%rsp)       # imm = 0x800
movl 0xc(%rsp,%r12,4), %edi
movq %r13, %rsi
movq %rbx, %rdx
leaq 0x50(%rsp), %rcx
movq %r14, %r8
movl %ebp, %r9d
callq 0x6a0e
movl $0x4c8, %edx            # imm = 0x4C8
movq %r15, %rdi
xorl %esi, %esi
callq 0x2110
movl 0x8(%rsp), %esi
movq %r13, %rdi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x2d54
testb %al, %al
je 0x65af
movq 0x274(%rsp), %rax
movq %rax, 0x20(%rsp,%r12,8)
incq %r12
jmp 0x6629
cmpb $0x0, 0xf60(%rsp)
movq 0x40(%rsp), %r13
movq 0x38(%rsp), %rbp
je 0x66fa
movq 0x4911(%rip), %rbx      # 0xafb8
leaq 0x1ae6(%rip), %rsi      # 0x8194
movq %rbx, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20c0
xorl %r12d, %r12d
leaq 0x18cc(%rip), %r14      # 0x7f94
cmpq $0x18, %r12
je 0x66fa
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21d0
movq %rax, %r15
leaq (%rsp,%r12), %rsi
addq $0x20, %rsi
movq %rax, %rdi
callq 0x3732
movq %r15, %rdi
callq 0x20c0
addq $0x8, %r12
jmp 0x66c8
movzwl 0x20(%rsp), %eax
testb $0x1, %al
jne 0x6731
orl $0x1, %eax
movzwl 0x28(%rsp), %ecx
cmpl %ecx, %eax
movq 0x18(%rsp), %rcx
jne 0x6772
movq 0x20(%rsp), %rax
movq %rax, (%rbp)
movl 0xc(%rsp), %eax
movl %eax, (%r13)
movl 0x10(%rsp), %eax
movl %eax, (%rcx)
movl 0x14(%rsp), %edi
jmp 0x6766
movzwl 0x28(%rsp), %eax
testb $0x1, %al
movq 0x18(%rsp), %rdx
jne 0x6772
orl $0x1, %eax
movzwl 0x30(%rsp), %ecx
cmpl %ecx, %eax
jne 0x6772
movq 0x28(%rsp), %rax
movq %rax, (%rbp)
movl 0x10(%rsp), %eax
movl %eax, (%r13)
movl 0x14(%rsp), %eax
movl %eax, (%rdx)
movl 0xc(%rsp), %edi
callq 0x22c0
movb $0x1, %al
jmp 0x65b1
xorl %ebx, %ebx
cmpq $0x3, %rbx
je 0x65af
movl 0xc(%rsp,%rbx,4), %edi
callq 0x22c0
incq %rbx
jmp 0x6774
leaq 0x1992(%rip), %rdi      # 0x8125
leaq 0xfb3(%rip), %rsi       # 0x774d
leaq 0x1992(%rip), %rcx      # 0x8133
movl $0x90e, %edx            # imm = 0x90E
callq 0x2120
leaq 0x1723(%rip), %rdi      # 0x7ed5
leaq 0xf94(%rip), %rsi       # 0x774d
leaq 0x1973(%rip), %rcx      # 0x8133
movl $0x90f, %edx            # imm = 0x90F
callq 0x2120
leaq 0x194c(%rip), %rdi      # 0x811d
leaq 0xf75(%rip), %rsi       # 0x774d
leaq 0x1954(%rip), %rcx      # 0x8133
movl $0x910, %edx            # imm = 0x910
callq 0x2120