#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_SET(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 sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int sysconf(int /*<<< orphan*/);
void *new_test_thread(void *arg) {
  cpu_set_t mask;
  int i = 0;
  int num = sysconf(_SC_NPROCESSORS_CONF);
  pthread_detach(pthread_self());
  CPU_ZERO(&mask);
  CPU_SET(1, &mask);
  if (sched_setaffinity(0, sizeof(mask), &mask) == -1) {
    printf("set affinity failed..\n");
  }
  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("new thread %d run on processor %d\n", getpid(), i);
    }
    while (1)
      ;
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<new_test_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1090 <pthread_self@plt>
mov    %eax,%edi
call   1070 <pthread_detach@plt>
lea    -0xc(%rbp),%rdi
call   1080 <CPU_ZERO@plt>
mov    $0x1,%edi
lea    -0xc(%rbp),%rsi
call   10c0 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1050 <sched_setaffinity@plt>
cmp    $0xffffffff,%eax
jne    1210 <new_test_thread+0x70>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1215 <new_test_thread+0x75>
lea    -0xc(%rbp),%rdi
call   1080 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   10b0 <sched_getaffinity@plt>
cmp    $0xffffffff,%eax
jne    1245 <new_test_thread+0xa5>
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    129b <new_test_thread+0xfb>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1060 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     1288 <new_test_thread+0xe8>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128d <new_test_thread+0xed>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    124c <new_test_thread+0xac>
jmp    12a0 <new_test_thread+0x100>
jmp    12a0 <new_test_thread+0x100>

x86 assembly (clang, -O2):
<new_test_thread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   10a0 <pthread_self@plt>
mov    %eax,%edi
call   1080 <pthread_detach@plt>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <CPU_ZERO@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
call   10d0 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1060 <sched_setaffinity@plt>
cmp    $0xffffffff,%eax
jne    1210 <new_test_thread+0x60>
lea    0xe25(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   10c0 <sched_getaffinity@plt>
cmp    $0xffffffff,%eax
je     1238 <new_test_thread+0x88>
test   %r15d,%r15d
jg     1249 <new_test_thread+0x99>
jmp    1290 <new_test_thread+0xe0>
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
test   %r15d,%r15d
jle    1290 <new_test_thread+0xe0>
lea    0x4(%rsp),%rbx
lea    0xdab(%rip),%r14
xor    %ebp,%ebp
jmp    1268 <new_test_thread+0xb8>
nopl   0x0(%rax)
add    $0x1,%ebp
cmp    %ebp,%r15d
je     1290 <new_test_thread+0xe0>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <CPU_ISSET@plt>
test   %rax,%rax
je     1260 <new_test_thread+0xb0>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1260 <new_test_thread+0xb0>
xchg   %ax,%ax
jmp    1290 <new_test_thread+0xe0>

LLVM IR (-O0):
@_SC_NPROCESSORS_CONF = global i32 0
@.str = [23 x i8] c"set affinity failed..\0A\00"
@.str.1 = [14 x i8] c"get failed..\0A\00"
@.str.2 = [35 x i8] c"new thread %d run on processor %d\0A\00"
define i8* @new_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)
%10 = call i32 @CPU_ZERO(i32* %3)
%11 = call i32 @CPU_SET(i32 1, i32* %3)
%12 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
br label %17
17: ; preds = %16
%18 = call i32 @CPU_ZERO(i32* %3)
%19 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
store i32 0, i32* %4
br label %24
24: ; preds = %37, %23
%25 = load i32, i32* %4
%26 = load i32, i32* %5
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %40
28: ; preds = %24
%29 = load i32, i32* %4
%30 = call i64 @CPU_ISSET(i32 %29, i32* %3)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = call i32 (...) @getpid()
%34 = load i32, i32* %4
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %34)
br label %36
36: ; preds = %32, %28
br label %37
37: ; preds = %36
%38 = load i32, i32* %4
%39 = add nsw i32 %38, 1
store i32 %39, i32* %4
br label %24
40: ; preds = %24
br label %41
41: ; preds = %40, %41
br label %41
}

LLVM IR (-O2):
@_SC_NPROCESSORS_CONF = global i32 0
@.str.2 = [35 x i8] c"new thread %d run on processor %d\0A\00"
@str = [13 x i8] c"get failed..\00"
@str.3 = [22 x i8] c"set affinity failed..\00"
define noalias i8* @new_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 @CPU_SET(i32 1, i32* %2)
%10 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.3, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = call i32 @CPU_ZERO(i32* %2)
%16 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %2)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = icmp sgt i32 %5, 0
br i1 %21, label %22, label %32
22: ; preds = %20, %29
%23 = phi i32 [ %30, %29 ], [ 0, %20 ]
%24 = call i64 @CPU_ISSET(i32 %23, i32* %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = call i32 (...) @getpid()
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 %23)
br label %29
29: ; preds = %22, %26
%30 = add nuw nsw i32 %23, 1
%31 = icmp eq i32 %30, %5
br i1 %31, label %32, label %22
32: ; preds = %29, %20
br label %33
33: ; preds = %32, %33
br label %33
}