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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sa_sys_read_u32(char *, char *, scalar_t__ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
char *strdup(char *);
char *get_host_by_fcid(uint32_t fcid) {
  struct dirent *dp;
  DIR *dir;
  char *host = NULL;
  char path[1024];
  uint32_t port_id = 0;
  dir = opendir("/sys/class/fc_host");
  if (!dir)
    return NULL;
  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;
    snprintf(path, sizeof(path), "%s/%s", "/sys/class/fc_host", dp->d_name);
    sa_sys_read_u32(path, "port_id", &port_id);
    if (port_id != fcid)
      continue;
    host = strdup(dp->d_name);
    break;
  }
  closedir(dir);
  return host;
}


x86 assembly (clang, -O0):
<get_host_by_fcid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x438(%rbp)
lea    0xe77(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11aa <get_host_by_fcid+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    12a9 <get_host_by_fcid+0x149>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1298 <get_host_by_fcid+0x138>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11ee <get_host_by_fcid+0x8e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    11ee <get_host_by_fcid+0x8e>
jmp    1286 <get_host_by_fcid+0x126>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    121b <get_host_by_fcid+0xbb>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
jne    121b <get_host_by_fcid+0xbb>
jmp    1286 <get_host_by_fcid+0x126>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xdde(%rip),%rdx
lea    0xdc4(%rip),%rcx
call   1040 <snprintf@plt>
lea    -0x430(%rbp),%rdi
lea    0xdca(%rip),%rsi
lea    -0x438(%rbp),%rdx
call   1050 <sa_sys_read_u32@plt>
mov    -0x438(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1271 <get_host_by_fcid+0x111>
jmp    1286 <get_host_by_fcid+0x126>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x28(%rbp)
jmp    1298 <get_host_by_fcid+0x138>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
jmp    11b7 <get_host_by_fcid+0x57>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_host_by_fcid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
lea    0xe7c(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     122b <get_host_by_fcid+0xcb>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     121f <get_host_by_fcid+0xbf>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
lea    0xe64(%rip),%r13
lea    0x8(%rsp),%rbp
jmp    1200 <get_host_by_fcid+0xa0>
nopl   0x0(%rax)
mov    %r14,%rdi
mov    $0x400,%esi
lea    0xe44(%rip),%rdx
lea    0xe2a(%rip),%rcx
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1050 <sa_sys_read_u32@plt>
cmp    %r15,0x8(%rsp)
je     1242 <get_host_by_fcid+0xe2>
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     121f <get_host_by_fcid+0xbf>
mov    (%rbx),%r8
movzbl 0x1(%r8),%eax
cmpb   $0x2e,(%r8)
jne    1212 <get_host_by_fcid+0xb2>
test   %al,%al
je     11f0 <get_host_by_fcid+0x90>
cmp    $0x2e,%al
jne    11c0 <get_host_by_fcid+0x60>
cmpb   $0x0,0x2(%r8)
jne    11c0 <get_host_by_fcid+0x60>
jmp    11f0 <get_host_by_fcid+0x90>
xor    %ebx,%ebx
mov    %r12,%rdi
call   1060 <closedir@plt>
jmp    122d <get_host_by_fcid+0xcd>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
jmp    1221 <get_host_by_fcid+0xc1>

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [19 x i8] c"/sys/class/fc_host\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [8 x i8] c"port_id\00"
define noalias i8* @get_host_by_fcid(i64 %0) {
%2 = alloca [1024 x i8]
%3 = alloca i64
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %42, label %8
8: ; preds = %1
%9 = tail call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %39, label %11
11: ; preds = %8, %36
%12 = phi %struct.dirent* [ %37, %36 ], [ %9, %8 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 46
%17 = getelementptr inbounds i8, i8* %14, i64 1
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
%20 = select i1 %16, i1 %19, i1 false
br i1 %20, label %36, label %21
21: ; preds = %11
%22 = icmp eq i8 %18, 46
br i1 %22, label %23, label %27
23: ; preds = %21
%24 = getelementptr inbounds i8, i8* %14, i64 2
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %23, %21
%28 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %14)
%29 = call i32 @sa_sys_read_u32(i8* %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64* %3)
%30 = load i64, i64* %3
%31 = icmp eq i64 %30, %0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%34 = load i8*, i8** %33
%35 = call i8* @strdup(i8* %34)
br label %39
36: ; preds = %11, %27, %23
%37 = call %struct.dirent* @readdir(i32* %6)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %11
39: ; preds = %36, %8, %32
%40 = phi i8* [ %35, %32 ], [ null, %8 ], [ null, %36 ]
%41 = call i32 @closedir(i32* %6)
br label %42
42: ; preds = %1, %39
%43 = phi i8* [ %40, %39 ], [ null, %1 ]
ret i8* %43
}