#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;
};

/* Variables and functions */
int /*<<< orphan*/ die(char *);
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, char const *);
void list_devices(void) {
  struct ibv_device **dev_list = NULL;
  int num_device, i;
  do {
    if (!(dev_list = ibv_get_device_list(&num_device)))
      die("No IB-device available. get_device_list returned NULL");
  } while (0);
  for (i = 0; i < num_device; i++) {
    const char *devname = ibv_get_device_name(dev_list[i]);
    printf("[%d]\t%s\n", i, devname);
  }
  ibv_free_device_list(dev_list);
}


x86 assembly (clang, -O0):
<list_devices>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <ibv_get_device_list@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    1183 <list_devices+0x33>
lea    0xe82(%rip),%rdi
call   1070 <die@plt>
jmp    1188 <list_devices+0x38>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d1 <list_devices+0x81>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <ibv_get_device_name@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xe78(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    118f <list_devices+0x3f>
mov    -0x8(%rbp),%rdi
call   1050 <ibv_free_device_list@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_devices>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xc(%rsp),%rdi
call   1040 <ibv_get_device_list@plt>
mov    %rax,%r14
test   %rax,%rax
je     1174 <list_devices+0x24>
cmpl   $0x0,0xc(%rsp)
jg     1187 <list_devices+0x37>
jmp    11b4 <list_devices+0x64>
lea    0xe85(%rip),%rdi
call   1070 <die@plt>
cmpl   $0x0,0xc(%rsp)
jle    11b4 <list_devices+0x64>
xor    %ebx,%ebx
lea    0xea6(%rip),%r15
mov    (%r14,%rbx,8),%rdi
call   1060 <ibv_get_device_name@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %rax,%rdx
call   1030 <printf@plt>
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
jl     1190 <list_devices+0x40>
mov    %r14,%rdi
call   1050 <ibv_free_device_list@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ibv_device = type { i32 }
@.str = [54 x i8] c"No IB-device available. get_device_list returned NULL\00"
@.str.1 = [9 x i8] c"[%d]\09%s\0A\00"
define void @list_devices() {
%1 = alloca %struct.ibv_device**
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store %struct.ibv_device** null, %struct.ibv_device*** %1
br label %5
5: ; preds = %0
%6 = call %struct.ibv_device** @ibv_get_device_list(i32* %2)
store %struct.ibv_device** %6, %struct.ibv_device*** %1
%7 = icmp ne %struct.ibv_device** %6, null
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = call i32 @die(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %5
br label %11
11: ; preds = %10
store i32 0, i32* %3
br label %12
12: ; preds = %26, %11
%13 = load i32, i32* %3
%14 = load i32, i32* %2
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %29
16: ; preds = %12
%17 = load %struct.ibv_device**, %struct.ibv_device*** %1
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %17, i64 %19
%21 = load %struct.ibv_device*, %struct.ibv_device** %20
%22 = call i8* @ibv_get_device_name(%struct.ibv_device* %21)
store i8* %22, i8** %4
%23 = load i32, i32* %3
%24 = load i8*, i8** %4
%25 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %23, i8* %24)
br label %26
26: ; preds = %16
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
br label %12
29: ; preds = %12
%30 = load %struct.ibv_device**, %struct.ibv_device*** %1
%31 = call i32 @ibv_free_device_list(%struct.ibv_device** %30)
ret void
}

LLVM IR (-O2):
%struct.ibv_device = type { i32 }
@.str = [54 x i8] c"No IB-device available. get_device_list returned NULL\00"
@.str.1 = [9 x i8] c"[%d]\09%s\0A\00"
define void @list_devices() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call %struct.ibv_device** @ibv_get_device_list(i32* %1)
%4 = icmp eq %struct.ibv_device** %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @die(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = load i32, i32* %1
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %7, %10
%11 = phi i64 [ %17, %10 ], [ 0, %7 ]
%12 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %3, i64 %11
%13 = load %struct.ibv_device*, %struct.ibv_device** %12
%14 = call i8* @ibv_get_device_name(%struct.ibv_device* %13)
%15 = trunc i64 %11 to i32
%16 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %15, i8* %14)
%17 = add nuw nsw i64 %11, 1
%18 = load i32, i32* %1
%19 = sext i32 %18 to i64
%20 = icmp slt i64 %17, %19
br i1 %20, label %10, label %21
21: ; preds = %10, %7
%22 = call i32 @ibv_free_device_list(%struct.ibv_device** %3)
ret void
}