// File: /workspace/github_c_makefile_star2_O0/mvermeulen[P]wspy/topdown.c
// Function: read_counters
void read_counters(struct counter_group *counter_group_list,int stop_counters){
  int i,j;
  int status;
  struct counter_group *cgroup;
  
  struct read_format { uint64_t value, time_enabled, time_running,id; } rf;

  for (cgroup = counter_group_list;cgroup;cgroup = cgroup->next){
    debug("Stopping %s counters\n",cgroup->label);
    for (i=0;i<cgroup->ncounters;i++){
      if (cgroup->cinfo[i].fd != -1){
	status = read(cgroup->cinfo[i].fd,&rf,sizeof(rf));
	if (status == -1){
	  error("unable to read %s counter %s, errno=%d - %s\n",
		cgroup->label,cgroup->cinfo[i].label,errno,strerror(errno));
	} else {
	  cgroup->cinfo[i].prev_read = cgroup->cinfo[i].last_read;
	  cgroup->cinfo[i].last_read = rf.value;
	  // save only the delta since this was last read...
	  cgroup->cinfo[i].value = rf.value - cgroup->cinfo[i].prev_read;
	  cgroup->cinfo[i].time_running = rf.time_running;
	  cgroup->cinfo[i].time_enabled = rf.time_enabled;
	  // TODO: add scaling for performance counters...

	  if (stop_counters){
	    status = ioctl(cgroup->cinfo[i].fd,PERF_EVENT_IOC_DISABLE,0);
	  
	    if (status != 0){
	      error("unable to stop %s counter %s, errno=%d,%s\n",
		    cgroup->label,cgroup->cinfo[i].label,errno,strerror(errno));
	    } else {
	      debug("   stopped %s counter, name=%s, value=%lu enabled=%lu running=%lu\n",
		    cgroup->label,cgroup->cinfo[i].label,cgroup->cinfo[i].value,
		    rf.time_enabled,rf.time_running);
	    }
	  }
	}
      }
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x64f8
movq -0x20(%rbp), %rax
movq (%rax), %rsi
leaq 0x88d2(%rip), %rdi      # 0xeb52
movb $0x0, %al
callq 0xbfe0
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x64e5
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
cmpl $-0x1, 0x28(%rax)
je 0x64d5
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %edi
leaq -0x40(%rbp), %rsi
movl $0x20, %edx
callq 0x3170
movl %eax, -0x18(%rbp)
cmpl $-0x1, -0x18(%rbp)
jne 0x6344
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
callq 0x3040
movl (%rax), %eax
movl %eax, -0x44(%rbp)
callq 0x3040
movl (%rax), %edi
callq 0x3300
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
movl -0x44(%rbp), %ecx
movq %rax, %r8
leaq 0x8830(%rip), %rdi      # 0xeb68
movb $0x0, %al
callq 0xbc00
jmp 0x64d3
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x38(%rax), %rcx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rdx
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x40(%rax)
movq -0x40(%rbp), %rcx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rdx
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x38(%rax)
movq -0x40(%rbp), %rcx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rdx
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
subq 0x40(%rax), %rcx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rdx
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x30(%rax)
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rdx
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x48(%rax)
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rdx
imulq $0x58, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x50(%rax)
cmpl $0x0, -0xc(%rbp)
je 0x64d1
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %edi
movl $0x2401, %esi           # imm = 0x2401
xorl %edx, %edx
movb $0x0, %al
callq 0x3130
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x6485
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
callq 0x3040
movl (%rax), %eax
movl %eax, -0x5c(%rbp)
callq 0x3040
movl (%rax), %edi
callq 0x3300
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movl -0x5c(%rbp), %ecx
movq %rax, %r8
leaq 0x8719(%rip), %rdi      # 0xeb95
movb $0x0, %al
callq 0xbc00
jmp 0x64cf
movq -0x20(%rbp), %rax
movq (%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x30(%rax), %rcx
movq -0x38(%rbp), %r8
movq -0x30(%rbp), %r9
leaq 0x86f8(%rip), %rdi      # 0xebc0
movb $0x0, %al
callq 0xbfe0
jmp 0x64d1
jmp 0x64d3
jmp 0x64d5
jmp 0x64d7
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x628e
jmp 0x64e7
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x6267
addq $0x70, %rsp
popq %rbp
retq
nop