#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 ibv_device {
  int dummy;
};
typedef int /*<<< orphan*/ hwloc_topology_t;
typedef int /*<<< orphan*/ hwloc_bitmap_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ hwloc_bitmap_alloc();
int /*<<< orphan*/ hwloc_bitmap_asprintf(char **, int /*<<< orphan*/);
int /*<<< orphan*/ hwloc_bitmap_free(int /*<<< orphan*/);
int hwloc_ibv_get_device_cpuset(int /*<<< orphan*/, struct ibv_device *,
                                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*/ ibv_free_device_list(struct ibv_device **);
struct ibv_device **ibv_get_device_list(int *);
char *ibv_get_device_name(struct ibv_device *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(void) {
  hwloc_topology_t topology;
  struct ibv_device **dev_list, *dev;
  int count, i;
  int err;
  dev_list = ibv_get_device_list(&count);
  if (!dev_list) {
    fprintf(stderr, "ibv_get_device_list failed\n");
    return 0;
  }
  printf("ibv_get_device_list found %d devices\n", count);
  hwloc_topology_init(&topology);
  hwloc_topology_load(topology);
  for (i = 0; i < count; i++) {
    hwloc_bitmap_t set;
    dev = dev_list[i];
    set = hwloc_bitmap_alloc();
    err = hwloc_ibv_get_device_cpuset(topology, dev, set);
    if (err < 0) {
      printf("failed to get cpuset for device %d (%s)\n", i,
             ibv_get_device_name(dev));
    } else {
      char *cpuset_string = NULL;
      hwloc_bitmap_asprintf(&cpuset_string, set);
      printf("got cpuset %s for device %d (%s)\n", cpuset_string, i,
             ibv_get_device_name(dev));
      free(cpuset_string);
    }
    hwloc_bitmap_free(set);
  }
  hwloc_topology_destroy(topology);
  ibv_free_device_list(dev_list);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x1c(%rbp),%rdi
call   10b0 <ibv_get_device_list@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1218 <main+0x48>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1325 <main+0x155>
mov    -0x1c(%rbp),%esi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    -0x8(%rbp),%rdi
call   1060 <hwloc_topology_init@plt>
mov    -0x8(%rbp),%edi
call   1080 <hwloc_topology_load@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    130d <main+0x13d>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <hwloc_bitmap_alloc@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   10e0 <hwloc_ibv_get_device_cpuset@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12ab <main+0xdb>
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   10d0 <ibv_get_device_name@plt>
mov    -0x34(%rbp),%esi
mov    %rax,%rdx
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    12f7 <main+0x127>
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%esi
lea    -0x30(%rbp),%rdi
call   1070 <hwloc_bitmap_asprintf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   10d0 <ibv_get_device_name@plt>
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%edx
mov    %rax,%rcx
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%edi
call   1050 <hwloc_bitmap_free@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1241 <main+0x71>
mov    -0x8(%rbp),%edi
call   10f0 <hwloc_topology_destroy@plt>
mov    -0x10(%rbp),%rdi
call   10c0 <ibv_free_device_list@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
call   10b0 <ibv_get_device_list@plt>
test   %rax,%rax
je     12d3 <main+0x103>
mov    %rax,%r13
mov    0x8(%rsp),%esi
lea    0xe1d(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xc(%rsp),%rdi
call   1060 <hwloc_topology_init@plt>
mov    0xc(%rsp),%edi
call   1080 <hwloc_topology_load@plt>
cmpl   $0x0,0x8(%rsp)
jle    12c0 <main+0xf0>
lea    0xe40(%rip),%r12
xor    %ebx,%ebx
jmp    1283 <main+0xb3>
nop
movq   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rdi
mov    %ebp,%esi
call   1070 <hwloc_bitmap_asprintf@plt>
mov    0x10(%rsp),%r15
mov    %r14,%rdi
call   10d0 <ibv_get_device_name@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
mov    %ebp,%edi
call   1050 <hwloc_bitmap_free@plt>
add    $0x1,%rbx
movslq 0x8(%rsp),%rax
cmp    %rax,%rbx
jge    12c0 <main+0xf0>
mov    0x0(%r13,%rbx,8),%r14
xor    %eax,%eax
call   1030 <hwloc_bitmap_alloc@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    %eax,%edx
call   10e0 <hwloc_ibv_get_device_cpuset@plt>
test   %eax,%eax
jns    1230 <main+0x60>
mov    %r14,%rdi
call   10d0 <ibv_get_device_name@plt>
lea    0xd90(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
jmp    126e <main+0x9e>
mov    0xc(%rsp),%edi
call   10f0 <hwloc_topology_destroy@plt>
mov    %r13,%rdi
call   10c0 <ibv_free_device_list@plt>
jmp    12e8 <main+0x118>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
lea    0xd1d(%rip),%rsi
call   10a0 <fprintf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ibv_device = type { i32 }
@stderr = global i32 0
@.str = [28 x i8] c"ibv_get_device_list failed\0A\00"
@.str.1 = [38 x i8] c"ibv_get_device_list found %d devices\0A\00"
@.str.2 = [41 x i8] c"failed to get cpuset for device %d (%s)\0A\00"
@.str.3 = [34 x i8] c"got cpuset %s for device %d (%s)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.ibv_device**
%4 = alloca %struct.ibv_device*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %1
%10 = call %struct.ibv_device** @ibv_get_device_list(i32* %5)
store %struct.ibv_device** %10, %struct.ibv_device*** %3
%11 = load %struct.ibv_device**, %struct.ibv_device*** %3
%12 = icmp ne %struct.ibv_device** %11, null
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %65
16: ; preds = %0
%17 = load i32, i32* %5
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 @hwloc_topology_init(i32* %2)
%20 = load i32, i32* %2
%21 = call i32 @hwloc_topology_load(i32 %20)
store i32 0, i32* %6
br label %22
22: ; preds = %57, %16
%23 = load i32, i32* %6
%24 = load i32, i32* %5
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %60
26: ; preds = %22
%27 = load %struct.ibv_device**, %struct.ibv_device*** %3
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %27, i64 %29
%31 = load %struct.ibv_device*, %struct.ibv_device** %30
store %struct.ibv_device* %31, %struct.ibv_device** %4
%32 = call i32 (...) @hwloc_bitmap_alloc()
store i32 %32, i32* %8
%33 = load i32, i32* %2
%34 = load %struct.ibv_device*, %struct.ibv_device** %4
%35 = load i32, i32* %8
%36 = call i32 @hwloc_ibv_get_device_cpuset(i32 %33, %struct.ibv_device* %34, i32 %35)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %26
%40 = load i32, i32* %6
%41 = load %struct.ibv_device*, %struct.ibv_device** %4
%42 = call i8* @ibv_get_device_name(%struct.ibv_device* %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %40, i8* %42)
br label %54
44: ; preds = %26
store i8* null, i8** %9
%45 = load i32, i32* %8
%46 = call i32 @hwloc_bitmap_asprintf(i8** %9, i32 %45)
%47 = load i8*, i8** %9
%48 = load i32, i32* %6
%49 = load %struct.ibv_device*, %struct.ibv_device** %4
%50 = call i8* @ibv_get_device_name(%struct.ibv_device* %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %47, i32 %48, i8* %50)
%52 = load i8*, i8** %9
%53 = call i32 @free(i8* %52)
br label %54
54: ; preds = %44, %39
%55 = load i32, i32* %8
%56 = call i32 @hwloc_bitmap_free(i32 %55)
br label %57
57: ; preds = %54
%58 = load i32, i32* %6
%59 = add nsw i32 %58, 1
store i32 %59, i32* %6
br label %22
60: ; preds = %22
%61 = load i32, i32* %2
%62 = call i32 @hwloc_topology_destroy(i32 %61)
%63 = load %struct.ibv_device**, %struct.ibv_device*** %3
%64 = call i32 @ibv_free_device_list(%struct.ibv_device** %63)
store i32 0, i32* %1
br label %65
65: ; preds = %60, %13
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
%struct.ibv_device = type { i32 }
@stderr = global i32 0
@.str = [28 x i8] c"ibv_get_device_list failed\0A\00"
@.str.1 = [38 x i8] c"ibv_get_device_list found %d devices\0A\00"
@.str.2 = [41 x i8] c"failed to get cpuset for device %d (%s)\0A\00"
@.str.3 = [34 x i8] c"got cpuset %s for device %d (%s)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = call %struct.ibv_device** @ibv_get_device_list(i32* %2)
%7 = icmp eq %struct.ibv_device** %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %51
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = call i32 @hwloc_topology_init(i32* %1)
%15 = load i32, i32* %1
%16 = call i32 @hwloc_topology_load(i32 %15)
%17 = load i32, i32* %2
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %47
19: ; preds = %11
%20 = bitcast i8** %3 to i8*
br label %21
21: ; preds = %19, %41
%22 = phi i64 [ 0, %19 ], [ %43, %41 ]
%23 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %6, i64 %22
%24 = load %struct.ibv_device*, %struct.ibv_device** %23
%25 = call i32 (...) @hwloc_bitmap_alloc()
%26 = load i32, i32* %1
%27 = call i32 @hwloc_ibv_get_device_cpuset(i32 %26, %struct.ibv_device* %24, i32 %25)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %21
%30 = call i8* @ibv_get_device_name(%struct.ibv_device* %24)
%31 = trunc i64 %22 to i32
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %31, i8* %30)
br label %41
33: ; preds = %21
store i8* null, i8** %3
%34 = call i32 @hwloc_bitmap_asprintf(i8** %3, i32 %25)
%35 = load i8*, i8** %3
%36 = call i8* @ibv_get_device_name(%struct.ibv_device* %24)
%37 = trunc i64 %22 to i32
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %35, i32 %37, i8* %36)
%39 = load i8*, i8** %3
%40 = call i32 @free(i8* %39)
br label %41
41: ; preds = %33, %29
%42 = call i32 @hwloc_bitmap_free(i32 %25)
%43 = add nuw nsw i64 %22, 1
%44 = load i32, i32* %2
%45 = sext i32 %44 to i64
%46 = icmp slt i64 %43, %45
br i1 %46, label %21, label %47
47: ; preds = %41, %11
%48 = load i32, i32* %1
%49 = call i32 @hwloc_topology_destroy(i32 %48)
%50 = call i32 @ibv_free_device_list(%struct.ibv_device** %6)
br label %51
51: ; preds = %47, %8
ret i32 0
}