#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;
};
struct ibv_context {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ __ibv_pretty_print(struct ibv_device *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ ibv_close_device(struct ibv_context *);
int /*<<< orphan*/ ibv_fork_init();
int /*<<< orphan*/ ibv_free_device_list(struct ibv_device **);
struct ibv_device **ibv_get_device_list(int *);
struct ibv_context *ibv_open_device(struct ibv_device *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int);
int main() {
  int num_ibv_devices;
  struct ibv_device **ibv_device_list;
  struct ibv_context *ibv_ctx;
  ibv_fork_init();
  ibv_device_list = ibv_get_device_list(&num_ibv_devices);
  if (ibv_device_list == NULL) {
    fprintf(stderr, "Error (%d): %s\n", errno, strerror(errno));
    return errno;
  }
  fprintf(stdout, "INFO: Found %d rdma device(s) on this host.\n",
          num_ibv_devices);
  for (unsigned i = 0; i < num_ibv_devices; i++)
    __ibv_pretty_print(ibv_device_list[i]);
  if (num_ibv_devices == 0)
    goto freelist;
  ibv_ctx = ibv_open_device(ibv_device_list[0]);
close:
  ibv_close_device(ibv_ctx);
freelist:
  ibv_free_device_list(ibv_device_list);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <ibv_fork_init@plt>
lea    -0x8(%rbp),%rdi
call   1060 <ibv_get_device_list@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fc <main+0x7c>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127f <main+0xff>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe01(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    1247 <main+0xc7>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    (%rax,%rcx,8),%rdi
call   1050 <__ibv_pretty_print@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    121d <main+0x9d>
cmpl   $0x0,-0x8(%rbp)
jne    1256 <main+0xd6>
jmp    126f <main+0xef>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <ibv_open_device@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <ibv_close_device@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ibv_free_device_list@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1030 <ibv_fork_init@plt>
lea    0xc(%rsp),%rdi
call   1060 <ibv_get_device_list@plt>
test   %rax,%rax
je     1204 <main+0x84>
mov    %rax,%rbx
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0xc(%rsp),%edx
lea    0xe5b(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <fprintf@plt>
cmpl   $0x0,0xc(%rsp)
je     11fa <main+0x7a>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%rbx,%rbp,8),%rdi
call   1050 <__ibv_pretty_print@plt>
add    $0x1,%rbp
mov    0xc(%rsp),%eax
cmp    %rax,%rbp
jb     11d0 <main+0x50>
test   %eax,%eax
je     11fa <main+0x7a>
mov    (%rbx),%rdi
call   1090 <ibv_open_device@plt>
mov    %rax,%rdi
call   1080 <ibv_close_device@plt>
mov    %rbx,%rdi
call   1070 <ibv_free_device_list@plt>
jmp    1236 <main+0xb6>
mov    0x2dd5(%rip),%rax
mov    (%rax),%ebx
mov    0x2dc4(%rip),%r14
mov    (%r14),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xddb(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r14),%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ibv_device = type { i32 }
%struct.ibv_context = type { i32 }
@stderr = global i32 0
@.str = [16 x i8] c"Error (%d): %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.1 = [45 x i8] c"INFO: Found %d rdma device(s) on this host.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.ibv_device**
%4 = alloca %struct.ibv_context*
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (...) @ibv_fork_init()
%7 = call %struct.ibv_device** @ibv_get_device_list(i32* %2)
store %struct.ibv_device** %7, %struct.ibv_device*** %3
%8 = load %struct.ibv_device**, %struct.ibv_device*** %3
%9 = icmp eq %struct.ibv_device** %8, null
br i1 %9, label %10, label %17
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %14)
%16 = load i32, i32* @errno
store i32 %16, i32* %1
br label %50
17: ; preds = %0
%18 = load i32, i32* @stdout
%19 = load i32, i32* %2
%20 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 0, i32* %5
br label %21
21: ; preds = %32, %17
%22 = load i32, i32* %5
%23 = load i32, i32* %2
%24 = icmp ult i32 %22, %23
br i1 %24, label %25, label %35
25: ; preds = %21
%26 = load %struct.ibv_device**, %struct.ibv_device*** %3
%27 = load i32, i32* %5
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %26, i64 %28
%30 = load %struct.ibv_device*, %struct.ibv_device** %29
%31 = call i32 @__ibv_pretty_print(%struct.ibv_device* %30)
br label %32
32: ; preds = %25
%33 = load i32, i32* %5
%34 = add i32 %33, 1
store i32 %34, i32* %5
br label %21
35: ; preds = %21
%36 = load i32, i32* %2
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %35
br label %47
39: ; preds = %35
%40 = load %struct.ibv_device**, %struct.ibv_device*** %3
%41 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %40, i64 0
%42 = load %struct.ibv_device*, %struct.ibv_device** %41
%43 = call %struct.ibv_context* @ibv_open_device(%struct.ibv_device* %42)
store %struct.ibv_context* %43, %struct.ibv_context** %4
br label %44
44: ; preds = %39
%45 = load %struct.ibv_context*, %struct.ibv_context** %4
%46 = call i32 @ibv_close_device(%struct.ibv_context* %45)
br label %47
47: ; preds = %44, %38
%48 = load %struct.ibv_device**, %struct.ibv_device*** %3
%49 = call i32 @ibv_free_device_list(%struct.ibv_device** %48)
store i32 0, i32* %1
br label %50
50: ; preds = %47, %10
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.ibv_device = type { i32 }
%struct.ibv_context = type { i32 }
@stderr = global i32 0
@.str = [16 x i8] c"Error (%d): %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.1 = [45 x i8] c"INFO: Found %d rdma device(s) on this host.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @ibv_fork_init()
%4 = call %struct.ibv_device** @ibv_get_device_list(i32* %1)
%5 = icmp eq %struct.ibv_device** %4, null
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8, i8* %9)
%11 = load i32, i32* @errno
br label %35
12: ; preds = %0
%13 = load i32, i32* @stdout
%14 = load i32, i32* %1
%15 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %1
%17 = icmp eq i32 %16, 0
br i1 %17, label %33, label %20
18: ; preds = %20
%19 = icmp eq i32 %26, 0
br i1 %19, label %33, label %29
20: ; preds = %12, %20
%21 = phi i64 [ %25, %20 ], [ 0, %12 ]
%22 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %4, i64 %21
%23 = load %struct.ibv_device*, %struct.ibv_device** %22
%24 = call i32 @__ibv_pretty_print(%struct.ibv_device* %23)
%25 = add nuw nsw i64 %21, 1
%26 = load i32, i32* %1
%27 = zext i32 %26 to i64
%28 = icmp ult i64 %25, %27
br i1 %28, label %20, label %18
29: ; preds = %18
%30 = load %struct.ibv_device*, %struct.ibv_device** %4
%31 = call %struct.ibv_context* @ibv_open_device(%struct.ibv_device* %30)
%32 = call i32 @ibv_close_device(%struct.ibv_context* %31)
br label %33
33: ; preds = %12, %18, %29
%34 = call i32 @ibv_free_device_list(%struct.ibv_device** %4)
br label %35
35: ; preds = %33, %6
%36 = phi i32 [ %11, %6 ], [ 0, %33 ]
ret i32 %36
}