#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 dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strstr(char *, char *);
int get_number_of_adapters(void) {
  struct dirent *dp;
  int num = 0;
  DIR *dir;
  dir = opendir("/sys/class/fc_host");
  if (!dir)
    return errno;
  for (dp = readdir(dir); dp != NULL; dp = readdir(dir)) {
    if (dp->d_name[0] == '.' && dp->d_name[1] == '\0')
      continue;
    if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
      continue;
    if (strstr(dp->d_name, "host"))
      num++;
  }
  closedir(dir);
  return num;
}


x86 assembly (clang, -O0):
<get_number_of_adapters>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x14(%rbp)
lea    0xeaa(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117b <get_number_of_adapters+0x3b>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1238 <get_number_of_adapters+0xf8>
mov    -0x20(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1229 <get_number_of_adapters+0xe9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11bf <get_number_of_adapters+0x7f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    11bf <get_number_of_adapters+0x7f>
jmp    1217 <get_number_of_adapters+0xd7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    11ec <get_number_of_adapters+0xac>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
jne    11ec <get_number_of_adapters+0xac>
jmp    1217 <get_number_of_adapters+0xd7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe14(%rip),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
je     1212 <get_number_of_adapters+0xd2>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1217 <get_number_of_adapters+0xd7>
mov    -0x20(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x10(%rbp)
jmp    1188 <get_number_of_adapters+0x48>
mov    -0x20(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_number_of_adapters>:
push   %rbp
push   %r14
push   %rbx
lea    0xeb5(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11b2 <get_number_of_adapters+0x72>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11a8 <get_number_of_adapters+0x68>
lea    0xea0(%rip),%r14
jmp    118c <get_number_of_adapters+0x4c>
mov    %r14,%rsi
call   1060 <strstr@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     11a8 <get_number_of_adapters+0x68>
mov    (%rax),%rdi
movzbl 0x1(%rdi),%eax
cmpb   $0x2e,(%rdi)
jne    119c <get_number_of_adapters+0x5c>
test   %al,%al
je     117f <get_number_of_adapters+0x3f>
cmp    $0x2e,%al
jne    1170 <get_number_of_adapters+0x30>
cmpb   $0x0,0x2(%rdi)
jne    1170 <get_number_of_adapters+0x30>
jmp    117f <get_number_of_adapters+0x3f>
mov    %rbx,%rdi
call   1040 <closedir@plt>
jmp    11bb <get_number_of_adapters+0x7b>
mov    0x2e27(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [19 x i8] c"/sys/class/fc_host\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"host\00"
define i32 @get_number_of_adapters() {
%1 = alloca i32
%2 = alloca %struct.dirent*
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %3
%5 = call i32* @opendir(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %4
%6 = load i32*, i32** %4
%7 = icmp ne i32* %6, null
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = load i32, i32* @errno
store i32 %9, i32* %1
br label %67
10: ; preds = %0
%11 = load i32*, i32** %4
%12 = call %struct.dirent* @readdir(i32* %11)
store %struct.dirent* %12, %struct.dirent** %2
br label %13
13: ; preds = %60, %10
%14 = load %struct.dirent*, %struct.dirent** %2
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %63
16: ; preds = %13
%17 = load %struct.dirent*, %struct.dirent** %2
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 46
br i1 %23, label %24, label %33
24: ; preds = %16
%25 = load %struct.dirent*, %struct.dirent** %2
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %24
br label %60
33: ; preds = %24, %16
%34 = load %struct.dirent*, %struct.dirent** %2
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 46
br i1 %40, label %41, label %50
41: ; preds = %33
%42 = load %struct.dirent*, %struct.dirent** %2
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = getelementptr inbounds i8, i8* %44, i64 2
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %41
br label %60
50: ; preds = %41, %33
%51 = load %struct.dirent*, %struct.dirent** %2
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = call i64 @strstr(i8* %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %50
%57 = load i32, i32* %3
%58 = add nsw i32 %57, 1
store i32 %58, i32* %3
br label %59
59: ; preds = %56, %50
br label %60
60: ; preds = %59, %49, %32
%61 = load i32*, i32** %4
%62 = call %struct.dirent* @readdir(i32* %61)
store %struct.dirent* %62, %struct.dirent** %2
br label %13
63: ; preds = %13
%64 = load i32*, i32** %4
%65 = call i32 @closedir(i32* %64)
%66 = load i32, i32* %3
store i32 %66, i32* %1
br label %67
67: ; preds = %63, %8
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [19 x i8] c"/sys/class/fc_host\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"host\00"
define i32 @get_number_of_adapters() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = load i32, i32* @errno
br label %37
5: ; preds = %0
%6 = tail call %struct.dirent* @readdir(i32* %1)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %34, label %8
8: ; preds = %5, %30
%9 = phi i32 [ %31, %30 ], [ 0, %5 ]
%10 = phi %struct.dirent* [ %32, %30 ], [ %6, %5 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 46
%15 = getelementptr inbounds i8, i8* %12, i64 1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %30, label %19
19: ; preds = %8
%20 = icmp eq i8 %16, 46
br i1 %20, label %21, label %25
21: ; preds = %19
%22 = getelementptr inbounds i8, i8* %12, i64 2
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %21, %19
%26 = tail call i64 @strstr(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp ne i64 %26, 0
%28 = zext i1 %27 to i32
%29 = add nsw i32 %9, %28
br label %30
30: ; preds = %8, %25, %21
%31 = phi i32 [ %9, %21 ], [ %29, %25 ], [ %9, %8 ]
%32 = tail call %struct.dirent* @readdir(i32* %1)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %34, label %8
34: ; preds = %30, %5
%35 = phi i32 [ 0, %5 ], [ %31, %30 ]
%36 = tail call i32 @closedir(i32* %1)
br label %37
37: ; preds = %34, %3
%38 = phi i32 [ %35, %34 ], [ %4, %3 ]
ret i32 %38
}