#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ hwloc_topology_t;
typedef TYPE_1__ *hwloc_obj_t;
typedef int /*<<< orphan*/ hwloc_cpuset_t;
struct TYPE_3__ {
  int /*<<< orphan*/ cpuset;
};

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ HWLOC_CPUBIND_PROCESS;
int /*<<< orphan*/ HWLOC_OBJ_CORE;
int /*<<< orphan*/ emit_binding(int /*<<< orphan*/ *);
int getpid();
int /*<<< orphan*/ hwloc_bitmap_alloc();
int /*<<< orphan*/ hwloc_bitmap_copy(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ hwloc_bitmap_free(int /*<<< orphan*/);
int /*<<< orphan*/ hwloc_bitmap_singlify(int /*<<< orphan*/);
int /*<<< orphan*/ hwloc_get_cpubind(int /*<<< orphan*/, int /*<<< orphan*/,
                                     int /*<<< orphan*/);
unsigned int hwloc_get_nbobjs_by_type(int /*<<< orphan*/, int /*<<< orphan*/);
TYPE_1__ *hwloc_get_obj_by_type(int /*<<< orphan*/, int /*<<< orphan*/,
                                unsigned int);
int /*<<< orphan*/ hwloc_set_cpubind(int /*<<< orphan*/, int /*<<< orphan*/,
                                     int /*<<< orphan*/);
int /*<<< orphan*/ hwloc_topology_destroy(int /*<<< orphan*/);
int /*<<< orphan*/ hwloc_topology_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ hwloc_topology_load(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  int erc = EXIT_SUCCESS;
  unsigned ncores = 0;
  hwloc_cpuset_t cpu_set = hwloc_bitmap_alloc(),
                 first_bind = hwloc_bitmap_alloc();
  hwloc_topology_t topology;
  hwloc_obj_t last_core;
  hwloc_topology_init(&topology);
  hwloc_topology_load(topology);
  ncores = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_CORE);
  last_core = hwloc_get_obj_by_type(topology, HWLOC_OBJ_CORE, ncores - 1);
  hwloc_get_cpubind(topology, first_bind, HWLOC_CPUBIND_PROCESS);
  printf("starting rebinding test on pid %d\n", getpid());
  emit_binding(&topology);
  printf("changing binding...\n");
  hwloc_bitmap_copy(cpu_set, last_core->cpuset);
  hwloc_bitmap_singlify(cpu_set);
  hwloc_set_cpubind(topology, cpu_set, HWLOC_CPUBIND_PROCESS);
  emit_binding(&topology);
  printf("reverting binding...\n");
  hwloc_set_cpubind(topology, first_bind, HWLOC_CPUBIND_PROCESS);
  emit_binding(&topology);
  printf("done with rebinding test\n");
  hwloc_bitmap_free(cpu_set);
  hwloc_bitmap_free(first_bind);
  hwloc_topology_destroy(topology);
  return erc;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <hwloc_bitmap_alloc@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <hwloc_bitmap_alloc@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <hwloc_topology_init@plt>
mov    -0x18(%rbp),%edi
call   1070 <hwloc_topology_load@plt>
mov    -0x18(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1050 <hwloc_get_nbobjs_by_type@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   10f0 <hwloc_get_obj_by_type@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
call   10b0 <hwloc_get_cpubind@plt>
mov    $0x0,%al
call   1080 <getpid@plt>
mov    %eax,%esi
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    -0x18(%rbp),%rdi
call   10d0 <emit_binding@plt>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x10(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
call   10a0 <hwloc_bitmap_copy@plt>
mov    -0x10(%rbp),%edi
call   10e0 <hwloc_bitmap_singlify@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
call   10c0 <hwloc_set_cpubind@plt>
lea    -0x18(%rbp),%rdi
call   10d0 <emit_binding@plt>
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2d02(%rip),%rax
mov    (%rax),%edx
call   10c0 <hwloc_set_cpubind@plt>
lea    -0x18(%rbp),%rdi
call   10d0 <emit_binding@plt>
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x10(%rbp),%edi
call   1040 <hwloc_bitmap_free@plt>
mov    -0x14(%rbp),%edi
call   1040 <hwloc_bitmap_free@plt>
mov    -0x18(%rbp),%edi
call   1100 <hwloc_topology_destroy@plt>
mov    -0x8(%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
mov    0x2dce(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1030 <hwloc_bitmap_alloc@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <hwloc_bitmap_alloc@plt>
mov    %eax,%r12d
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1060 <hwloc_topology_init@plt>
mov    0x4(%rsp),%edi
call   1080 <hwloc_topology_load@plt>
mov    0x4(%rsp),%edi
mov    0x2d9f(%rip),%rbx
mov    (%rbx),%esi
call   1050 <hwloc_get_nbobjs_by_type@plt>
mov    0x4(%rsp),%edi
mov    (%rbx),%esi
lea    -0x1(%rax),%edx
call   1100 <hwloc_get_obj_by_type@plt>
mov    %rax,%rbx
mov    0x4(%rsp),%edi
mov    0x2d8c(%rip),%r13
mov    0x0(%r13),%edx
mov    %r12d,%esi
call   10c0 <hwloc_get_cpubind@plt>
xor    %eax,%eax
call   1090 <getpid@plt>
lea    0xd8a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %r15,%rdi
call   10e0 <emit_binding@plt>
lea    0xd95(%rip),%rdi
call   1070 <puts@plt>
mov    (%rbx),%esi
mov    %ebp,%edi
call   10b0 <hwloc_bitmap_copy@plt>
mov    %ebp,%edi
call   10f0 <hwloc_bitmap_singlify@plt>
mov    0x4(%rsp),%edi
mov    0x0(%r13),%edx
mov    %ebp,%esi
call   10d0 <hwloc_set_cpubind@plt>
mov    %r15,%rdi
call   10e0 <emit_binding@plt>
lea    0xd76(%rip),%rdi
call   1070 <puts@plt>
mov    0x4(%rsp),%edi
mov    0x0(%r13),%edx
mov    %r12d,%esi
call   10d0 <hwloc_set_cpubind@plt>
mov    %r15,%rdi
call   10e0 <emit_binding@plt>
lea    0xd67(%rip),%rdi
call   1070 <puts@plt>
mov    %ebp,%edi
call   1040 <hwloc_bitmap_free@plt>
mov    %r12d,%edi
call   1040 <hwloc_bitmap_free@plt>
mov    0x4(%rsp),%edi
call   1110 <hwloc_topology_destroy@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
@EXIT_SUCCESS = global i32 0
@HWLOC_OBJ_CORE = global i32 0
@HWLOC_CPUBIND_PROCESS = global i32 0
@.str = [35 x i8] c"starting rebinding test on pid %d\0A\00"
@.str.1 = [21 x i8] c"changing binding...\0A\00"
@.str.2 = [22 x i8] c"reverting binding...\0A\00"
@.str.3 = [26 x i8] c"done with rebinding test\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.TYPE_3__*
store i32 0, i32* %1
%8 = load i32, i32* @EXIT_SUCCESS
store i32 %8, i32* %2
store i32 0, i32* %3
%9 = call i32 (...) @hwloc_bitmap_alloc()
store i32 %9, i32* %4
%10 = call i32 (...) @hwloc_bitmap_alloc()
store i32 %10, i32* %5
%11 = call i32 @hwloc_topology_init(i32* %6)
%12 = load i32, i32* %6
%13 = call i32 @hwloc_topology_load(i32 %12)
%14 = load i32, i32* %6
%15 = load i32, i32* @HWLOC_OBJ_CORE
%16 = call i32 @hwloc_get_nbobjs_by_type(i32 %14, i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %6
%18 = load i32, i32* @HWLOC_OBJ_CORE
%19 = load i32, i32* %3
%20 = sub i32 %19, 1
%21 = call %struct.TYPE_3__* @hwloc_get_obj_by_type(i32 %17, i32 %18, i32 %20)
store %struct.TYPE_3__* %21, %struct.TYPE_3__** %7
%22 = load i32, i32* %6
%23 = load i32, i32* %5
%24 = load i32, i32* @HWLOC_CPUBIND_PROCESS
%25 = call i32 @hwloc_get_cpubind(i32 %22, i32 %23, i32 %24)
%26 = call i32 (...) @getpid()
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = call i32 @emit_binding(i32* %6)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %4
%31 = load %struct.TYPE_3__*, %struct.TYPE_3__** %7
%32 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @hwloc_bitmap_copy(i32 %30, i32 %33)
%35 = load i32, i32* %4
%36 = call i32 @hwloc_bitmap_singlify(i32 %35)
%37 = load i32, i32* %6
%38 = load i32, i32* %4
%39 = load i32, i32* @HWLOC_CPUBIND_PROCESS
%40 = call i32 @hwloc_set_cpubind(i32 %37, i32 %38, i32 %39)
%41 = call i32 @emit_binding(i32* %6)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* %6
%44 = load i32, i32* %5
%45 = load i32, i32* @HWLOC_CPUBIND_PROCESS
%46 = call i32 @hwloc_set_cpubind(i32 %43, i32 %44, i32 %45)
%47 = call i32 @emit_binding(i32* %6)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32, i32* %4
%50 = call i32 @hwloc_bitmap_free(i32 %49)
%51 = load i32, i32* %5
%52 = call i32 @hwloc_bitmap_free(i32 %51)
%53 = load i32, i32* %6
%54 = call i32 @hwloc_topology_destroy(i32 %53)
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
@EXIT_SUCCESS = global i32 0
@HWLOC_OBJ_CORE = global i32 0
@HWLOC_CPUBIND_PROCESS = global i32 0
@.str = [35 x i8] c"starting rebinding test on pid %d\0A\00"
@str = [20 x i8] c"changing binding...\00"
@str.4 = [21 x i8] c"reverting binding...\00"
@str.5 = [25 x i8] c"done with rebinding test\00"
define i32 @main() {
%1 = alloca i32
%2 = load i32, i32* @EXIT_SUCCESS
%3 = tail call i32 (...) @hwloc_bitmap_alloc()
%4 = tail call i32 (...) @hwloc_bitmap_alloc()
%5 = bitcast i32* %1 to i8*
%6 = call i32 @hwloc_topology_init(i32* %1)
%7 = load i32, i32* %1
%8 = call i32 @hwloc_topology_load(i32 %7)
%9 = load i32, i32* %1
%10 = load i32, i32* @HWLOC_OBJ_CORE
%11 = call i32 @hwloc_get_nbobjs_by_type(i32 %9, i32 %10)
%12 = load i32, i32* %1
%13 = load i32, i32* @HWLOC_OBJ_CORE
%14 = add i32 %11, -1
%15 = call %struct.TYPE_3__* @hwloc_get_obj_by_type(i32 %12, i32 %13, i32 %14)
%16 = load i32, i32* %1
%17 = load i32, i32* @HWLOC_CPUBIND_PROCESS
%18 = call i32 @hwloc_get_cpubind(i32 %16, i32 %4, i32 %17)
%19 = call i32 (...) @getpid()
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32 @emit_binding(i32* %1)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%23 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %15, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @hwloc_bitmap_copy(i32 %3, i32 %24)
%26 = call i32 @hwloc_bitmap_singlify(i32 %3)
%27 = load i32, i32* %1
%28 = load i32, i32* @HWLOC_CPUBIND_PROCESS
%29 = call i32 @hwloc_set_cpubind(i32 %27, i32 %3, i32 %28)
%30 = call i32 @emit_binding(i32* %1)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.4, i64 0, i64 0))
%32 = load i32, i32* %1
%33 = load i32, i32* @HWLOC_CPUBIND_PROCESS
%34 = call i32 @hwloc_set_cpubind(i32 %32, i32 %4, i32 %33)
%35 = call i32 @emit_binding(i32* %1)
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
%37 = call i32 @hwloc_bitmap_free(i32 %3)
%38 = call i32 @hwloc_bitmap_free(i32 %4)
%39 = load i32, i32* %1
%40 = call i32 @hwloc_topology_destroy(i32 %39)
ret i32 %2
}