// File: /workspace/github_c_makefile_star2_O2/mvermeulen[P]wspy/wspy.c
// Function: main
int main(int argc,char *const argv[],char *const envp[]){
  int i;
  int status;
  struct rusage rusage;
  struct counter_group *cgroup;
  outfile = stdout;
  num_procs = get_nprocs();
  clocks_per_second = sysconf(_SC_CLK_TCK);
  
  if (parse_options(argc,argv)){
      fatal("usage: %s -[abcistv][-o <file>] <cmd><args>...\n"
	    "\t--per-core or -a          - metrics per core\n"
	    "\t--rusage or -r            - show getrusage(2) information\n"
	    "\t--tree <file>             - create CSV of processes\n"
	    "\t--tree-cmdline            - record full command lines\n"
	    "\t--tree-vmsize             - virtual memory size\n"
	    "\t-o <file>                 - send output to file\n"
	    "\t--csv                     - create csv output\n"
	    "\t--interval <sec>          - read every <sec> seconds\n"
	    "\t--verbose or -v           - print verbose information\n"
	    "\n"
	    "\t--software or -s          - software counters\n"
	    "\t--ipc or i                - IPC counters\n"
	    "\t--branch or -b            - branch counters\n"
	    "\t--dcache                  - L1 dcache counters\n"
	    "\t--icache                  - L1 icache counters\n"
	    "\t--cache2 or -c            - L2 cache counters\n"
	    "\t--cache3                  - L3 cache counters\n"
	    "\t--memory                  - memory counters\n"
	    "\t--opcache                 - opcache counters\n"
	    "\t--tlb                     - TLB counters\n"
	    "\t--topdown or -t           - topdown counters, level 1\n"
	    "\t--topdown2                - topdown counters, level 2\n"
	    "\t--topdown-frontend        - topdown related to fe\n"
	    "\t--topdown-optlb           - topdown related to opcache, dtlb\n"
	    ,argv[0]);
  }

  if (inventory_cpu() != 0){
    fatal("unable to query CPU information\n");
  }

  check_nmi_watchdog();

#if AMDGPU
  gpu_info_initialize();
#endif

  // parse the event tables
  setup_raw_events();

  // set up either system-wide or core-specific counters
  if (aflag){
    for (i=0;i<cpu_info->num_cores;i++){
      if (cpu_info->coreinfo[i].is_available &&
	  ((cpu_info->coreinfo[i].vendor == CORE_AMD_ZEN)||(cpu_info->coreinfo[i].vendor == CORE_AMD_ZEN5)||(cpu_info->coreinfo[i].vendor == CORE_INTEL_CORE))){
	setup_counter_groups(&cpu_info->coreinfo[i].core_specific_counters);
      }
    }
  } else {
    setup_counter_groups(&cpu_info->systemwide_counters);
  }
  // software counters are only system-wide
  if (counter_mask & COUNTER_SOFTWARE){
    if (cgroup = software_counter_group("software")){
      cgroup->next = cpu_info->systemwide_counters;
      cpu_info->systemwide_counters = cgroup;
    }
  }

  // set up core-specific and system-wide counters
  for (i=0;i<cpu_info->num_cores;i++){
    if (cpu_info->coreinfo[i].core_specific_counters)
      setup_counters(cpu_info->coreinfo[i].core_specific_counters);
  }
  setup_counters(cpu_info->systemwide_counters);

  // start core-specific and system-wide counters
  for (i=0;i<cpu_info->num_cores;i++){
    if (cpu_info->coreinfo[i].core_specific_counters)
      start_counters(cpu_info->coreinfo[i].core_specific_counters);
  }
  start_counters(cpu_info->systemwide_counters);

  signal(SIGINT,SIG_IGN);

  // create CSV headers
  if (csvflag){
    if (interval){
      fprintf(outfile,"time,");
    }
    if (sflag) print_system(PRINT_CSV_HEADER);
    if (!interval && xflag) print_usage(NULL,PRINT_CSV_HEADER);
    print_metrics(cpu_info->systemwide_counters,PRINT_CSV_HEADER);
    fprintf(outfile,"\n");
  }

  // let the child start after two seconds
  sleep(2);

  if (sflag) read_system();

  clock_gettime(CLOCK_REALTIME,&start_time);
  if (launch_child(command_line_argc,command_line_argv,envp)){
    fatal("unable to launch %s\n",command_line_argv[0]);
  }

  if (interval){
    signal(SIGALRM,timer_callback);
    alarm(interval);
  }

  write(child_pipe[1],"start\n",6);
  if (treeflag){
    ptrace_setup(child_pid);
    fprintf(treefile,"0.000 %d root\n",child_pid);
    ptrace_loop();
    getrusage(RUSAGE_CHILDREN,&rusage);
  } else {
    // without ptrace, this process waits to complete
    wait4(child_pid,&status,0,&rusage);
  }

  is_still_running = 0;
  
  clock_gettime(CLOCK_REALTIME,&finish_time);

  read_system();

  // -----  
  // stop core-specific and system-wide counters
  for (i=0;i<cpu_info->num_cores;i++){
    if (cpu_info->coreinfo[i].core_specific_counters)
      read_counters(cpu_info->coreinfo[i].core_specific_counters,1);
  }
  read_counters(cpu_info->systemwide_counters,1);

  if (csvflag){
    if (interval){
      double elapsed;
  
      elapsed = finish_time.tv_sec + finish_time.tv_nsec / 1000000000.0 -
	start_time.tv_sec - start_time.tv_nsec / 1000000000.0;    
      fprintf(outfile,"%4.1f,",elapsed);
    }
    if (sflag) print_system(PRINT_CSV);
    if (xflag && !interval) print_usage(&rusage,PRINT_CSV);
    print_metrics(cpu_info->systemwide_counters,PRINT_CSV);    
    fprintf(outfile,"\n");    
  } else {
    if (sflag) print_system(PRINT_NORMAL);
    if (xflag) print_usage(&rusage,PRINT_NORMAL);
    print_metrics(cpu_info->systemwide_counters,PRINT_NORMAL);
  }

  for (i=0;i<cpu_info->num_cores;i++){
    if (cpu_info->coreinfo[i].core_specific_counters){
      if (csvflag){
	print_usage(&rusage,PRINT_CSV);
      } else {
	fprintf(outfile,"##### core %2d #######################\n",i);
      }
      print_metrics(cpu_info->coreinfo[i].core_specific_counters,csvflag?PRINT_CSV:PRINT_NORMAL);
      if (csvflag) fprintf(outfile,"\n");
    }
  }

  if (oflag) fclose(outfile);
  // -----

#if AMDGPU
  gpu_info_finalize();
#endif
  
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movl %edi, %ebp
movq 0xc75b(%rip), %rax      # 0xffc0
movq (%rax), %rax
movq %rax, 0xd899(%rip)      # 0x11108
callq 0x3350
movl %eax, 0xd8a2(%rip)      # 0x1111c
pushq $0x2
popq %rdi
callq 0x3290
movl %eax, 0xd898(%rip)      # 0x11120
movl %ebp, %edi
movq %r14, %rsi
callq 0x346c
testl %eax, %eax
je 0x38a7
movq (%r14), %rsi
leaq 0x6b00(%rip), %rdi      # 0xa3a0
xorl %eax, %eax
callq 0x8f2f
callq 0x8ac0
testl %eax, %eax
je 0x38be
leaq 0x6f95(%rip), %rdi      # 0xa84c
xorl %eax, %eax
callq 0x8f2f
callq 0x5493
callq 0x4030
cmpl $0x0, 0xd809(%rip)      # 0x110d8
leaq 0xe89a(%rip), %r13      # 0x12170
movq (%r13), %rdi
je 0x3921
pushq $0x48
popq %r14
xorl %r15d, %r15d
pushq $0x4c
popq %rbp
movl 0xc(%rdi), %eax
cmpq %rax, %r15
jae 0x392a
movq 0x18(%rdi), %rax
testb $0x1, -0x44(%rax,%r14)
je 0x3918
movl -0x48(%rax,%r14), %ecx
cmpl $0x6, %ecx
ja 0x3918
btl %ecx, %ebp
jae 0x3918
addq %r14, %rax
movq %rax, %rdi
callq 0x4e86
movq (%r13), %rdi
incq %r15
addq $0x60, %r14
jmp 0x38e6
addq $0x20, %rdi
callq 0x4e86
testb $-0x80, 0xc8a4(%rip)    # 0x101d5
jne 0x3939
movq (%r13), %rcx
jmp 0x395a
leaq 0x6930(%rip), %rdi      # 0xa270
callq 0x4b56
movq (%r13), %rcx
testq %rax, %rax
je 0x395a
movq 0x20(%rcx), %rdx
movq %rdx, 0x20(%rax)
movq %rax, 0x20(%rcx)
pushq $0x48
popq %r14
xorl %r15d, %r15d
movl 0xc(%rcx), %eax
cmpq %rax, %r15
jae 0x3988
movq 0x18(%rcx), %rax
movq (%rax,%r14), %rdi
testq %rdi, %rdi
je 0x397f
callq 0x5079
movq (%r13), %rcx
incq %r15
addq $0x60, %r14
jmp 0x3961
movq 0x20(%rcx), %rdi
callq 0x5079
movq (%r13), %rax
pushq $0x48
popq %r14
xorl %r15d, %r15d
movl 0xc(%rax), %ecx
cmpq %rcx, %r15
jae 0x39c3
movq 0x18(%rax), %rcx
movq (%rcx,%r14), %rdi
testq %rdi, %rdi
je 0x39ba
callq 0x527f
movq (%r13), %rax
incq %r15
addq $0x60, %r14
jmp 0x399c
movq 0x20(%rax), %rdi
callq 0x527f
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
callq 0x31b0
cmpl $0x0, 0xd70a(%rip)      # 0x110e8
je 0x3a4e
cmpl $0x0, 0xd705(%rip)      # 0x110ec
je 0x3a02
movq 0xd718(%rip), %rcx      # 0x11108
leaq 0x828f(%rip), %rdi      # 0xbc86
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
cmpl $0x0, 0xd6d7(%rip)      # 0x110e0
je 0x3a13
pushq $0x2
popq %rdi
callq 0x9a3e
cmpl $0x0, 0xd6d2(%rip)      # 0x110ec
jne 0x3a2f
cmpl $0x0, 0xc7ad(%rip)      # 0x101d0
je 0x3a2f
pushq $0x2
popq %rsi
xorl %edi, %edi
callq 0x54df
movq (%r13), %rax
movq 0x20(%rax), %rdi
pushq $0x2
popq %rsi
callq 0x881a
movq 0xd6c2(%rip), %rsi      # 0x11108
pushq $0xa
popq %rdi
callq 0x3150
pushq $0x2
popq %rdi
callq 0x3310
cmpl $0x0, 0xd683(%rip)      # 0x110e0
je 0x3a64
callq 0x96d4
leaq 0xe6f5(%rip), %r14      # 0x12160
xorl %edi, %edi
movq %r14, %rsi
callq 0x3070
movl 0xd69d(%rip), %edi      # 0x11118
movq 0xd68e(%rip), %rsi      # 0x11110
movq %rbx, %rdx
callq 0x40d4
testl %eax, %eax
je 0x3aa6
movq 0xd67b(%rip), %rax      # 0x11110
movq (%rax), %rsi
leaq 0x6dce(%rip), %rdi      # 0xa86d
xorl %eax, %eax
callq 0x8f2f
cmpl $0x0, 0xd63f(%rip)      # 0x110ec
je 0x3ac9
leaq 0x4ece(%rip), %rsi      # 0x8984
pushq $0xe
popq %rdi
callq 0x31b0
movl 0xd628(%rip), %edi      # 0x110ec
callq 0x3120
leaq 0xd66c(%rip), %rax      # 0x1113c
movl 0x4(%rax), %edi
leaq 0x6da8(%rip), %rsi      # 0xa882
pushq $0x6
popq %rdx
callq 0x3080
cmpl $0x0, 0xd607(%rip)      # 0x110f0
leaq 0xd648(%rip), %rbx      # 0x11138
movl (%rbx), %edi
je 0x3b24
callq 0x4254
movq 0xd600(%rip), %rdi      # 0x11100
movl (%rbx), %edx
leaq 0x6d80(%rip), %rsi      # 0xa889
xorl %eax, %eax
callq 0x31c0
callq 0x43a6
pushq $-0x1
popq %rdi
leaq 0x8(%rsp), %rsi
callq 0x3270
jmp 0x3b35
leaq 0x4(%rsp), %rsi
leaq 0x8(%rsp), %rcx
xorl %edx, %edx
callq 0x3240
leaq 0xd608(%rip), %rax      # 0x11144
movl $0x0, (%rax)
leaq 0xe607(%rip), %rbx      # 0x12150
xorl %r15d, %r15d
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3070
callq 0x96d4
movq (%r13), %rax
pushq $0x48
popq %r12
pushq $0x1
popq %rbp
movl 0xc(%rax), %ecx
cmpq %rcx, %r15
jae 0x3b8f
movq 0x18(%rax), %rcx
movq (%rcx,%r12), %rdi
testq %rdi, %rdi
je 0x3b86
movl %ebp, %esi
callq 0x5342
movq (%r13), %rax
incq %r15
addq $0x60, %r12
jmp 0x3b66
movq 0x20(%rax), %rdi
pushq $0x1
popq %rsi
callq 0x5342
cmpl $0x0, 0xd546(%rip)      # 0x110e8
je 0x3c49
cmpl $0x0, 0xd53d(%rip)      # 0x110ec
je 0x3bf8
cvtsi2sdq (%rbx), %xmm1
cvtsi2sdq 0x8(%rbx), %xmm0
movsd 0x6624(%rip), %xmm2     # 0xa1e8
divsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsi2sdq (%r14), %xmm1
cvtsi2sdq 0x8(%r14), %xmm3
subsd %xmm1, %xmm0
divsd %xmm2, %xmm3
subsd %xmm3, %xmm0
movq 0xd51e(%rip), %rdi      # 0x11108
leaq 0x853e(%rip), %rsi      # 0xc12f
movb $0x1, %al
callq 0x31c0
cmpl $0x0, 0xd4e1(%rip)      # 0x110e0
je 0x3c09
pushq $0x1
popq %rdi
callq 0x9a3e
cmpl $0x0, 0xc5c0(%rip)      # 0x101d0
je 0x3c28
cmpl $0x0, 0xd4d3(%rip)      # 0x110ec
jne 0x3c28
leaq 0x8(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x54df
movq (%r13), %rax
movq 0x20(%rax), %rdi
pushq $0x1
popq %rsi
callq 0x881a
movq 0xd4c9(%rip), %rsi      # 0x11108
pushq $0xa
popq %rdi
callq 0x3150
jmp 0x3c7d
cmpl $0x0, 0xd490(%rip)      # 0x110e0
je 0x3c59
xorl %edi, %edi
callq 0x9a3e
cmpl $0x0, 0xc570(%rip)      # 0x101d0
je 0x3c6e
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x54df
movq (%r13), %rax
movq 0x20(%rax), %rdi
xorl %esi, %esi
callq 0x881a
pushq $0x48
popq %rbx
pushq $0xa
popq %rbp
leaq 0x8(%rsp), %r14
pushq $0x1
popq %r15
xorl %r12d, %r12d
movq (%r13), %rax
movl 0xc(%rax), %ecx
cmpq %rcx, %r12
jae 0x3d15
movq 0x18(%rax), %rax
cmpq $0x0, (%rax,%rbx)
je 0x3d09
cmpl $0x0, 0xd43b(%rip)      # 0x110e8
je 0x3cbc
movq %r14, %rdi
movl %r15d, %esi
callq 0x54df
jmp 0x3cd4
movq 0xd445(%rip), %rdi      # 0x11108
leaq 0x6bce(%rip), %rsi      # 0xa898
movl %r12d, %edx
xorl %eax, %eax
callq 0x31c0
movq (%r13), %rax
movq 0x18(%rax), %rax
movq (%rax,%rbx), %rdi
xorl %esi, %esi
cmpl $0x0, 0xd3ff(%rip)      # 0x110e8
setne %sil
callq 0x881a
cmpl $0x0, 0xd3ef(%rip)      # 0x110e8
je 0x3d09
movq 0xd406(%rip), %rsi      # 0x11108
movl %ebp, %edi
callq 0x3150
incq %r12
addq $0x60, %rbx
jmp 0x3c8f
cmpl $0x0, 0xd3c0(%rip)      # 0x110dc
je 0x3d2a
movq 0xd3e3(%rip), %rdi      # 0x11108
callq 0x30a0
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop