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

/* 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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
scalar_t__ pthread_getaffinity_np(scalar_t__, int, int /*<<< orphan*/ *);
scalar_t__ pthread_self();
scalar_t__ pthread_setaffinity_np(scalar_t__, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
void *myfun(void *arg) {
  cpu_set_t mask;
  cpu_set_t get;
  char buf[256];
  int i;
  int j;
  int num = sysconf(_SC_NPROCESSORS_CONF);
  printf("system has %d processor(s)\n", num);
  for (i = 0; i < num; i++) {
    CPU_ZERO(&mask);
    CPU_SET(i, &mask);
    if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
      fprintf(stderr, "set thread affinity failed\n");
    }
    CPU_ZERO(&get);
    if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
      fprintf(stderr, "get thread affinity failed\n");
    }
    for (j = 0; j < num; j++) {
      if (CPU_ISSET(j, &get)) {
        printf("thread %d is running in processor %d\n", (int)pthread_self(),
               j);
      }
    }
    j = 0;
    while (j++ < 100000000) {
      memset(buf, 0, sizeof(buf));
    }
  }
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<myfun>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,-0x12c(%rbp)
mov    -0x12c(%rbp),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x124(%rbp)
mov    -0x124(%rbp),%eax
cmp    -0x12c(%rbp),%eax
jge    1354 <myfun+0x1a4>
lea    -0x14(%rbp),%rdi
call   1090 <CPU_ZERO@plt>
mov    -0x124(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   10d0 <CPU_SET@plt>
mov    $0x0,%al
call   10a0 <pthread_self@plt>
mov    %rax,%rdi
mov    $0x4,%esi
lea    -0x14(%rbp),%rdx
call   1030 <pthread_setaffinity_np@plt>
cmp    $0x0,%rax
jge    1252 <myfun+0xa2>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
call   1070 <fprintf@plt>
lea    -0x18(%rbp),%rdi
call   1090 <CPU_ZERO@plt>
mov    $0x0,%al
call   10a0 <pthread_self@plt>
mov    %rax,%rdi
mov    $0x4,%esi
lea    -0x18(%rbp),%rdx
call   10c0 <pthread_getaffinity_np@plt>
cmp    $0x0,%rax
jge    1292 <myfun+0xe2>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x128(%rbp)
mov    -0x128(%rbp),%eax
cmp    -0x12c(%rbp),%eax
jge    12fd <myfun+0x14d>
mov    -0x128(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     12e4 <myfun+0x134>
mov    $0x0,%al
call   10a0 <pthread_self@plt>
mov    %eax,%esi
mov    -0x128(%rbp),%edx
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12e9 <myfun+0x139>
mov    -0x128(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x128(%rbp)
jmp    129c <myfun+0xec>
movl   $0x0,-0x128(%rbp)
mov    -0x128(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x128(%rbp)
cmp    $0x5f5e100,%eax
jge    133b <myfun+0x18b>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
jmp    1307 <myfun+0x157>
jmp    1340 <myfun+0x190>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    11f1 <myfun+0x41>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myfun>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,%ebx
lea    0xe28(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
jle    1306 <myfun+0x156>
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xe5a(%rip),%r15
xor    %r14d,%r14d
nopl   (%rax)
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <CPU_ZERO@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   10d0 <CPU_SET@plt>
xor    %eax,%eax
call   10a0 <pthread_self@plt>
mov    %rax,%rdi
mov    $0x4,%esi
mov    %rbp,%rdx
call   1030 <pthread_setaffinity_np@plt>
test   %rax,%rax
jns    1249 <myfun+0x99>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
call   1070 <fprintf@plt>
mov    %r14d,0x4(%rsp)
mov    %r12,%rdi
call   1090 <CPU_ZERO@plt>
xor    %eax,%eax
call   10a0 <pthread_self@plt>
mov    %rax,%rdi
mov    $0x4,%esi
mov    %r12,%rdx
call   10c0 <pthread_getaffinity_np@plt>
test   %rax,%rax
jns    1287 <myfun+0xd7>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
call   1070 <fprintf@plt>
xor    %r14d,%r14d
jmp    1299 <myfun+0xe9>
nopl   0x0(%rax)
add    $0x1,%r14d
cmp    %r14d,%ebx
je     12d0 <myfun+0x120>
mov    %r14d,%edi
mov    %r12,%rsi
call   1060 <CPU_ISSET@plt>
test   %rax,%rax
je     1290 <myfun+0xe0>
xor    %eax,%eax
call   10a0 <pthread_self@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1290 <myfun+0xe0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x5f5e100,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
add    $0xffffffff,%ebp
jne    12e0 <myfun+0x130>
mov    0x4(%rsp),%r14d
add    $0x1,%r14d
cmp    %ebx,%r14d
jne    1200 <myfun+0x50>
xor    %edi,%edi
call   1080 <pthread_exit@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_SC_NPROCESSORS_CONF = global i32 0
@.str = [28 x i8] c"system has %d processor(s)\0A\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"set thread affinity failed\0A\00"
@.str.2 = [28 x i8] c"get thread affinity failed\0A\00"
@.str.3 = [38 x i8] c"thread %d is running in processor %d\0A\00"
define i8* @myfun(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i32, i32* @_SC_NPROCESSORS_CONF
%11 = call i32 @sysconf(i32 %10)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 0, i32* %7
br label %14
14: ; preds = %63, %1
%15 = load i32, i32* %7
%16 = load i32, i32* %9
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %66
18: ; preds = %14
%19 = call i32 @CPU_ZERO(i32* %4)
%20 = load i32, i32* %7
%21 = call i32 @CPU_SET(i32 %20, i32* %4)
%22 = call i64 (...) @pthread_self()
%23 = call i64 @pthread_setaffinity_np(i64 %22, i32 4, i32* %4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %25, %18
%29 = call i32 @CPU_ZERO(i32* %5)
%30 = call i64 (...) @pthread_self()
%31 = call i64 @pthread_getaffinity_np(i64 %30, i32 4, i32* %5)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %33, %28
store i32 0, i32* %8
br label %37
37: ; preds = %51, %36
%38 = load i32, i32* %8
%39 = load i32, i32* %9
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %54
41: ; preds = %37
%42 = load i32, i32* %8
%43 = call i64 @CPU_ISSET(i32 %42, i32* %5)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = call i64 (...) @pthread_self()
%47 = trunc i64 %46 to i32
%48 = load i32, i32* %8
%49 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %47, i32 %48)
br label %50
50: ; preds = %45, %41
br label %51
51: ; preds = %50
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
br label %37
54: ; preds = %37
store i32 0, i32* %8
br label %55
55: ; preds = %59, %54
%56 = load i32, i32* %8
%57 = add nsw i32 %56, 1
store i32 %57, i32* %8
%58 = icmp slt i32 %56, 100000000
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%61 = call i32 @memset(i8* %60, i32 0, i32 256)
br label %55
62: ; preds = %55
br label %63
63: ; preds = %62
%64 = load i32, i32* %7
%65 = add nsw i32 %64, 1
store i32 %65, i32* %7
br label %14
66: ; preds = %14
%67 = call i32 @pthread_exit(i32* null)
%68 = load i8*, i8** %2
ret i8* %68
}

LLVM IR (-O2):
@_SC_NPROCESSORS_CONF = global i32 0
@.str = [28 x i8] c"system has %d processor(s)\0A\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"set thread affinity failed\0A\00"
@.str.2 = [28 x i8] c"get thread affinity failed\0A\00"
@.str.3 = [38 x i8] c"thread %d is running in processor %d\0A\00"
define noalias i8* @myfun(i8* nocapture readnone %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* @_SC_NPROCESSORS_CONF
%9 = tail call i32 @sysconf(i32 %8)
%10 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp sgt i32 %9, 0
br i1 %11, label %12, label %50
12: ; preds = %1, %47
%13 = phi i32 [ %48, %47 ], [ 0, %1 ]
%14 = call i32 @CPU_ZERO(i32* %2)
%15 = call i32 @CPU_SET(i32 %13, i32* %2)
%16 = call i64 (...) @pthread_self()
%17 = call i64 @pthread_setaffinity_np(i64 %16, i32 4, i32* %2)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %19, %12
%23 = call i32 @CPU_ZERO(i32* %3)
%24 = call i64 (...) @pthread_self()
%25 = call i64 @pthread_getaffinity_np(i64 %24, i32 4, i32* %3)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %22, %27
br label %31
31: ; preds = %30, %39
%32 = phi i32 [ %40, %39 ], [ 0, %30 ]
%33 = call i64 @CPU_ISSET(i32 %32, i32* %3)
%34 = icmp eq i64 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = call i64 (...) @pthread_self()
%37 = trunc i64 %36 to i32
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 %32)
br label %39
39: ; preds = %31, %35
%40 = add nuw nsw i32 %32, 1
%41 = icmp eq i32 %40, %9
br i1 %41, label %42, label %31
42: ; preds = %39, %42
%43 = phi i32 [ %44, %42 ], [ 0, %39 ]
%44 = add nuw nsw i32 %43, 1
%45 = call i32 @memset(i8* %7, i32 0, i32 256)
%46 = icmp eq i32 %44, 100000000
br i1 %46, label %47, label %42
47: ; preds = %42
%48 = add nuw nsw i32 %13, 1
%49 = icmp eq i32 %48, %9
br i1 %49, label %50, label %12
50: ; preds = %47, %1
%51 = call i32 @pthread_exit(i32* null)
ret i8* undef
}