// File: /workspace/github_cpp_makefile_star2_O1/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 0x7bb3
movq %rdi, %r12
cmpw $0x0, (%rdi)
je 0x7bd2
movq %rsi, 0x10(%rsp)
testq %rsi, %rsi
je 0x7bf1
movq %r9, %rbx
movq %rcx, %r13
movq %rdx, %r14
testl %r8d, %r8d
jne 0x7924
callq 0x4b17
movl %eax, %r8d
andl $0x3fff, %r8d           # imm = 0x3FFF
orl $0x4000, %r8d           # imm = 0x4000
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, (%r14)
movl %eax, (%r13)
movl $0x800, (%rsp)          # imm = 0x800
testq %rbx, %rbx
movq %r14, 0x18(%rsp)
je 0x7946
movl 0x4(%rbx), %ebx
jmp 0x7948
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 0x7c10
movl %eax, 0x4(%rsp,%r14,4)
testl %eax, %eax
js 0x7b2b
incq %r14
cmpq $0x3, %r14
jne 0x7956
movq %r13, 0x40(%rsp)
xorl %r15d, %r15d
leaq 0x156(%rsp), %rbx
movw %r15w, 0x100(%rbx)
leaq 0x54(%rsp), %r14
movw %r15w, 0x100(%r14)
movl 0x4(%rsp,%r15,4), %edi
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %rcx
movl $0x1, %r8d
movl %ebp, %r9d
callq 0x6a54
incq %r15
cmpq $0x3, %r15
jne 0x79a1
xorl %r12d, %r12d
leaq 0x720(%rsp), %r13
movq %rsp, %rbx
leaq 0x4c(%rsp), %r14
leaq 0x258(%rsp), %r15
movl $0x800, (%rsp)          # imm = 0x800
movl 0x4(%rsp,%r12,4), %edi
movq %r13, %rsi
movq %rbx, %rdx
leaq 0x50(%rsp), %rcx
movq %r14, %r8
movl %ebp, %r9d
callq 0x7f4a
movl $0x4c8, %edx            # imm = 0x4C8
movq %r15, %rdi
xorl %esi, %esi
callq 0x2110
movl (%rsp), %esi
movq %r13, %rdi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x3248
testb %al, %al
je 0x7b9f
movq 0x274(%rsp), %rax
movq %rax, 0x20(%rsp,%r12,8)
incq %r12
cmpq $0x3, %r12
jne 0x79e1
cmpb $0x0, 0xf60(%rsp)
je 0x7aed
movq 0x5563(%rip), %r12      # 0xcfb8
leaq 0x2740(%rip), %rsi      # 0xa19c
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 0x2501(%rip), %rbx      # 0x9f9c
movl $0xd, %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x21f0
leaq (%rsp,%r14), %rsi
addq $0x20, %rsi
movq %r12, %rdi
callq 0x3f5b
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 0x7a9b
movzwl 0x20(%rsp), %eax
testb $0x1, %al
movq 0x40(%rsp), %rdx
jne 0x7b4b
orl $0x1, %eax
movzwl 0x28(%rsp), %ecx
cmpl %ecx, %eax
movq 0x18(%rsp), %rcx
jne 0x7b8b
movq 0x20(%rsp), %rax
movq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movl 0x4(%rsp), %eax
movl %eax, (%rcx)
movl 0x8(%rsp), %eax
movl %eax, (%rdx)
movl 0xc(%rsp), %edi
jmp 0x7b82
testq %r14, %r14
je 0x7b9f
incq %r14
leal -0x2(%r14), %eax
movl 0x4(%rsp,%rax,4), %edi
callq 0x22c0
decq %r14
cmpq $0x1, %r14
jg 0x7b33
jmp 0x7b9f
movzwl 0x28(%rsp), %eax
testb $0x1, %al
movq 0x18(%rsp), %rsi
jne 0x7b8b
orl $0x1, %eax
movzwl 0x30(%rsp), %ecx
cmpl %ecx, %eax
jne 0x7b8b
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movl 0x8(%rsp), %eax
movl %eax, (%rsi)
movl 0xc(%rsp), %eax
movl %eax, (%rdx)
movl 0x4(%rsp), %edi
callq 0x22c0
movb $0x1, %al
jmp 0x7ba1
xorl %ebx, %ebx
movl 0x4(%rsp,%rbx,4), %edi
callq 0x22c0
incq %rbx
cmpq $0x3, %rbx
jne 0x7b8d
xorl %eax, %eax
addq $0xf28, %rsp            # imm = 0xF28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2573(%rip), %rdi      # 0xa12d
leaq 0x1b94(%rip), %rsi      # 0x9755
leaq 0x2573(%rip), %rcx      # 0xa13b
movl $0x90e, %edx            # imm = 0x90E
callq 0x2120
leaq 0x2304(%rip), %rdi      # 0x9edd
leaq 0x1b75(%rip), %rsi      # 0x9755
leaq 0x2554(%rip), %rcx      # 0xa13b
movl $0x90f, %edx            # imm = 0x90F
callq 0x2120
leaq 0x252d(%rip), %rdi      # 0xa125
leaq 0x1b56(%rip), %rsi      # 0x9755
leaq 0x2535(%rip), %rcx      # 0xa13b
movl $0x910, %edx            # imm = 0x910
callq 0x2120