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

/* Variables and functions */
int /*<<< orphan*/ CPU_CLR(int, int /*<<< orphan*/ *);
scalar_t__ CPU_COUNT(int /*<<< orphan*/ *);
scalar_t__ CPU_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
scalar_t__ ENOSYS;
scalar_t__ errno;
int /*<<< orphan*/ get_sgdt();
int /*<<< orphan*/ get_sidt();
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
scalar_t__ sched_getaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int sched_getcpu();
scalar_t__ sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int main(void) {
  int result = 0, cpu = 0, cpu2;
  cpu_set_t cs, cs2;
  if (sched_getaffinity(getpid(), sizeof(cs), &cs) != 0) {
    printf("sched_getaffinity() failed : %m\n");
    return -1;
  }
  while (CPU_COUNT(&cs) != 0) {
    if (CPU_ISSET(cpu, &cs)) {
      CPU_ZERO(&cs2);
      CPU_SET(cpu, &cs2);
      if (sched_setaffinity(getpid(), sizeof(cs2), &cs2) != 0) {
        printf("setaffinity(%d) failed: %m\n", cpu);
        result = 1;
      } else {
        cpu2 = sched_getcpu();
        if (cpu2 == -1 && errno == ENOSYS) {
          puts("getcpu syscall not implemented");
          return -1;
        }
        if (cpu2 != cpu) {
          printf("getcpu results %d not possible\n", cpu2);
          result = 1;
        }
      }
      printf("CPU %d\nIDT : %p\nGDT : %p\n\n", cpu, get_sidt(), get_sgdt());
      CPU_CLR(cpu, &cs);
    }
    ++cpu;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
mov    $0x4,%esi
lea    -0x14(%rbp),%rdx
call   10d0 <sched_getaffinity@plt>
cmp    $0x0,%rax
je     1228 <main+0x58>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <main+0x196>
jmp    122d <main+0x5d>
lea    -0x14(%rbp),%rdi
call   1070 <CPU_COUNT@plt>
cmp    $0x0,%rax
je     135f <main+0x18f>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   10a0 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     1351 <main+0x181>
lea    -0x18(%rbp),%rdi
call   10b0 <CPU_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10e0 <CPU_SET@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
mov    $0x4,%esi
lea    -0x18(%rbp),%rdx
call   1090 <sched_setaffinity@plt>
cmp    $0x0,%rax
je     12a9 <main+0xd9>
mov    -0xc(%rbp),%esi
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x8(%rbp)
jmp    1318 <main+0x148>
mov    $0x0,%al
call   10c0 <sched_getcpu@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    12ef <main+0x11f>
mov    0x2d14(%rip),%rax
mov    (%rax),%rax
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%rax
jne    12ef <main+0x11f>
lea    0xd5f(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <main+0x196>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1313 <main+0x143>
mov    -0x10(%rbp),%esi
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x8(%rbp)
jmp    1318 <main+0x148>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <get_sidt@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <get_sgdt@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   10f0 <CPU_CLR@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    122d <main+0x5d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rsp,%rdx
mov    %eax,%edi
mov    $0x4,%esi
call   10d0 <sched_getaffinity@plt>
test   %rax,%rax
je     1210 <main+0x40>
lea    0xe01(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
jmp    1302 <main+0x132>
mov    %rsp,%rdi
call   1070 <CPU_COUNT@plt>
test   %rax,%rax
je     1302 <main+0x132>
mov    %rsp,%rbx
lea    0x4(%rsp),%r14
lea    0xe4c(%rip),%r12
lea    0xdea(%rip),%r13
xor    %ebp,%ebp
jmp    1288 <main+0xb8>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <get_sidt@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <get_sgdt@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10f0 <CPU_CLR@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1070 <CPU_COUNT@plt>
test   %rax,%rax
je     1300 <main+0x130>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10a0 <CPU_ISSET@plt>
test   %rax,%rax
je     1278 <main+0xa8>
mov    %r14,%rdi
call   10b0 <CPU_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   10e0 <CPU_SET@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1090 <sched_setaffinity@plt>
test   %rax,%rax
jne    1240 <main+0x70>
xor    %eax,%eax
call   10c0 <sched_getcpu@plt>
cmp    $0xffffffff,%eax
jne    12ea <main+0x11a>
mov    0x2cfd(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2d03(%rip),%rdx
cmp    (%rdx),%rcx
je     1313 <main+0x143>
cmp    %eax,%ebp
je     124c <main+0x7c>
lea    0xd63(%rip),%rdi
mov    %eax,%esi
jmp    1245 <main+0x75>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd23(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    1302 <main+0x132>

LLVM IR (-O0):
@.str = [33 x i8] c"sched_getaffinity() failed : %m\0A\00"
@.str.1 = [28 x i8] c"setaffinity(%d) failed: %m\0A\00"
@errno = global i64 0
@ENOSYS = global i64 0
@.str.2 = [31 x i8] c"getcpu syscall not implemented\00"
@.str.3 = [32 x i8] c"getcpu results %d not possible\0A\00"
@.str.4 = [27 x i8] c"CPU %d\0AIDT : %p\0AGDT : %p\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
%7 = call i32 (...) @getpid()
%8 = call i64 @sched_getaffinity(i32 %7, i32 4, i32* %5)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %59
12: ; preds = %0
br label %13
13: ; preds = %55, %12
%14 = call i64 @CPU_COUNT(i32* %5)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %58
16: ; preds = %13
%17 = load i32, i32* %3
%18 = call i64 @CPU_ISSET(i32 %17, i32* %5)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %55
20: ; preds = %16
%21 = call i32 @CPU_ZERO(i32* %6)
%22 = load i32, i32* %3
%23 = call i32 @CPU_SET(i32 %22, i32* %6)
%24 = call i32 (...) @getpid()
%25 = call i64 @sched_setaffinity(i32 %24, i32 4, i32* %6)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i32, i32* %3
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %28)
store i32 1, i32* %2
br label %48
30: ; preds = %20
%31 = call i32 (...) @sched_getcpu()
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @ENOSYS
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @puts(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %59
40: ; preds = %34, %30
%41 = load i32, i32* %4
%42 = load i32, i32* %3
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* %4
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %45)
store i32 1, i32* %2
br label %47
47: ; preds = %44, %40
br label %48
48: ; preds = %47, %27
%49 = load i32, i32* %3
%50 = call i32 (...) @get_sidt()
%51 = call i32 (...) @get_sgdt()
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %49, i32 %50, i32 %51)
%53 = load i32, i32* %3
%54 = call i32 @CPU_CLR(i32 %53, i32* %5)
br label %55
55: ; preds = %48, %16
%56 = load i32, i32* %3
%57 = add nsw i32 %56, 1
store i32 %57, i32* %3
br label %13
58: ; preds = %13
store i32 0, i32* %1
br label %59
59: ; preds = %58, %38, %10
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@.str = [33 x i8] c"sched_getaffinity() failed : %m\0A\00"
@.str.1 = [28 x i8] c"setaffinity(%d) failed: %m\0A\00"
@errno = global i64 0
@ENOSYS = global i64 0
@.str.2 = [31 x i8] c"getcpu syscall not implemented\00"
@.str.3 = [32 x i8] c"getcpu results %d not possible\0A\00"
@.str.4 = [27 x i8] c"CPU %d\0AIDT : %p\0AGDT : %p\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (...) @getpid()
%6 = call i64 @sched_getaffinity(i32 %5, i32 4, i32* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i64 @CPU_COUNT(i32* %1)
%10 = icmp eq i64 %9, 0
br i1 %10, label %47, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %47
13: ; preds = %8, %43
%14 = phi i32 [ %44, %43 ], [ 0, %8 ]
%15 = call i64 @CPU_ISSET(i32 %14, i32* %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %43, label %17
17: ; preds = %13
%18 = call i32 @CPU_ZERO(i32* %2)
%19 = call i32 @CPU_SET(i32 %14, i32* %2)
%20 = call i32 (...) @getpid()
%21 = call i64 @sched_setaffinity(i32 %20, i32 4, i32* %2)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %38
25: ; preds = %17
%26 = call i32 (...) @sched_getcpu()
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENOSYS
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %47
34: ; preds = %28, %25
%35 = icmp eq i32 %26, %14
br i1 %35, label %38, label %36
36: ; preds = %34
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %26)
br label %38
38: ; preds = %34, %36, %23
%39 = call i32 (...) @get_sidt()
%40 = call i32 (...) @get_sgdt()
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %14, i32 %39, i32 %40)
%42 = call i32 @CPU_CLR(i32 %14, i32* %1)
br label %43
43: ; preds = %38, %13
%44 = add nuw nsw i32 %14, 1
%45 = call i64 @CPU_COUNT(i32* %1)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %13
47: ; preds = %43, %8, %32, %11
%48 = phi i32 [ -1, %11 ], [ -1, %32 ], [ 0, %8 ], [ 0, %43 ]
ret i32 %48
}