#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 */
struct futex_t {
  int dummy;
};
struct args {
  int dummy;
};
typedef int /*<<< orphan*/ mask;
typedef int /*<<< orphan*/ cpu_set_t;

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SYS_gettid;
int /*<<< orphan*/ futex_wait(struct futex_t **);
int /*<<< orphan*/ futex_wake(struct futex_t **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, unsigned long);
unsigned long pthread_self();
scalar_t__ pthread_setaffinity_np(unsigned long, int, int /*<<< orphan*/ *);
int syscall(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int /*<<< orphan*/);
void *print_msg(void *arg) {
  struct futex_t *ftx = (struct futex_t *)arg;
  unsigned long count = 0;
  struct args *curr_arg = (struct args *)arg;
  cpu_set_t mask;
  printf("tid = %d(%ld) START\n", syscall(SYS_gettid), pthread_self());
  CPU_ZERO(&mask);
  CPU_SET(0, &mask);
  if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
    perror("sched_setaffinity");
  } else {
    printf("tid = %d affine to CPU %d\n", syscall(SYS_gettid), 0);
  }
  while (1) {
    futex_wait(&ftx);
    printf("tid = %d, count = %ld\n", syscall(SYS_gettid), count);
    count++;
    usleep(0);
    futex_wake(&ftx);
  }
}


x86 assembly (clang, -O0):
<print_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1050 <syscall@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <pthread_self@plt>
mov    -0x28(%rbp),%esi
mov    %rax,%rdx
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
lea    -0x24(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
xor    %edi,%edi
lea    -0x24(%rbp),%rsi
call   10b0 <CPU_SET@plt>
mov    $0x0,%al
call   1080 <pthread_self@plt>
mov    %rax,%rdi
mov    $0x4,%esi
lea    -0x24(%rbp),%rdx
call   1030 <pthread_setaffinity_np@plt>
cmp    $0x0,%rax
jge    1235 <print_msg+0x95>
lea    0xdea(%rip),%rdi
call   1090 <perror@plt>
jmp    1255 <print_msg+0xb5>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1050 <syscall@plt>
mov    %eax,%esi
lea    0xddb(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <printf@plt>
jmp    125a <print_msg+0xba>
lea    -0x10(%rbp),%rdi
call   1060 <futex_wait@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1050 <syscall@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
lea    0xdc4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    %rax,-0x18(%rbp)
xor    %edi,%edi
call   10c0 <usleep@plt>
lea    -0x10(%rbp),%rdi
call   10a0 <futex_wake@plt>
jmp    125a <print_msg+0xba>

x86 assembly (clang, -O2):
<print_msg>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,0x8(%rsp)
mov    0x2e31(%rip),%r12
mov    (%r12),%edi
call   1050 <syscall@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <pthread_self@plt>
lea    0xe30(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
call   1040 <printf@plt>
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <CPU_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10b0 <CPU_SET@plt>
xor    %eax,%eax
call   1080 <pthread_self@plt>
mov    %rax,%rdi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1030 <pthread_setaffinity_np@plt>
test   %rax,%rax
js     1273 <print_msg+0xd3>
mov    (%r12),%edi
call   1050 <syscall@plt>
lea    0xe0a(%rip),%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1040 <printf@plt>
xor    %ebx,%ebx
lea    0x8(%rsp),%r14
lea    0xe0e(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1060 <futex_wait@plt>
mov    (%r12),%edi
call   1050 <syscall@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
add    $0x1,%rbx
xor    %edi,%edi
call   10c0 <usleep@plt>
mov    %r14,%rdi
call   10a0 <futex_wake@plt>
jmp    1240 <print_msg+0xa0>
lea    0xd9b(%rip),%rdi
call   1090 <perror@plt>
jmp    1226 <print_msg+0x86>

LLVM IR (-O0):
%struct.futex_t = type { i32 }
%struct.args = type { i32 }
@.str = [21 x i8] c"tid = %d(%ld) START\0A\00"
@SYS_gettid = global i32 0
@.str.1 = [18 x i8] c"sched_setaffinity\00"
@.str.2 = [27 x i8] c"tid = %d affine to CPU %d\0A\00"
@.str.3 = [23 x i8] c"tid = %d, count = %ld\0A\00"
define i8* @print_msg(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.futex_t*
%4 = alloca i64
%5 = alloca %struct.args*
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = bitcast i8* %7 to %struct.futex_t*
store %struct.futex_t* %8, %struct.futex_t** %3
store i64 0, i64* %4
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to %struct.args*
store %struct.args* %10, %struct.args** %5
%11 = load i32, i32* @SYS_gettid
%12 = call i32 @syscall(i32 %11)
%13 = call i64 (...) @pthread_self()
%14 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %12, i64 %13)
%15 = call i32 @CPU_ZERO(i32* %6)
%16 = call i32 @CPU_SET(i32 0, i32* %6)
%17 = call i64 (...) @pthread_self()
%18 = call i64 @pthread_setaffinity_np(i64 %17, i32 4, i32* %6)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %26
22: ; preds = %1
%23 = load i32, i32* @SYS_gettid
%24 = call i32 @syscall(i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %24, i64 0)
br label %26
26: ; preds = %22, %20
br label %27
27: ; preds = %26, %27
%28 = call i32 @futex_wait(%struct.futex_t** %3)
%29 = load i32, i32* @SYS_gettid
%30 = call i32 @syscall(i32 %29)
%31 = load i64, i64* %4
%32 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %30, i64 %31)
%33 = load i64, i64* %4
%34 = add i64 %33, 1
store i64 %34, i64* %4
%35 = call i32 @usleep(i32 0)
%36 = call i32 @futex_wake(%struct.futex_t** %3)
br label %27
}

LLVM IR (-O2):
%struct.futex_t = type { i32 }
@.str = [21 x i8] c"tid = %d(%ld) START\0A\00"
@SYS_gettid = global i32 0
@.str.1 = [18 x i8] c"sched_setaffinity\00"
@.str.2 = [27 x i8] c"tid = %d affine to CPU %d\0A\00"
@.str.3 = [23 x i8] c"tid = %d, count = %ld\0A\00"
define noalias i8* @print_msg(i8* %0) {
%2 = alloca %struct.futex_t*
%3 = alloca i32
%4 = bitcast %struct.futex_t** %2 to i8*
%5 = bitcast %struct.futex_t** %2 to i8**
store i8* %0, i8** %5
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @SYS_gettid
%8 = tail call i32 @syscall(i32 %7)
%9 = tail call i64 (...) @pthread_self()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %8, i64 %9)
%11 = call i32 @CPU_ZERO(i32* %3)
%12 = call i32 @CPU_SET(i32 0, i32* %3)
%13 = call i64 (...) @pthread_self()
%14 = call i64 @pthread_setaffinity_np(i64 %13, i32 4, i32* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %22
18: ; preds = %1
%19 = load i32, i32* @SYS_gettid
%20 = call i32 @syscall(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %20, i64 0)
br label %22
22: ; preds = %18, %16
br label %23
23: ; preds = %22, %23
%24 = phi i64 [ %29, %23 ], [ 0, %22 ]
%25 = call i32 @futex_wait(%struct.futex_t** %2)
%26 = load i32, i32* @SYS_gettid
%27 = call i32 @syscall(i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %27, i64 %24)
%29 = add i64 %24, 1
%30 = call i32 @usleep(i32 0)
%31 = call i32 @futex_wake(%struct.futex_t** %2)
br label %23
}