#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets_nonl(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int enclosure_maint_mode(const char *sg) {
  char devsg[PATH_MAX];
  char sgstate[128];
  FILE *fp;
  snprintf(devsg, PATH_MAX, "/sys/class/scsi_generic/%s/device/state", sg);
  fp = fopen(devsg, "r");
  if (!fp) {
    perror(devsg);
    fprintf(stderr,
            "Unable to open enclosure "
            "state file : %s\n",
            devsg);
    return -1;
  }
  if (fgets_nonl(sgstate, 128, fp) == NULL) {
    fprintf(stderr,
            "Unable to read the state of "
            "enclosure device : %s\n",
            sg);
    fclose(fp);
    return -1;
  }
  if (!strcmp(sgstate, "offline")) {
    fprintf(stderr,
            "Enclosure \"%s\" is offline.\n Cannot run"
            " diagnostics/LED operations.\n",
            sg);
    fclose(fp);
    return 1;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<enclosure_maint_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0xb8(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
lea    0xe3e(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0xb8(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
jne    122d <enclosure_maint_mode+0xbd>
mov    -0xb8(%rbp),%rdi
call   1090 <perror@plt>
mov    -0xb8(%rbp),%rdx
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0xac(%rbp)
jmp    12fe <enclosure_maint_mode+0x18e>
lea    -0xa0(%rbp),%rdi
mov    -0xa8(%rbp),%rdx
mov    $0x80,%esi
call   1030 <fgets_nonl@plt>
cmp    $0x0,%rax
jne    128a <enclosure_maint_mode+0x11a>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdf1(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0xa8(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0xac(%rbp)
jmp    12fe <enclosure_maint_mode+0x18e>
lea    -0xa0(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    12e1 <enclosure_maint_mode+0x171>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdd5(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0xa8(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0xac(%rbp)
jmp    12fe <enclosure_maint_mode+0x18e>
mov    -0xa8(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xac(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enclosure_maint_mode>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%r14
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe69(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rcx
call   1050 <snprintf@plt>
lea    0xe7f(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     124c <enclosure_maint_mode+0xec>
mov    %rax,%r15
lea    -0xa0(%rbp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1030 <fgets_nonl@plt>
test   %rax,%rax
je     1210 <enclosure_maint_mode+0xb0>
movabs $0x656e696c66666f,%rax
cmp    %rax,-0xa0(%rbp)
jne    1237 <enclosure_maint_mode+0xd7>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe8e(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
jmp    1241 <enclosure_maint_mode+0xe1>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%eax
jmp    1241 <enclosure_maint_mode+0xe1>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1080 <perror@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1241 <enclosure_maint_mode+0xe1>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [40 x i8] c"/sys/class/scsi_generic/%s/device/state\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [42 x i8] c"Unable to open enclosure state file : %s\0A\00"
@.str.3 = [51 x i8] c"Unable to read the state of enclosure device : %s\0A\00"
@.str.4 = [8 x i8] c"offline\00"
@.str.5 = [68 x i8] c"Enclosure \22%s\22 is offline.\0A Cannot run diagnostics/LED operations.\0A\00"
define i32 @enclosure_maint_mode(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [128 x i8]
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
%13 = load i32, i32* @PATH_MAX
%14 = load i8*, i8** %3
%15 = call i32 @snprintf(i8* %12, i32 %13, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %7
%17 = load i32*, i32** %7
%18 = icmp ne i32* %17, null
br i1 %18, label %23, label %19
19: ; preds = %1
%20 = call i32 @perror(i8* %12)
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %12)
store i32 -1, i32* %2
store i32 1, i32* %8
br label %47
23: ; preds = %1
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%25 = load i32*, i32** %7
%26 = call i32* @fgets_nonl(i8* %24, i32 128, i32* %25)
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %3
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = load i32*, i32** %7
%33 = call i32 @fclose(i32* %32)
store i32 -1, i32* %2
store i32 1, i32* %8
br label %47
34: ; preds = %23
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%36 = call i32 @strcmp(i8* %35, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%37 = icmp ne i32 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %3
%41 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.5, i64 0, i64 0), i8* %40)
%42 = load i32*, i32** %7
%43 = call i32 @fclose(i32* %42)
store i32 1, i32* %2
store i32 1, i32* %8
br label %47
44: ; preds = %34
%45 = load i32*, i32** %7
%46 = call i32 @fclose(i32* %45)
store i32 0, i32* %2
store i32 1, i32* %8
br label %47
47: ; preds = %44, %38, %28, %19
%48 = load i8*, i8** %4
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [40 x i8] c"/sys/class/scsi_generic/%s/device/state\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [42 x i8] c"Unable to open enclosure state file : %s\0A\00"
@.str.3 = [51 x i8] c"Unable to read the state of enclosure device : %s\0A\00"
@.str.5 = [68 x i8] c"Enclosure \22%s\22 is offline.\0A Cannot run diagnostics/LED operations.\0A\00"
define i32 @enclosure_maint_mode(i8* %0) {
%2 = alloca [128 x i8]
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%7 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = call i32 @perror(i8* %5)
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %31
14: ; preds = %1
%15 = call i32* @fgets_nonl(i8* %6, i32 128, i32* %8)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%20 = call i32 @fclose(i32* %8)
br label %31
21: ; preds = %14
%22 = bitcast [128 x i8]* %2 to i64*
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, 28550371717047919
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%28 = call i32 @fclose(i32* %8)
br label %31
29: ; preds = %21
%30 = call i32 @fclose(i32* %8)
br label %31
31: ; preds = %29, %25, %17, %10
%32 = phi i32 [ -1, %17 ], [ 0, %29 ], [ 1, %25 ], [ -1, %10 ]
ret i32 %32
}