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

/* Variables and functions */
int asprintf(char **, char *, char *, char const *, int, ...);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int get_ctlr_num(char const *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int stat(char *, struct stat *);
char *strdup(char *);
char *strstr(char *, char *);
char *get_host_from_netdev(const char *netdev) {
  struct dirent *dp;
  char *host = NULL;
  char *path = NULL;
  DIR *dir;
  int ret;
  int ctlr_num;
  ctlr_num = get_ctlr_num(netdev);
  if (ctlr_num == -1)
    return NULL;
  ret = asprintf(&path, "%s/%s/ctlr_%d/", "/sys/class/net", netdev, ctlr_num);
  if (ret == -1)
    return NULL;
  dir = opendir(path);
  free(path);
  path = NULL;
  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;
    host = strstr(dp->d_name, "host");
    if (host) {
      struct stat sb;
      ret = asprintf(&path, "%s/%s/ctlr_%d/%s/fc_host/%s", "/sys/class/net",
                     netdev, ctlr_num, host, host);
      if (ret == -1)
        goto out_closedir;
      ret = stat(path, &sb);
      free(path);
      path = NULL;
      if (ret == -1)
        host = NULL;
      break;
    }
  }
out_closedir:
  closedir(dir);
  return host ? strdup(host) : NULL;
}


x86 assembly (clang, -O0):
<get_host_from_netdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <get_ctlr_num@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11cf <get_host_from_netdev+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    139f <get_host_from_netdev+0x20f>
mov    -0x10(%rbp),%rcx
mov    -0x38(%rbp),%r8d
lea    -0x28(%rbp),%rdi
lea    0xe1e(%rip),%rsi
lea    0xe26(%rip),%rdx
mov    $0x0,%al
call   1080 <asprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    120a <get_host_from_netdev+0x7a>
movq   $0x0,-0x8(%rbp)
jmp    139f <get_host_from_netdev+0x20f>
mov    -0x28(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1240 <get_host_from_netdev+0xb0>
movq   $0x0,-0x8(%rbp)
jmp    139f <get_host_from_netdev+0x20f>
mov    -0x30(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1361 <get_host_from_netdev+0x1d1>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1284 <get_host_from_netdev+0xf4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1284 <get_host_from_netdev+0xf4>
jmp    134f <get_host_from_netdev+0x1bf>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    12b1 <get_host_from_netdev+0x121>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
jne    12b1 <get_host_from_netdev+0x121>
jmp    134f <get_host_from_netdev+0x1bf>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xd5f(%rip),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     134a <get_host_from_netdev+0x1ba>
mov    -0x10(%rbp),%rcx
mov    -0x38(%rbp),%r8d
mov    -0x20(%rbp),%r9
mov    -0x20(%rbp),%rax
lea    -0x28(%rbp),%rdi
lea    0xd35(%rip),%rsi
lea    0xd1a(%rip),%rdx
mov    %rax,(%rsp)
mov    $0x0,%al
call   1080 <asprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1312 <get_host_from_netdev+0x182>
jmp    1366 <get_host_from_netdev+0x1d6>
mov    -0x28(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1345 <get_host_from_netdev+0x1b5>
movq   $0x0,-0x20(%rbp)
jmp    1361 <get_host_from_netdev+0x1d1>
jmp    134f <get_host_from_netdev+0x1bf>
mov    -0x30(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
jmp    124d <get_host_from_netdev+0xbd>
jmp    1366 <get_host_from_netdev+0x1d6>
mov    -0x30(%rbp),%rdi
call   1050 <closedir@plt>
cmpq   $0x0,-0x20(%rbp)
je     138c <get_host_from_netdev+0x1fc>
mov    -0x20(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x48(%rbp)
jmp    1397 <get_host_from_netdev+0x207>
xor    %eax,%eax
mov    %rax,-0x48(%rbp)
jmp    1397 <get_host_from_netdev+0x207>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_host_from_netdev>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
movq   $0x0,0x10(%rsp)
call   1090 <get_ctlr_num@plt>
cmp    $0xffffffff,%eax
je     12e1 <get_host_from_netdev+0x151>
mov    %eax,%r15d
lea    0xe40(%rip),%rsi
lea    0xe48(%rip),%rdx
xor    %ebx,%ebx
lea    0x10(%rsp),%rdi
mov    %r14,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1080 <asprintf@plt>
cmp    $0xffffffff,%eax
je     12e3 <get_host_from_netdev+0x153>
mov    0x10(%rsp),%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
movq   $0x0,0x10(%rsp)
test   %rbx,%rbx
je     12e1 <get_host_from_netdev+0x151>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     12d9 <get_host_from_netdev+0x149>
lea    0xdf9(%rip),%r12
jmp    124e <get_host_from_netdev+0xbe>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
jne    126a <get_host_from_netdev+0xda>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     12d9 <get_host_from_netdev+0x149>
mov    (%rax),%rdi
movzbl 0x1(%rdi),%eax
cmpb   $0x2e,(%rdi)
jne    125e <get_host_from_netdev+0xce>
test   %al,%al
je     123d <get_host_from_netdev+0xad>
cmp    $0x2e,%al
jne    1230 <get_host_from_netdev+0xa0>
cmpb   $0x0,0x2(%rdi)
jne    1230 <get_host_from_netdev+0xa0>
jmp    123d <get_host_from_netdev+0xad>
mov    %rax,%rbp
mov    %rax,(%rsp)
lea    0xdab(%rip),%rsi
lea    0xd90(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    %r14,%rcx
mov    %r15d,%r8d
mov    %rax,%r9
xor    %eax,%eax
call   1080 <asprintf@plt>
cmp    $0xffffffff,%eax
je     12c4 <get_host_from_netdev+0x134>
mov    0x10(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   1060 <stat@plt>
mov    %eax,%r14d
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
movq   $0x0,0x10(%rsp)
cmp    $0xffffffff,%r14d
je     12d9 <get_host_from_netdev+0x149>
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %rbp,%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbx
jmp    12e3 <get_host_from_netdev+0x153>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [15 x i8] c"%s/%s/ctlr_%d/\00"
@.str.1 = [15 x i8] c"/sys/class/net\00"
@.str.2 = [5 x i8] c"host\00"
@.str.3 = [28 x i8] c"%s/%s/ctlr_%d/%s/fc_host/%s\00"
define i8* @get_host_from_netdev(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
store i8* %0, i8** %3
store i8* null, i8** %5
store i8* null, i8** %6
%11 = load i8*, i8** %3
%12 = call i32 @get_ctlr_num(i8* %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %1
store i8* null, i8** %2
br label %112
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = load i32, i32* %9
%19 = call i32 (i8**, i8*, i8*, i8*, i32, ...) @asprintf(i8** %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %17, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %16
store i8* null, i8** %2
br label %112
23: ; preds = %16
%24 = load i8*, i8** %6
%25 = call i32* @opendir(i8* %24)
store i32* %25, i32** %7
%26 = load i8*, i8** %6
%27 = call i32 @free(i8* %26)
store i8* null, i8** %6
%28 = load i32*, i32** %7
%29 = icmp ne i32* %28, null
br i1 %29, label %31, label %30
30: ; preds = %23
store i8* null, i8** %2
br label %112
31: ; preds = %23
%32 = load i32*, i32** %7
%33 = call %struct.dirent* @readdir(i32* %32)
store %struct.dirent* %33, %struct.dirent** %4
br label %34
34: ; preds = %97, %31
%35 = load %struct.dirent*, %struct.dirent** %4
%36 = icmp ne %struct.dirent* %35, null
br i1 %36, label %37, label %100
37: ; preds = %34
%38 = load %struct.dirent*, %struct.dirent** %4
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = getelementptr inbounds i8, i8* %40, i64 0
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 46
br i1 %44, label %45, label %54
45: ; preds = %37
%46 = load %struct.dirent*, %struct.dirent** %4
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = getelementptr inbounds i8, i8* %48, i64 1
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %45
br label %97
54: ; preds = %45, %37
%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 = getelementptr inbounds i8, i8* %57, i64 1
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 46
br i1 %61, label %62, label %71
62: ; preds = %54
%63 = load %struct.dirent*, %struct.dirent** %4
%64 = getelementptr inbounds %struct.dirent, %struct.dirent* %63, i32 0, i32 0
%65 = load i8*, i8** %64
%66 = getelementptr inbounds i8, i8* %65, i64 2
%67 = load i8, i8* %66
%68 = sext i8 %67 to i32
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %62
br label %97
71: ; preds = %62, %54
%72 = load %struct.dirent*, %struct.dirent** %4
%73 = getelementptr inbounds %struct.dirent, %struct.dirent* %72, i32 0, i32 0
%74 = load i8*, i8** %73
%75 = call i8* @strstr(i8* %74, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %75, i8** %5
%76 = load i8*, i8** %5
%77 = icmp ne i8* %76, null
br i1 %77, label %78, label %96
78: ; preds = %71
%79 = load i8*, i8** %3
%80 = load i32, i32* %9
%81 = load i8*, i8** %5
%82 = load i8*, i8** %5
%83 = call i32 (i8**, i8*, i8*, i8*, i32, ...) @asprintf(i8** %6, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %79, i32 %80, i8* %81, i8* %82)
store i32 %83, i32* %8
%84 = load i32, i32* %8
%85 = icmp eq i32 %84, -1
br i1 %85, label %86, label %87
86: ; preds = %78
br label %101
87: ; preds = %78
%88 = load i8*, i8** %6
%89 = call i32 @stat(i8* %88, %struct.stat* %10)
store i32 %89, i32* %8
%90 = load i8*, i8** %6
%91 = call i32 @free(i8* %90)
store i8* null, i8** %6
%92 = load i32, i32* %8
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %95
94: ; preds = %87
store i8* null, i8** %5
br label %95
95: ; preds = %94, %87
br label %100
96: ; preds = %71
br label %97
97: ; preds = %96, %70, %53
%98 = load i32*, i32** %7
%99 = call %struct.dirent* @readdir(i32* %98)
store %struct.dirent* %99, %struct.dirent** %4
br label %34
100: ; preds = %95, %34
br label %101
101: ; preds = %100, %86
%102 = load i32*, i32** %7
%103 = call i32 @closedir(i32* %102)
%104 = load i8*, i8** %5
%105 = icmp ne i8* %104, null
br i1 %105, label %106, label %109
106: ; preds = %101
%107 = load i8*, i8** %5
%108 = call i8* @strdup(i8* %107)
br label %110
109: ; preds = %101
br label %110
110: ; preds = %109, %106
%111 = phi i8* [ %108, %106 ], [ null, %109 ]
store i8* %111, i8** %2
br label %112
112: ; preds = %110, %30, %22, %15
%113 = load i8*, i8** %2
ret i8* %113
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [15 x i8] c"%s/%s/ctlr_%d/\00"
@.str.1 = [15 x i8] c"/sys/class/net\00"
@.str.2 = [5 x i8] c"host\00"
@.str.3 = [28 x i8] c"%s/%s/ctlr_%d/%s/fc_host/%s\00"
define noalias i8* @get_host_from_netdev(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = tail call i32 @get_ctlr_num(i8* %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %58, label %7
7: ; preds = %1
%8 = call i32 (i8**, i8*, i8*, i8*, i32, ...) @asprintf(i8** %2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %5)
%9 = icmp eq i32 %8, -1
br i1 %9, label %58, label %10
10: ; preds = %7
%11 = load i8*, i8** %2
%12 = call i32* @opendir(i8* %11)
%13 = load i8*, i8** %2
%14 = call i32 @free(i8* %13)
store i8* null, i8** %2
%15 = icmp eq i32* %12, null
br i1 %15, label %58, label %16
16: ; preds = %10
%17 = call %struct.dirent* @readdir(i32* %12)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %19, label %21
19: ; preds = %50, %16
%20 = call i32 @closedir(i32* %12)
br label %58
21: ; preds = %16, %50
%22 = phi %struct.dirent* [ %51, %50 ], [ %17, %16 ]
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 46
%27 = getelementptr inbounds i8, i8* %24, i64 1
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
%30 = select i1 %26, i1 %29, i1 false
br i1 %30, label %50, label %31
31: ; preds = %21
%32 = icmp eq i8 %28, 46
br i1 %32, label %33, label %37
33: ; preds = %31
%34 = getelementptr inbounds i8, i8* %24, i64 2
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 0
br i1 %36, label %50, label %37
37: ; preds = %33, %31
%38 = call i8* @strstr(i8* dereferenceable(1) %24, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%39 = icmp eq i8* %38, null
br i1 %39, label %50, label %40
40: ; preds = %37
%41 = bitcast %struct.stat* %3 to i8*
%42 = call i32 (i8**, i8*, i8*, i8*, i32, ...) @asprintf(i8** %2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %5, i8* %38, i8* %38)
%43 = icmp eq i32 %42, -1
br i1 %43, label %55, label %44
44: ; preds = %40
%45 = load i8*, i8** %2
%46 = call i32 @stat(i8* %45, %struct.stat* %3)
%47 = load i8*, i8** %2
%48 = call i32 @free(i8* %47)
store i8* null, i8** %2
%49 = icmp eq i32 %46, -1
br i1 %49, label %53, label %55
50: ; preds = %21, %37, %33
%51 = call %struct.dirent* @readdir(i32* %12)
%52 = icmp eq %struct.dirent* %51, null
br i1 %52, label %19, label %21
53: ; preds = %44
%54 = call i32 @closedir(i32* %12)
br label %58
55: ; preds = %44, %40
%56 = call i32 @closedir(i32* %12)
%57 = call i8* @strdup(i8* %38)
br label %58
58: ; preds = %53, %19, %55, %10, %7, %1
%59 = phi i8* [ null, %1 ], [ null, %7 ], [ null, %10 ], [ %57, %55 ], [ null, %53 ], [ null, %19 ]
ret i8* %59
}