#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 /*<<< orphan*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
int fscanf(int /*<<< orphan*/ *, char *, char **);
int /*<<< orphan*/ perror(char *);
char *read_sysfs_state_file(char const *path) {
  FILE *f = fopen(path, "r");
  char *token = NULL;
  int ret;
  if (f == NULL) {
    perror("open sysfs state file");
    exit(1);
  }
  while ((ret = fscanf(f, "%ms", &token)) == 1) {
    if (token[0] == '[')
      break;
    free(token);
    token = NULL;
  }
  if (ferror(f) && !feof(f)) {
    perror("read sysfs state file");
    exit(1);
  }
  if (fclose(f)) {
    perror("close sysfs state file");
    exit(1);
  }
  return token;
}


x86 assembly (clang, -O0):
<read_sysfs_state_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c9 <read_sysfs_state_file+0x49>
lea    0xe48(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11ce <read_sysfs_state_file+0x4e>
mov    -0x10(%rbp),%rdi
lea    0xe3f(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1060 <fscanf@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x1,%eax
jne    1219 <read_sysfs_state_file+0x99>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5b,%eax
jne    1203 <read_sysfs_state_file+0x83>
jmp    1219 <read_sysfs_state_file+0x99>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
jmp    11ce <read_sysfs_state_file+0x4e>
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1254 <read_sysfs_state_file+0xd4>
mov    -0x10(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
jne    1254 <read_sysfs_state_file+0xd4>
lea    0xdd7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     127d <read_sysfs_state_file+0xfd>
lea    0xdc4(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sysfs_state_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe70(%rip),%rsi
call   1080 <fopen@plt>
movq   $0x0,0x8(%rsp)
test   %rax,%rax
je     1230 <read_sysfs_state_file+0xb0>
mov    %rax,%rbx
lea    0xe67(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    11fb <read_sysfs_state_file+0x7b>
lea    0xe4e(%rip),%r14
lea    0x8(%rsp),%r15
nop
mov    0x8(%rsp),%rdi
cmpb   $0x5b,(%rdi)
je     11fb <read_sysfs_state_file+0x7b>
call   1030 <free@plt>
movq   $0x0,0x8(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
je     11d0 <read_sysfs_state_file+0x50>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     1214 <read_sysfs_state_file+0x94>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     1242 <read_sysfs_state_file+0xc2>
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
jne    1239 <read_sysfs_state_file+0xb9>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdcb(%rip),%rdi
jmp    1249 <read_sysfs_state_file+0xc9>
lea    0xdf2(%rip),%rdi
jmp    1249 <read_sysfs_state_file+0xc9>
lea    0xdd3(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"open sysfs state file\00"
@.str.2 = [4 x i8] c"%ms\00"
@.str.3 = [22 x i8] c"read sysfs state file\00"
@.str.4 = [23 x i8] c"close sysfs state file\00"
define i8* @read_sysfs_state_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
store i8* null, i8** %4
%8 = load i32*, i32** %3
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
br label %14
14: ; preds = %25, %13
%15 = load i32*, i32** %3
%16 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %4)
store i32 %16, i32* %5
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 91
br i1 %23, label %24, label %25
24: ; preds = %18
br label %28
25: ; preds = %18
%26 = load i8*, i8** %4
%27 = call i32 @free(i8* %26)
store i8* null, i8** %4
br label %14
28: ; preds = %24, %14
%29 = load i32*, i32** %3
%30 = call i64 @ferror(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %28
%33 = load i32*, i32** %3
%34 = call i32 @feof(i32* %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %32, %28
%40 = load i32*, i32** %3
%41 = call i64 @fclose(i32* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %39
%47 = load i8*, i8** %4
ret i8* %47
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"open sysfs state file\00"
@.str.2 = [4 x i8] c"%ms\00"
@.str.3 = [22 x i8] c"read sysfs state file\00"
@.str.4 = [23 x i8] c"close sysfs state file\00"
define i8* @read_sysfs_state_file(i8* nocapture %0) {
%2 = alloca i8*
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = icmp eq i32* %3, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %2)
%8 = icmp eq i32 %7, 1
br i1 %8, label %12, label %20
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6, %16
%13 = load i8*, i8** %2
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 91
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = call i32 @free(i8* %13)
store i8* null, i8** %2
%18 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %2)
%19 = icmp eq i32 %18, 1
br i1 %19, label %12, label %20
20: ; preds = %16, %12, %6
%21 = call i64 @ferror(i32* %3)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = call i32 @feof(i32* %3)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %23, %20
%30 = call i64 @fclose(i32* %3)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
%36 = load i8*, i8** %2
ret i8* %36
}