#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 sched_param {
  int sched_priority;
};
typedef int /*<<< orphan*/ cpu_set_t;
typedef int /*<<< orphan*/ cpu_set;

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(long, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SCHED_FIFO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pthread_self();
scalar_t__ pthread_setaffinity_np(int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ *);
scalar_t__ pthread_setschedparam(int /*<<< orphan*/, int /*<<< orphan*/,
                                 struct sched_param *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
void *thread_function(void *arg) {
  long int cpu = (long int)arg;
  cpu_set_t cpu_set;
  struct sched_param param;
  int i;
  CPU_ZERO(&cpu_set);
  CPU_SET(cpu, &cpu_set);
  if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set) != 0) {
    perror("pthread_setaffinity_np");
    exit(EXIT_FAILURE);
  }
  param.sched_priority = 99;
  if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) != 0) {
    perror("pthread_setschedparam");
    exit(EXIT_FAILURE);
  }
  fprintf(stderr, "Thread sur CPU %ld va demarrer...\n", cpu);
  sleep(1);
  for (i = 0; i < 500000000; i++)
    ;
  return NULL;
}


x86 assembly (clang, -O0):
<thread_function>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1060 <CPU_ZERO@plt>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   10a0 <CPU_SET@plt>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%edi
mov    $0x4,%esi
lea    -0x14(%rbp),%rdx
call   1030 <pthread_setaffinity_np@plt>
cmp    $0x0,%rax
je     11f5 <thread_function+0x65>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movl   $0x63,-0x18(%rbp)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <pthread_setschedparam@plt>
cmp    $0x0,%rax
je     123b <thread_function+0xab>
lea    0xdef(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdde(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1dcd6500,-0x1c(%rbp)
jge    1285 <thread_function+0xf5>
jmp    1277 <thread_function+0xe7>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1265 <thread_function+0xd5>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_function>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <CPU_ZERO@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <CPU_SET@plt>
xor    %eax,%eax
call   1070 <pthread_self@plt>
mov    %eax,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1030 <pthread_setaffinity_np@plt>
test   %rax,%rax
jne    121c <thread_function+0x8c>
movl   $0x63,(%rsp)
xor    %eax,%eax
call   1070 <pthread_self@plt>
mov    0x2e09(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1040 <pthread_setschedparam@plt>
test   %rax,%rax
jne    1225 <thread_function+0x95>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xddd(%rip),%rdi
jmp    122c <thread_function+0x9c>
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [23 x i8] c"pthread_setaffinity_np\00"
@EXIT_FAILURE = global i32 0
@SCHED_FIFO = global i32 0
@.str.1 = [22 x i8] c"pthread_setschedparam\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"Thread sur CPU %ld va demarrer...\0A\00"
define i8* @thread_function(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca %struct.sched_param
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = ptrtoint i8* %7 to i64
store i64 %8, i64* %3
%9 = call i32 @CPU_ZERO(i32* %4)
%10 = load i64, i64* %3
%11 = call i32 @CPU_SET(i64 %10, i32* %4)
%12 = call i32 (...) @pthread_self()
%13 = call i64 @pthread_setaffinity_np(i32 %12, i32 4, i32* %4)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %1
%20 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %5, i32 0, i32 0
store i32 99, i32* %20
%21 = call i32 (...) @pthread_self()
%22 = load i32, i32* @SCHED_FIFO
%23 = call i64 @pthread_setschedparam(i32 %21, i32 %22, %struct.sched_param* %5)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %19
%30 = load i32, i32* @stderr
%31 = load i64, i64* %3
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i64 %31)
%33 = call i32 @sleep(i32 1)
store i32 0, i32* %6
br label %34
34: ; preds = %38, %29
%35 = load i32, i32* %6
%36 = icmp slt i32 %35, 500000000
br i1 %36, label %37, label %41
37: ; preds = %34
br label %38
38: ; preds = %37
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
store i32 %40, i32* %6
br label %34
41: ; preds = %34
ret i8* null
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@.str = [23 x i8] c"pthread_setaffinity_np\00"
@EXIT_FAILURE = global i32 0
@SCHED_FIFO = global i32 0
@.str.1 = [22 x i8] c"pthread_setschedparam\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"Thread sur CPU %ld va demarrer...\0A\00"
define noalias i8* @thread_function(i8* %0) {
%2 = alloca i32
%3 = alloca %struct.sched_param
%4 = ptrtoint i8* %0 to i64
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.sched_param* %3 to i8*
%7 = call i32 @CPU_ZERO(i32* %2)
%8 = call i32 @CPU_SET(i64 %4, i32* %2)
%9 = call i32 (...) @pthread_self()
%10 = call i64 @pthread_setaffinity_np(i32 %9, i32 4, i32* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %1
%17 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i64 0, i32 0
store i32 99, i32* %17
%18 = call i32 (...) @pthread_self()
%19 = load i32, i32* @SCHED_FIFO
%20 = call i64 @pthread_setschedparam(i32 %18, i32 %19, %struct.sched_param* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %16
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i64 %4)
%29 = call i32 @sleep(i32 1)
ret i8* null
}