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

/* Variables and functions */
scalar_t__ CPU_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ _SC_NPROCESSORS_CONF;
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ pthread_detach(int /*<<< orphan*/);
int /*<<< orphan*/ pthread_self();
int sched_getaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int sysconf(int /*<<< orphan*/);
void *child_test_thread(void *arg) {
  cpu_set_t mask;
  int i = 0;
  int num = sysconf(_SC_NPROCESSORS_CONF);
  pthread_detach(pthread_self());
  while (1) {
    CPU_ZERO(&mask);
    if (sched_getaffinity(0, sizeof(mask), &mask) == -1) {
      printf("get failed..\n");
    }
    for (i = 0; i < num; i++) {
      if (CPU_ISSET(i, &mask))
        printf("child thread %d run on processor %d\n", getpid(), i);
    }
    while (1)
      ;
  }
}


x86 assembly (clang, -O0):
<child_test_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1080 <pthread_self@plt>
mov    %eax,%edi
call   1060 <pthread_detach@plt>
lea    -0xc(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   10a0 <sched_getaffinity@plt>
cmp    $0xffffffff,%eax
jne    11e2 <child_test_thread+0x62>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1238 <child_test_thread+0xb8>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     1225 <child_test_thread+0xa5>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122a <child_test_thread+0xaa>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11e9 <child_test_thread+0x69>
jmp    123d <child_test_thread+0xbd>
jmp    123d <child_test_thread+0xbd>

x86 assembly (clang, -O2):
<child_test_thread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <pthread_self@plt>
mov    %eax,%edi
call   1070 <pthread_detach@plt>
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbp,%rdx
call   10b0 <sched_getaffinity@plt>
cmp    $0xffffffff,%eax
je     11dc <child_test_thread+0x4c>
test   %ebx,%ebx
jg     11ec <child_test_thread+0x5c>
jmp    1230 <child_test_thread+0xa0>
lea    0xe42(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
jle    1230 <child_test_thread+0xa0>
lea    0x4(%rsp),%r14
lea    0xe08(%rip),%r15
xor    %ebp,%ebp
jmp    1207 <child_test_thread+0x77>
nopl   0x0(%rax)
add    $0x1,%ebp
cmp    %ebp,%ebx
je     1230 <child_test_thread+0xa0>
mov    %ebp,%edi
mov    %r14,%rsi
call   1060 <CPU_ISSET@plt>
test   %rax,%rax
je     1200 <child_test_thread+0x70>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1200 <child_test_thread+0x70>
nopl   (%rax)
jmp    1230 <child_test_thread+0xa0>

LLVM IR (-O0):
@_SC_NPROCESSORS_CONF = global i32 0
@.str = [14 x i8] c"get failed..\0A\00"
@.str.1 = [37 x i8] c"child thread %d run on processor %d\0A\00"
define i8* @child_test_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
%6 = load i32, i32* @_SC_NPROCESSORS_CONF
%7 = call i32 @sysconf(i32 %6)
store i32 %7, i32* %5
%8 = call i32 (...) @pthread_self()
%9 = call i32 @pthread_detach(i32 %8)
br label %10
10: ; preds = %1
%11 = call i32 @CPU_ZERO(i32* %3)
%12 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
store i32 0, i32* %4
br label %17
17: ; preds = %30, %16
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %33
21: ; preds = %17
%22 = load i32, i32* %4
%23 = call i64 @CPU_ISSET(i32 %22, i32* %3)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = call i32 (...) @getpid()
%27 = load i32, i32* %4
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27)
br label %29
29: ; preds = %25, %21
br label %30
30: ; preds = %29
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %17
33: ; preds = %17
br label %34
34: ; preds = %33, %34
br label %34
}

LLVM IR (-O2):
@_SC_NPROCESSORS_CONF = global i32 0
@.str.1 = [37 x i8] c"child thread %d run on processor %d\0A\00"
@str = [13 x i8] c"get failed..\00"
define noalias i8* @child_test_thread(i8* nocapture readnone %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @_SC_NPROCESSORS_CONF
%5 = tail call i32 @sysconf(i32 %4)
%6 = tail call i32 (...) @pthread_self()
%7 = tail call i32 @pthread_detach(i32 %6)
%8 = call i32 @CPU_ZERO(i32* %2)
%9 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %2)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
%14 = icmp sgt i32 %5, 0
br i1 %14, label %15, label %25
15: ; preds = %13, %22
%16 = phi i32 [ %23, %22 ], [ 0, %13 ]
%17 = call i64 @CPU_ISSET(i32 %16, i32* %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = call i32 (...) @getpid()
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %16)
br label %22
22: ; preds = %15, %19
%23 = add nuw nsw i32 %16, 1
%24 = icmp eq i32 %23, %5
br i1 %24, label %25, label %15
25: ; preds = %22, %13
br label %26
26: ; preds = %25, %26
br label %26
}