#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;
  scalar_t__ d_type;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
scalar_t__ DT_LNK;
int EINVAL;
int ENOENT;
int ENOMEM;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *strdup(char const *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char *, int /*<<< orphan*/);
char *strrchr(char *, char);
int strtoul(char *, char **, int);
int scsi_host_offset(char const *hostdev_sysname) {
  DIR *dir = NULL;
  struct dirent *dent = NULL;
  char *base = NULL;
  char *pos = NULL;
  int basenum = -1;
  int rc = 0;
  base = strdup(hostdev_sysname);
  if (base == NULL) {
    return -ENOMEM;
  }
  pos = strrchr(base, '/');
  if (pos == NULL) {
    free(base);
    return -EINVAL;
  }
  pos[0] = '\0';
  dir = opendir(base);
  if (dir == NULL) {
    rc = -errno;
    free(base);
    return rc;
  }
  for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
    char *rest = NULL;
    int i = 0;
    if (dent->d_name[0] == '.') {
      continue;
    }
    if (dent->d_type != DT_DIR && dent->d_type != DT_LNK) {
      continue;
    }
    if (strncmp(dent->d_name, "host", strlen("host")) != 0) {
      continue;
    }
    i = strtoul(&dent->d_name[4], &rest, 10);
    if (rest[0] != '\0') {
      continue;
    }
    if (basenum == -1 || i < basenum) {
      basenum = i;
    }
  }
  closedir(dir);
  if (basenum == -1) {
    return -ENOENT;
  }
  return basenum;
}


x86 assembly (clang, -O0):
<scsi_host_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f5 <scsi_host_offset+0x65>
xor    %eax,%eax
mov    0x2df5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13ab <scsi_host_offset+0x21b>
mov    -0x28(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    122e <scsi_host_offset+0x9e>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    0x2da4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13ab <scsi_host_offset+0x21b>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    126f <scsi_host_offset+0xdf>
xor    %eax,%eax
mov    0x2d7a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13ab <scsi_host_offset+0x21b>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     137f <scsi_host_offset+0x1ef>
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    12ae <scsi_host_offset+0x11e>
jmp    136d <scsi_host_offset+0x1dd>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d1b(%rip),%rcx
cmp    (%rcx),%rax
je     12e3 <scsi_host_offset+0x153>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2ceb(%rip),%rcx
cmp    (%rcx),%rax
je     12e3 <scsi_host_offset+0x153>
jmp    136d <scsi_host_offset+0x1dd>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
lea    0xd0b(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
lea    0xcf9(%rip),%rsi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     131b <scsi_host_offset+0x18b>
jmp    136d <scsi_host_offset+0x1dd>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
add    $0x4,%rdi
lea    -0x40(%rbp),%rsi
mov    $0xa,%edx
call   10a0 <strtoul@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     134c <scsi_host_offset+0x1bc>
jmp    136d <scsi_host_offset+0x1dd>
cmpl   $0xffffffff,-0x34(%rbp)
je     1362 <scsi_host_offset+0x1d2>
mov    -0x44(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1368 <scsi_host_offset+0x1d8>
mov    -0x44(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    136d <scsi_host_offset+0x1dd>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
jmp    127c <scsi_host_offset+0xec>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
cmpl   $0xffffffff,-0x34(%rbp)
jne    13a5 <scsi_host_offset+0x215>
xor    %eax,%eax
mov    0x2c15(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13ab <scsi_host_offset+0x21b>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scsi_host_offset>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
call   10b0 <strdup@plt>
test   %rax,%rax
je     129b <scsi_host_offset+0x10b>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
test   %rax,%rax
je     12a4 <scsi_host_offset+0x114>
movb   $0x0,(%rax)
mov    %rbp,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12c5 <scsi_host_offset+0x135>
mov    %rax,%r13
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12dc <scsi_host_offset+0x14c>
mov    %rax,%rbp
mov    $0xffffffff,%r12d
mov    0x2dde(%rip),%r15
lea    0xdff(%rip),%r14
jmp    1240 <scsi_host_offset+0xb0>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1268 <scsi_host_offset+0xd8>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12b5 <scsi_host_offset+0x125>
movq   $0x0,(%rsp)
mov    0x0(%rbp),%rbx
cmpb   $0x2e,(%rbx)
je     1230 <scsi_host_offset+0xa0>
mov    0x8(%rbp),%rax
cmp    (%r15),%rax
je     1210 <scsi_host_offset+0x80>
mov    0x2d5f(%rip),%rcx
cmp    (%rcx),%rax
jne    1230 <scsi_host_offset+0xa0>
jmp    1210 <scsi_host_offset+0x80>
mov    0x0(%rbp),%rdi
add    $0x4,%rdi
mov    %rsp,%rsi
mov    $0xa,%edx
call   10a0 <strtoul@plt>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    1230 <scsi_host_offset+0xa0>
cmp    %r12d,%eax
mov    %r12d,%ecx
cmovl  %eax,%ecx
cmp    $0xffffffff,%r12d
cmove  %eax,%ecx
mov    %ecx,%r12d
jmp    1230 <scsi_host_offset+0xa0>
mov    0x2d3e(%rip),%rax
jmp    12eb <scsi_host_offset+0x15b>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x2d15(%rip),%rax
jmp    12eb <scsi_host_offset+0x15b>
mov    %r13,%rdi
call   1080 <closedir@plt>
cmp    $0xffffffff,%r12d
jne    12f1 <scsi_host_offset+0x161>
jmp    12e4 <scsi_host_offset+0x154>
mov    0x2d04(%rip),%rax
xor    %r12d,%r12d
sub    (%rax),%r12d
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    12f1 <scsi_host_offset+0x161>
mov    %r13,%rdi
call   1080 <closedir@plt>
mov    0x2cc5(%rip),%rax
xor    %r12d,%r12d
sub    (%rax),%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@ENOMEM = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@DT_DIR = global i64 0
@DT_LNK = global i64 0
@.str = [5 x i8] c"host\00"
@ENOENT = global i32 0
define i32 @scsi_host_offset(i8* nocapture %0) {
%2 = alloca i8*
%3 = tail call i8* @strdup(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @ENOMEM
%7 = sub nsw i32 0, %6
br label %70
8: ; preds = %1
%9 = tail call i8* @strrchr(i8* %3, i8 signext 47)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = tail call i32 @free(i8* %3)
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
br label %70
15: ; preds = %8
store i8 0, i8* %9
%16 = tail call i32* @opendir(i8* %3)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
%21 = tail call i32 @free(i8* %3)
br label %70
22: ; preds = %15
%23 = tail call %struct.dirent* @readdir(i32* %16)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @closedir(i32* %16)
br label %67
27: ; preds = %22
%28 = bitcast i8** %2 to i8*
br label %29
29: ; preds = %27, %60
%30 = phi i32 [ -1, %27 ], [ %61, %60 ]
%31 = phi %struct.dirent* [ %23, %27 ], [ %62, %60 ]
store i8* null, i8** %2
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i64 0, i32 0
%33 = load i8*, i8** %32
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 46
br i1 %35, label %60, label %36
36: ; preds = %29
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i64 0, i32 1
%38 = load i64, i64* %37
%39 = load i64, i64* @DT_DIR
%40 = icmp eq i64 %38, %39
%41 = load i64, i64* @DT_LNK
%42 = icmp eq i64 %38, %41
%43 = select i1 %40, i1 true, i1 %42
br i1 %43, label %44, label %60
44: ; preds = %36
%45 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%46 = tail call i64 @strncmp(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %60
48: ; preds = %44
%49 = load i8*, i8** %32
%50 = getelementptr inbounds i8, i8* %49, i64 4
%51 = call i32 @strtoul(i8* %50, i8** %2, i32 10)
%52 = load i8*, i8** %2
%53 = load i8, i8* %52
%54 = icmp eq i8 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %48
%56 = icmp eq i32 %30, -1
%57 = icmp slt i32 %51, %30
%58 = select i1 %56, i1 true, i1 %57
%59 = select i1 %58, i32 %51, i32 %30
br label %60
60: ; preds = %48, %44, %36, %29, %55
%61 = phi i32 [ %59, %55 ], [ %30, %29 ], [ %30, %36 ], [ %30, %44 ], [ %30, %48 ]
%62 = tail call %struct.dirent* @readdir(i32* %16)
%63 = icmp eq %struct.dirent* %62, null
br i1 %63, label %64, label %29
64: ; preds = %60
%65 = tail call i32 @closedir(i32* %16)
%66 = icmp eq i32 %61, -1
br i1 %66, label %67, label %70
67: ; preds = %25, %64
%68 = load i32, i32* @ENOENT
%69 = sub nsw i32 0, %68
br label %70
70: ; preds = %64, %67, %18, %11, %5
%71 = phi i32 [ %7, %5 ], [ %14, %11 ], [ %20, %18 ], [ %69, %67 ], [ %61, %64 ]
ret i32 %71
}