#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 int /*<<< orphan*/ cpu_set_t;

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ _SC_NPROCESSORS_ONLN;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
unsigned int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ topology(int);
int main(int argc, char *argv[]) {
  int core;
  cpu_set_t set;
  int rc = -1;
  unsigned logical = sysconf(_SC_NPROCESSORS_ONLN);
  printf("Logical cpus: %d\n ", logical);
  for (core = 0; core < logical; core++) {
    CPU_ZERO(&set);
    CPU_SET(core, &set);
    printf("Logical Core %d\n", core);
    if (sched_setaffinity(getpid(), sizeof(cpu_set_t), &set)) {
      perror("sched_setaffinity");
      goto out;
    }
    topology(core);
    printf("\n");
  }
  rc = 0;
out:
  return rc;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x20(%rbp),%eax
jae    124e <main+0xce>
lea    -0x18(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10a0 <CPU_SET@plt>
mov    -0x14(%rbp),%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
mov    $0x4,%esi
lea    -0x18(%rbp),%rdx
call   1060 <sched_setaffinity@plt>
cmp    $0x0,%rax
je     122a <main+0xaa>
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
jmp    1255 <main+0xd5>
mov    -0x14(%rbp),%edi
call   1030 <topology@plt>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c6 <main+0x46>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,%r15d
lea    0xe51(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r15d,%r15d
je     1224 <main+0x94>
lea    0x4(%rsp),%rbx
lea    0xe48(%rip),%r14
xor    %ebp,%ebp
nopl   (%rax)
mov    %rbx,%rdi
call   1080 <CPU_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10b0 <CPU_SET@plt>
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1070 <sched_setaffinity@plt>
test   %rax,%rax
jne    1231 <main+0xa1>
mov    %ebp,%edi
call   1030 <topology@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
add    $0x1,%ebp
cmp    %ebp,%r15d
jne    11d0 <main+0x40>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdec(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1224 <main+0x94>

LLVM IR (-O0):
@_SC_NPROCESSORS_ONLN = global i32 0
@.str = [19 x i8] c"Logical cpus: %d\0A \00"
@.str.1 = [17 x i8] c"Logical Core %d\0A\00"
@.str.2 = [18 x i8] c"sched_setaffinity\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %8
%10 = load i32, i32* @_SC_NPROCESSORS_ONLN
%11 = call i32 @sysconf(i32 %10)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 0, i32* %6
br label %14
14: ; preds = %33, %2
%15 = load i32, i32* %6
%16 = load i32, i32* %9
%17 = icmp ult i32 %15, %16
br i1 %17, label %18, label %36
18: ; preds = %14
%19 = call i32 @CPU_ZERO(i32* %7)
%20 = load i32, i32* %6
%21 = call i32 @CPU_SET(i32 %20, i32* %7)
%22 = load i32, i32* %6
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = call i32 (...) @getpid()
%25 = call i64 @sched_setaffinity(i32 %24, i32 4, i32* %7)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %37
29: ; preds = %18
%30 = load i32, i32* %6
%31 = call i32 @topology(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %29
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %14
36: ; preds = %14
store i32 0, i32* %8
br label %37
37: ; preds = %36, %27
%38 = load i32, i32* %8
ret i32 %38
}

LLVM IR (-O2):
@_SC_NPROCESSORS_ONLN = global i32 0
@.str = [19 x i8] c"Logical cpus: %d\0A \00"
@.str.1 = [17 x i8] c"Logical Core %d\0A\00"
@.str.2 = [18 x i8] c"sched_setaffinity\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @_SC_NPROCESSORS_ONLN
%6 = tail call i32 @sysconf(i32 %5)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %6, 0
br i1 %8, label %24, label %9
9: ; preds = %2, %19
%10 = phi i32 [ %22, %19 ], [ 0, %2 ]
%11 = call i32 @CPU_ZERO(i32* %3)
%12 = call i32 @CPU_SET(i32 %10, i32* %3)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%14 = call i32 (...) @getpid()
%15 = call i64 @sched_setaffinity(i32 %14, i32 4, i32* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %9
%18 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %24
19: ; preds = %9
%20 = call i32 @topology(i32 %10)
%21 = call i32 @putchar(i32 10)
%22 = add nuw i32 %10, 1
%23 = icmp eq i32 %22, %6
br i1 %23, label %24, label %9
24: ; preds = %19, %2, %17
%25 = phi i32 [ -1, %17 ], [ 0, %2 ], [ 0, %19 ]
ret i32 %25
}