// File: /workspace/github_c_makefile_star2_O1/mvermeulen[P]wspy/topdown.c
// Function: setup_counters
void setup_counters(struct counter_group *counter_group_list){
  unsigned int mask = 0;
  int i,j,k;
  int count;
  int nerror = 0;
  int status;
  int group_id = -1;
  struct counter_group *cgroup;
  struct perf_event_attr pe;

  struct cpu_core_info *coreinfo;
  struct counter_def *counter_def;
  int ncounters;

  // create system-wide counters
  for (cgroup = counter_group_list;cgroup;cgroup = cgroup->next){
    debug("Setting up %s counters\n",cgroup->label);
    for (i=0;i<cgroup->ncounters;i++){
      if (cpu_info->vendor == VENDOR_INTEL){
	group_id = intel_group_id;
      } else {
	if (cgroup->cinfo[i].is_group_leader == 1) group_id = -1;
      }
      memset(&pe,0,sizeof(pe));
      pe.type = (cgroup->type_id==PERF_TYPE_RAW)?cgroup->cinfo[i].device_type:cgroup->type_id;
      pe.config = cgroup->cinfo[i].config;
      pe.sample_type = PERF_SAMPLE_IDENTIFIER; // is this needed?
      pe.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING;
      pe.size = sizeof(struct perf_event_attr);
      //      pe.exclude_guest = 1; // is this needed
      pe.inherit = 1;
      pe.disabled = 1;

      if (pe.type == PERF_TYPE_L3){
	// read from any process on this core
	pe.exclude_guest = 0;
	status = perf_event_open(&pe,-1,0,group_id,0);
      } else {
	// read from current process
	status = perf_event_open(&pe,0,-1,group_id,PERF_FLAG_FD_CLOEXEC);	
      }


      if (status == -1){
	error("unable to create %s performance counter, name=%s, errno=%d - %s\n",
	      cgroup->label,cgroup->cinfo[i].label,errno,strerror(errno));
	cgroup->cinfo[i].fd = -1;
	nerror++;
      } else {
	cgroup->cinfo[i].fd = status;
	ioctl(cgroup->cinfo[i].fd,PERF_EVENT_IOC_ENABLE,0);
	debug("   create %s performance counter, name=%s\n",cgroup->label,cgroup->cinfo[i].label);
	if (group_id == -1){
	  group_id = cgroup->cinfo[i].fd;
	  intel_group_id = group_id;
	}
      }
    }
  }
  if (nerror) fatal("unable to open performance counters\n");
}

// Assembly:
testq %rdi, %rdi
je 0x532c
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
leaq 0xe034(%rip), %r12      # 0x13160
leaq 0x10(%rsp), %r13
xorl %r14d, %r14d
movq (%rbx), %rsi
leaq 0x7917(%rip), %rdi      # 0xca55
xorl %eax, %eax
callq 0x9600
cmpl $0x0, 0xc(%rbx)
jle 0x52fb
xorl %r15d, %r15d
xorl %ebp, %ebp
movq (%r12), %rax
cmpl $0x2, (%rax)
jne 0x5168
movl 0xcf4d(%rip), %eax      # 0x120b0
movl %eax, (%rsp)
jmp 0x517f
movq 0x18(%rbx), %rax
movb 0xc(%rax,%r15), %al
andb $0x1, %al
negb %al
movl $0x0, %eax
sbbl %eax, %eax
orl %eax, (%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl 0x8(%rbx), %eax
cmpl $0x4, %eax
jne 0x51be
movq 0x18(%rbx), %rax
movl 0x10(%rax,%r15), %eax
movl %eax, 0x10(%rsp)
movq 0x18(%rbx), %rcx
movq 0x18(%rcx,%r15), %rcx
movq %rcx, 0x18(%rsp)
movq $0x10000, 0x28(%rsp)    # imm = 0x10000
movq $0x3, 0x30(%rsp)
movl $0x80, 0x14(%rsp)
movq 0x38(%rsp), %rcx
orq $0x3, %rcx
movq %rcx, 0x38(%rsp)
cmpl $0xe, %eax
jne 0x521b
andq $-0x100001, %rcx        # imm = 0xFFEFFFFF
movq %rcx, 0x38(%rsp)
movq %r13, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
movl (%rsp), %ecx
xorl %r8d, %r8d
jmp 0x522e
movl $0x8, %r8d
movq %r13, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl (%rsp), %ecx
callq 0x4b34
cmpl $-0x1, %eax
je 0x5284
movq 0x18(%rbx), %rcx
movl %eax, 0x28(%rcx,%r15)
movl $0x2400, %esi           # imm = 0x2400
movl %eax, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x3110
movq (%rbx), %rsi
movq 0x18(%rbx), %rax
movq (%rax,%r15), %rdx
leaq 0x784b(%rip), %rdi      # 0xcaae
xorl %eax, %eax
callq 0x9600
cmpl $-0x1, (%rsp)
jne 0x52e7
movq 0x18(%rbx), %rax
movl 0x28(%rax,%r15), %eax
movl %eax, (%rsp)
movl %eax, 0xce2e(%rip)      # 0x120b0
jmp 0x52e7
movq (%rbx), %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rbx), %rax
movq (%rax,%r15), %r12
callq 0x3040
movl %r14d, 0x4(%rsp)
movq %r13, %r14
movl (%rax), %r13d
movl %r13d, %edi
callq 0x32f0
leaq 0x77ba(%rip), %rdi      # 0xca6d
movq 0x8(%rsp), %rsi
movq %r12, %rdx
leaq 0xde9e(%rip), %r12      # 0x13160
movl %r13d, %ecx
movq %r14, %r13
movl 0x4(%rsp), %r14d
movq %rax, %r8
xorl %eax, %eax
callq 0x9256
movq 0x18(%rbx), %rax
movl $0xffffffff, 0x28(%rax,%r15) # imm = 0xFFFFFFFF
incl %r14d
incq %rbp
movslq 0xc(%rbx), %rax
addq $0x58, %r15
cmpq %rax, %rbp
jl 0x5154
movq 0x20(%rbx), %rbx
testq %rbx, %rbx
jne 0x5134
testl %r14d, %r14d
je 0x531b
leaq 0x77c5(%rip), %rdi      # 0xcad9
xorl %eax, %eax
callq 0x9161
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq