// File: /workspace/github_c_makefile_star2_O1/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 0x13d3(%rip), %r15      # 0x3418
movq 0x2f94(%rip), %r13      # 0x4fe0
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1120
cmpl $0x6b, %eax
jg 0x2075
cmpl $-0x1, %eax
je 0x20bf
cmpl $0x63, %eax
jne 0x20aa
movq (%r13), %rdi
callq 0x1130
movl %eax, %ebx
jmp 0x204c
cmpl $0x6c, %eax
je 0x208b
cmpl $0x71, %eax
jne 0x209c
movl $0x1, 0x3067(%rip)      # 0x50f0
jmp 0x204c
movq (%r13), %rdi
callq 0x1130
movl %eax, 0x303e(%rip)      # 0x50d8
jmp 0x204c
cmpl $0x74, %eax
jne 0x2269
xorl %r12d, %r12d
jmp 0x204c
cmpl $0x68, %eax
jne 0x2269
callq 0x1fc4
xorl %ebp, %ebp
jmp 0x2291
movl %ebx, %edi
callq 0x138c
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x10d0
movl %eax, %edi
callq 0x1040
callq 0x1c7c
movslq %ebx, %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x10a0
movq %rax, %r14
movl $0x0, 0x4(%rsp)
testl %r15d, %r15d
jle 0x2144
leaq 0x1bf(%rip), %r15       # 0x22c1
leaq 0x4(%rsp), %r13
movslq %ebp, %rax
leaq (%r14,%rax,8), %rdi
xorl %esi, %esi
testb $0x1, %r12b
je 0x2121
leaq 0x263(%rip), %rdx       # 0x2380
xorl %ecx, %ecx
jmp 0x2127
movq %r15, %rdx
movq %r13, %rcx
callq 0x1100
movl %eax, %ebp
testl %eax, %eax
jne 0x2281
movl 0x4(%rsp), %ebp
incl %ebp
movl %ebp, 0x4(%rsp)
cmpl %ebx, %ebp
jl 0x2107
movl 0x2f8e(%rip), %edi      # 0x50d8
callq 0x1180
movl $0x1, 0x2f9b(%rip)      # 0x50f4
leaq 0x170b(%rip), %r15      # 0x386b
movq %r15, %rdi
callq 0x1070
leaq 0x12cc(%rip), %rdi      # 0x343b
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
movq %r15, %rdi
callq 0x1070
cmpl $0x0, 0x2f69(%rip)      # 0x50f0
je 0x21e3
movl $0x0, 0x4(%rsp)
testl %ebx, %ebx
jle 0x21e3
leaq 0x12ba(%rip), %r15      # 0x3456
leaq 0x12d8(%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 0x21d5
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 0x21a5
leaq 0x16a2(%rip), %rdi      # 0x388c
callq 0x1070
movl $0x0, 0x4(%rsp)
testl %ebx, %ebx
jle 0x225e
callq 0x1110
movq %rax, %r15
leaq 0x1293(%rip), %r12      # 0x349d
leaq 0x12e3(%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 0x22a2
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 0x2213
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x1c4c
jmp 0x2291
leaq 0x1629(%rip), %rdi      # 0x3899
callq 0x1070
callq 0x1fc4
movl $0x1, %ebp
jmp 0x2291
leaq 0x1198(%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 0x1225(%rip), %rdi      # 0x34ce
leaq 0x1226(%rip), %rsi      # 0x34d6
leaq 0x1226(%rip), %rcx      # 0x34dd
movl $0x137, %edx            # imm = 0x137
callq 0x1090