#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int getc(int /*<<< orphan*/ *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ write_to_log(char *, int);
bool battery_is_charging(const char *battery_path_status,
                         const char *charging_string) {
  FILE *fp;
  int c = 0;
  int i, j = (int)strlen(charging_string);
  if ((fp = fopen(battery_path_status, "r")) == NULL) {
    write_to_log(
        "Error in battery_is_charging: Battery status file could not be opened",
        true);
    exit(EXIT_FAILURE);
  }
  for (i = 0; i <= j; i++) {
    if ((c = getc(fp)) == (int)'\n')
      break;
    if (c != (int)charging_string[i])
      break;
  }
  fclose(fp);
  return (c == (int)'\n' && charging_string[i] == '\0');
}


x86 assembly (clang, -O0):
<battery_is_charging>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11c0 <battery_is_charging+0x60>
lea    0xe5a(%rip),%rdi
mov    $0x1,%esi
call   1040 <write_to_log@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     121c <battery_is_charging+0xbc>
mov    -0x18(%rbp),%rdi
call   1080 <getc@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xa,%eax
jne    11ed <battery_is_charging+0x8d>
jmp    121c <battery_is_charging+0xbc>
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
je     1209 <battery_is_charging+0xa9>
jmp    121c <battery_is_charging+0xbc>
jmp    120e <battery_is_charging+0xae>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11c7 <battery_is_charging+0x67>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmpl   $0xa,-0x1c(%rbp)
mov    %al,-0x25(%rbp)
jne    1249 <battery_is_charging+0xe9>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
sete   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<battery_is_charging>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rax,%r15
lea    0xe80(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ec <battery_is_charging+0x8c>
mov    %rax,%r12
test   $0x80000000,%r15d
jne    11c2 <battery_is_charging+0x62>
movslq %r15d,%r15
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %r12,%rdi
call   1080 <getc@plt>
cmp    $0xa,%eax
je     11ce <battery_is_charging+0x6e>
movsbl (%r14,%rbx,1),%ecx
cmp    %ecx,%eax
jne    11c2 <battery_is_charging+0x62>
lea    0x1(%rbx),%rax
cmp    %r15,%rbx
mov    %rax,%rbx
jl     11a0 <battery_is_charging+0x40>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11e0 <battery_is_charging+0x80>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmpb   $0x0,(%r14,%rbx,1)
sete   %al
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe0f(%rip),%rdi
mov    $0x1,%esi
call   1040 <write_to_log@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [70 x i8] c"Error in battery_is_charging: Battery status file could not be opened\00"
@EXIT_FAILURE = global i32 0
define i32 @battery_is_charging(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %6
%9 = load i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
%11 = trunc i64 %10 to i32
store i32 %11, i32* %8
%12 = load i8*, i8** %3
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %5
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = call i32 @write_to_log(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i64 0, i64 0), i32 1)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
store i32 0, i32* %7
br label %20
20: ; preds = %40, %19
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = icmp sle i32 %21, %22
br i1 %23, label %24, label %43
24: ; preds = %20
%25 = load i32*, i32** %5
%26 = call i32 @getc(i32* %25)
store i32 %26, i32* %6
%27 = icmp eq i32 %26, 10
br i1 %27, label %28, label %29
28: ; preds = %24
br label %43
29: ; preds = %24
%30 = load i32, i32* %6
%31 = load i8*, i8** %4
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %30, %36
br i1 %37, label %38, label %39
38: ; preds = %29
br label %43
39: ; preds = %29
br label %40
40: ; preds = %39
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %20
43: ; preds = %38, %28, %20
%44 = load i32*, i32** %5
%45 = call i32 @fclose(i32* %44)
%46 = load i32, i32* %6
%47 = icmp eq i32 %46, 10
br i1 %47, label %48, label %56
48: ; preds = %43
%49 = load i8*, i8** %4
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 0
br label %56
56: ; preds = %48, %43
%57 = phi i1 [ false, %43 ], [ %55, %48 ]
%58 = zext i1 %57 to i32
ret i32 %58
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [70 x i8] c"Error in battery_is_charging: Battery status file could not be opened\00"
@EXIT_FAILURE = global i32 0
define i32 @battery_is_charging(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = and i64 %3, 2147483648
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %28
9: ; preds = %6
%10 = shl i64 %3, 32
%11 = ashr exact i64 %10, 32
br label %16
12: ; preds = %2
%13 = tail call i32 @write_to_log(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i64 0, i64 0), i32 1)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %9, %20
%17 = phi i64 [ 0, %9 ], [ %25, %20 ]
%18 = tail call i32 @getc(i32* %4)
%19 = icmp eq i32 %18, 10
br i1 %19, label %30, label %20
20: ; preds = %16
%21 = getelementptr inbounds i8, i8* %1, i64 %17
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %18, %23
%25 = add nuw nsw i64 %17, 1
%26 = icmp sge i64 %17, %11
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %28, label %16
28: ; preds = %20, %6
%29 = tail call i32 @fclose(i32* %4)
br label %37
30: ; preds = %16
%31 = tail call i32 @fclose(i32* %4)
%32 = and i64 %17, 4294967295
%33 = getelementptr inbounds i8, i8* %1, i64 %32
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 0
%36 = zext i1 %35 to i32
br label %37
37: ; preds = %28, %30
%38 = phi i32 [ %36, %30 ], [ 0, %28 ]
ret i32 %38
}