#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ logdebug(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int sysfs_check_ac_state(void) {
  FILE *fp;
  char ac_state;
  int ret;
  fp = fopen("/sys/class/power_supply/ADP1/online", "r");
  if (fp == NULL)
    return -1;
  ret = fread(&ac_state, 1, 1, fp);
  if (ferror(fp) != 0) {
    logdebug("power: Error reading sysfs AC state: %s\n", strerror(errno));
    return -1;
  }
  fclose(fp);
  if (ac_state == '1')
    return 1;
  if (ac_state == '0')
    return 0;
  return -2;
}


x86 assembly (clang, -O0):
<sysfs_check_ac_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <sysfs_check_ac_state+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <sysfs_check_ac_state+0xca>
mov    -0x10(%rbp),%rcx
lea    -0x11(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11e8 <sysfs_check_ac_state+0x88>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe4f(%rip),%rdi
call   1060 <logdebug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <sysfs_check_ac_state+0xca>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movsbl -0x11(%rbp),%eax
cmp    $0x31,%eax
jne    120a <sysfs_check_ac_state+0xaa>
movl   $0x1,-0x4(%rbp)
jmp    122a <sysfs_check_ac_state+0xca>
movsbl -0x11(%rbp),%eax
cmp    $0x30,%eax
jne    1223 <sysfs_check_ac_state+0xc3>
movl   $0x0,-0x4(%rbp)
jmp    122a <sysfs_check_ac_state+0xca>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_check_ac_state>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1070 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11ef <sysfs_check_ac_state+0x8f>
mov    %rax,%rbx
lea    0xf(%rsp),%rdi
mov    $0x1,%r14d
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11d0 <sysfs_check_ac_state+0x70>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe5f(%rip),%rdi
mov    %eax,%esi
call   1060 <logdebug@plt>
jmp    11ef <sysfs_check_ac_state+0x8f>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0xf(%rsp),%al
xor    %ecx,%ecx
cmp    $0x30,%al
sete   %cl
lea    (%rcx,%rcx,1),%ebp
add    $0xfffffffe,%ebp
cmp    $0x31,%al
cmove  %r14d,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"/sys/class/power_supply/ADP1/online\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [41 x i8] c"power: Error reading sysfs AC state: %s\0A\00"
@errno = global i32 0
define i32 @sysfs_check_ac_state() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8
%4 = alloca i32
%5 = call i32* @fopen(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %9
8: ; preds = %0
store i32 -1, i32* %1
br label %32
9: ; preds = %0
%10 = load i32*, i32** %2
%11 = call i32 @fread(i8* %3, i32 1, i32 1, i32* %10)
store i32 %11, i32* %4
%12 = load i32*, i32** %2
%13 = call i64 @ferror(i32* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 @logdebug(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %17)
store i32 -1, i32* %1
br label %32
19: ; preds = %9
%20 = load i32*, i32** %2
%21 = call i32 @fclose(i32* %20)
%22 = load i8, i8* %3
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 49
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 1, i32* %1
br label %32
26: ; preds = %19
%27 = load i8, i8* %3
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 48
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 0, i32* %1
br label %32
31: ; preds = %26
store i32 -2, i32* %1
br label %32
32: ; preds = %31, %30, %25, %15, %8
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [36 x i8] c"/sys/class/power_supply/ADP1/online\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [41 x i8] c"power: Error reading sysfs AC state: %s\0A\00"
@errno = global i32 0
define i32 @sysfs_check_ac_state() {
%1 = alloca i8
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %19, label %4
4: ; preds = %0
%5 = call i32 @fread(i8* %1, i32 1, i32 1, i32* %2)
%6 = tail call i64 @ferror(i32* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 @logdebug(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %19
12: ; preds = %4
%13 = tail call i32 @fclose(i32* %2)
%14 = load i8, i8* %1
%15 = icmp eq i8 %14, 48
%16 = select i1 %15, i32 0, i32 -2
%17 = icmp eq i8 %14, 49
%18 = select i1 %17, i32 1, i32 %16
br label %19
19: ; preds = %12, %0, %8
%20 = phi i32 [ -1, %8 ], [ -1, %0 ], [ %18, %12 ]
ret i32 %20
}