// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x0, -0x20(%rbp)
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x60d4
movq -0x30(%rbp), %rax
movq (%rax), %rsi
leaq 0x8c42(%rip), %rdi      # 0xea47
movb $0x0, %al
callq 0xbfe0
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x60c1
leaq 0xe346(%rip), %rax      # 0x14170
movq (%rax), %rax
cmpl $0x2, (%rax)
jne 0x5e3d
movl 0xd288(%rip), %eax      # 0x130c0
movl %eax, -0x28(%rbp)
jmp 0x5e66
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movb 0xc(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x5e64
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
jmp 0x5e66
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x3120
movq -0x30(%rbp), %rax
cmpl $0x4, 0x8(%rax)
jne 0x5ea1
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, -0xc8(%rbp)
jmp 0x5eae
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0xb0(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq %rax, -0xa8(%rbp)
movq $0x10000, -0x98(%rbp) # imm = 0x10000
movq $0x3, -0x90(%rbp)
movl $0x80, -0xac(%rbp)
movq -0x88(%rbp), %rax
andq $-0x3, %rax
orq $0x2, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
andq $-0x2, %rax
orq $0x1, %rax
movq %rax, -0x88(%rbp)
cmpl $0xe, -0xb0(%rbp)
jne 0x5f65
movq -0x88(%rbp), %rax
andq $-0x100001, %rax        # imm = 0xFFEFFFFF
orq $0x0, %rax
movq %rax, -0x88(%rbp)
movl -0x28(%rbp), %ecx
leaq -0xb0(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
callq 0x52c0
movl %eax, -0x24(%rbp)
jmp 0x5f84
movl -0x28(%rbp), %ecx
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x8, %r8d
callq 0x52c0
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x24(%rbp)
jne 0x601f
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
callq 0x3040
movl (%rax), %eax
movl %eax, -0xcc(%rbp)
callq 0x3040
movl (%rax), %edi
callq 0x3300
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
movl -0xcc(%rbp), %ecx
movq %rax, %r8
leaq 0x8a6f(%rip), %rdi      # 0xea5f
movb $0x0, %al
callq 0xbc00
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl $0xffffffff, 0x28(%rax) # imm = 0xFFFFFFFF
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x60b1
movl -0x24(%rbp), %ecx
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rdx
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x28(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %edi
movl $0x2400, %esi           # imm = 0x2400
xorl %edx, %edx
movb $0x0, %al
callq 0x3130
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdx
leaq 0x8a20(%rip), %rdi      # 0xeaa0
movb $0x0, %al
callq 0xbfe0
cmpl $-0x1, -0x28(%rbp)
jne 0x60af
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl %eax, 0xd011(%rip)      # 0x130c0
jmp 0x60b1
jmp 0x60b3
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x5e13
jmp 0x60c3
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x5dec
cmpl $0x0, -0x20(%rbp)
je 0x60e8
leaq 0x89ea(%rip), %rdi      # 0xeacb
movb $0x0, %al
callq 0xbaf0
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)