// File: /workspace/github_c_makefile_star2_O1/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 0xd755(%rip), %rax      # 0x10fc0
movq (%rax), %rax
movq %rax, 0xe883(%rip)      # 0x120f8
callq 0x3340
movl %eax, 0xe88c(%rip)      # 0x1210c
movl $0x2, %edi
callq 0x3280
movl %eax, 0xe880(%rip)      # 0x12110
movl %ebp, %edi
movq %r14, %rsi
callq 0x345c
testl %eax, %eax
je 0x38af
movq (%r14), %rsi
leaq 0x7af8(%rip), %rdi      # 0xb3a0
xorl %eax, %eax
callq 0x9161
callq 0x8cb4
testl %eax, %eax
je 0x38c6
leaq 0x7f8d(%rip), %rdi      # 0xb84c
xorl %eax, %eax
callq 0x9161
callq 0x5561
callq 0x4068
cmpl $0x0, 0xe7f1(%rip)      # 0x120c8
leaq 0xf882(%rip), %r12      # 0x13160
movq (%r12), %rdi
je 0x3930
cmpl $0x0, 0xc(%rdi)
je 0x3939
movl $0x48, %r14d
xorl %r15d, %r15d
movl $0x4c, %ebp
movq 0x18(%rdi), %rdi
testb $0x1, -0x44(%rdi,%r14)
je 0x391b
movl -0x48(%rdi,%r14), %eax
cmpl $0x6, %eax
ja 0x391b
btl %eax, %ebp
jae 0x391b
addq %r14, %rdi
callq 0x4eff
incq %r15
movq (%r12), %rdi
movl 0xc(%rdi), %eax
addq $0x60, %r14
cmpq %rax, %r15
jb 0x38f8
jmp 0x3939
addq $0x20, %rdi
callq 0x4eff
testb $-0x80, 0xd885(%rip)    # 0x111c5
je 0x3963
leaq 0x7927(%rip), %rdi      # 0xb270
callq 0x4bb2
testq %rax, %rax
je 0x3963
movq (%r12), %rcx
movq 0x20(%rcx), %rdx
movq %rdx, 0x20(%rax)
movq %rax, 0x20(%rcx)
movq (%r12), %rax
cmpl $0x0, 0xc(%rax)
je 0x399b
movl $0x48, %r14d
xorl %r15d, %r15d
movq 0x18(%rax), %rax
movq (%rax,%r14), %rdi
testq %rdi, %rdi
je 0x3988
callq 0x5101
incq %r15
movq (%r12), %rax
movl 0xc(%rax), %ecx
addq $0x60, %r14
cmpq %rcx, %r15
jb 0x3976
movq 0x20(%rax), %rdi
callq 0x5101
movq (%r12), %rax
cmpl $0x0, 0xc(%rax)
je 0x39dc
movl $0x48, %r14d
xorl %r15d, %r15d
movq 0x18(%rax), %rax
movq (%rax,%r14), %rdi
testq %rdi, %rdi
je 0x39c9
callq 0x532d
incq %r15
movq (%r12), %rax
movl 0xc(%rax), %ecx
addq $0x60, %r14
cmpq %rcx, %r15
jb 0x39b7
movq 0x20(%rax), %rdi
callq 0x532d
movl $0x1, %esi
movl $0x2, %edi
callq 0x31b0
cmpl $0x0, 0xe6dd(%rip)      # 0x120d8
je 0x3a77
cmpl $0x0, 0xe6d8(%rip)      # 0x120dc
je 0x3a23
movq 0xe6eb(%rip), %rcx      # 0x120f8
leaq 0x9272(%rip), %rdi      # 0xcc86
movl $0x5, %esi
movl $0x1, %edx
callq 0x32c0
cmpl $0x0, 0xe6a6(%rip)      # 0x120d0
je 0x3a36
movl $0x2, %edi
callq 0x9cf7
cmpl $0x0, 0xe69f(%rip)      # 0x120dc
jne 0x3a54
cmpl $0x0, 0xd77a(%rip)      # 0x111c0
je 0x3a54
xorl %edi, %edi
movl $0x2, %esi
callq 0x55ad
movq (%r12), %rax
movq 0x20(%rax), %rdi
movl $0x2, %esi
callq 0x898c
movq 0xe68b(%rip), %rsi      # 0x120f8
movl $0xa, %edi
callq 0x3150
movl $0x2, %edi
callq 0x3300
cmpl $0x0, 0xe648(%rip)      # 0x120d0
je 0x3a8f
callq 0x992d
leaq 0xf6ba(%rip), %r14      # 0x13150
xorl %edi, %edi
movq %r14, %rsi
callq 0x3070
movl 0xe662(%rip), %edi      # 0x12108
movq 0xe653(%rip), %rsi      # 0x12100
movq %rbx, %rdx
callq 0x410e
testl %eax, %eax
je 0x3ad1
movq 0xe640(%rip), %rax      # 0x12100
movq (%rax), %rsi
leaq 0x7da3(%rip), %rdi      # 0xb86d
xorl %eax, %eax
callq 0x9161
cmpl $0x0, 0xe604(%rip)      # 0x120dc
je 0x3af6
leaq 0x5095(%rip), %rsi      # 0x8b76
movl $0xe, %edi
callq 0x31b0
movl 0xe5eb(%rip), %edi      # 0x120dc
callq 0x3120
leaq 0xe62f(%rip), %rax      # 0x1212c
movl 0x4(%rax), %edi
leaq 0x7d7b(%rip), %rsi      # 0xb882
movl $0x6, %edx
callq 0x3080
cmpl $0x0, 0xe5c8(%rip)      # 0x120e0
leaq 0xe609(%rip), %rbx      # 0x12128
movl (%rbx), %edi
je 0x3b55
callq 0x428b
movq 0xe5c1(%rip), %rdi      # 0x120f0
movl (%rbx), %edx
leaq 0x7d51(%rip), %rsi      # 0xb889
xorl %eax, %eax
callq 0x31c0
callq 0x43df
leaq 0x8(%rsp), %rsi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3260
jmp 0x3b66
leaq 0x4(%rsp), %rsi
leaq 0x8(%rsp), %rcx
xorl %edx, %edx
callq 0x3230
leaq 0xe5c7(%rip), %rax      # 0x12134
movl $0x0, (%rax)
leaq 0xf5c6(%rip), %rbx      # 0x13140
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3070
callq 0x992d
movq (%r12), %rax
cmpl $0x0, 0xc(%rax)
je 0x3bc6
movl $0x48, %r15d
xorl %r13d, %r13d
movq 0x18(%rax), %rax
movq (%rax,%r15), %rdi
testq %rdi, %rdi
je 0x3bb3
movl $0x1, %esi
callq 0x53fc
incq %r13
movq (%r12), %rax
movl 0xc(%rax), %ecx
addq $0x60, %r15
cmpq %rcx, %r13
jb 0x3b9c
movq 0x20(%rax), %rdi
movl $0x1, %esi
callq 0x53fc
cmpl $0x0, 0xe4fd(%rip)      # 0x120d8
je 0x3c8d
cmpl $0x0, 0xe4f4(%rip)      # 0x120dc
je 0x3c34
cvtsi2sdq (%rbx), %xmm1
cvtsi2sdq 0x8(%rbx), %xmm0
movsd 0x75eb(%rip), %xmm2     # 0xb1e8
divsd %xmm2, %xmm0
addsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq (%r14), %xmm1
cvtsi2sdq 0x8(%r14), %xmm3
subsd %xmm1, %xmm0
divsd %xmm2, %xmm3
subsd %xmm3, %xmm0
movq 0xe4d2(%rip), %rdi      # 0x120f8
leaq 0x9502(%rip), %rsi      # 0xd12f
movb $0x1, %al
callq 0x31c0
cmpl $0x0, 0xe495(%rip)      # 0x120d0
je 0x3c47
movl $0x1, %edi
callq 0x9cf7
cmpl $0x0, 0xd572(%rip)      # 0x111c0
je 0x3c68
cmpl $0x0, 0xe485(%rip)      # 0x120dc
jne 0x3c68
leaq 0x8(%rsp), %rdi
movl $0x1, %esi
callq 0x55ad
movq (%r12), %rax
movq 0x20(%rax), %rdi
movl $0x1, %esi
callq 0x898c
movq 0xe477(%rip), %rsi      # 0x120f8
movl $0xa, %edi
callq 0x3150
jmp 0x3cc1
cmpl $0x0, 0xe43c(%rip)      # 0x120d0
je 0x3c9d
xorl %edi, %edi
callq 0x9cf7
cmpl $0x0, 0xd51c(%rip)      # 0x111c0
je 0x3cb2
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x55ad
movq (%r12), %rax
movq 0x20(%rax), %rdi
xorl %esi, %esi
callq 0x898c
movq (%r12), %rax
cmpl $0x0, 0xc(%rax)
je 0x3d6a
movl $0x48, %r13d
leaq 0x7bbc(%rip), %rbx      # 0xb898
leaq 0x8(%rsp), %r14
xorl %r15d, %r15d
movq 0x18(%rax), %rax
cmpq $0x0, (%rax,%r13)
je 0x3d53
cmpl $0x0, 0xe3e2(%rip)      # 0x120d8
je 0x3d07
movq %r14, %rdi
movl $0x1, %esi
callq 0x55ad
jmp 0x3d1b
movq 0xe3ea(%rip), %rdi      # 0x120f8
movq %rbx, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x31c0
movq (%r12), %rax
movq 0x18(%rax), %rax
movq (%rax,%r13), %rdi
xorl %esi, %esi
cmpl $0x0, 0xe3a8(%rip)      # 0x120d8
setne %sil
callq 0x898c
cmpl $0x0, 0xe398(%rip)      # 0x120d8
je 0x3d53
movq 0xe3af(%rip), %rsi      # 0x120f8
movl $0xa, %edi
callq 0x3150
incq %r15
movq (%r12), %rax
movl 0xc(%rax), %ecx
addq $0x60, %r13
cmpq %rcx, %r15
jb 0x3ce4
cmpl $0x0, 0xe35b(%rip)      # 0x120cc
je 0x3d7f
movq 0xe37e(%rip), %rdi      # 0x120f8
callq 0x30a0
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop