#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 option {
  char *name;
  int has_arg;
  char val;
  int /*<<< orphan*/ member_0;
};
struct ibv_pd {
  int dummy;
};
struct ibv_mr {
  int dummy;
};
struct ibv_device {
  int dummy;
};
struct ibv_context {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ IBV_ACCESS_LOCAL_WRITE;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getopt_long(int, char **, char *, struct option *, int /*<<< orphan*/ *);
struct ibv_pd *ibv_alloc_pd(struct ibv_context *);
scalar_t__ ibv_close_device(struct ibv_context *);
scalar_t__ ibv_dealloc_pd(struct ibv_pd *);
int /*<<< orphan*/ ibv_dereg_mr(struct ibv_mr *);
int /*<<< orphan*/ ibv_free_device_list(struct ibv_device **);
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 *);
struct ibv_mr *ibv_reg_mr(struct ibv_pd *, char *, size_t, int /*<<< orphan*/);
char *malloc(size_t);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
char *strdupa(int /*<<< orphan*/);
int strtol(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  struct ibv_device *ib_dev;
  char *ib_devname = NULL;
  int ib_port = 1;
  while (1) {
    int c;
    static struct option long_options[] = {
        {.name = "ib-dev", .has_arg = 1, .val = 'd'},
        {.name = "ib-port", .has_arg = 1, .val = 'i'},
        {0}};
    c = getopt_long(argc, argv, "d:i:", long_options, NULL);
    if (c == -1)
      break;
    switch (c) {
    case 'd':
      ib_devname = strdupa(optarg);
      break;
    case 'i':
      ib_port = strtol(optarg, NULL, 10);
      if (ib_port < 0) {
        usage(argv[0]);
        return 1;
      }
      break;
    default:
      usage(argv[0]);
      return 1;
    }
  }
  struct ibv_device **dev_list = ibv_get_device_list(NULL);
  if (!dev_list) {
    fprintf(stderr, "Failed to get IB devices list: errnor=%d\n", errno);
    return 1;
  }
  if (!ib_devname) {
    ib_dev = *dev_list;
    if (!ib_dev) {
      fprintf(stderr, "No IB devices found\n");
      return 1;
    }
  } else {
    int i;
    for (i = 0; dev_list[i]; ++i)
      if (!strcmp(ibv_get_device_name(dev_list[i]), ib_devname))
        break;
    ib_dev = dev_list[i];
    if (!ib_dev) {
      fprintf(stderr, "IB device %s not found\n", ib_devname);
      return 1;
    }
  }
  struct ibv_context *context = ibv_open_device(ib_dev);
  if (!context) {
    fprintf(stderr, "Couldn't get context for %s: errno=%d\n",
            ibv_get_device_name(ib_dev), errno);
    return 1;
  }
  struct ibv_pd *pd = ibv_alloc_pd(context);
  if (!pd) {
    fprintf(stderr, "Couldn't allocate protection domain: errno=%d\n", errno);
    return 1;
  }
  size_t size;
  for (size = 1; size < 256 * 1024 * 1024; size *= 2) {
    char *buffer = malloc(size);
    struct ibv_mr *mr;
    mr = ibv_reg_mr(pd, buffer, size, IBV_ACCESS_LOCAL_WRITE);
    if (mr) {
      printf("ibv_reg_mr: size = %zu OK\n", size);
    } else {
      printf("ibv_reg_mr: size = %zu NG\n", size);
      fprintf(stderr, "Couldn't register memory region: errno=%d\n", errno);
      return 1;
    }
    ibv_dereg_mr(mr);
  }
  if (ibv_dealloc_pd(pd)) {
    fprintf(stderr, "Couldn't deallocate PD\n");
    return 1;
  }
  if (ibv_close_device(context)) {
    fprintf(stderr, "Couldn't release context\n");
    return 1;
  }
  ibv_free_device_list(dev_list);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xdcc(%rip),%rdx
lea    0x2e66(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <getopt_long@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1266 <main+0x56>
jmp    1300 <main+0xf0>
mov    -0x28(%rbp),%eax
mov    %eax,-0x64(%rbp)
sub    $0x64,%eax
je     128b <main+0x7b>
jmp    127a <main+0x6a>
mov    -0x64(%rbp),%eax
sub    $0x69,%eax
je     12a2 <main+0x92>
jmp    12e3 <main+0xd3>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10e0 <strdupa@plt>
mov    %rax,-0x20(%rbp)
jmp    12fb <main+0xeb>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12de <main+0xce>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1100 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f9 <main+0x3e9>
jmp    12fb <main+0xeb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1100 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f9 <main+0x3e9>
jmp    1235 <main+0x25>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <ibv_get_device_list@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1344 <main+0x134>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
mov    0x2ca0(%rip),%rax
mov    (%rax),%edx
lea    0xce3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f9 <main+0x3e9>
cmpq   $0x0,-0x20(%rbp)
jne    138d <main+0x17d>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1388 <main+0x178>
mov    0x2c64(%rip),%rax
mov    (%rax),%edi
lea    0xcc9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f9 <main+0x3e9>
jmp    142c <main+0x21c>
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     13e5 <main+0x1d5>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1120 <ibv_get_device_name@plt>
mov    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    13d2 <main+0x1c2>
jmp    13e5 <main+0x1d5>
jmp    13d7 <main+0x1c7>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1394 <main+0x184>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1427 <main+0x217>
mov    0x2bc9(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xc3f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f9 <main+0x3e9>
jmp    142c <main+0x21c>
mov    -0x18(%rbp),%rdi
call   1110 <ibv_open_device@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1482 <main+0x272>
mov    0x2b85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
call   1120 <ibv_get_device_name@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
mov    0x2b62(%rip),%rax
mov    (%rax),%ecx
lea    0xbfc(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f9 <main+0x3e9>
mov    -0x40(%rbp),%rdi
call   1130 <ibv_alloc_pd@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    14c6 <main+0x2b6>
mov    0x2b2f(%rip),%rax
mov    (%rax),%edi
mov    0x2b1e(%rip),%rax
mov    (%rax),%edx
lea    0xbdf(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f9 <main+0x3e9>
movq   $0x1,-0x50(%rbp)
cmpq   $0x10000000,-0x50(%rbp)
10
jae    157d <main+0x36d>
mov    -0x50(%rbp),%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    0x2ae4(%rip),%rax
mov    (%rax),%ecx
call   1060 <ibv_reg_mr@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     1527 <main+0x317>
mov    -0x50(%rbp),%rsi
lea    0xba4(%rip),%rdi
call   1040 <printf@plt>
jmp    1563 <main+0x353>
mov    -0x50(%rbp),%rsi
lea    0xbaa(%rip),%rdi
call   1040 <printf@plt>
mov    0x2a92(%rip),%rax
mov    (%rax),%edi
mov    0x2a81(%rip),%rax
mov    (%rax),%edx
lea    0xba7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f9 <main+0x3e9>
mov    -0x60(%rbp),%rdi
call   10b0 <ibv_dereg_mr@plt>
mov    -0x50(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    14ce <main+0x2be>
mov    -0x48(%rbp),%rdi
call   10a0 <ibv_dealloc_pd@plt>
cmp    $0x0,%rax
je     15b3 <main+0x3a3>
mov    0x2a39(%rip),%rax
mov    (%rax),%edi
lea    0xb82(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f9 <main+0x3e9>
mov    -0x40(%rbp),%rdi
call   10f0 <ibv_close_device@plt>
cmp    $0x0,%rax
je     15e9 <main+0x3d9>
mov    0x2a03(%rip),%rax
mov    (%rax),%edi
lea    0xb64(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f9 <main+0x3e9>
mov    -0x30(%rbp),%rdi
call   10d0 <ibv_free_device_list@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %r15d,%r15d
lea    0xde5(%rip),%r14
lea    0x2e7f(%rip),%r12
mov    0x2dc0(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1030 <getopt_long@plt>
cmp    $0x69,%eax
jne    126e <main+0x5e>
mov    0x0(%r13),%edi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
test   %eax,%eax
jns    1240 <main+0x30>
jmp    1286 <main+0x76>
cmp    $0xffffffff,%eax
je     12a2 <main+0x92>
cmp    $0x64,%eax
jne    1286 <main+0x76>
mov    0x0(%r13),%edi
call   10e0 <strdupa@plt>
mov    %rax,%r15
jmp    1240 <main+0x30>
mov    (%rbx),%rdi
call   1100 <usage@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1090 <ibv_get_device_list@plt>
test   %rax,%rax
je     12fa <main+0xea>
mov    %rax,%r14
mov    (%rax),%rbp
test   %r15,%r15
je     131f <main+0x10f>
test   %rbp,%rbp
je     13fe <main+0x1ee>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
call   1120 <ibv_get_device_name@plt>
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1339 <main+0x129>
mov    0x8(%r14,%rbx,8),%rbp
add    $0x1,%rbx
test   %rbp,%rbp
jne    12d0 <main+0xc0>
jmp    13fe <main+0x1ee>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
mov    0x2cbe(%rip),%rax
mov    (%rax),%edx
lea    0xd01(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    128e <main+0x7e>
test   %rbp,%rbp
jne    1348 <main+0x138>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
lea    0xd0a(%rip),%rsi
jmp    14b8 <main+0x2a8>
mov    %ebx,%eax
mov    (%r14,%rax,8),%rbp
test   %rbp,%rbp
je     13fe <main+0x1ee>
mov    %rbp,%rdi
call   1110 <ibv_open_device@plt>
test   %rax,%rax
je     141d <main+0x20d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1130 <ibv_alloc_pd@plt>
test   %rax,%rax
je     144f <main+0x23f>
mov    %rax,%r13
mov    %rbx,(%rsp)
mov    $0x1,%ebx
mov    0x2c60(%rip),%r15
lea    0xd3a(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10c0 <malloc@plt>
mov    (%r15),%ecx
mov    %r13,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1060 <ibv_reg_mr@plt>
test   %rax,%rax
je     146d <main+0x25d>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbp,%rdi
call   10b0 <ibv_dereg_mr@plt>
lea    (%rbx,%rbx,1),%rax
cmp    $0x8000000,%rbx
mov    %rax,%rbx
jb     1390 <main+0x180>
mov    %r13,%rdi
call   10a0 <ibv_dealloc_pd@plt>
test   %rax,%rax
je     149a <main+0x28a>
mov    0x2be0(%rip),%rax
mov    (%rax),%edi
lea    0xd29(%rip),%rsi
jmp    14b8 <main+0x2a8>
mov    0x2bcb(%rip),%rax
mov    (%rax),%edi
lea    0xc45(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    128e <main+0x7e>
mov    0x2bac(%rip),%rax
mov    (%rax),%ebx
mov    %rbp,%rdi
call   1120 <ibv_get_device_name@plt>
mov    0x2b93(%rip),%rcx
mov    (%rcx),%ecx
lea    0xc2d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    128e <main+0x7e>
mov    0x2b7a(%rip),%rax
mov    (%rax),%edi
mov    0x2b69(%rip),%rax
mov    (%rax),%edx
lea    0xc2a(%rip),%rsi
jmp    1313 <main+0x103>
lea    0xc68(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x2b4d(%rip),%rax
mov    (%rax),%edi
mov    0x2b3c(%rip),%rax
mov    (%rax),%edx
lea    0xc62(%rip),%rsi
jmp    1313 <main+0x103>
mov    (%rsp),%rdi
call   10f0 <ibv_close_device@plt>
test   %rax,%rax
je     14c4 <main+0x2b4>
mov    0x2b21(%rip),%rax
mov    (%rax),%edi
lea    0xc82(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    128e <main+0x7e>
mov    %r14,%rdi
call   10d0 <ibv_free_device_list@plt>
xor    %eax,%eax
jmp    1293 <main+0x83>

LLVM IR (-O0):
%struct.option = type { i8*, i32, i8, i32 }
%struct.ibv_device = type { i32 }
%struct.ibv_context = type { i32 }
%struct.ibv_pd = type { i32 }
%struct.ibv_mr = type { i32 }
@main.long_options = internal global [3 x %struct.option] [%struct.option { i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i32 0, i32 0), i32 1, i8 100, i32 0 }, %struct.option { i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i32 0, i32 0), i32 1, i8 105, i32 0 }, %struct.option zeroinitializer]
@.str = [7 x i8] c"ib-dev\00"
@.str.1 = [8 x i8] c"ib-port\00"
@.str.2 = [5 x i8] c"d:i:\00"
@optarg = global i32 0
@stderr = global i32 0
@.str.3 = [42 x i8] c"Failed to get IB devices list: errnor=%d\0A\00"
@errno = global i32 0
@.str.4 = [21 x i8] c"No IB devices found\0A\00"
@.str.5 = [24 x i8] c"IB device %s not found\0A\00"
@.str.6 = [39 x i8] c"Couldn't get context for %s: errno=%d\0A\00"
@.str.7 = [47 x i8] c"Couldn't allocate protection domain: errno=%d\0A\00"
@IBV_ACCESS_LOCAL_WRITE = global i32 0
@.str.8 = [27 x i8] c"ibv_reg_mr: size = %zu OK\0A\00"
@.str.9 = [27 x i8] c"ibv_reg_mr: size = %zu NG\0A\00"
@.str.10 = [43 x i8] c"Couldn't register memory region: errno=%d\0A\00"
@.str.11 = [24 x i8] c"Couldn't deallocate PD\0A\00"
@.str.12 = [26 x i8] c"Couldn't release context\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 i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.ibv_device**
%11 = alloca i32
%12 = alloca %struct.ibv_context*
%13 = alloca %struct.ibv_pd*
%14 = alloca i64
%15 = alloca i8*
%16 = alloca %struct.ibv_mr*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i32 1, i32* %8
br label %17
17: ; preds = %2, %45
%18 = load i32, i32* %4
%19 = load i8**, i8*** %5
%20 = call i32 @getopt_long(i32 %18, i8** %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %struct.option* getelementptr inbounds ([3 x %struct.option], [3 x %struct.option]* @main.long_options, i64 0, i64 0), i32* null)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %17
br label %46
24: ; preds = %17
%25 = load i32, i32* %9
switch i32 %25, label %40 [
i32 100, label %26
i32 105, label %29
]
26: ; preds = %24
%27 = load i32, i32* @optarg
%28 = call i8* @strdupa(i32 %27)
store i8* %28, i8** %7
br label %45
29: ; preds = %24
%30 = load i32, i32* @optarg
%31 = call i32 @strtol(i32 %30, i32* null, i32 10)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = call i32 @usage(i8* %37)
store i32 1, i32* %3
br label %168
39: ; preds = %29
br label %45
40: ; preds = %24
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 0
%43 = load i8*, i8** %42
%44 = call i32 @usage(i8* %43)
store i32 1, i32* %3
br label %168
45: ; preds = %39, %26
br label %17
46: ; preds = %23
%47 = call %struct.ibv_device** @ibv_get_device_list(i32* null)
store %struct.ibv_device** %47, %struct.ibv_device*** %10
%48 = load %struct.ibv_device**, %struct.ibv_device*** %10
%49 = icmp ne %struct.ibv_device** %48, null
br i1 %49, label %54, label %50
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = load i32, i32* @errno
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %52)
store i32 1, i32* %3
br label %168
54: ; preds = %46
%55 = load i8*, i8** %7
%56 = icmp ne i8* %55, null
br i1 %56, label %66, label %57
57: ; preds = %54
%58 = load %struct.ibv_device**, %struct.ibv_device*** %10
%59 = load %struct.ibv_device*, %struct.ibv_device** %58
store %struct.ibv_device* %59, %struct.ibv_device** %6
%60 = load %struct.ibv_device*, %struct.ibv_device** %6
%61 = icmp ne %struct.ibv_device* %60, null
br i1 %61, label %65, label %62
62: ; preds = %57
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %168
65: ; preds = %57
br label %102
66: ; preds = %54
store i32 0, i32* %11
br label %67
67: ; preds = %86, %66
%68 = load %struct.ibv_device**, %struct.ibv_device*** %10
%69 = load i32, i32* %11
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %68, i64 %70
%72 = load %struct.ibv_device*, %struct.ibv_device** %71
%73 = icmp ne %struct.ibv_device* %72, null
br i1 %73, label %74, label %89
74: ; preds = %67
%75 = load %struct.ibv_device**, %struct.ibv_device*** %10
%76 = load i32, i32* %11
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %75, i64 %77
%79 = load %struct.ibv_device*, %struct.ibv_device** %78
%80 = call i8* @ibv_get_device_name(%struct.ibv_device* %79)
%81 = load i8*, i8** %7
%82 = call i32 @strcmp(i8* %80, i8* %81)
%83 = icmp ne i32 %82, 0
br i1 %83, label %85, label %84
84: ; preds = %74
br label %89
85: ; preds = %74
br label %86
86: ; preds = %85
%87 = load i32, i32* %11
%88 = add nsw i32 %87, 1
store i32 %88, i32* %11
br label %67
89: ; preds = %84, %67
%90 = load %struct.ibv_device**, %struct.ibv_device*** %10
%91 = load i32, i32* %11
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %90, i64 %92
%94 = load %struct.ibv_device*, %struct.ibv_device** %93
store %struct.ibv_device* %94, %struct.ibv_device** %6
%95 = load %struct.ibv_device*, %struct.ibv_device** %6
%96 = icmp ne %struct.ibv_device* %95, null
br i1 %96, label %101, label %97
97: ; preds = %89
%98 = load i32, i32* @stderr
%99 = load i8*, i8** %7
%100 = call i32 (i32, i8*, ...) @fprintf(i32 %98, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %99)
store i32 1, i32* %3
br label %168
101: ; preds = %89
br label %102
102: ; preds = %101, %65
%103 = load %struct.ibv_device*, %struct.ibv_device** %6
%104 = call %struct.ibv_context* @ibv_open_device(%struct.ibv_device* %103)
store %struct.ibv_context* %104, %struct.ibv_context** %12
%105 = load %struct.ibv_context*, %struct.ibv_context** %12
%106 = icmp ne %struct.ibv_context* %105, null
br i1 %106, label %113, label %107
107: ; preds = %102
%108 = load i32, i32* @stderr
%109 = load %struct.ibv_device*, %struct.ibv_device** %6
%110 = call i8* @ibv_get_device_name(%struct.ibv_device* %109)
%111 = load i32, i32* @errno
%112 = call i32 (i32, i8*, ...) @fprintf(i32 %108, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %110, i32 %111)
store i32 1, i32* %3
br label %168
113: ; preds = %102
%114 = load %struct.ibv_context*, %struct.ibv_context** %12
%115 = call %struct.ibv_pd* @ibv_alloc_pd(%struct.ibv_context* %114)
store %struct.ibv_pd* %115, %struct.ibv_pd** %13
%116 = load %struct.ibv_pd*, %struct.ibv_pd** %13
%117 = icmp ne %struct.ibv_pd* %116, null
br i1 %117, label %122, label %118
118: ; preds = %113
%119 = load i32, i32* @stderr
%120 = load i32, i32* @errno
%121 = call i32 (i32, i8*, ...) @fprintf(i32 %119, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.7, i64 0, i64 0), i32 %120)
store i32 1, i32* %3
br label %168
122: ; preds = %113
store i64 1, i64* %14
br label %123
123: ; preds = %148, %122
%124 = load i64, i64* %14
%125 = icmp ult i64 %124, 268435456
br i1 %125, label %126, label %151
126: ; preds = %123
%127 = load i64, i64* %14
%128 = call i8* @malloc(i64 %127)
store i8* %128, i8** %15
%129 = load %struct.ibv_pd*, %struct.ibv_pd** %13
%130 = load i8*, i8** %15
%131 = load i64, i64* %14
%132 = load i32, i32* @IBV_ACCESS_LOCAL_WRITE
%133 = call %struct.ibv_mr* @ibv_reg_mr(%struct.ibv_pd* %129, i8* %130, i64 %131, i32 %132)
store %struct.ibv_mr* %133, %struct.ibv_mr** %16
%134 = load %struct.ibv_mr*, %struct.ibv_mr** %16
%135 = icmp ne %struct.ibv_mr* %134, null
br i1 %135, label %136, label %139
136: ; preds = %126
%137 = load i64, i64* %14
%138 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i64 %137)
br label %145
139: ; preds = %126
%140 = load i64, i64* %14
%141 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0), i64 %140)
%142 = load i32, i32* @stderr
%143 = load i32, i32* @errno
%144 = call i32 (i32, i8*, ...) @fprintf(i32 %142, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.10, i64 0, i64 0), i32 %143)
store i32 1, i32* %3
br label %168
145: ; preds = %136
%146 = load %struct.ibv_mr*, %struct.ibv_mr** %16
%147 = call i32 @ibv_dereg_mr(%struct.ibv_mr* %146)
br label %148
148: ; preds = %145
%149 = load i64, i64* %14
%150 = mul i64 %149, 2
store i64 %150, i64* %14
br label %123
151: ; preds = %123
%152 = load %struct.ibv_pd*, %struct.ibv_pd** %13
%153 = call i64 @ibv_dealloc_pd(%struct.ibv_pd* %152)
%154 = icmp ne i64 %153, 0
br i1 %154, label %155, label %158
155: ; preds = %151
%156 = load i32, i32* @stderr
%157 = call i32 (i32, i8*, ...) @fprintf(i32 %156, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0))
store i32 1, i32* %3
br label %168
158: ; preds = %151
%159 = load %struct.ibv_context*, %struct.ibv_context** %12
%160 = call i64 @ibv_close_device(%struct.ibv_context* %159)
%161 = icmp ne i64 %160, 0
br i1 %161, label %162, label %165
162: ; preds = %158
%163 = load i32, i32* @stderr
%164 = call i32 (i32, i8*, ...) @fprintf(i32 %163, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.12, i64 0, i64 0))
store i32 1, i32* %3
br label %168
165: ; preds = %158
%166 = load %struct.ibv_device**, %struct.ibv_device*** %10
%167 = call i32 @ibv_free_device_list(%struct.ibv_device** %166)
store i32 0, i32* %3
br label %168
168: ; preds = %165, %162, %155, %139, %118, %107, %97, %62, %50, %40, %34
%169 = load i32, i32* %3
ret i32 %169
}

LLVM IR (-O2):
%struct.option = type { i8*, i32, i8, i32 }
%struct.ibv_device = type { i32 }
%struct.ibv_context = type { i32 }
%struct.ibv_pd = type { i32 }
%struct.ibv_mr = type { i32 }
@main.long_options = internal global [3 x %struct.option] [%struct.option { i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i32 0, i32 0), i32 1, i8 100, i32 0 }, %struct.option { i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i32 0, i32 0), i32 1, i8 105, i32 0 }, %struct.option zeroinitializer]
@.str = [7 x i8] c"ib-dev\00"
@.str.1 = [8 x i8] c"ib-port\00"
@.str.2 = [5 x i8] c"d:i:\00"
@optarg = global i32 0
@stderr = global i32 0
@.str.3 = [42 x i8] c"Failed to get IB devices list: errnor=%d\0A\00"
@errno = global i32 0
@.str.4 = [21 x i8] c"No IB devices found\0A\00"
@.str.5 = [24 x i8] c"IB device %s not found\0A\00"
@.str.6 = [39 x i8] c"Couldn't get context for %s: errno=%d\0A\00"
@.str.7 = [47 x i8] c"Couldn't allocate protection domain: errno=%d\0A\00"
@IBV_ACCESS_LOCAL_WRITE = global i32 0
@.str.8 = [27 x i8] c"ibv_reg_mr: size = %zu OK\0A\00"
@.str.9 = [27 x i8] c"ibv_reg_mr: size = %zu NG\0A\00"
@.str.10 = [43 x i8] c"Couldn't register memory region: errno=%d\0A\00"
@.str.11 = [24 x i8] c"Couldn't deallocate PD\0A\00"
@.str.12 = [26 x i8] c"Couldn't release context\0A\00"
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %7, %2
%4 = phi i8* [ %9, %7 ], [ null, %2 ]
br label %5
5: ; preds = %3, %10
%6 = tail call i32 @getopt_long(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %struct.option* getelementptr inbounds ([3 x %struct.option], [3 x %struct.option]* @main.long_options, i64 0, i64 0), i32* null)
switch i32 %6, label %17 [
i32 -1, label %20
i32 100, label %7
i32 105, label %10
]
7: ; preds = %5
%8 = load i32, i32* @optarg
%9 = tail call i8* @strdupa(i32 %8)
br label %3
10: ; preds = %5
%11 = load i32, i32* @optarg
%12 = tail call i32 @strtol(i32 %11, i32* null, i32 10)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %5
14: ; preds = %10
%15 = load i8*, i8** %1
%16 = tail call i32 @usage(i8* %15)
br label %104
17: ; preds = %5
%18 = load i8*, i8** %1
%19 = tail call i32 @usage(i8* %18)
br label %104
20: ; preds = %5
%21 = tail call %struct.ibv_device** @ibv_get_device_list(i32* null)
%22 = icmp eq %struct.ibv_device** %21, null
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %104
27: ; preds = %20
%28 = icmp eq i8* %4, null
%29 = load %struct.ibv_device*, %struct.ibv_device** %21
%30 = icmp eq %struct.ibv_device* %29, null
br i1 %28, label %32, label %31
31: ; preds = %27
br i1 %30, label %55, label %36
32: ; preds = %27
br i1 %30, label %33, label %58
33: ; preds = %32
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %104
36: ; preds = %31, %42
%37 = phi i64 [ %43, %42 ], [ 0, %31 ]
%38 = phi %struct.ibv_device* [ %45, %42 ], [ %29, %31 ]
%39 = tail call i8* @ibv_get_device_name(%struct.ibv_device* %38)
%40 = tail call i32 @strcmp(i8* dereferenceable(1) %39, i8* %4)
%41 = icmp eq i32 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %36
%43 = add nuw i64 %37, 1
%44 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %21, i64 %43
%45 = load %struct.ibv_device*, %struct.ibv_device** %44
%46 = icmp eq %struct.ibv_device* %45, null
br i1 %46, label %55, label %36
47: ; preds = %36
%48 = and i64 %37, 4294967295
%49 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %21, i64 %48
%50 = load %struct.ibv_device*, %struct.ibv_device** %49
%51 = icmp eq %struct.ibv_device* %50, null
br i1 %51, label %52, label %58
52: ; preds = %47
%53 = load i32, i32* @stderr
%54 = tail call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %4)
br label %104
55: ; preds = %42, %31
%56 = load i32, i32* @stderr
%57 = tail call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %4)
br label %104
58: ; preds = %47, %32
%59 = phi %struct.ibv_device* [ %29, %32 ], [ %50, %47 ]
%60 = tail call %struct.ibv_context* @ibv_open_device(%struct.ibv_device* %59)
%61 = icmp eq %struct.ibv_context* %60, null
br i1 %61, label %62, label %67
62: ; preds = %58
%63 = load i32, i32* @stderr
%64 = tail call i8* @ibv_get_device_name(%struct.ibv_device* %59)
%65 = load i32, i32* @errno
%66 = tail call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %64, i32 %65)
br label %104
67: ; preds = %58
%68 = tail call %struct.ibv_pd* @ibv_alloc_pd(%struct.ibv_context* %60)
%69 = icmp eq %struct.ibv_pd* %68, null
br i1 %69, label %70, label %74
70: ; preds = %67
%71 = load i32, i32* @stderr
%72 = load i32, i32* @errno
%73 = tail call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.7, i64 0, i64 0), i32 %72)
br label %104
74: ; preds = %67, %85
%75 = phi i64 [ %88, %85 ], [ 1, %67 ]
%76 = tail call i8* @malloc(i64 %75)
%77 = load i32, i32* @IBV_ACCESS_LOCAL_WRITE
%78 = tail call %struct.ibv_mr* @ibv_reg_mr(%struct.ibv_pd* %68, i8* %76, i64 %75, i32 %77)
%79 = icmp eq %struct.ibv_mr* %78, null
br i1 %79, label %80, label %85
80: ; preds = %74
%81 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0), i64 %75)
%82 = load i32, i32* @stderr
%83 = load i32, i32* @errno
%84 = tail call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.10, i64 0, i64 0), i32 %83)
br label %104
85: ; preds = %74
%86 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i64 %75)
%87 = tail call i32 @ibv_dereg_mr(%struct.ibv_mr* %78)
%88 = shl nuw nsw i64 %75, 1
%89 = icmp ult i64 %75, 134217728
br i1 %89, label %74, label %90
90: ; preds = %85
%91 = tail call i64 @ibv_dealloc_pd(%struct.ibv_pd* %68)
%92 = icmp eq i64 %91, 0
br i1 %92, label %96, label %93
93: ; preds = %90
%94 = load i32, i32* @stderr
%95 = tail call i32 (i32, i8*, ...) @fprintf(i32 %94, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0))
br label %104
96: ; preds = %90
%97 = tail call i64 @ibv_close_device(%struct.ibv_context* %60)
%98 = icmp eq i64 %97, 0
br i1 %98, label %102, label %99
99: ; preds = %96
%100 = load i32, i32* @stderr
%101 = tail call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.12, i64 0, i64 0))
br label %104
102: ; preds = %96
%103 = tail call i32 @ibv_free_device_list(%struct.ibv_device** %21)
br label %104
104: ; preds = %14, %17, %80, %55, %52, %23, %33, %70, %102, %99, %93, %62
%105 = phi i32 [ 1, %55 ], [ 1, %33 ], [ 1, %23 ], [ 1, %62 ], [ 1, %70 ], [ 1, %93 ], [ 1, %99 ], [ 0, %102 ], [ 1, %52 ], [ 1, %80 ], [ 1, %17 ], [ 1, %14 ]
ret i32 %105
}