#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*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
char tolower(char);
char getState() {
  FILE *fp;
  char state;
  if (!(fp = fopen("/sys/class/power_supply/BAT0/status", "r"))) {
    perror("fopen");
    return 0;
  }
  state = fgetc(fp);
  fclose(fp);
  return tolower(state);
}


x86 assembly (clang, -O0):
<getState>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
lea    0xebe(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    118e <getState+0x3e>
lea    0xea6(%rip),%rdi
call   1070 <perror@plt>
movb   $0x0,-0x1(%rbp)
jmp    11af <getState+0x5f>
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movsbl -0x11(%rbp),%edi
call   1050 <tolower@plt>
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getState>:
push   %rbp
push   %rbx
push   %rax
lea    0xea6(%rip),%rdi
lea    0xec3(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     118f <getState+0x3f>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
movsbl %bpl,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <tolower@plt>
lea    0xe90(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"/sys/class/power_supply/BAT0/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
define signext i8 @getState() {
%1 = alloca i8
%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 = icmp ne i32* %4, null
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i8 0, i8* %1
br label %15
8: ; preds = %0
%9 = load i32*, i32** %2
%10 = call signext i8 @fgetc(i32* %9)
store i8 %10, i8* %3
%11 = load i32*, i32** %2
%12 = call i32 @fclose(i32* %11)
%13 = load i8, i8* %3
%14 = call signext i8 @tolower(i8 signext %13)
store i8 %14, i8* %1
br label %15
15: ; preds = %8, %6
%16 = load i8, i8* %1
ret i8 %16
}

LLVM IR (-O2):
@.str = [36 x i8] c"/sys/class/power_supply/BAT0/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
define signext i8 @getState() {
%1 = 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))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %9
5: ; preds = %0
%6 = tail call signext i8 @fgetc(i32* %1)
%7 = tail call i32 @fclose(i32* %1)
%8 = tail call signext i8 @tolower(i8 signext %6)
br label %9
9: ; preds = %5, %3
%10 = phi i8 [ %8, %5 ], [ 0, %3 ]
ret i8 %10
}