// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x10b6(%rip), %rdx      # 0x3798
callq 0x1120
movl %eax, -0x18(%rbp)
cmpl $-0x1, %eax
je 0x278d
movl -0x18(%rbp), %eax
addl $-0x63, %eax
movl %eax, %ecx
movq %rcx, -0x40(%rbp)
subl $0x11, %eax
ja 0x2769
movq -0x40(%rbp), %rax
leaq 0xd21(%rip), %rcx       # 0x3430
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x28c1(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
callq 0x1130
movl %eax, -0x14(%rbp)
jmp 0x2788
callq 0x2630
movl $0x0, -0x4(%rbp)
jmp 0x29eb
movq 0x289c(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
callq 0x1130
movl %eax, 0x2986(%rip)      # 0x50d8
jmp 0x2788
movl $0x1, 0x2992(%rip)      # 0x50f0
jmp 0x2788
movl $0x1, -0x2c(%rbp)
jmp 0x2788
leaq 0x1030(%rip), %rdi      # 0x37a0
movb $0x0, %al
callq 0x1070
callq 0x2630
movl $0x1, -0x4(%rbp)
jmp 0x29eb
jmp 0x26d4
movl -0x14(%rbp), %edi
callq 0x1440
xorl %eax, %eax
movl %eax, %edi
callq 0x10d0
movl %eax, %edi
callq 0x1040
callq 0x21a0
movslq -0x14(%rbp), %rdi
movl $0x8, %esi
callq 0x10a0
movq %rax, -0x28(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2854
cmpl $0x0, -0x2c(%rbp)
je 0x27fd
movq -0x28(%rbp), %rdi
movslq -0x1c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x211(%rip), %rdx       # 0x2a00
leaq -0x1c(%rbp), %rcx
callq 0x1100
movl %eax, -0x20(%rbp)
jmp 0x2822
movq -0x28(%rbp), %rdi
movslq -0x1c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %ecx
leaq 0x2b9(%rip), %rdx       # 0x2ad0
movq %rcx, %rsi
callq 0x1100
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x2844
movl -0x20(%rbp), %esi
leaq 0xf7e(%rip), %rdi       # 0x37b0
movb $0x0, %al
callq 0x1070
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x29eb
jmp 0x2846
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x27c3
movl 0x287e(%rip), %edi      # 0x50d8
callq 0x1180
movl $0x1, 0x288b(%rip)      # 0x50f4
leaq 0xf5b(%rip), %rdi       # 0x37cb
movb $0x0, %al
callq 0x1070
movl -0x14(%rbp), %esi
leaq 0xf6c(%rip), %rdi       # 0x37ed
movb $0x0, %al
callq 0x1070
leaq 0xf3c(%rip), %rdi       # 0x37cb
movb $0x0, %al
callq 0x1070
cmpl $0x0, 0x2853(%rip)      # 0x50f0
je 0x2902
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2900
movq -0x28(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1170
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0xf33(%rip), %rdi       # 0x3808
movb $0x0, %al
callq 0x1070
cmpl $0x0, -0x30(%rbp)
je 0x28f3
movl -0x30(%rbp), %esi
leaq 0xf41(%rip), %rdi       # 0x382d
movb $0x0, %al
callq 0x1070
jmp 0x28f5
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x28a6
jmp 0x2902
leaq 0xf46(%rip), %rdi       # 0x384f
movb $0x0, %al
callq 0x1070
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x29dd
movq -0x28(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
callq 0x1110
movq -0x48(%rbp), %rsi
movq %rax, %rdx
leaq 0xf17(%rip), %rdi       # 0x385d
movb $0x0, %al
callq 0x1070
movq -0x28(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1150
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x296d
jmp 0x298c
leaq 0xf1a(%rip), %rdi       # 0x388e
leaq 0xf1b(%rip), %rsi       # 0x3896
movl $0x137, %edx            # imm = 0x137
leaq 0xf16(%rip), %rcx       # 0x389d
callq 0x1080
movq -0x28(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
callq 0x1110
movq -0x50(%rbp), %rsi
movq %rax, %rdx
leaq 0xf05(%rip), %rdi       # 0x38b4
movb $0x0, %al
callq 0x1070
cmpl $0x0, -0x34(%rbp)
je 0x29cd
movl -0x34(%rbp), %esi
leaq 0xf21(%rip), %rdi       # 0x38e7
movb $0x0, %al
callq 0x1070
jmp 0x29cf
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2917
movb $0x0, %al
callq 0x2160
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)