// File: /workspace/github_c_makefile_star2_O3/stevenmherring[P]DNS/main.c
// Function: main
int main(int argc, char ** argv) {
  int numthreads = 1; // default to 1
  int c, i, rv;
  pthread_t *tinfo;
  int stress_squatting = 0;

  // Read options from command line:
  //   # clients from command line, as well as seed file
  //   Simulation length
  //   Block if a name is already taken ("Squat")
  //   Stress test "squatting"
  while ((c = getopt (argc, argv, "c:hl:qt")) != -1) {
    switch (c) {
      case 'c':
      numthreads = atoi(optarg);
      break;
      case 'h':
      help();
      return 0;
      case 'l':
      simulation_length = atoi(optarg);
      break;
      case 'q':
      allow_squatting = 1;
      break;
      case 't':
      stress_squatting = 1;
      break;
      default:
      printf ("Unknown option\n");
      help();
      return 1;
    }
  }


  // Create initial data structure, populate with initial entries
  // Note: Each variant of the tree has a different init function, statically compiled in
  init(numthreads);
  srandom(time(0));
  // Run the self-tests if we are in debug mode 
  #ifdef DEBUG
  self_tests();
  #endif

  // Launch client threads
  tinfo = calloc(numthreads, sizeof(pthread_t));
  for (i = 0; i < numthreads; i++) {

    if (stress_squatting) {
      rv = pthread_create(&tinfo[i], NULL,
        &squatter_stress, &i);
      } else {
        rv = pthread_create(&tinfo[i], NULL,
          &client, NULL);
        }
        if (rv != 0) {
          printf ("Thread creation failed %d\n", rv);
          return rv;
        }
      }

      // After the simulation is done, shut it down
      sleep (simulation_length);
      finished = 1;
      #ifdef DEBUG
      printf("\n------------------------------\n\n");
      printf("You have %d worker threads", numthreads);
      printf("\n------------------------------\n\n");
      #endif
      // Wait for all clients to exit.  If we are allowing blocking,
      // cancel the threads, since they may hang forever
      if (allow_squatting) {
        for (i = 0; i < numthreads; i++) {
          int rv = pthread_cancel(tinfo[i]);
          //pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
          #ifdef DEBUG
          printf("Thread: %ld queued for cancellation\n", tinfo[i]);
          #endif
          if (rv != 0)
          printf ("Uh oh.  pthread_cancel failed %d\n", rv);
        }
      }
      printf("All canceled\n");
      for (i = 0; i < numthreads; i++) {
        #ifdef DEBUG
        printf("Thread: %ld attempting to join with Thread: %ld\n", tinfo[i], pthread_self());
        #endif
        int rv = pthread_join(tinfo[i], NULL);
        assert(rv == 0);
        #ifdef DEBUG
        printf("Thread: %ld: successfully joined with Thread: %ld\n",tinfo[i], pthread_self());
        #endif
        if (rv != 0)
        printf ("Uh oh.  pthread_join failed %d\n", rv);
      }

      #ifdef DEBUG
      /* Print the final tree for fun */
      print();
      #endif

      return 0;
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movl %edi, %ebp
movl $0x1, %ebx
movb $0x1, %r12b
leaq 0x13a7(%rip), %r15      # 0x3418
movq 0x2f68(%rip), %r13      # 0x4fe0
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1120
cmpl $0x6b, %eax
jg 0x20a1
cmpl $-0x1, %eax
je 0x20eb
cmpl $0x63, %eax
jne 0x20d6
movq (%r13), %rdi
callq 0x1130
movl %eax, %ebx
jmp 0x2078
cmpl $0x6c, %eax
je 0x20b7
cmpl $0x71, %eax
jne 0x20c8
movl $0x1, 0x303b(%rip)      # 0x50f0
jmp 0x2078
movq (%r13), %rdi
callq 0x1130
movl %eax, 0x3012(%rip)      # 0x50d8
jmp 0x2078
cmpl $0x74, %eax
jne 0x2294
xorl %r12d, %r12d
jmp 0x2078
cmpl $0x68, %eax
jne 0x2294
callq 0x1ff0
xorl %ebp, %ebp
jmp 0x22db
movl %ebx, %edi
callq 0x138a
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x10d0
movl %eax, %edi
callq 0x1040
callq 0x1ca8
movslq %ebx, %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x10a0
movq %rax, %r14
movl $0x0, 0x4(%rsp)
testl %r15d, %r15d
jle 0x2170
leaq 0x1dd(%rip), %r15       # 0x230b
leaq 0x4(%rsp), %r13
movslq %ebp, %rax
leaq (%r14,%rax,8), %rdi
xorl %esi, %esi
testb $0x1, %r12b
je 0x214d
leaq 0x281(%rip), %rdx       # 0x23ca
xorl %ecx, %ecx
jmp 0x2153
movq %r15, %rdx
movq %r13, %rcx
callq 0x1100
movl %eax, %ebp
testl %eax, %eax
jne 0x22ac
movl 0x4(%rsp), %ebp
incl %ebp
movl %ebp, 0x4(%rsp)
cmpl %ebx, %ebp
jl 0x2133
movl 0x2f62(%rip), %edi      # 0x50d8
callq 0x1180
movl $0x1, 0x2f6f(%rip)      # 0x50f4
leaq 0x16df(%rip), %r15      # 0x386b
movq %r15, %rdi
callq 0x1070
leaq 0x12a0(%rip), %rdi      # 0x343b
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
movq %r15, %rdi
callq 0x1070
cmpl $0x0, 0x2f3d(%rip)      # 0x50f0
je 0x2213
movl $0x0, 0x4(%rsp)
testl %ebx, %ebx
jle 0x22be
leaq 0x128a(%rip), %r15      # 0x3456
leaq 0x12a8(%rip), %r12      # 0x347b
xorl %eax, %eax
cltq
movq (%r14,%rax,8), %rdi
callq 0x1170
movl %eax, %ebp
movslq 0x4(%rsp), %rax
movq (%r14,%rax,8), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x1080
testl %ebp, %ebp
je 0x2205
movq %r12, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x1080
movl 0x4(%rsp), %eax
incl %eax
movl %eax, 0x4(%rsp)
cmpl %ebx, %eax
jl 0x21d5
leaq 0x1672(%rip), %rdi      # 0x388c
callq 0x1070
movl $0x0, 0x4(%rsp)
testl %ebx, %ebx
jle 0x22d2
callq 0x1110
movq %rax, %r15
leaq 0x125f(%rip), %r12      # 0x349d
leaq 0x12af(%rip), %r13      # 0x34f4
xorl %eax, %eax
cltq
movq (%r14,%rax,8), %rsi
movq %r12, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1080
movslq 0x4(%rsp), %rax
movq (%r14,%rax,8), %rdi
xorl %esi, %esi
callq 0x1150
testl %eax, %eax
jne 0x22ec
movslq 0x4(%rsp), %rax
movq (%r14,%rax,8), %rsi
movq %r13, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1080
movl 0x4(%rsp), %eax
incl %eax
movl %eax, 0x4(%rsp)
cmpl %ebx, %eax
jl 0x2247
jmp 0x22d2
leaq 0x15fe(%rip), %rdi      # 0x3899
callq 0x1070
callq 0x1ff0
movl $0x1, %ebp
jmp 0x22db
leaq 0x116d(%rip), %rdi      # 0x3420
movl %ebp, %esi
xorl %eax, %eax
callq 0x1080
jmp 0x22db
leaq 0x15c7(%rip), %rdi      # 0x388c
callq 0x1070
movl $0x0, 0x4(%rsp)
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x1c77
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11db(%rip), %rdi      # 0x34ce
leaq 0x11dc(%rip), %rsi      # 0x34d6
leaq 0x11dc(%rip), %rcx      # 0x34dd
movl $0x137, %edx            # imm = 0x137
callq 0x1090