// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %rbx
movb $0x1, %r12b
leaq 0x1427(%rip), %r15      # 0x3418
movq 0x3fe8(%rip), %r13      # 0x5fe0
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1120
cmpl $0x6c, %eax
je 0x201b
cmpl $0x71, %eax
jne 0x202c
movl $0x1, 0x40d7(%rip)      # 0x60f0
jmp 0x1ff8
movq (%r13), %rdi
callq 0x1130
movl %eax, 0x40ae(%rip)      # 0x60d8
jmp 0x1ff8
cmpl $-0x1, %eax
je 0x2062
cmpl $0x74, %eax
je 0x2051
cmpl $0x68, %eax
je 0x2056
cmpl $0x63, %eax
jne 0x20d7
movq (%r13), %rdi
callq 0x1130
movl %eax, %ebx
jmp 0x1ff8
xorl %r12d, %r12d
jmp 0x1ff8
callq 0x1f72
xorl %ebp, %ebp
jmp 0x2214
movl %ebx, %edi
callq 0x137e
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x10d0
movl %eax, %edi
callq 0x1040
callq 0x1c60
movslq %ebx, %rdi
pushq $0x8
popq %rsi
callq 0x10a0
movq %rax, %r14
leaq 0x1b1(%rip), %r15       # 0x2244
leaq 0x4(%rsp), %r13
movl %ebp, 0x4(%rsp)
cmpl %ebx, %ebp
jge 0x20f0
movslq %ebp, %rax
leaq (%r14,%rax,8), %rdi
xorl %esi, %esi
testb $0x1, %r12b
je 0x20ba
leaq 0x23d(%rip), %rdx       # 0x22f3
xorl %ecx, %ecx
jmp 0x20c0
movq %r15, %rdx
movq %r13, %rcx
callq 0x1100
movl %eax, %ebp
testl %eax, %eax
jne 0x2204
movl 0x4(%rsp), %ebp
incl %ebp
jmp 0x2098
leaq 0x17bb(%rip), %rdi      # 0x3899
callq 0x1070
callq 0x1f72
pushq $0x1
popq %rbp
jmp 0x2214
movl 0x3fe2(%rip), %edi      # 0x60d8
callq 0x1180
movl $0x1, 0x3fef(%rip)      # 0x60f4
leaq 0x175f(%rip), %r15      # 0x386b
movq %r15, %rdi
callq 0x1070
leaq 0x1320(%rip), %rdi      # 0x343b
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
movq %r15, %rdi
callq 0x1070
cmpl $0x0, 0x3fbd(%rip)      # 0x60f0
je 0x2185
leaq 0x131a(%rip), %r15      # 0x3456
leaq 0x1338(%rip), %r12      # 0x347b
xorl %eax, %eax
movl %eax, 0x4(%rsp)
cmpl %ebx, %eax
jge 0x2185
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 0x217d
movq %r12, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x1080
movl 0x4(%rsp), %eax
incl %eax
jmp 0x2145
leaq 0x1700(%rip), %rdi      # 0x388c
callq 0x1070
leaq 0x1305(%rip), %r15      # 0x349d
leaq 0x1355(%rip), %r12      # 0x34f4
xorl %eax, %eax
movl %eax, 0x4(%rsp)
cmpl %ebx, %eax
jge 0x21f9
cltq
movq (%r14,%rax,8), %rbp
callq 0x1110
movq %rax, %r13
movq %r15, %rdi
movq %rbp, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1080
movslq 0x4(%rsp), %rax
movq (%r14,%rax,8), %rdi
xorl %esi, %esi
callq 0x1150
testl %eax, %eax
jne 0x2225
movslq 0x4(%rsp), %rax
movq (%r14,%rax,8), %rsi
movq %r12, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1080
movl 0x4(%rsp), %eax
incl %eax
jmp 0x21a1
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x1c31
jmp 0x2214
leaq 0x1215(%rip), %rdi      # 0x3420
movl %ebp, %esi
xorl %eax, %eax
callq 0x1080
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12a2(%rip), %rdi      # 0x34ce
leaq 0x12a3(%rip), %rsi      # 0x34d6
leaq 0x12a3(%rip), %rcx      # 0x34dd
movl $0x137, %edx            # imm = 0x137
callq 0x1090