// File: /workspace/github_cpp_makefile_star2_O1/Distrotech[P]stund/server.cxx
// Function: main
int
main(int argc, char* argv[])
{
   assert( sizeof(UInt8 ) == 1 );
   assert( sizeof(UInt16) == 2 );
   assert( sizeof(UInt32) == 4 );

   initNetwork();

   clog << "STUN server version "  <<  STUN_VERSION << endl;
      
   StunAddress4 myAddr;
   StunAddress4 altAddr;
   bool verbose=false;
   bool background=false;
   
   myAddr.addr = 0;
   altAddr.addr = 0;
   myAddr.port = STUN_PORT;
   altAddr.port = STUN_PORT+1;
   int myPort = 0;
   int altPort = 0;
   int myMediaPort = 0;
   
   UInt32 interfaces[10];
   int numInterfaces = stunFindLocalInterfaces(interfaces,10);

   if (numInterfaces == 2)
   {
      myAddr.addr = interfaces[0];
      myAddr.port = STUN_PORT;
      altAddr.addr = interfaces[1];
      altAddr.port = STUN_PORT+1;
   }

   for ( int arg = 1; arg<argc; arg++ )
   {
      if ( !strcmp( argv[arg] , "-v" ) )
      {
         verbose = true;
      }
      else if ( !strcmp( argv[arg] , "-b" ) )
      {
         background = true;
      }
      else if ( !strcmp( argv[arg] , "-h" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         stunParseServerName(argv[arg], myAddr);
      }
      else if ( !strcmp( argv[arg] , "-a" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         stunParseServerName(argv[arg], altAddr);
      }
      else if ( !strcmp( argv[arg] , "-p" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         myPort = UInt16(strtol( argv[arg], NULL, 10));
      }
      else if ( !strcmp( argv[arg] , "-o" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         altPort = UInt16(strtol( argv[arg], NULL, 10));
      }
      else if ( !strcmp( argv[arg] , "-m" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         myMediaPort = UInt16(strtol( argv[arg], NULL, 10));
      }
      else
      {
         usage();
         exit(-1);
      }
   }

   if ( myPort != 0 )
   {
      myAddr.port = myPort;
   }
   if ( altPort != 0 )
   {
      altAddr.port = altPort;
   }
   
   if (  (myAddr.addr == 0) || (altAddr.addr == 0) )
   {
      clog << "If your machine does not have exactly two ethernet interfaces, "
           << "you must specify the server and alt server" << endl;
      //usage();
      //exit(-1);
   }

   if ( myAddr.port == altAddr.port )
   {
      altAddr.port = myAddr.port+1;
   }

   if ( verbose )
   {
      clog << "Running with on interface "
           << myAddr << " with alternate " 
           << altAddr << endl;
   }
    
   if (
      ( myAddr.addr  == 0 ) ||
      ( myAddr.port  == 0 ) ||
      //( altAddr.addr == 0 ) ||
      ( altAddr.port == 0 ) 
      )
   {
      cerr << "Bad command line" << endl;
      exit(1);
   }
   
   if ( altAddr.addr == 0 )
   {
      cerr << "Warning - no alternate ip address STUN will not work" << endl;
      //exit(1);
   }
   
#if defined(WIN32)
   int pid=0;

   if ( background )
   {
      cerr << "The -b background option does not work in windows" << endl;
      exit(-1);
   }
#else
   pid_t pid=0;

   if ( background )
   {
      pid = fork();

      if (pid < 0)
      {
         cerr << "fork: unable to fork" << endl;
         exit(-1);
      }
   }
#endif

   if (pid == 0) //child or not using background
   {
      StunServerInfo info;
      bool ok = stunInitServer(info, myAddr, altAddr, myMediaPort, verbose);
      
      int c=0;
      while (ok)
      {
         ok = stunServerProcess(info, verbose);
         c++;
         if ( verbose && (c%1000 == 0) ) 
         {
            clog << "*";
         }
      }
      // Notreached
   }
   
   return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f68, %rsp           # imm = 0x2F68
movq %rsi, %r15
movl %edi, %ebp
callq 0x7dbf
movq 0x97ec(%rip), %rbx      # 0xbfc0
leaq 0x5a66(%rip), %rsi      # 0x8241
movl $0x14, %edx
movq %rbx, %rdi
callq 0x2200
leaq 0x5a67(%rip), %rsi      # 0x8256
movl $0x4, %edx
movq %rbx, %rdi
callq 0x2200
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
xorl %eax, %eax
movl %eax, 0x18(%rsp)
movl $0x0, 0x20(%rsp)
movl %eax, 0x10(%rsp)
movw $0xd96, 0x14(%rsp)      # imm = 0xD96
movw $0xd97, 0xc(%rsp)       # imm = 0xD97
leaq 0x30(%rsp), %rdi
movl $0xa, %esi
callq 0x5fd0
cmpl $0x2, %eax
jne 0x2875
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
movl %eax, 0x18(%rsp)
movw $0xd96, 0x14(%rsp)      # imm = 0xD96
movl %ecx, 0x10(%rsp)
movw $0xd97, 0xc(%rsp)       # imm = 0xD97
movl $0x0, %eax
movq %rax, 0x28(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, %r12d
movl $0x0, %r13d
cmpl $0x2, %ebp
jl 0x2a3b
xorl %r13d, %r13d
movl $0x1, %ebx
leaq 0x59b0(%rip), %r12      # 0x825b
movl $0x0, 0x24(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movslq %ebx, %rax
movq (%r15,%rax,8), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x2220
testl %eax, %eax
je 0x2996
movq %r14, %rdi
leaq 0x596e(%rip), %rsi      # 0x825e
callq 0x2220
testl %eax, %eax
je 0x299b
movq %r14, %rdi
leaq 0x595a(%rip), %rsi      # 0x8261
callq 0x2220
testl %eax, %eax
je 0x29a4
movq %r14, %rdi
leaq 0x5946(%rip), %rsi      # 0x8264
callq 0x2220
testl %eax, %eax
je 0x29bc
movq %r14, %rdi
leaq 0x5932(%rip), %rsi      # 0x8267
callq 0x2220
testl %eax, %eax
je 0x29e3
movq %r14, %rdi
leaq 0x591e(%rip), %rsi      # 0x826a
callq 0x2220
testl %eax, %eax
je 0x2a09
movq %r14, %rdi
leaq 0x590a(%rip), %rsi      # 0x826d
callq 0x2220
testl %eax, %eax
jne 0x2c66
incl %ebx
cmpl %ebp, %ebx
jge 0x2c66
movslq %ebx, %rax
movq (%r15,%rax,8), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2230
movzwl %ax, %eax
movl %eax, 0x24(%rsp)
jmp 0x29d7
movb $0x1, %r13b
jmp 0x29d7
movb $0x1, %al
movq %rax, 0x28(%rsp)
jmp 0x29d7
incl %ebx
cmpl %ebp, %ebx
jge 0x2c66
movslq %ebx, %rax
movq (%r15,%rax,8), %rdi
leaq 0x14(%rsp), %rsi
jmp 0x29d2
incl %ebx
cmpl %ebp, %ebx
jge 0x2c66
movslq %ebx, %rax
movq (%r15,%rax,8), %rdi
leaq 0xc(%rsp), %rsi
callq 0x4977
incl %ebx
cmpl %ebp, %ebx
jl 0x28cc
jmp 0x2a2f
incl %ebx
cmpl %ebp, %ebx
jge 0x2c66
movslq %ebx, %rax
movq (%r15,%rax,8), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2230
movzwl %ax, %eax
movl %eax, 0x20(%rsp)
jmp 0x29d7
incl %ebx
cmpl %ebp, %ebx
jge 0x2c66
movslq %ebx, %rax
movq (%r15,%rax,8), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2230
movzwl %ax, %eax
movl %eax, 0x1c(%rsp)
jmp 0x29d7
movl 0x24(%rsp), %r12d
movq 0x9585(%rip), %rbx      # 0xbfc0
movl 0x20(%rsp), %eax
testl %eax, %eax
je 0x2a48
movw %ax, 0x14(%rsp)
movl 0x1c(%rsp), %eax
testl %eax, %eax
je 0x2a55
movw %ax, 0xc(%rsp)
cmpl $0x0, 0x18(%rsp)
je 0x2a63
cmpl $0x0, 0x10(%rsp)
jne 0x2ab2
leaq 0x5806(%rip), %rsi      # 0x8270
movl $0x3f, %edx
movq %rbx, %rdi
callq 0x2200
leaq 0x5832(%rip), %rsi      # 0x82b0
movl $0x2a, %edx
movq %rbx, %rdi
callq 0x2200
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
movzwl 0x14(%rsp), %eax
cmpw 0xc(%rsp), %ax
jne 0x2ac5
incl %eax
movw %ax, 0xc(%rsp)
testb $0x1, %r13b
je 0x2b41
movq 0x94ee(%rip), %rbx      # 0xbfc0
leaq 0x5802(%rip), %rsi      # 0x82db
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x2200
leaq 0x14(%rsp), %rsi
movq %rbx, %rdi
callq 0x39cb
movq %rax, %rbx
leaq 0x57f9(%rip), %rsi      # 0x82f6
movl $0x10, %edx
movq %rax, %rdi
callq 0x2200
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0x39cb
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
cmpl $0x0, 0x18(%rsp)
je 0x2c75
cmpw $0x0, 0x14(%rsp)
je 0x2c75
cmpw $0x0, 0xc(%rsp)
je 0x2c75
cmpl $0x0, 0x10(%rsp)
jne 0x2bad
movq 0x947e(%rip), %rbx      # 0xbff0
leaq 0x579f(%rip), %rsi      # 0x8318
movl $0x34, %edx
movq %rbx, %rdi
callq 0x2200
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
xorl %eax, %eax
testb $0x1, 0x28(%rsp)
je 0x2bc3
callq 0x20a0
testl %eax, %eax
js 0x2c9a
testl %eax, %eax
jne 0x2c52
movq %r13, %r14
movzbl %r13b, %ebx
andl $0x1, %ebx
leaq 0x60(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0xc(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
callq 0x5398
testb %al, %al
je 0x2c52
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0x1, %r12d
movl %r12d, %eax
imulq $0x10624dd3, %rax, %rax # imm = 0x10624DD3
shrq $0x26, %rax
imull $0x3e8, %eax, %r13d     # imm = 0x3E8
leaq 0x60(%rsp), %rdi
movl %ebx, %esi
callq 0x55e5
movl %eax, %ebp
addl %r15d, %r13d
sete %al
andb %r14b, %al
cmpb $0x1, %al
jne 0x2c47
movl $0x1, %edx
movq 0x9385(%rip), %rdi      # 0xbfc0
leaq 0x5720(%rip), %rsi      # 0x8362
callq 0x2200
decl %r15d
incl %r12d
testb %bpl, %bpl
jne 0x2bff
xorl %eax, %eax
addq $0x2f68, %rsp           # imm = 0x2F68
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x245c
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2210
movq 0x9374(%rip), %rdi      # 0xbff0
leaq 0x5684(%rip), %rsi      # 0x8307
callq 0x21e0
movq %rax, %rdi
callq 0x20e0
movl $0x1, %edi
callq 0x2210
movq 0x934f(%rip), %rdi      # 0xbff0
leaq 0x56a5(%rip), %rsi      # 0x834d
callq 0x21e0
movq %rax, %rdi
callq 0x20e0
jmp 0x2c6b
nop