// File: /workspace/github_cpp_makefile_star2_O3/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 0x7bb5
movq %rdi, %r12
cmpw $0x0, (%rdi)
je 0x7bd4
movq %rsi, 0x10(%rsp)
testq %rsi, %rsi
je 0x7bf3
movq %r9, %rbx
movq %rcx, %r13
movq %rdx, %r14
testl %r8d, %r8d
jne 0x7930
callq 0x4b28
movl %eax, %r8d
andl $0x3fff, %r8d           # imm = 0x3FFF
orl $0x4000, %r8d           # imm = 0x4000
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, (%r14)
movl %eax, (%r13)
testq %rbx, %rbx
movq %r14, 0x18(%rsp)
je 0x794b
movl 0x4(%rbx), %ebx
jmp 0x794d
xorl %ebx, %ebx
movl %r8d, %r15d
xorl %r14d, %r14d
movzbl 0xf60(%rsp), %ebp
leal (%r15,%r14), %eax
movzwl %ax, %edi
movl %ebx, %esi
movl %ebp, %edx
callq 0x7c14
movl %eax, (%rsp,%r14,4)
testl %eax, %eax
js 0x7b30
incq %r14
cmpq $0x3, %r14
jne 0x795b
movq %r13, 0x40(%rsp)
xorl %r15d, %r15d
leaq 0x156(%rsp), %rbx
movw %r15w, 0x100(%rbx)
leaq 0x54(%rsp), %r14
movw %r15w, 0x100(%r14)
movl (%rsp,%r15,4), %edi
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %rcx
movl $0x1, %r8d
movl %ebp, %r9d
callq 0x6af5
incq %r15
cmpq $0x3, %r15
jne 0x79a5
xorl %r12d, %r12d
leaq 0x720(%rsp), %r13
leaq 0xc(%rsp), %rbx
leaq 0x4c(%rsp), %r14
leaq 0x258(%rsp), %r15
movl $0x800, 0xc(%rsp)       # imm = 0x800
movl (%rsp,%r12,4), %edi
movq %r13, %rsi
movq %rbx, %rdx
leaq 0x50(%rsp), %rcx
movq %r14, %r8
movl %ebp, %r9d
callq 0x7f81
movl $0x4c8, %edx            # imm = 0x4C8
movq %r15, %rdi
xorl %esi, %esi
callq 0x2110
movl 0xc(%rsp), %esi
movq %r13, %rdi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x31ac
testb %al, %al
je 0x7ba1
movq 0x274(%rsp), %rax
movq %rax, 0x20(%rsp,%r12,8)
incq %r12
cmpq $0x3, %r12
jne 0x79e6
cmpb $0x0, 0xf60(%rsp)
je 0x7af3
movq 0x555d(%rip), %r12      # 0xcfb8
leaq 0x2732(%rip), %rsi      # 0xa194
movl $0x1b, %edx
movq %r12, %rdi
callq 0x21f0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
xorl %r14d, %r14d
leaq 0x24f3(%rip), %rbx      # 0x9f94
movl $0xd, %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x21f0
leaq (%rsp,%r14), %rsi
addq $0x20, %rsi
movq %r12, %rdi
callq 0x3dbb
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
addq $0x8, %r14
cmpq $0x18, %r14
jne 0x7aa1
movzwl 0x20(%rsp), %eax
testb $0x1, %al
movq 0x40(%rsp), %rdx
jne 0x7b4f
orl $0x1, %eax
movzwl 0x28(%rsp), %ecx
cmpl %ecx, %eax
movq 0x18(%rsp), %rcx
jne 0x7b8e
movq 0x20(%rsp), %rax
movq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movl (%rsp), %eax
movl %eax, (%rcx)
movl 0x4(%rsp), %eax
movl %eax, (%rdx)
movl 0x8(%rsp), %edi
jmp 0x7b85
testq %r14, %r14
je 0x7ba1
incq %r14
leal -0x2(%r14), %eax
movl (%rsp,%rax,4), %edi
callq 0x22b0
decq %r14
cmpq $0x1, %r14
jg 0x7b38
jmp 0x7ba1
movzwl 0x28(%rsp), %eax
testb $0x1, %al
movq 0x18(%rsp), %rsi
jne 0x7b8e
orl $0x1, %eax
movzwl 0x30(%rsp), %ecx
cmpl %ecx, %eax
jne 0x7b8e
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movl 0x4(%rsp), %eax
movl %eax, (%rsi)
movl 0x8(%rsp), %eax
movl %eax, (%rdx)
movl (%rsp), %edi
callq 0x22b0
movb $0x1, %al
jmp 0x7ba3
xorl %ebx, %ebx
movl (%rsp,%rbx,4), %edi
callq 0x22b0
incq %rbx
cmpq $0x3, %rbx
jne 0x7b90
xorl %eax, %eax
addq $0xf28, %rsp            # imm = 0xF28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2569(%rip), %rdi      # 0xa125
leaq 0x1b8a(%rip), %rsi      # 0x974d
leaq 0x2569(%rip), %rcx      # 0xa133
movl $0x90e, %edx            # imm = 0x90E
callq 0x2120
leaq 0x22fa(%rip), %rdi      # 0x9ed5
leaq 0x1b6b(%rip), %rsi      # 0x974d
leaq 0x254a(%rip), %rcx      # 0xa133
movl $0x90f, %edx            # imm = 0x90F
callq 0x2120
leaq 0x2523(%rip), %rdi      # 0xa11d
leaq 0x1b4c(%rip), %rsi      # 0x974d
leaq 0x252b(%rip), %rcx      # 0xa133
movl $0x910, %edx            # imm = 0x910
callq 0x2120
nop