#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 PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char const *);
int valid_enclosure_device(const char *sg) {
  struct dirent *edirent, *sdirent;
  DIR *edir, *sdir;
  char path[PATH_MAX];
  edir = opendir("/sys/class/enclosure");
  if (!edir) {
    fprintf(stderr, "System does not have SCSI enclosure(s).\n");
    return -1;
  }
  while ((edirent = readdir(edir)) != NULL) {
    if (!strcmp(edirent->d_name, ".") || !strcmp(edirent->d_name, ".."))
      continue;
    snprintf(path, PATH_MAX, "%s/%s/device/scsi_generic",
             "/sys/class/enclosure", edirent->d_name);
    sdir = opendir(path);
    if (!sdir)
      continue;
    while ((sdirent = readdir(sdir)) != NULL) {
      if (!strcmp(sdirent->d_name, ".") || !strcmp(sdirent->d_name, ".."))
        continue;
      if (!strcmp(sdirent->d_name, sg))
        goto out;
    }
    closedir(sdir);
  }
  closedir(edir);
  fprintf(stderr, "%s is not a valid enclosure device\n", sg);
  return -1;
out:
  closedir(sdir);
  closedir(edir);
  return 0;
}


x86 assembly (clang, -O0):
<valid_enclosure_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
lea    0xe61(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11dd <valid_enclosure_device+0x7d>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    135b <valid_enclosure_device+0x1fb>
jmp    11e2 <valid_enclosure_device+0x82>
mov    -0x28(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1304 <valid_enclosure_device+0x1a4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe38(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     1231 <valid_enclosure_device+0xd1>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1236 <valid_enclosure_device+0xd6>
jmp    11e2 <valid_enclosure_device+0x82>
mov    -0x50(%rbp),%rdi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
lea    0xdf0(%rip),%rdx
lea    0xda8(%rip),%rcx
call   1040 <snprintf@plt>
mov    -0x50(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    127a <valid_enclosure_device+0x11a>
jmp    11e2 <valid_enclosure_device+0x82>
jmp    127f <valid_enclosure_device+0x11f>
mov    -0x30(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12f6 <valid_enclosure_device+0x196>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd9b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     12ce <valid_enclosure_device+0x16e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd7e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    12d3 <valid_enclosure_device+0x173>
jmp    127f <valid_enclosure_device+0x11f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    12f1 <valid_enclosure_device+0x191>
jmp    133b <valid_enclosure_device+0x1db>
jmp    127f <valid_enclosure_device+0x11f>
mov    -0x30(%rbp),%rdi
call   1050 <closedir@plt>
jmp    11e2 <valid_enclosure_device+0x82>
mov    -0x28(%rbp),%rdi
call   1050 <closedir@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd3a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    135b <valid_enclosure_device+0x1fb>
mov    -0x30(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x28(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valid_enclosure_device>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,-0x30(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
lea    0xe66(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12b7 <valid_enclosure_device+0x157>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1292 <valid_enclosure_device+0x132>
lea    0xe7c(%rip),%r15
lea    0xe74(%rip),%r13
jmp    11e9 <valid_enclosure_device+0x89>
nopl   0x0(%rax)
mov    %r14,%rdi
call   1050 <closedir@plt>
mov    %r12,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1292 <valid_enclosure_device+0x132>
mov    (%rax),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11d8 <valid_enclosure_device+0x78>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11d8 <valid_enclosure_device+0x78>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%r14
mov    %r14,%rdi
lea    0xe20(%rip),%rdx
lea    0xdd8(%rip),%rcx
mov    %rbx,%r8
call   1040 <snprintf@plt>
mov    %r14,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11d8 <valid_enclosure_device+0x78>
mov    %rax,%r14
mov    %r14,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11d0 <valid_enclosure_device+0x70>
mov    (%rax),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1240 <valid_enclosure_device+0xe0>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1240 <valid_enclosure_device+0xe0>
mov    %rbx,%rdi
mov    -0x30(%rbp),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    1240 <valid_enclosure_device+0xe0>
mov    %r14,%rdi
call   1050 <closedir@plt>
mov    %r12,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
jmp    12d3 <valid_enclosure_device+0x173>
mov    %r12,%rdi
call   1050 <closedir@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
mov    -0x30(%rbp),%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12ce <valid_enclosure_device+0x16e>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [21 x i8] c"/sys/class/enclosure\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"System does not have SCSI enclosure(s).\0A\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [26 x i8] c"%s/%s/device/scsi_generic\00"
@.str.5 = [36 x i8] c"%s is not a valid enclosure device\0A\00"
define i32 @valid_enclosure_device(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca %struct.dirent*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
%15 = call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %6
%16 = load i32*, i32** %6
%17 = icmp ne i32* %16, null
br i1 %17, label %21, label %18
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %10
br label %90
21: ; preds = %1
br label %22
22: ; preds = %76, %48, %38, %21
%23 = load i32*, i32** %6
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %4
%25 = icmp ne %struct.dirent* %24, null
br i1 %25, label %26, label %79
26: ; preds = %22
%27 = load %struct.dirent*, %struct.dirent** %4
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @strcmp(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %26
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @strcmp(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %32, %26
br label %22
39: ; preds = %32
%40 = load i32, i32* @PATH_MAX
%41 = load %struct.dirent*, %struct.dirent** %4
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = call i32 @snprintf(i8* %14, i32 %40, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %43)
%45 = call i32* @opendir(i8* %14)
store i32* %45, i32** %7
%46 = load i32*, i32** %7
%47 = icmp ne i32* %46, null
br i1 %47, label %49, label %48
48: ; preds = %39
br label %22
49: ; preds = %39
br label %50
50: ; preds = %75, %66, %49
%51 = load i32*, i32** %7
%52 = call %struct.dirent* @readdir(i32* %51)
store %struct.dirent* %52, %struct.dirent** %5
%53 = icmp ne %struct.dirent* %52, null
br i1 %53, label %54, label %76
54: ; preds = %50
%55 = load %struct.dirent*, %struct.dirent** %5
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 @strcmp(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %54
%61 = load %struct.dirent*, %struct.dirent** %5
%62 = getelementptr inbounds %struct.dirent, %struct.dirent* %61, i32 0, i32 0
%63 = load i8*, i8** %62
%64 = call i32 @strcmp(i8* %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%65 = icmp ne i32 %64, 0
br i1 %65, label %67, label %66
66: ; preds = %60, %54
br label %50
67: ; preds = %60
%68 = load %struct.dirent*, %struct.dirent** %5
%69 = getelementptr inbounds %struct.dirent, %struct.dirent* %68, i32 0, i32 0
%70 = load i8*, i8** %69
%71 = load i8*, i8** %3
%72 = call i32 @strcmp(i8* %70, i8* %71)
%73 = icmp ne i32 %72, 0
br i1 %73, label %75, label %74
74: ; preds = %67
br label %85
75: ; preds = %67
br label %50
76: ; preds = %50
%77 = load i32*, i32** %7
%78 = call i32 @closedir(i32* %77)
br label %22
79: ; preds = %22
%80 = load i32*, i32** %6
%81 = call i32 @closedir(i32* %80)
%82 = load i32, i32* @stderr
%83 = load i8*, i8** %3
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %83)
store i32 -1, i32* %2
store i32 1, i32* %10
br label %90
85: ; preds = %74
%86 = load i32*, i32** %7
%87 = call i32 @closedir(i32* %86)
%88 = load i32*, i32** %6
%89 = call i32 @closedir(i32* %88)
store i32 0, i32* %2
store i32 1, i32* %10
br label %90
90: ; preds = %85, %79, %18
%91 = load i8*, i8** %8
%92 = load i32, i32* %2
ret i32 %92
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [21 x i8] c"/sys/class/enclosure\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"System does not have SCSI enclosure(s).\0A\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [26 x i8] c"%s/%s/device/scsi_generic\00"
@.str.5 = [36 x i8] c"%s is not a valid enclosure device\0A\00"
define i32 @valid_enclosure_device(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %50, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %57
13: ; preds = %7, %22
%14 = phi %struct.dirent* [ %23, %22 ], [ %8, %7 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @strcmp(i8* dereferenceable(1) %16, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %13
%20 = call i32 @strcmp(i8* dereferenceable(1) %16, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %13, %19, %48, %25
%23 = call %struct.dirent* @readdir(i32* %5)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %50, label %13
25: ; preds = %19
%26 = load i32, i32* @PATH_MAX
%27 = call i32 @snprintf(i8* %4, i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %16)
%28 = call i32* @opendir(i8* %4)
%29 = icmp eq i32* %28, null
br i1 %29, label %22, label %30
30: ; preds = %25
%31 = call %struct.dirent* @readdir(i32* %28)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %48, label %33
33: ; preds = %30, %42
%34 = phi %struct.dirent* [ %43, %42 ], [ %31, %30 ]
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i64 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @strcmp(i8* dereferenceable(1) %36, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp eq i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %33
%40 = call i32 @strcmp(i8* dereferenceable(1) %36, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %33, %39, %45
%43 = call %struct.dirent* @readdir(i32* %28)
%44 = icmp eq %struct.dirent* %43, null
br i1 %44, label %48, label %33
45: ; preds = %39
%46 = call i32 @strcmp(i8* dereferenceable(1) %36, i8* dereferenceable(1) %0)
%47 = icmp eq i32 %46, 0
br i1 %47, label %54, label %42
48: ; preds = %42, %30
%49 = call i32 @closedir(i32* %28)
br label %22
50: ; preds = %22, %7
%51 = call i32 @closedir(i32* %5)
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %57
54: ; preds = %45
%55 = call i32 @closedir(i32* %28)
%56 = call i32 @closedir(i32* %5)
br label %57
57: ; preds = %54, %50, %10
%58 = phi i32 [ 0, %54 ], [ -1, %50 ], [ -1, %10 ]
ret i32 %58
}