// File: /workspace/github_cpp_makefile_star2_O3/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 $0x2f58, %rsp           # imm = 0x2F58
movq %rsi, %r15
movl %edi, %ebp
callq 0x7e46
movq 0x97fc(%rip), %r14      # 0xbfc0
leaq 0x5a76(%rip), %rsi      # 0x8241
movl $0x14, %edx
movq %r14, %rdi
callq 0x2200
leaq 0x5a77(%rip), %rsi      # 0x8256
movl $0x4, %edx
movq %r14, %rdi
callq 0x2200
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
xorl %ebx, %ebx
movl %ebx, 0xc(%rsp)
movl %ebx, 0x4(%rsp)
movw $0xd96, 0x8(%rsp)       # imm = 0xD96
movw $0xd97, (%rsp)          # imm = 0xD97
leaq 0x20(%rsp), %rdi
movl $0xa, %esi
callq 0x609d
cmpl $0x2, %eax
jne 0x285b
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
movl %eax, 0xc(%rsp)
movw $0xd96, 0x8(%rsp)       # imm = 0xD96
movl %ecx, 0x4(%rsp)
movw $0xd97, (%rsp)          # imm = 0xD97
movl $0x0, %r13d
movl $0x0, %eax
movq %rax, 0x10(%rsp)
cmpl $0x2, %ebp
jl 0x29fa
movq $0x0, 0x10(%rsp)
movl $0x1, %r12d
xorl %ebx, %ebx
movl $0x0, 0x1c(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movslq %r12d, %rax
movq (%r15,%rax,8), %rax
cmpb $0x2d, (%rax)
jne 0x2c14
cmpb $0x76, 0x1(%rax)
jne 0x28b3
cmpb $0x0, 0x2(%rax)
je 0x293a
cmpb $0x62, 0x1(%rax)
jne 0x28c3
cmpb $0x0, 0x2(%rax)
je 0x2946
cmpb $0x68, 0x1(%rax)
jne 0x28cf
cmpb $0x0, 0x2(%rax)
je 0x294e
cmpb $0x61, 0x1(%rax)
jne 0x28df
cmpb $0x0, 0x2(%rax)
je 0x2968
cmpb $0x70, 0x1(%rax)
jne 0x28ef
cmpb $0x0, 0x2(%rax)
je 0x2985
cmpb $0x6f, 0x1(%rax)
jne 0x28ff
cmpb $0x0, 0x2(%rax)
je 0x29aa
cmpb $0x6d, 0x1(%rax)
jne 0x2c14
cmpb $0x0, 0x2(%rax)
jne 0x2c14
incl %r12d
cmpl %ebp, %r12d
jge 0x2c14
movslq %r12d, %rax
movq (%r15,%rax,8), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2220
movzwl %ax, %ebx
jmp 0x29d0
movb $0x1, %al
movq %rax, 0x10(%rsp)
jmp 0x29d0
movb $0x1, %r13b
jmp 0x29d0
incl %r12d
cmpl %ebp, %r12d
jge 0x2c14
movslq %r12d, %rax
movq (%r15,%rax,8), %rdi
leaq 0x8(%rsp), %rsi
jmp 0x297e
incl %r12d
cmpl %ebp, %r12d
jge 0x2c14
movslq %r12d, %rax
movq (%r15,%rax,8), %rdi
movq %rsp, %rsi
callq 0x49d4
jmp 0x29d0
incl %r12d
cmpl %ebp, %r12d
jge 0x2c14
movslq %r12d, %rax
movq (%r15,%rax,8), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2220
movzwl %ax, %r14d
jmp 0x29d0
incl %r12d
cmpl %ebp, %r12d
jge 0x2c14
movslq %r12d, %rax
movq (%r15,%rax,8), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2220
movzwl %ax, %eax
movl %eax, 0x1c(%rsp)
incl %r12d
cmpl %ebp, %r12d
jl 0x2893
testl %r14d, %r14d
je 0x29e7
movw %r14w, 0x8(%rsp)
movl 0x1c(%rsp), %eax
testl %eax, %eax
je 0x29f3
movw %ax, (%rsp)
movq 0x95c6(%rip), %r14      # 0xbfc0
cmpl $0x0, 0xc(%rsp)
je 0x2a08
cmpl $0x0, 0x4(%rsp)
jne 0x2a57
leaq 0x584c(%rip), %rsi      # 0x825b
movl $0x3f, %edx
movq %r14, %rdi
callq 0x2200
leaq 0x5878(%rip), %rsi      # 0x829b
movl $0x2a, %edx
movq %r14, %rdi
callq 0x2200
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
movzwl 0x8(%rsp), %eax
movzwl (%rsp), %ecx
cmpw %cx, %ax
jne 0x2a6c
leal 0x1(%rax), %ecx
movw %cx, (%rsp)
testb $0x1, 0x10(%rsp)
je 0x2afa
movq 0x9542(%rip), %r15      # 0xbfc0
leaq 0x5841(%rip), %rsi      # 0x82c6
movl $0x1a, %edx
movq %r15, %rdi
callq 0x2200
leaq 0x8(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x3877
movq %rax, %r15
leaq 0x5835(%rip), %rsi      # 0x82e1
movl $0x10, %edx
movq %rax, %rdi
callq 0x2200
movq %rsp, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3877
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
movzwl (%r14), %eax
movzwl (%r12), %ecx
cmpl $0x0, 0xc(%rsp)
je 0x2c23
testw %ax, %ax
je 0x2c23
testw %cx, %cx
je 0x2c23
cmpl $0x0, 0x4(%rsp)
jne 0x2b60
movq 0x94cb(%rip), %r14      # 0xbff0
leaq 0x57d7(%rip), %rsi      # 0x8303
movl $0x34, %edx
movq %r14, %rdi
callq 0x2200
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
testb $0x1, %r13b
je 0x2b79
callq 0x20a0
testl %eax, %eax
js 0x2c48
jne 0x2c00
movzbl 0x10(%rsp), %ebp
andl $0x1, %ebp
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %rsp, %rdx
movl %ebx, %ecx
movl %ebp, %r8d
callq 0x5472
testb %al, %al
je 0x2c00
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0x1, %r14d
leaq 0x50(%rsp), %rbx
movl %r14d, %eax
imulq $0x10624dd3, %rax, %rax # imm = 0x10624DD3
shrq $0x26, %rax
imull $0x3e8, %eax, %r15d     # imm = 0x3E8
movq %rbx, %rdi
movl %ebp, %esi
callq 0x571c
movl %eax, %r12d
addl %r13d, %r15d
sete %al
andb 0x10(%rsp), %al
cmpb $0x1, %al
jne 0x2bf5
movl $0x1, %edx
movq 0x93d7(%rip), %rdi      # 0xbfc0
leaq 0x575d(%rip), %rsi      # 0x834d
callq 0x2200
decl %r13d
incl %r14d
testb %r12b, %r12b
jne 0x2bad
xorl %eax, %eax
addq $0x2f58, %rsp           # imm = 0x2F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x244c
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2210
movq 0x93c6(%rip), %rdi      # 0xbff0
leaq 0x56c1(%rip), %rsi      # 0x82f2
callq 0x21e0
movq %rax, %rdi
callq 0x20e0
movl $0x1, %edi
callq 0x2210
movq 0x93a1(%rip), %rdi      # 0xbff0
leaq 0x56e2(%rip), %rsi      # 0x8338
callq 0x21e0
movq %rax, %rdi
callq 0x20e0
jmp 0x2c19
nopl (%rax)