#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 /*<<< orphan*/ sleep(int);
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);
    }
    sleep(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    0x2e36(%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    11f2 <child_test_thread+0x62>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1248 <child_test_thread+0xb8>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     1235 <child_test_thread+0xa5>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123a <child_test_thread+0xaa>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11f9 <child_test_thread+0x69>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    11c2 <child_test_thread+0x32>

x86 assembly (clang, -O2):
<child_test_thread>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e2d(%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    0xc(%rsp),%r14
lea    0xe4f(%rip),%r15
lea    0xe23(%rip),%r12
jmp    11ea <child_test_thread+0x4a>
nop
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %r14,%rdi
call   1080 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   10b0 <sched_getaffinity@plt>
cmp    $0xffffffff,%eax
je     1210 <child_test_thread+0x70>
test   %ebx,%ebx
jg     121c <child_test_thread+0x7c>
jmp    11e0 <child_test_thread+0x40>
nopl   0x0(%rax)
mov    %r15,%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
jle    11e0 <child_test_thread+0x40>
xor    %ebp,%ebp
jmp    1227 <child_test_thread+0x87>
add    $0x1,%ebp
cmp    %ebp,%ebx
je     11e0 <child_test_thread+0x40>
mov    %ebp,%edi
mov    %r14,%rsi
call   1060 <CPU_ISSET@plt>
test   %rax,%rax
je     1220 <child_test_thread+0x80>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r12,%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1220 <child_test_thread+0x80>

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, %33
%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
%34 = call i32 @sleep(i32 1)
br label %10
}

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 = icmp sgt i32 %5, 0
br label %9
9: ; preds = %26, %1
%10 = call i32 @CPU_ZERO(i32* %2)
%11 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
br i1 %8, label %16, label %26
16: ; preds = %15, %23
%17 = phi i32 [ %24, %23 ], [ 0, %15 ]
%18 = call i64 @CPU_ISSET(i32 %17, i32* %2)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = call i32 (...) @getpid()
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %17)
br label %23
23: ; preds = %16, %20
%24 = add nuw nsw i32 %17, 1
%25 = icmp eq i32 %24, %5
br i1 %25, label %26, label %16
26: ; preds = %23, %15
%27 = call i32 @sleep(i32 1)
br label %9
}