#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 */
int /*<<< orphan*/ _IOLBF;
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*/ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  struct ibv_device **dev_list;
  struct ibv_context *context;
  struct ibv_async_event event;
  setvbuf(stdout, NULL, _IOLBF, 0);
  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)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1080 <setvbuf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <ibv_get_device_list@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1203 <main+0x63>
lea    0xe0e(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fa <main+0x15a>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1234 <main+0x94>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fa <main+0x15a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1090 <ibv_open_device@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1287 <main+0xe7>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <ibv_get_device_name@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xdbf(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fa <main+0x15a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <ibv_get_device_name@plt>
mov    %rax,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xdad(%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     12cd <main+0x12d>
movl   $0x1,-0x4(%rbp)
jmp    12fa <main+0x15a>
mov    -0x28(%rbp),%edi
call   10c0 <event_name_str@plt>
mov    %rax,%rsi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x28(%rbp),%rdi
call   1070 <ibv_ack_async_event@plt>
jmp    12aa <main+0x10a>
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
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1080 <setvbuf@plt>
xor    %edi,%edi
call   1060 <ibv_get_device_list@plt>
test   %rax,%rax
je     12a5 <main+0x105>
mov    %rax,%rbx
mov    (%rax),%rdi
test   %rdi,%rdi
je     1266 <main+0xc6>
call   1090 <ibv_open_device@plt>
test   %rax,%rax
je     127f <main+0xdf>
mov    %rax,%r15
mov    (%rbx),%rdi
call   10a0 <ibv_get_device_name@plt>
mov    (%r15),%edx
lea    0xe4c(%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    12b1 <main+0x111>
lea    0xe3e(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    (%rsp),%edi
call   10c0 <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     1230 <main+0x90>
jmp    12b1 <main+0x111>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12b1 <main+0x111>
mov    0x2d5a(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%rdi
call   10a0 <ibv_get_device_name@plt>
lea    0xd9c(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12b1 <main+0x111>
lea    0xd54(%rip),%rdi
call   10b0 <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 }
@stdout = global i32 0
@_IOLBF = global i32 0
@.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 = load i32, i32* @stdout
%10 = load i32, i32* @_IOLBF
%11 = call i32 @setvbuf(i32 %9, i32* null, i32 %10, i32 0)
%12 = call %struct.ibv_device** @ibv_get_device_list(i32* null)
store %struct.ibv_device** %12, %struct.ibv_device*** %6
%13 = load %struct.ibv_device**, %struct.ibv_device*** %6
%14 = icmp ne %struct.ibv_device** %13, null
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %60
17: ; preds = %2
%18 = load %struct.ibv_device**, %struct.ibv_device*** %6
%19 = load %struct.ibv_device*, %struct.ibv_device** %18
%20 = icmp ne %struct.ibv_device* %19, null
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %60
24: ; preds = %17
%25 = load %struct.ibv_device**, %struct.ibv_device*** %6
%26 = load %struct.ibv_device*, %struct.ibv_device** %25
%27 = call %struct.ibv_context* @ibv_open_device(%struct.ibv_device* %26)
store %struct.ibv_context* %27, %struct.ibv_context** %7
%28 = load %struct.ibv_context*, %struct.ibv_context** %7
%29 = icmp ne %struct.ibv_context* %28, null
br i1 %29, label %36, label %30
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = load %struct.ibv_device**, %struct.ibv_device*** %6
%33 = load %struct.ibv_device*, %struct.ibv_device** %32
%34 = call i8* @ibv_get_device_name(%struct.ibv_device* %33)
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i32 1, i32* %3
br label %60
36: ; preds = %24
%37 = load %struct.ibv_device**, %struct.ibv_device*** %6
%38 = load %struct.ibv_device*, %struct.ibv_device** %37
%39 = call i8* @ibv_get_device_name(%struct.ibv_device* %38)
%40 = load %struct.ibv_context*, %struct.ibv_context** %7
%41 = getelementptr inbounds %struct.ibv_context, %struct.ibv_context* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %39, i32 %42)
br label %44
44: ; preds = %36, %49
%45 = load %struct.ibv_context*, %struct.ibv_context** %7
%46 = call i64 @ibv_get_async_event(%struct.ibv_context* %45, %struct.ibv_async_event* %8)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %44
store i32 1, i32* %3
br label %60
49: ; preds = %44
%50 = getelementptr inbounds %struct.ibv_async_event, %struct.ibv_async_event* %8, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i8* @event_name_str(i32 %51)
%53 = getelementptr inbounds %struct.ibv_async_event, %struct.ibv_async_event* %8, i32 0, i32 0
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.ibv_async_event, %struct.ibv_async_event* %8, i32 0, i32 1
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %52, i32 %54, i32 %57)
%59 = call i32 @ibv_ack_async_event(%struct.ibv_async_event* %8)
br label %44
60: ; preds = %48, %30, %21, %15
%61 = load i32, i32* %3
ret i32 %61
}

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 }
@stdout = global i32 0
@_IOLBF = global i32 0
@.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 = load i32, i32* @stdout
%6 = load i32, i32* @_IOLBF
%7 = tail call i32 @setvbuf(i32 %5, i32* null, i32 %6, i32 0)
%8 = tail call %struct.ibv_device** @ibv_get_device_list(i32* null)
%9 = icmp eq %struct.ibv_device** %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %46
12: ; preds = %2
%13 = load %struct.ibv_device*, %struct.ibv_device** %8
%14 = icmp eq %struct.ibv_device* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %46
18: ; preds = %12
%19 = tail call %struct.ibv_context* @ibv_open_device(%struct.ibv_device* %13)
%20 = icmp eq %struct.ibv_context* %19, null
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = load %struct.ibv_device*, %struct.ibv_device** %8
%24 = tail call i8* @ibv_get_device_name(%struct.ibv_device* %23)
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %46
26: ; preds = %18
%27 = load %struct.ibv_device*, %struct.ibv_device** %8
%28 = tail call i8* @ibv_get_device_name(%struct.ibv_device* %27)
%29 = getelementptr inbounds %struct.ibv_context, %struct.ibv_context* %19, i64 0, i32 0
%30 = load i32, i32* %29
%31 = tail call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %28, i32 %30)
%32 = call i64 @ibv_get_async_event(%struct.ibv_context* %19, %struct.ibv_async_event* %3)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %46
34: ; preds = %26
%35 = getelementptr inbounds %struct.ibv_async_event, %struct.ibv_async_event* %3, i64 0, i32 0
%36 = getelementptr inbounds %struct.ibv_async_event, %struct.ibv_async_event* %3, i64 0, i32 1, i32 0
br label %37
37: ; preds = %34, %37
%38 = load i32, i32* %35
%39 = call i8* @event_name_str(i32 %38)
%40 = load i32, i32* %35
%41 = load i32, i32* %36
%42 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %39, i32 %40, i32 %41)
%43 = call i32 @ibv_ack_async_event(%struct.ibv_async_event* %3)
%44 = call i64 @ibv_get_async_event(%struct.ibv_context* %19, %struct.ibv_async_event* %3)
%45 = icmp eq i64 %44, 0
br i1 %45, label %37, label %46
46: ; preds = %37, %26, %21, %15, %10
ret i32 1
}