// File: dekanfrus[P]pThreads/osproj2a.c
// Function: main
int main( int argc, char *argv[] )
{
  /* local variables */
  int ii;
  int retval;
  pthread_t p_threads[MAX_THREADS];
  pthread_attr_t attr;
  double computed_pi;

  /* initialize local variables */
  retval = 0;

  pthread_attr_init( &attr );
  pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );

  /* parse command line arguments into sample points and number of threads */
  /* there is no error checking here!!!!! */
  sample_points = atoi(argv[1]);
  num_threads = atoi(argv[2]);

  /* uncomment this block if you want interactive input!!!! */
  /* if so...comment out the two statements above */
    
  /*printf( "Enter number of sample points: " );
  scanf( "%d", &sample_points );
  printf( "Enter number of threads: " );
  scanf( "%d%", &num_threads );*/
  
  // Store the current time in the "start" variable
  start = clock();
  
  total_hits = 0;
  sample_points_per_thread = sample_points / num_threads;

  // Create the number of threads as requested by the user from the command line
  // Each thread will execute the compute_pi function
  for( ii=0; ii<num_threads; ii++ )
    {
	  
      hits[ii] = ii;
      pthread_create( &p_threads[ ii ], &attr, compute_pi, (void *) &hits[ii] );
	  
    }

  // Join all threads
  for( ii=0; ii<num_threads; ii++ )
    {
       pthread_join( p_threads[ ii ], NULL );
       total_hits += hits[ ii ];
	   
    }
  
   computed_pi = 4.0 * (double) total_hits / ((double) (sample_points));

   // Store the final clock time
   end = clock();

   printf( "Computed PI = %lf\n", computed_pi );

   // Calculate the total execution time and then print that to the screen
   total = (double)(end - start) / CLOCKS_PER_SEC;

   printf("Execution Time = %lf\n", total);

  /* return to calling environment */
  return( retval );
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1060, %rsp           # imm = 0x1060
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
leaq -0x1058(%rbp), %rdi
callq 0x1080
leaq -0x1058(%rbp), %rdi
xorl %esi, %esi
callq 0x1040
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1090
movl %eax, 0x2e73(%rip)      # 0x4080
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1090
movl %eax, 0x3674(%rip)      # 0x4894
callq 0x1050
movq %rax, 0x3674(%rip)      # 0x48a0
movl $0x0, 0x2e4e(%rip)      # 0x4084
movl 0x2e44(%rip), %eax      # 0x4080
cltd
idivl 0x3651(%rip)            # 0x4894
movl %eax, 0x3647(%rip)      # 0x4890
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl 0x363b(%rip), %eax      # 0x4894
jge 0x12ae
movl -0x14(%rbp), %edx
movslq -0x14(%rbp), %rcx
leaq 0x2e27(%rip), %rax      # 0x4090
movl %edx, (%rax,%rcx,4)
movslq -0x14(%rbp), %rax
leaq -0x1020(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
movslq -0x14(%rbp), %rax
leaq 0x2e07(%rip), %rcx      # 0x4090
shlq $0x2, %rax
addq %rax, %rcx
leaq -0x1058(%rbp), %rsi
leaq 0xf2(%rip), %rdx        # 0x1390
callq 0x10b0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1250
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl 0x35d6(%rip), %eax      # 0x4894
jge 0x12fa
movslq -0x14(%rbp), %rax
movq -0x1020(%rbp,%rax,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1060
movslq -0x14(%rbp), %rcx
leaq 0x2db0(%rip), %rax      # 0x4090
movl (%rax,%rcx,4), %eax
addl 0x2d9b(%rip), %eax      # 0x4084
movl %eax, 0x2d95(%rip)      # 0x4084
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x12b5
cvtsi2sdl 0x2d82(%rip), %xmm1 # 0x4084
movsd 0xd06(%rip), %xmm0      # 0x2010
mulsd %xmm1, %xmm0
cvtsi2sdl 0x2d6a(%rip), %xmm1 # 0x4080
divsd %xmm1, %xmm0
movsd %xmm0, -0x1060(%rbp)
callq 0x1050
movq %rax, 0x357a(%rip)      # 0x48a8
movsd -0x1060(%rbp), %xmm0
leaq 0xcf3(%rip), %rdi       # 0x2030
movb $0x1, %al
callq 0x1030
movq 0x355d(%rip), %rax      # 0x48a8
subq 0x354e(%rip), %rax      # 0x48a0
cvtsi2sd %rax, %xmm0
movsd 0xca9(%rip), %xmm1      # 0x2008
divsd %xmm1, %xmm0
movsd %xmm0, 0x352d(%rip)     # 0x4898
movsd 0x3525(%rip), %xmm0     # 0x4898
leaq 0xcc9(%rip), %rdi       # 0x2043
movb $0x1, %al
callq 0x1030
movl -0x18(%rbp), %eax
addq $0x1060, %rsp           # imm = 0x1060
popq %rbp
retq
nopl (%rax)