// File: /workspace/github_c_makefile_star2_O2/mvermeulen[P]wspy/cpu_info.c
// Function: inventory_cpu
int inventory_cpu(void){
  unsigned int eax,ebx,ecx,edx;
  int i;
  int nwarn = 0;
  struct stat statbuf;
  FILE *fp;

  cpu_info = calloc(1,sizeof(struct cpu_info));

  // vendor string
  union cpuid0_vendor {
    struct { unsigned int ebx, edx, ecx; };
    char vendor[12];
  } cpuid_0;
  
  __cpuid(0,eax,ebx,ecx,edx);
  cpuid_0.ebx = ebx;
  cpuid_0.ecx = ecx;  
  cpuid_0.edx = edx;
  if (!strncmp(cpuid_0.vendor,"AuthenticAMD",12))
    cpu_info->vendor = VENDOR_AMD;
  else if (!strncmp(cpuid_0.vendor,"GenuineIntel",12))
    cpu_info->vendor = VENDOR_INTEL;
  else {
    cpu_info->vendor = VENDOR_UNKNOWN;
    warning("unknown cpu %16s\n",cpuid_0.vendor);
  }
  
  // model and family
  union cpuid1_modelinfo {
    struct {
      unsigned int step: 4;
      unsigned int model: 4;
      unsigned int family: 4;
      unsigned int ptype: 2;
      unsigned int pad1: 2;
      unsigned int emodel: 4;
      unsigned int efamily: 8;
      unsigned int pad2: 4;
    };
    unsigned int eax;
  } cpuid_1;

  __cpuid(1,eax,ebx,ecx,edx);
  cpuid_1.eax = eax;
  cpu_info->family = (cpuid_1.family==0xf)?
    cpuid_1.family+cpuid_1.efamily:
    cpuid_1.family;
  cpu_info->model = ((cpuid_1.family==0x6)||(cpuid_1.family==0xf))?
    (cpuid_1.emodel<<4) + cpuid_1.model:
    cpuid_1.model;

  // number of cores
  cpu_info->num_cores = get_nprocs();
  cpu_info->coreinfo = calloc(cpu_info->num_cores,
			      sizeof(struct cpu_core_info));

  // specify the core type
  for (i=0;i<cpu_info->num_cores;i++){
    if (cpu_info->vendor == VENDOR_AMD){
      if ((cpu_info->family == 0x17) || (cpu_info->family == 0x19)){
	// Zen
	cpu_info->coreinfo[i].vendor = CORE_AMD_ZEN;
      } else if (cpu_info->family == 0x1a) {
	// Zen5
	cpu_info->coreinfo[i].vendor = CORE_AMD_ZEN5;
      } else {
	cpu_info->coreinfo[i].vendor = CORE_AMD_UNKNOWN;
	if (nwarn == 0){
	  warning("unimplemented AMD CPU, family %x, model %x\n",
		  cpu_info->family,cpu_info->model);
	  nwarn++;
	}
      }
    } else if (cpu_info->vendor == VENDOR_INTEL){
      if ((cpu_info->family == 6) &&
	  ((cpu_info->model == 0xba)||(cpu_info->model == 0xb7)||
	   (cpu_info->model == 0x9a)||(cpu_info->model == 0x97)||
	   (cpu_info->model == 0xa7))){
	cpu_info->coreinfo[i].vendor = CORE_INTEL_CORE;
      } else {
	cpu_info->coreinfo[i].vendor = CORE_INTEL_UNKNOWN;
	if (nwarn == 0){
	  warning("unimplemented Intel CPU, family %x, model %x\n",
		  cpu_info->family,cpu_info->model);
	}
	nwarn++;
      }
    }
    cpu_info->coreinfo->is_available = 0;
    cpu_info->coreinfo->is_counter_started = 0;
  }
  // fix up hybrid cores for Raptor Lake and Alder Lake
  if (cpu_info->vendor == VENDOR_INTEL &&
      cpu_info->family == 6 &&
      ((cpu_info->model == 0xba)||(cpu_info->model == 0xb7)||
       (cpu_info->model == 0x9a)||(cpu_info->model == 0x97))){
    if (stat("/sys/devices/cpu_atom/cpus",&statbuf) != -1){
      cpu_info->is_hybrid = 1;
      if (fp = fopen("/sys/devices/cpu_atom/cpus","r")){
	int low,high;
	if (fscanf(fp,"%d-%d",&low,&high) == 2){
	  for (i=low;i<=high;i++){
	    cpu_info->coreinfo[i].vendor = CORE_INTEL_ATOM;
	  }
	}
	fclose(fp);
      }
    }
    if (stat("/sys/devices/cpu_core/cpus",&statbuf) != -1){
      if (fp = fopen("/sys/devices/cpu_core/cpus","r")){
	int low,high;
	if (fscanf(fp,"%d-%d",&low,&high) == 2){
	  for (i=low;i<=high;i++){
	    cpu_info->coreinfo[i].vendor = CORE_INTEL_CORE;
	  }
	}
	fclose(fp);
      }
    }    
  }
  // check affinity mask for available CPUs
  cpu_set_t set;
  cpu_info->num_cores_available;
  CPU_ZERO(&set);
  if (sched_getaffinity(getpid(),sizeof(set),&set) == -1){
    fatal("unable to get CPU affinity\n");
  }
  for (i=0;i<cpu_info->num_cores;i++){
    if (CPU_ISSET(i,&set)){
      cpu_info->coreinfo[i].is_available = 1;
      cpu_info->num_cores_available++;
    }
  }
  
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
pushq $0x1
popq %rdi
pushq $0x28
popq %rsi
callq 0x3190
movq %rax, %r14
movq %rax, 0x968a(%rip)      # 0x12170
xorl %eax, %eax
xchgq %rbx, %rsi
cpuid
xchgq %rbx, %rsi
leaq 0x4(%rsp), %rdi
movl %esi, (%rdi)
movl %ecx, 0x8(%rdi)
movl %edx, 0x4(%rdi)
leaq 0x3f43(%rip), %rsi      # 0xca47
pushq $0xc
popq %rdx
callq 0x3220
testl %eax, %eax
je 0x8b44
leaq 0x3f3d(%rip), %rsi      # 0xca54
leaq 0x4(%rsp), %rdi
pushq $0xc
popq %rdx
callq 0x3220
testl %eax, %eax
je 0x8b4d
leaq 0x3f32(%rip), %rdi      # 0xca61
leaq 0x4(%rsp), %rsi
xorl %eax, %eax
callq 0x9121
movq 0x962e(%rip), %r14      # 0x12170
jmp 0x8b54
movl $0x1, (%r14)
jmp 0x8b54
movl $0x2, (%r14)
pushq $0x1
popq %rax
xchgq %rbx, %rsi
cpuid
xchgq %rbx, %rsi
movl %eax, %ecx
shrl $0x8, %ecx
andl $0xf, %ecx
movl %eax, %edx
shrl $0x14, %edx
movzbl %dl, %edx
addl $0xf, %edx
cmpl $0xf, %ecx
cmovnel %ecx, %edx
movl %edx, 0x4(%r14)
je 0x8b83
cmpl $0x6, %ecx
jne 0x8b98
movl %eax, %ecx
shrl $0xc, %ecx
andl $0xf0, %ecx
shrl $0x4, %eax
andl $0xf, %eax
orl %ecx, %eax
jmp 0x8b9e
shrl $0x4, %eax
andl $0xf, %eax
movl %eax, 0x8(%r14)
callq 0x3350
movl %eax, %ebp
movq 0x95c0(%rip), %r13      # 0x12170
movl %eax, 0xc(%r13)
movl %eax, %edi
pushq $0x60
popq %rsi
callq 0x3190
movq %rax, 0x18(%r13)
movl $0xffffff69, %r12d      # imm = 0xFFFFFF69
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %r14d, %r14d
movl %ebp, %ecx
movl (%r13), %eax
cmpq %rcx, %rbx
jae 0x8cd6
cmpl $0x2, %eax
je 0x8c11
cmpl $0x1, %eax
jne 0x8ca1
movl 0x4(%r13), %esi
cmpl $0x17, %esi
je 0x8c00
cmpl $0x1a, %esi
je 0x8c6c
cmpl $0x19, %esi
jne 0x8c7a
movq 0x18(%r13), %rax
movl $0x2, (%rax,%r15)
jmp 0x8ca1
movl 0x4(%r13), %esi
cmpl $0x6, %esi
jne 0x8c44
movl 0x8(%r13), %eax
addl %r12d, %eax
cmpl $0x23, %eax
ja 0x8c44
movabsq $0x900010009, %rcx      # imm = 0x900010009
btq %rax, %rcx
jae 0x8c44
movq 0x18(%r13), %rax
movl $0x6, (%rax,%r15)
jmp 0x8ca1
movq 0x18(%r13), %rax
movl $0x4, (%rax,%r15)
testl %r14d, %r14d
jne 0x8c67
movl 0x8(%r13), %edx
leaq 0x3e3f(%rip), %rdi      # 0xca9f
xorl %eax, %eax
callq 0x9121
incl %r14d
jmp 0x8ca1
movq 0x18(%r13), %rax
movl $0x3, (%rax,%r15)
jmp 0x8ca1
movq 0x18(%r13), %rax
movl $0x1, (%rax,%r15)
testl %r14d, %r14d
jne 0x8ca1
movl 0x8(%r13), %edx
leaq 0x3ddd(%rip), %rdi      # 0xca73
xorl %eax, %eax
callq 0x9121
pushq $0x1
popq %r14
movq 0x94c8(%rip), %rax      # 0x12170
movq 0x18(%rax), %rax
andb $-0x2, 0x4(%rax)
movq 0x94b9(%rip), %rax      # 0x12170
movq 0x18(%rax), %rax
andb $-0x3, 0x4(%rax)
incq %rbx
movq 0x94a7(%rip), %r13      # 0x12170
movl 0xc(%r13), %ebp
addq $0x60, %r15
jmp 0x8bd0
cmpl $0x2, %eax
jne 0x8e29
cmpl $0x6, 0x4(%r13)
jne 0x8e29
movl $0xffffff69, %eax       # imm = 0xFFFFFF69
addl 0x8(%r13), %eax
cmpl $0x23, %eax
ja 0x8e29
movabsq $0x900000009, %rcx      # imm = 0x900000009
btq %rax, %rcx
jae 0x8e29
leaq 0x3db6(%rip), %rdi      # 0xcacd
leaq 0x98(%rsp), %rsi
callq 0x31e0
cmpl $-0x1, %eax
je 0x8da2
movq 0x9440(%rip), %rax      # 0x12170
orb $0x1, 0x14(%rax)
leaq 0x3d92(%rip), %rdi      # 0xcacd
leaq 0x25dc(%rip), %rsi      # 0xb31e
callq 0x3260
testq %rax, %rax
je 0x8da2
movq %rax, %rbx
leaq 0x3d92(%rip), %rsi      # 0xcae8
leaq 0x10(%rsp), %rdx
movq %rsp, %rcx
movq %rax, %rdi
xorl %eax, %eax
callq 0x3060
cmpl $0x2, %eax
jne 0x8d9a
movslq 0x10(%rsp), %rax
movq 0x93f7(%rip), %rcx      # 0x12170
imulq $0x60, %rax, %rdx
movslq (%rsp), %rsi
cmpq %rsi, %rax
jg 0x8d9a
movq 0x18(%rcx), %rsi
movl $0x5, (%rsi,%rdx)
incq %rax
addq $0x60, %rdx
jmp 0x8d7d
movq %rbx, %rdi
callq 0x30a0
leaq 0x3d45(%rip), %rdi      # 0xcaee
leaq 0x98(%rsp), %rsi
callq 0x31e0
cmpl $-0x1, %eax
je 0x8e29
leaq 0x3d2c(%rip), %rdi      # 0xcaee
leaq 0x2555(%rip), %rsi      # 0xb31e
callq 0x3260
testq %rax, %rax
je 0x8e29
movq %rax, %rbx
leaq 0x3d0b(%rip), %rsi      # 0xcae8
leaq 0x10(%rsp), %rdx
movq %rsp, %rcx
movq %rax, %rdi
xorl %eax, %eax
callq 0x3060
cmpl $0x2, %eax
jne 0x8e21
movslq 0x10(%rsp), %rax
movq 0x9370(%rip), %rcx      # 0x12170
imulq $0x60, %rax, %rdx
movslq (%rsp), %rsi
cmpq %rsi, %rax
jg 0x8e21
movq 0x18(%rcx), %rsi
movl $0x6, (%rsi,%rdx)
incq %rax
addq $0x60, %rdx
jmp 0x8e04
movq %rbx, %rdi
callq 0x30a0
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rbx
movaps %xmm0, 0x70(%rbx)
movaps %xmm0, 0x60(%rbx)
movaps %xmm0, 0x50(%rbx)
movaps %xmm0, 0x40(%rbx)
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
callq 0x3090
movl $0x80, %esi
movl %eax, %edi
movq %rbx, %rdx
callq 0x32e0
cmpl $-0x1, %eax
jne 0x8e77
leaq 0x3c99(%rip), %rdi      # 0xcb09
xorl %eax, %eax
callq 0x8f2f
pushq $0x4
popq %rax
movq 0x92ef(%rip), %rdx      # 0x12170
xorl %ecx, %ecx
movl 0xc(%rdx), %esi
cmpq %rsi, %rcx
jae 0x8ec1
cmpq $0x3ff, %rcx            # imm = 0x3FF
ja 0x8eb8
movq %rcx, %rsi
shrq $0x6, %rsi
movq 0x10(%rsp,%rsi,8), %rsi
btq %rcx, %rsi
jae 0x8eb8
movq 0x18(%rdx), %rdx
orb $0x1, (%rdx,%rax)
movq 0x92bb(%rip), %rdx      # 0x12170
incl 0x10(%rdx)
incq %rcx
addq $0x60, %rax
jmp 0x8e83
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)