#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..");
  }
  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);
    }
    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    0x2e16(%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    1220 <new_test_thread+0x70>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1225 <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    1255 <new_test_thread+0xa5>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12ab <new_test_thread+0xfb>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1060 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     1298 <new_test_thread+0xe8>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    129d <new_test_thread+0xed>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    125c <new_test_thread+0xac>
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    1225 <new_test_thread+0x75>

x86 assembly (clang, -O2):
<new_test_thread>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10a0 <pthread_self@plt>
mov    %eax,%edi
call   1080 <pthread_detach@plt>
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <CPU_ZERO@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
call   10d0 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbp,%rdx
call   1060 <sched_setaffinity@plt>
cmp    $0xffffffff,%eax
jne    1226 <new_test_thread+0x66>
lea    0xde1(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%r14
lea    0xe07(%rip),%r15
lea    0xddd(%rip),%r12
jmp    124a <new_test_thread+0x8a>
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    %r14,%rdi
call   1090 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   10c0 <sched_getaffinity@plt>
cmp    $0xffffffff,%eax
je     1270 <new_test_thread+0xb0>
test   %ebx,%ebx
jg     127c <new_test_thread+0xbc>
jmp    1240 <new_test_thread+0x80>
nopl   0x0(%rax)
mov    %r15,%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
jle    1240 <new_test_thread+0x80>
xor    %ebp,%ebp
jmp    1287 <new_test_thread+0xc7>
add    $0x1,%ebp
cmp    %ebp,%ebx
je     1240 <new_test_thread+0x80>
mov    %ebp,%edi
mov    %r14,%rsi
call   1070 <CPU_ISSET@plt>
test   %rax,%rax
je     1280 <new_test_thread+0xc0>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r12,%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1280 <new_test_thread+0xc0>

LLVM IR (-O0):
@_SC_NPROCESSORS_CONF = global i32 0
@.str = [22 x i8] c"set affinity failed..\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 ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
br label %17
17: ; preds = %16, %40
%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
%41 = call i32 @sleep(i32 1)
br label %17
}

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