#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__ feof(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*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ *strstr(char *, char *);
int procfs_check_ac_state(void) {
  FILE *fp;
  char buf[128];
  int ret;
  fp = fopen("/proc/acpi/ac_adapter/ADP1/state", "r");
  if (fp == NULL)
    return -1;
  ret = fread(buf, 1, 127, fp);
  if (ferror(fp) != 0) {
    logdebug("acpi: Error reading proc AC state: %s\n", strerror(errno));
    return -1;
  }
  if (feof(fp) == 0) {
    logdebug("acpi: Error reading proc AC state: buffer too small\n");
    return -1;
  }
  fclose(fp);
  buf[ret] = '\0';
  if (strstr(buf, "on-line\n") != NULL)
    return 1;
  if (strstr(buf, "off-line\n") != NULL)
    return 0;
  return -2;
}


x86 assembly (clang, -O0):
<procfs_check_ac_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
lea    0xe6e(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b9 <procfs_check_ac_state+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <procfs_check_ac_state+0x134>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7f,%edx
call   1040 <fread@plt>
mov    %eax,-0x94(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1216 <procfs_check_ac_state+0x96>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1070 <logdebug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <procfs_check_ac_state+0x134>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
jne    1243 <procfs_check_ac_state+0xc3>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1070 <logdebug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <procfs_check_ac_state+0x134>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movslq -0x94(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     1284 <procfs_check_ac_state+0x104>
movl   $0x1,-0x4(%rbp)
jmp    12b4 <procfs_check_ac_state+0x134>
lea    -0x90(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     12ad <procfs_check_ac_state+0x12d>
movl   $0x0,-0x4(%rbp)
jmp    12b4 <procfs_check_ac_state+0x134>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<procfs_check_ac_state>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
lea    0xe6e(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11ee <procfs_check_ac_state+0x6e>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x7f,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11fc <procfs_check_ac_state+0x7c>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe3e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <logdebug@plt>
mov    %ebp,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     1250 <procfs_check_ac_state+0xd0>
mov    %rbx,%rdi
call   1050 <fclose@plt>
movslq %r14d,%rax
movb   $0x0,(%rsp,%rax,1)
lea    0xe60(%rip),%rsi
mov    %rsp,%rdi
call   10a0 <strstr@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    11ee <procfs_check_ac_state+0x6e>
lea    0xe50(%rip),%rsi
mov    %rsp,%rdi
call   10a0 <strstr@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
lea    (%rcx,%rcx,1),%ebp
add    $0xfffffffe,%ebp
jmp    11ee <procfs_check_ac_state+0x6e>
lea    0xdf3(%rip),%rdi
xor    %eax,%eax
call   1070 <logdebug@plt>
jmp    11ee <procfs_check_ac_state+0x6e>

LLVM IR (-O0):
@.str = [33 x i8] c"/proc/acpi/ac_adapter/ADP1/state\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [39 x i8] c"acpi: Error reading proc AC state: %s\0A\00"
@errno = global i32 0
@.str.3 = [53 x i8] c"acpi: Error reading proc AC state: buffer too small\0A\00"
@.str.4 = [9 x i8] c"on-line\0A\00"
@.str.5 = [10 x i8] c"off-line\0A\00"
define i32 @procfs_check_ac_state() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = call i32* @fopen(i8* getelementptr inbounds ([33 x i8], [33 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 %42
9: ; preds = %0
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%11 = load i32*, i32** %2
%12 = call i32 @fread(i8* %10, i32 1, i32 127, i32* %11)
store i32 %12, i32* %4
%13 = load i32*, i32** %2
%14 = call i64 @ferror(i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %9
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 (i8*, ...) @logdebug(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %18)
store i32 -1, i32* %1
br label %42
20: ; preds = %9
%21 = load i32*, i32** %2
%22 = call i64 @feof(i32* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i8*, ...) @logdebug(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %42
26: ; preds = %20
%27 = load i32*, i32** %2
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %30
store i8 0, i8* %31
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%33 = call i32* @strstr(i8* %32, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %36
35: ; preds = %26
store i32 1, i32* %1
br label %42
36: ; preds = %26
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%38 = call i32* @strstr(i8* %37, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%39 = icmp ne i32* %38, null
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 0, i32* %1
br label %42
41: ; preds = %36
store i32 -2, i32* %1
br label %42
42: ; preds = %41, %40, %35, %24, %16, %8
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@.str = [33 x i8] c"/proc/acpi/ac_adapter/ADP1/state\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [39 x i8] c"acpi: Error reading proc AC state: %s\0A\00"
@errno = global i32 0
@.str.3 = [53 x i8] c"acpi: Error reading proc AC state: buffer too small\0A\00"
@.str.4 = [9 x i8] c"on-line\0A\00"
@.str.5 = [10 x i8] c"off-line\0A\00"
define i32 @procfs_check_ac_state() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %28, label %5
5: ; preds = %0
%6 = call i32 @fread(i8* %2, i32 1, i32 127, i32* %3)
%7 = tail call i64 @ferror(i32* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %5
%10 = load i32, i32* @errno
%11 = tail call i32 @strerror(i32 %10)
%12 = tail call i32 (i8*, ...) @logdebug(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %28
13: ; preds = %5
%14 = tail call i64 @feof(i32* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 (i8*, ...) @logdebug(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
br label %28
18: ; preds = %13
%19 = tail call i32 @fclose(i32* %3)
%20 = sext i32 %6 to i64
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32* @strstr(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = call i32* @strstr(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%26 = icmp eq i32* %25, null
%27 = select i1 %26, i32 -2, i32 0
br label %28
28: ; preds = %24, %18, %0, %16, %9
%29 = phi i32 [ -1, %9 ], [ -1, %16 ], [ -1, %0 ], [ 1, %18 ], [ %27, %24 ]
ret i32 %29
}