#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_2__ TYPE_1__;

/* Type definitions */
struct ibv_device {
  int dummy;
};
struct ibv_context {
  int async_fd;
};
struct TYPE_2__ {
  int port_num;
};
struct ibv_async_event {
  int event_type;
  TYPE_1__ element;
};

/* Variables and functions */
char *event_name_str(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ ibv_ack_async_event(struct ibv_async_event *);
scalar_t__ ibv_get_async_event(struct ibv_context *, struct ibv_async_event *);
struct ibv_device **ibv_get_device_list(int /*<<< orphan*/ *);
char *ibv_get_device_name(struct ibv_device *);
struct ibv_context *ibv_open_device(struct ibv_device *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  struct ibv_device **dev_list;
  struct ibv_context *context;
  struct ibv_async_event event;
  dev_list = ibv_get_device_list(NULL);
  if (!dev_list) {
    perror("Failed to get IB devices list");
    return 1;
  }
  if (!*dev_list) {
    fprintf(stderr, "No IB devices found\n");
    return 1;
  }
  context = ibv_open_device(*dev_list);
  if (!context) {
    fprintf(stderr, "Couldn't get context for %s\n",
            ibv_get_device_name(*dev_list));
    return 1;
  }
  printf("%s: async event FD %d\n", ibv_get_device_name(*dev_list),
         context->async_fd);
  while (1) {
    if (ibv_get_async_event(context, &event))
      return 1;
    printf("  event_type %s (%d), port %d\n", event_name_str(event.event_type),
           event.event_type, event.element.port_num);
    ibv_ack_async_event(&event);
  }
  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   1060 <ibv_get_device_list@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d6 <main+0x46>
lea    0xe3b(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cd <main+0x13d>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1207 <main+0x77>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cd <main+0x13d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1080 <ibv_open_device@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    125a <main+0xca>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1090 <ibv_get_device_name@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cd <main+0x13d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1090 <ibv_get_device_name@plt>
mov    %rax,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <ibv_get_async_event@plt>
cmp    $0x0,%rax
je     12a0 <main+0x110>
movl   $0x1,-0x4(%rbp)
jmp    12cd <main+0x13d>
mov    -0x28(%rbp),%edi
call   10b0 <event_name_str@plt>
mov    %rax,%rsi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x28(%rbp),%rdi
call   1070 <ibv_ack_async_event@plt>
jmp    127d <main+0xed>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %edi,%edi
call   1060 <ibv_get_device_list@plt>
test   %rax,%rax
je     1285 <main+0xf5>
mov    %rax,%rbx
mov    (%rax),%rdi
test   %rdi,%rdi
je     1246 <main+0xb6>
call   1080 <ibv_open_device@plt>
test   %rax,%rax
je     125f <main+0xcf>
mov    %rax,%r15
mov    (%rbx),%rdi
call   1090 <ibv_get_device_name@plt>
mov    (%r15),%edx
lea    0xe77(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rsp,%rsi
mov    %r15,%rdi
call   1040 <ibv_get_async_event@plt>
test   %rax,%rax
jne    1291 <main+0x101>
lea    0xe69(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%edi
call   10b0 <event_name_str@plt>
mov    (%rsp),%edx
mov    0x4(%rsp),%ecx
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
call   1070 <ibv_ack_async_event@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <ibv_get_async_event@plt>
test   %rax,%rax
je     1210 <main+0x80>
jmp    1291 <main+0x101>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1291 <main+0x101>
mov    0x2d7a(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%rdi
call   1090 <ibv_get_device_name@plt>
lea    0xdbc(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1291 <main+0x101>
lea    0xd74(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ibv_device = type { i32 }
%struct.ibv_context = type { i32 }
%struct.ibv_async_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [30 x i8] c"Failed to get IB devices list\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"No IB devices found\0A\00"
@.str.2 = [29 x i8] c"Couldn't get context for %s\0A\00"
@.str.3 = [23 x i8] c"%s: async event FD %d\0A\00"
@.str.4 = [31 x i8] c" event_type %s (%d), port %d\0A\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_context*
%8 = alloca %struct.ibv_async_event
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 %14, label %12
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %57
14: ; preds = %2
%15 = load %struct.ibv_device**, %struct.ibv_device*** %6
%16 = load %struct.ibv_device*, %struct.ibv_device** %15
%17 = icmp ne %struct.ibv_device* %16, null
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %57
21: ; preds = %14
%22 = load %struct.ibv_device**, %struct.ibv_device*** %6
%23 = load %struct.ibv_device*, %struct.ibv_device** %22
%24 = call %struct.ibv_context* @ibv_open_device(%struct.ibv_device* %23)
store %struct.ibv_context* %24, %struct.ibv_context** %7
%25 = load %struct.ibv_context*, %struct.ibv_context** %7
%26 = icmp ne %struct.ibv_context* %25, null
br i1 %26, label %33, label %27
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load %struct.ibv_device**, %struct.ibv_device*** %6
%30 = load %struct.ibv_device*, %struct.ibv_device** %29
%31 = call i8* @ibv_get_device_name(%struct.ibv_device* %30)
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %31)
store i32 1, i32* %3
br label %57
33: ; preds = %21
%34 = load %struct.ibv_device**, %struct.ibv_device*** %6
%35 = load %struct.ibv_device*, %struct.ibv_device** %34
%36 = call i8* @ibv_get_device_name(%struct.ibv_device* %35)
%37 = load %struct.ibv_context*, %struct.ibv_context** %7
%38 = getelementptr inbounds %struct.ibv_context, %struct.ibv_context* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %36, i32 %39)
br label %41
41: ; preds = %33, %46
%42 = load %struct.ibv_context*, %struct.ibv_context** %7
%43 = call i64 @ibv_get_async_event(%struct.ibv_context* %42, %struct.ibv_async_event* %8)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %41
store i32 1, i32* %3
br label %57
46: ; preds = %41
%47 = getelementptr inbounds %struct.ibv_async_event, %struct.ibv_async_event* %8, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i8* @event_name_str(i32 %48)
%50 = getelementptr inbounds %struct.ibv_async_event, %struct.ibv_async_event* %8, i32 0, i32 0
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.ibv_async_event, %struct.ibv_async_event* %8, i32 0, i32 1
%53 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %49, i32 %51, i32 %54)
%56 = call i32 @ibv_ack_async_event(%struct.ibv_async_event* %8)
br label %41
57: ; preds = %45, %27, %18, %12
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.ibv_async_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.ibv_device = type { i32 }
%struct.ibv_context = type { i32 }
@.str = [30 x i8] c"Failed to get IB devices list\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"No IB devices found\0A\00"
@.str.2 = [29 x i8] c"Couldn't get context for %s\0A\00"
@.str.3 = [23 x i8] c"%s: async event FD %d\0A\00"
@.str.4 = [31 x i8] c" event_type %s (%d), port %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.ibv_async_event
%4 = bitcast %struct.ibv_async_event* %3 to i8*
%5 = tail call %struct.ibv_device** @ibv_get_device_list(i32* null)
%6 = icmp eq %struct.ibv_device** %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %43
9: ; preds = %2
%10 = load %struct.ibv_device*, %struct.ibv_device** %5
%11 = icmp eq %struct.ibv_device* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %43
15: ; preds = %9
%16 = tail call %struct.ibv_context* @ibv_open_device(%struct.ibv_device* %10)
%17 = icmp eq %struct.ibv_context* %16, null
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load %struct.ibv_device*, %struct.ibv_device** %5
%21 = tail call i8* @ibv_get_device_name(%struct.ibv_device* %20)
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %43
23: ; preds = %15
%24 = load %struct.ibv_device*, %struct.ibv_device** %5
%25 = tail call i8* @ibv_get_device_name(%struct.ibv_device* %24)
%26 = getelementptr inbounds %struct.ibv_context, %struct.ibv_context* %16, i64 0, i32 0
%27 = load i32, i32* %26
%28 = tail call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %25, i32 %27)
%29 = call i64 @ibv_get_async_event(%struct.ibv_context* %16, %struct.ibv_async_event* %3)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %43
31: ; preds = %23
%32 = getelementptr inbounds %struct.ibv_async_event, %struct.ibv_async_event* %3, i64 0, i32 0
%33 = getelementptr inbounds %struct.ibv_async_event, %struct.ibv_async_event* %3, i64 0, i32 1, i32 0
br label %34
34: ; preds = %31, %34
%35 = load i32, i32* %32
%36 = call i8* @event_name_str(i32 %35)
%37 = load i32, i32* %32
%38 = load i32, i32* %33
%39 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %36, i32 %37, i32 %38)
%40 = call i32 @ibv_ack_async_event(%struct.ibv_async_event* %3)
%41 = call i64 @ibv_get_async_event(%struct.ibv_context* %16, %struct.ibv_async_event* %3)
%42 = icmp eq i64 %41, 0
br i1 %42, label %34, label %43
43: ; preds = %34, %23, %18, %12, %7
ret i32 1
}