#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 /*<<< orphan*/ name;
};

/* Variables and functions */
struct ibv_device **ibv_get_device_list(int /*<<< orphan*/ *);
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
struct ibv_device *ibFindDevice(const char *name) {
  struct ibv_device **devices;
  devices = ibv_get_device_list(NULL);
  if (devices == NULL)
    return NULL;
  if (name == NULL)
    return devices[0];
  for (int i = 0; devices[i] != NULL; i++) {
    if (strcmp(devices[i]->name, name) == 0)
      return devices[i];
  }
  return NULL;
}


x86 assembly (clang, -O0):
<ibFindDevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <ibv_get_device_list@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1151 <ibFindDevice+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11d7 <ibFindDevice+0xb7>
cmpq   $0x0,-0x10(%rbp)
jne    116c <ibFindDevice+0x4c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11d7 <ibFindDevice+0xb7>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11cf <ibFindDevice+0xaf>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    11bc <ibFindDevice+0x9c>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x8(%rbp)
jmp    11d7 <ibFindDevice+0xb7>
jmp    11c1 <ibFindDevice+0xa1>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1173 <ibFindDevice+0x53>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ibFindDevice>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %r12d,%r12d
xor    %edi,%edi
call   1040 <ibv_get_device_list@plt>
test   %rax,%rax
je     117a <ibFindDevice+0x5a>
mov    %rax,%r15
mov    (%rax),%r12
test   %r14,%r14
je     117a <ibFindDevice+0x5a>
test   %r12,%r12
je     116f <ibFindDevice+0x4f>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%r12),%edi
mov    %r14,%rsi
call   1030 <strcmp@plt>
test   %rax,%rax
je     1174 <ibFindDevice+0x54>
mov    0x8(%r15,%rbx,8),%r12
add    $0x1,%rbx
test   %r12,%r12
jne    1150 <ibFindDevice+0x30>
xor    %r12d,%r12d
jmp    117a <ibFindDevice+0x5a>
mov    %ebx,%eax
mov    (%r15,%rax,8),%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ibv_device = type { i32 }
define %struct.ibv_device* @ibFindDevice(i8* %0) {
%2 = alloca %struct.ibv_device*
%3 = alloca i8*
%4 = alloca %struct.ibv_device**
%5 = alloca i32
store i8* %0, i8** %3
%6 = call %struct.ibv_device** @ibv_get_device_list(i32* null)
store %struct.ibv_device** %6, %struct.ibv_device*** %4
%7 = load %struct.ibv_device**, %struct.ibv_device*** %4
%8 = icmp eq %struct.ibv_device** %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store %struct.ibv_device* null, %struct.ibv_device** %2
br label %47
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load %struct.ibv_device**, %struct.ibv_device*** %4
%15 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %14, i64 0
%16 = load %struct.ibv_device*, %struct.ibv_device** %15
store %struct.ibv_device* %16, %struct.ibv_device** %2
br label %47
17: ; preds = %10
store i32 0, i32* %5
br label %18
18: ; preds = %43, %17
%19 = load %struct.ibv_device**, %struct.ibv_device*** %4
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %19, i64 %21
%23 = load %struct.ibv_device*, %struct.ibv_device** %22
%24 = icmp ne %struct.ibv_device* %23, null
br i1 %24, label %25, label %46
25: ; preds = %18
%26 = load %struct.ibv_device**, %struct.ibv_device*** %4
%27 = load i32, i32* %5
%28 = sext 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 = getelementptr inbounds %struct.ibv_device, %struct.ibv_device* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i8*, i8** %3
%34 = call i64 @strcmp(i32 %32, i8* %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %25
%37 = load %struct.ibv_device**, %struct.ibv_device*** %4
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %37, i64 %39
%41 = load %struct.ibv_device*, %struct.ibv_device** %40
store %struct.ibv_device* %41, %struct.ibv_device** %2
br label %47
42: ; preds = %25
br label %43
43: ; preds = %42
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %5
br label %18
46: ; preds = %18
store %struct.ibv_device* null, %struct.ibv_device** %2
br label %47
47: ; preds = %46, %36, %13, %9
%48 = load %struct.ibv_device*, %struct.ibv_device** %2
ret %struct.ibv_device* %48
}

LLVM IR (-O2):
%struct.ibv_device = type { i32 }
define %struct.ibv_device* @ibFindDevice(i8* %0) {
%2 = tail call %struct.ibv_device** @ibv_get_device_list(i32* null)
%3 = icmp eq %struct.ibv_device** %2, null
br i1 %3, label %25, label %4
4: ; preds = %1
%5 = icmp eq i8* %0, null
%6 = load %struct.ibv_device*, %struct.ibv_device** %2
br i1 %5, label %25, label %7
7: ; preds = %4
%8 = icmp eq %struct.ibv_device* %6, null
br i1 %8, label %25, label %9
9: ; preds = %7, %20
%10 = phi i64 [ %21, %20 ], [ 0, %7 ]
%11 = phi %struct.ibv_device* [ %23, %20 ], [ %6, %7 ]
%12 = getelementptr inbounds %struct.ibv_device, %struct.ibv_device* %11, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i64 @strcmp(i32 %13, i8* %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %9
%17 = and i64 %10, 4294967295
%18 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %2, i64 %17
%19 = load %struct.ibv_device*, %struct.ibv_device** %18
br label %25
20: ; preds = %9
%21 = add nuw i64 %10, 1
%22 = getelementptr inbounds %struct.ibv_device*, %struct.ibv_device** %2, i64 %21
%23 = load %struct.ibv_device*, %struct.ibv_device** %22
%24 = icmp eq %struct.ibv_device* %23, null
br i1 %24, label %25, label %9
25: ; preds = %20, %4, %7, %16, %1
%26 = phi %struct.ibv_device* [ null, %1 ], [ %19, %16 ], [ null, %7 ], [ %6, %4 ], [ null, %20 ]
ret %struct.ibv_device* %26
}