#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef scalar_t__ uint32_t;
typedef int /*<<< orphan*/ cpu_set_t;

/* Variables and functions */
scalar_t__ CPU_COUNT(int /*<<< orphan*/ *);
scalar_t__ CPU_ISSET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_SET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
scalar_t__ cpu_physical(scalar_t__);
int /*<<< orphan*/ printf(char *, scalar_t__, scalar_t__, scalar_t__);
int /*<<< orphan*/ sched_getaffinity(int /*<<< orphan*/, int,
                                     int /*<<< orphan*/ *);
int main(int argc, const char *argv[]) {
  uint32_t all, hw, ht, index, found;
  cpu_set_t all_cpus;
  cpu_set_t hw_cpus;
  cpu_set_t ht_cpus;
  sched_getaffinity(0, sizeof(cpu_set_t), &all_cpus);
  CPU_ZERO(&hw_cpus);
  CPU_ZERO(&ht_cpus);
  all = CPU_COUNT(&all_cpus);
  index = found = 0;
  while (found < all) {
    if (CPU_ISSET(index, &all_cpus)) {
      if (cpu_physical(index))
        CPU_SET(index, &hw_cpus);
      else
        CPU_SET(index, &ht_cpus);
      found++;
    }
    index++;
  }
  hw = CPU_COUNT(&hw_cpus);
  ht = CPU_COUNT(&ht_cpus);
  printf("all: %u hw: %u ht: %u\n", all, hw, ht);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x3c(%rbp),%rdx
call   1080 <sched_getaffinity@plt>
lea    -0x40(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
lea    -0x44(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
lea    -0x3c(%rbp),%rdi
call   1040 <CPU_COUNT@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1239 <main+0xc9>
mov    -0x30(%rbp),%rdi
lea    -0x3c(%rbp),%rsi
call   1050 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     1228 <main+0xb8>
mov    -0x30(%rbp),%rdi
call   1060 <cpu_physical@plt>
cmp    $0x0,%rax
je     120f <main+0x9f>
mov    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1090 <CPU_SET@plt>
jmp    121c <main+0xac>
mov    -0x30(%rbp),%rdi
lea    -0x44(%rbp),%rsi
call   1090 <CPU_SET@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11c5 <main+0x55>
lea    -0x40(%rbp),%rdi
call   1040 <CPU_COUNT@plt>
mov    %rax,-0x20(%rbp)
lea    -0x44(%rbp),%rdi
call   1040 <CPU_COUNT@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xd9a(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x14(%rsp),%rbx
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1080 <sched_getaffinity@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1070 <CPU_ZERO@plt>
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   1070 <CPU_ZERO@plt>
mov    %rbx,%rdi
call   1040 <CPU_COUNT@plt>
mov    %rax,%r13
test   %rax,%rax
jle    1209 <main+0x99>
xor    %ebp,%ebp
lea    0x14(%rsp),%r12
xor    %ebx,%ebx
jmp    11d9 <main+0x69>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %r13,%rbp
jge    1209 <main+0x99>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <CPU_ISSET@plt>
test   %rax,%rax
je     11d0 <main+0x60>
mov    %rbx,%rdi
call   1060 <cpu_physical@plt>
test   %rax,%rax
mov    %r14,%rsi
cmove  %r15,%rsi
mov    %rbx,%rdi
call   1090 <CPU_SET@plt>
add    $0x1,%rbp
jmp    11d0 <main+0x60>
lea    0x10(%rsp),%rdi
call   1040 <CPU_COUNT@plt>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1040 <CPU_COUNT@plt>
lea    0xdd9(%rip),%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"all: %u hw: %u ht: %u\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %11)
%15 = call i32 @CPU_ZERO(i32* %12)
%16 = call i32 @CPU_ZERO(i32* %13)
%17 = call i64 @CPU_COUNT(i32* %11)
store i64 %17, i64* %6
store i64 0, i64* %10
store i64 0, i64* %9
br label %18
18: ; preds = %39, %2
%19 = load i64, i64* %10
%20 = load i64, i64* %6
%21 = icmp slt i64 %19, %20
br i1 %21, label %22, label %42
22: ; preds = %18
%23 = load i64, i64* %9
%24 = call i64 @CPU_ISSET(i64 %23, i32* %11)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %22
%27 = load i64, i64* %9
%28 = call i64 @cpu_physical(i64 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i64, i64* %9
%32 = call i32 @CPU_SET(i64 %31, i32* %12)
br label %36
33: ; preds = %26
%34 = load i64, i64* %9
%35 = call i32 @CPU_SET(i64 %34, i32* %13)
br label %36
36: ; preds = %33, %30
%37 = load i64, i64* %10
%38 = add nsw i64 %37, 1
store i64 %38, i64* %10
br label %39
39: ; preds = %36, %22
%40 = load i64, i64* %9
%41 = add nsw i64 %40, 1
store i64 %41, i64* %9
br label %18
42: ; preds = %18
%43 = call i64 @CPU_COUNT(i32* %12)
store i64 %43, i64* %7
%44 = call i64 @CPU_COUNT(i32* %13)
store i64 %44, i64* %8
%45 = load i64, i64* %6
%46 = load i64, i64* %7
%47 = load i64, i64* %8
%48 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i64 %45, i64 %46, i64 %47)
ret i32 0
}

LLVM IR (-O2):
@.str = [23 x i8] c"all: %u hw: %u ht: %u\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %3)
%10 = call i32 @CPU_ZERO(i32* %4)
%11 = call i32 @CPU_ZERO(i32* %5)
%12 = call i64 @CPU_COUNT(i32* %3)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %2, %25
%15 = phi i64 [ %26, %25 ], [ 0, %2 ]
%16 = phi i64 [ %27, %25 ], [ 0, %2 ]
%17 = call i64 @CPU_ISSET(i64 %16, i32* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %14
%20 = call i64 @cpu_physical(i64 %16)
%21 = icmp eq i64 %20, 0
%22 = select i1 %21, i32* %5, i32* %4
%23 = call i32 @CPU_SET(i64 %16, i32* %22)
%24 = add nsw i64 %15, 1
br label %25
25: ; preds = %19, %14
%26 = phi i64 [ %24, %19 ], [ %15, %14 ]
%27 = add nuw nsw i64 %16, 1
%28 = icmp slt i64 %26, %12
br i1 %28, label %14, label %29
29: ; preds = %25, %2
%30 = call i64 @CPU_COUNT(i32* %4)
%31 = call i64 @CPU_COUNT(i32* %5)
%32 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i64 %12, i64 %30, i64 %31)
ret i32 0
}