#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 /*<<< 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[128];
  edir = opendir("/sys/class/enclosure");
  if (!edir) {
    fprintf(stderr, "System does not have SCSI enclsoure(s).\n");
    return -1;
  }
  while ((edirent = readdir(edir)) != NULL) {
    if (!strcmp(edirent->d_name, ".") || !strcmp(edirent->d_name, ".."))
      continue;
    snprintf(path, 128, "%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    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe8a(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ad <valid_enclosure_device+0x4d>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131f <valid_enclosure_device+0x1bf>
jmp    11b2 <valid_enclosure_device+0x52>
mov    -0x28(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12d6 <valid_enclosure_device+0x176>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe68(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     1201 <valid_enclosure_device+0xa1>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe4b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1206 <valid_enclosure_device+0xa6>
jmp    11b2 <valid_enclosure_device+0x52>
lea    -0xb0(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x80,%esi
lea    0xe21(%rip),%rdx
lea    0xdd9(%rip),%rcx
call   1040 <snprintf@plt>
lea    -0xb0(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    124c <valid_enclosure_device+0xec>
jmp    11b2 <valid_enclosure_device+0x52>
jmp    1251 <valid_enclosure_device+0xf1>
mov    -0x30(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12c8 <valid_enclosure_device+0x168>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc9(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     12a0 <valid_enclosure_device+0x140>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdac(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    12a5 <valid_enclosure_device+0x145>
jmp    1251 <valid_enclosure_device+0xf1>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    12c3 <valid_enclosure_device+0x163>
jmp    1306 <valid_enclosure_device+0x1a6>
jmp    1251 <valid_enclosure_device+0xf1>
mov    -0x30(%rbp),%rdi
call   1050 <closedir@plt>
jmp    11b2 <valid_enclosure_device+0x52>
mov    -0x28(%rbp),%rdi
call   1050 <closedir@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd68(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131f <valid_enclosure_device+0x1bf>
mov    -0x30(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x28(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valid_enclosure_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%r14
lea    0xe85(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12a9 <valid_enclosure_device+0x149>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1285 <valid_enclosure_device+0x125>
lea    0xe9b(%rip),%r12
lea    0xe93(%rip),%rbx
jmp    11c9 <valid_enclosure_device+0x69>
nopl   (%rax)
mov    %rbp,%rdi
call   1050 <closedir@plt>
mov    %r15,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1285 <valid_enclosure_device+0x125>
mov    (%rax),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11b8 <valid_enclosure_device+0x58>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11b8 <valid_enclosure_device+0x58>
mov    %r15,%r13
mov    %r14,%r15
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x80,%esi
lea    0xe3f(%rip),%rdx
lea    0xdf7(%rip),%rcx
mov    %rbp,%r8
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    %r15,%r14
mov    %r13,%r15
call   1030 <opendir@plt>
test   %rax,%rax
je     11b8 <valid_enclosure_device+0x58>
mov    %rax,%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11b0 <valid_enclosure_device+0x50>
mov    (%rax),%r13
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1230 <valid_enclosure_device+0xd0>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1230 <valid_enclosure_device+0xd0>
mov    %r13,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    1230 <valid_enclosure_device+0xd0>
mov    %rbp,%rdi
call   1050 <closedir@plt>
mov    %r15,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
jmp    12c5 <valid_enclosure_device+0x165>
mov    %r15,%rdi
call   1050 <closedir@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12c0 <valid_enclosure_device+0x160>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd5c(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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