// File: /workspace/github_c_makefile_star2_O0/mvermeulen[P]wspy/topdown.c
// Function: raw_counter_group
struct counter_group *raw_counter_group(char *name,unsigned int mask){
  int i;
  int available_counters = (nmi_running)?5:6;
  
  struct raw_event *events;
  unsigned int num_events;
  switch(cpu_info->vendor){
  case VENDOR_AMD:
    events = amd_raw_events;
    num_events = sizeof(amd_raw_events)/sizeof(amd_raw_events[0]);
    break;
  case VENDOR_INTEL:
    events = intel_raw_events;
    num_events = sizeof(intel_raw_events)/sizeof(intel_raw_events[0]);
    break;
  default:
    return NULL;
  }

  int num_counters = 0;
  for (i=0;i<num_events;i++){
    if (events[i].use & mask) num_counters++;
  }
  
  struct counter_group *cgroup = calloc(1,sizeof(struct counter_group));  
  cgroup->label = strdup(name);
  cgroup->type_id = PERF_TYPE_RAW;
  cgroup->ncounters = num_counters;
  cgroup->cinfo = calloc(cgroup->ncounters,sizeof(struct counter_info));
  cgroup->mask = mask;
  int count = 0;
  for (i=0;i<num_events;i++){
    if (events[i].use & mask){
      cgroup->cinfo[count].label = events[i].name;
      cgroup->cinfo[count].config = events[i].raw.config;
      cgroup->cinfo[count].device_type = events[i].device_type;
      // chunk into multiplex groups if needed
      if (cpu_info->vendor == VENDOR_AMD){
	if (((count % available_counters) == 0) || (events[i].device_type != PERF_TYPE_RAW))
	  cgroup->cinfo[count].is_group_leader = 1;
      }
      count++;
    }
  }
  
  return cgroup;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl 0xd97b(%rip), %ecx      # 0x13130
xorl %eax, %eax
subl $0x1, %ecx
adcl $0x5, %eax
movl %eax, -0x1c(%rbp)
leaq 0xe9a9(%rip), %rax      # 0x14170
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0x40(%rbp)
subl $0x1, %eax
je 0x57e0
jmp 0x57d6
movl -0x40(%rbp), %eax
subl $0x2, %eax
je 0x57f4
jmp 0x5808
leaq 0xd279(%rip), %rax      # 0x12a60
movq %rax, -0x28(%rbp)
movl $0x2a, -0x2c(%rbp)
jmp 0x5815
leaq 0xcfa5(%rip), %rax      # 0x127a0
movq %rax, -0x28(%rbp)
movl $0x16, -0x2c(%rbp)
jmp 0x5815
movq $0x0, -0x8(%rbp)
jmp 0x59e1
movl $0x0, -0x30(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x585b
movq -0x28(%rbp), %rax
movslq -0x18(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
andl -0x14(%rbp), %eax
cmpl $0x0, %eax
je 0x584e
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x5850
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x5823
movl $0x1, %edi
movl $0x28, %esi
callq 0x31a0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x32f0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
movl $0x4, 0x8(%rax)
movl -0x30(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x38(%rbp), %rax
movslq 0xc(%rax), %rdi
movl $0x58, %esi
callq 0x31a0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x14(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x10(%rax)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x59d9
movq -0x28(%rbp), %rax
movslq -0x18(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
andl -0x14(%rbp), %eax
cmpl $0x0, %eax
je 0x59c9
movq -0x28(%rbp), %rax
movslq -0x18(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x3c(%rbp), %rdx
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movslq -0x18(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rcx
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x3c(%rbp), %rdx
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rax
movslq -0x18(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x3c(%rbp), %rdx
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
leaq 0xe7fa(%rip), %rax      # 0x14170
movq (%rax), %rax
cmpl $0x1, (%rax)
jne 0x59c0
movl -0x3c(%rbp), %eax
cltd
idivl -0x1c(%rbp)
cmpl $0x0, %edx
je 0x599f
movq -0x28(%rbp), %rax
movslq -0x18(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x4, 0x10(%rax)
je 0x59be
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x3c(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movb 0xc(%rax), %cl
andb $-0x2, %cl
orb $0x1, %cl
movb %cl, 0xc(%rax)
jmp 0x59c0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x59cb
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x58cb
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)