#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*/ do_device(struct ibv_device *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ ibv_free_device_list(struct ibv_device **);
struct ibv_device **ibv_get_device_list(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  struct ibv_device **dev_list, **dev_it;
  dev_list = ibv_get_device_list(NULL);
  if (!dev_list) {
    fprintf(stderr, "Failed to get IB devices list");
    return 1;
  }
  int dev_id = 0;
  for (dev_it = dev_list; *dev_it; dev_it++) {
    do_device(*dev_it, dev_id++);
  }
  ibv_free_device_list(dev_list);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <ibv_get_device_list@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118f <main+0x4f>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e4 <main+0xa4>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11d4 <main+0x94>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
call   1030 <do_device@plt>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    119e <main+0x5e>
mov    -0x18(%rbp),%rdi
call   1060 <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   %r14
push   %rbx
push   %rax
xor    %edi,%edi
call   1050 <ibv_get_device_list@plt>
test   %rax,%rax
je     1181 <main+0x41>
mov    %rax,%r14
mov    (%rax),%rdi
test   %rdi,%rdi
je     1175 <main+0x35>
xor    %ebx,%ebx
nopl   (%rax)
mov    %ebx,%esi
call   1030 <do_device@plt>
mov    0x8(%r14,%rbx,8),%rdi
add    $0x1,%rbx
test   %rdi,%rdi
jne    1160 <main+0x20>
mov    %r14,%rdi
call   1060 <ibv_free_device_list@plt>
xor    %eax,%eax
jmp    119b <main+0x5b>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.ibv_device = type { i32 }
@stderr = global i32 0
@.str = [30 x i8] c"Failed to get IB devices list\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.ibv_device**
%7 = alloca %struct.ibv_device**
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call %struct.ibv_device** @ibv_get_device_list(i32* null)
store %struct.ibv_device** %9, %struct.ibv_device*** %6
%10 = load %struct.ibv_device**, %struct.ibv_device*** %6
%11 = icmp ne %struct.ibv_device** %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %33
15: ; preds = %2
store i32 0, i32* %8
%16 = load %struct.ibv_device**, %struct.ibv_device*** %6
store %struct.ibv_device** %16, %struct.ibv_device*** %7
br label %17
17: ; preds = %27, %15
%18 = load %struct.ibv_device**, %struct.ibv_device*** %7
%19 = load %struct.ibv_device*, %struct.ibv_device** %18
%20 = icmp ne %struct.ibv_device* %19, null
br i1 %20, label %21, label %30
21: ; preds = %17
%22 = load %struct.ibv_device**, %struct.ibv_device*** %7
%23 = load %struct.ibv_device*, %struct.ibv_device** %22
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
%26 = call i32 @do_device(%struct.ibv_device* %23, i32 %24)
br label %27
27: ; preds = %21
%28 = load %struct.ibv_device**, %struct.ibv_device*** %7
%29 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %28, i32 1
store %struct.ibv_device** %29, %struct.ibv_device*** %7
br label %17
30: ; preds = %17
%31 = load %struct.ibv_device**, %struct.ibv_device*** %6
%32 = call i32 @ibv_free_device_list(%struct.ibv_device** %31)
store i32 0, i32* %3
br label %33
33: ; preds = %30, %12
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.ibv_device = type { i32 }
@stderr = global i32 0
@.str = [30 x i8] c"Failed to get IB devices list\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call %struct.ibv_device** @ibv_get_device_list(i32* null)
%4 = icmp eq %struct.ibv_device** %3, null
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load %struct.ibv_device*, %struct.ibv_device** %3
%7 = icmp eq %struct.ibv_device* %6, null
br i1 %7, label %20, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %22
11: ; preds = %5, %11
%12 = phi %struct.ibv_device* [ %18, %11 ], [ %6, %5 ]
%13 = phi i32 [ %15, %11 ], [ 0, %5 ]
%14 = phi %struct.ibv_device** [ %17, %11 ], [ %3, %5 ]
%15 = add nuw nsw i32 %13, 1
%16 = tail call i32 @do_device(%struct.ibv_device* %12, i32 %13)
%17 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %14, i64 1
%18 = load %struct.ibv_device*, %struct.ibv_device** %17
%19 = icmp eq %struct.ibv_device* %18, null
br i1 %19, label %20, label %11
20: ; preds = %11, %5
%21 = tail call i32 @ibv_free_device_list(%struct.ibv_device** %3)
br label %22
22: ; preds = %20, %8
%23 = phi i32 [ 0, %20 ], [ 1, %8 ]
ret i32 %23
}