#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 /*<<< orphan*/ 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;
  fp = fopen("/sys/class/power_supply/ADP1/online", "r");
  if (fp == NULL)
    return -1;
  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    1227 <sysfs_check_ac_state+0xc7>
mov    -0x10(%rbp),%rcx
lea    -0x11(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11e5 <sysfs_check_ac_state+0x85>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe52(%rip),%rdi
call   1060 <logdebug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <sysfs_check_ac_state+0xc7>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movsbl -0x11(%rbp),%eax
cmp    $0x31,%eax
jne    1207 <sysfs_check_ac_state+0xa7>
movl   $0x1,-0x4(%rbp)
jmp    1227 <sysfs_check_ac_state+0xc7>
movsbl -0x11(%rbp),%eax
cmp    $0x30,%eax
jne    1220 <sysfs_check_ac_state+0xc0>
movl   $0x0,-0x4(%rbp)
jmp    1227 <sysfs_check_ac_state+0xc7>
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 = 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* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 -1, i32* %1
br label %31
8: ; preds = %0
%9 = load i32*, i32** %2
%10 = call i32 @fread(i8* %3, i32 1, i32 1, i32* %9)
%11 = load i32*, i32** %2
%12 = call i64 @ferror(i32* %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @logdebug(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %16)
store i32 -1, i32* %1
br label %31
18: ; preds = %8
%19 = load i32*, i32** %2
%20 = call i32 @fclose(i32* %19)
%21 = load i8, i8* %3
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 49
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 1, i32* %1
br label %31
25: ; preds = %18
%26 = load i8, i8* %3
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 48
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 0, i32* %1
br label %31
30: ; preds = %25
store i32 -2, i32* %1
br label %31
31: ; preds = %30, %29, %24, %14, %7
%32 = load i32, i32* %1
ret i32 %32
}

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
}