// File: /workspace/github_c_makefile_star2_O1/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
movl $0x1, %edi
movl $0x28, %esi
callq 0x3190
movq %rax, %rsi
xorl %eax, %eax
xchgq %rbx, %rdi
cpuid
xchgq %rbx, %rdi
movl %edi, 0x4(%rsp)
movl %ecx, 0xc(%rsp)
movl %edx, 0x8(%rsp)
movabsq $0x69746e6568747541, %rax # imm = 0x69746E6568747541
xorq 0x4(%rsp), %rax
movq %rsi, 0xa45d(%rip)      # 0x13160
movl %ecx, %ecx
xorq $0x444d4163, %rcx       # imm = 0x444D4163
orq %rax, %rcx
je 0x8d4b
movabsq $0x49656e69756e6547, %rax # imm = 0x49656E69756E6547
xorq 0x4(%rsp), %rax
movl 0xc(%rsp), %ecx
xorq $0x6c65746e, %rcx       # imm = 0x6C65746E
orq %rax, %rcx
je 0x8d53
movl $0x0, (%rsi)
leaq 0x4d24(%rip), %rdi      # 0xda61
leaq 0x4(%rsp), %rsi
xorl %eax, %eax
callq 0x935d
jmp 0x8d59
movl $0x1, (%rsi)
jmp 0x8d59
movl $0x2, (%rsi)
movl $0x1, %eax
xchgq %rbx, %rsi
cpuid
xchgq %rbx, %rsi
movl %eax, %edx
shrl $0x8, %edx
andl $0xf, %edx
movl %eax, %ecx
shrl $0x14, %ecx
movzbl %cl, %esi
addl $0xf, %esi
cmpl $0xf, %edx
cmovnel %edx, %esi
movq 0xa3da(%rip), %rcx      # 0x13160
movl %esi, 0x4(%rcx)
je 0x8d90
cmpl $0x6, %edx
jne 0x8da5
movl %eax, %edx
shrl $0xc, %edx
andl $0xf0, %edx
shrl $0x4, %eax
andl $0xf, %eax
orl %edx, %eax
jmp 0x8dab
shrl $0x4, %eax
andl $0xf, %eax
movl %eax, 0x8(%rcx)
callq 0x3340
movq 0xa3a6(%rip), %rbx      # 0x13160
movl %eax, 0xc(%rbx)
movl %eax, %edi
movl $0x60, %esi
callq 0x3190
movq %rax, 0x18(%rbx)
movq 0xa38c(%rip), %rax      # 0x13160
cmpl $0x0, 0xc(%rax)
je 0x8ee4
movl $0xffffff69, %ebp       # imm = 0xFFFFFF69
movabsq $0x900010009, %r15      # imm = 0x900010009
leaq 0x4cab(%rip), %rbx      # 0xda9f
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %r14d, %r14d
movl (%rax), %ecx
cmpl $0x2, %ecx
je 0x8e2d
cmpl $0x1, %ecx
jne 0x8eac
movl 0x4(%rax), %esi
cmpl $0x17, %esi
je 0x8e1f
cmpl $0x1a, %esi
je 0x8e76
cmpl $0x19, %esi
jne 0x8e84
movq 0x18(%rax), %rax
movl $0x2, (%rax,%r12)
jmp 0x8eac
movl 0x4(%rax), %esi
cmpl $0x6, %esi
jne 0x8e53
movl 0x8(%rax), %ecx
addl %ebp, %ecx
cmpl $0x23, %ecx
ja 0x8e53
btq %rcx, %r15
jae 0x8e53
movq 0x18(%rax), %rax
movl $0x6, (%rax,%r12)
jmp 0x8eac
movq 0x18(%rax), %rcx
movl $0x4, (%rcx,%r12)
testl %r14d, %r14d
jne 0x8e71
movl 0x8(%rax), %edx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x935d
incl %r14d
jmp 0x8eac
movq 0x18(%rax), %rax
movl $0x3, (%rax,%r12)
jmp 0x8eac
movq 0x18(%rax), %rcx
movl $0x1, (%rcx,%r12)
testl %r14d, %r14d
jne 0x8eac
movl 0x8(%rax), %edx
leaq 0x4bd4(%rip), %rdi      # 0xda73
xorl %eax, %eax
callq 0x935d
movl $0x1, %r14d
movq 0xa2ad(%rip), %rax      # 0x13160
movq 0x18(%rax), %rax
andb $-0x2, 0x4(%rax)
movq 0xa29e(%rip), %rax      # 0x13160
movq 0x18(%rax), %rax
andb $-0x3, 0x4(%rax)
incq %r13
movq 0xa28c(%rip), %rax      # 0x13160
movl 0xc(%rax), %ecx
addq $0x60, %r12
cmpq %rcx, %r13
jb 0x8dfd
cmpl $0x2, (%rax)
jne 0x904b
cmpl $0x6, 0x4(%rax)
jne 0x904b
movl $0xffffff69, %ecx       # imm = 0xFFFFFF69
addl 0x8(%rax), %ecx
cmpl $0x23, %ecx
ja 0x904b
movabsq $0x900000009, %rax      # imm = 0x900000009
btq %rcx, %rax
jae 0x904b
leaq 0x4baa(%rip), %rdi      # 0xdacd
leaq 0x98(%rsp), %rsi
callq 0x31e0
cmpl $-0x1, %eax
je 0x8fbb
movq 0xa220(%rip), %rax      # 0x13160
orb $0x1, 0x14(%rax)
leaq 0x4b82(%rip), %rdi      # 0xdacd
leaq 0x33cc(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x8fbb
movq %rax, %rbx
leaq 0x4b82(%rip), %rsi      # 0xdae8
leaq 0x10(%rsp), %rdx
movq %rsp, %rcx
movq %rax, %rdi
xorl %eax, %eax
callq 0x3060
cmpl $0x2, %eax
jne 0x8fb3
movslq 0x10(%rsp), %rax
cmpl (%rsp), %eax
jg 0x8fb3
movq 0xa1d2(%rip), %rdx      # 0x13160
leaq (%rax,%rax,2), %rcx
decq %rax
shlq $0x5, %rcx
addq 0x18(%rdx), %rcx
movl $0x5, (%rcx)
movslq (%rsp), %rdx
incq %rax
addq $0x60, %rcx
cmpq %rdx, %rax
jl 0x8f9d
movq %rbx, %rdi
callq 0x30a0
leaq 0x4b2c(%rip), %rdi      # 0xdaee
leaq 0x98(%rsp), %rsi
callq 0x31e0
cmpl $-0x1, %eax
je 0x904b
leaq 0x4b13(%rip), %rdi      # 0xdaee
leaq 0x333c(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x904b
movq %rax, %rbx
leaq 0x4af2(%rip), %rsi      # 0xdae8
leaq 0x10(%rsp), %rdx
movq %rsp, %rcx
movq %rax, %rdi
xorl %eax, %eax
callq 0x3060
cmpl $0x2, %eax
jne 0x9043
movslq 0x10(%rsp), %rax
cmpl (%rsp), %eax
jg 0x9043
movq 0xa142(%rip), %rdx      # 0x13160
leaq (%rax,%rax,2), %rcx
decq %rax
shlq $0x5, %rcx
addq 0x18(%rdx), %rcx
movl $0x6, (%rcx)
movslq (%rsp), %rdx
incq %rax
addq $0x60, %rcx
cmpq %rdx, %rax
jl 0x902d
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 0x32d0
cmpl $-0x1, %eax
jne 0x9099
leaq 0x4a77(%rip), %rdi      # 0xdb09
xorl %eax, %eax
callq 0x9161
movq 0xa0c0(%rip), %rdx      # 0x13160
cmpl $0x0, 0xc(%rdx)
je 0x90f0
movl $0x4, %eax
xorl %ecx, %ecx
cmpq $0x3ff, %rcx            # imm = 0x3FF
ja 0x90da
movq %rcx, %rsi
shrq $0x6, %rsi
movq 0x10(%rsp,%rsi,8), %rsi
btq %rcx, %rsi
jae 0x90da
movq 0x18(%rdx), %rdx
orb $0x1, (%rdx,%rax)
movq 0xa089(%rip), %rdx      # 0x13160
incl 0x10(%rdx)
incq %rcx
movq 0xa07c(%rip), %rdx      # 0x13160
movl 0xc(%rdx), %esi
addq $0x60, %rax
cmpq %rsi, %rcx
jb 0x90ad
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq