#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 atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ isdigit(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
int /*<<< orphan*/ write_to_log(char *, int);
int get_battery_level(const char *battery_path_capacity) {
  FILE *fp;
  int i, c = 0;
  char battery_level_string[4];
  char log_string[500];
  int battery_level_int;
  static int failure;
  if ((fp = fopen(battery_path_capacity, "r")) == NULL) {
    write_to_log(
        "Error in get_battery_level: Battery capacity file could not be opened",
        true);
    exit(EXIT_FAILURE);
  }
  for (i = 0; i <= 4; i++) {
    if (!isdigit(c = getc(fp)))
      break;
    else
      battery_level_string[i] = (char)c;
  }
  fclose(fp);
  battery_level_string[i] = '\0';
  battery_level_int = atoi(battery_level_string);
  if (c != (int)'\n' || battery_level_int < 0) {
    snprintf(log_string, 500,
             "Battery capacity file could not be read properly. Last read char "
             "has ascii value: %d while it should be newline. Battery level "
             "read as: %d.",
             c, battery_level_int);
    for (i = 0; i < 500 && log_string[i]; i++)
      ;
    log_string[i] = '\0';
    write_to_log(log_string, true);
    failure++;
    if (failure == 3)
      exit(EXIT_FAILURE);
  } else
    failure = 0;
  return battery_level_int;
}


x86 assembly (clang, -O0):
<get_battery_level>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11d3 <get_battery_level+0x53>
lea    0xe47(%rip),%rdi
mov    $0x1,%esi
call   1050 <write_to_log@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jg     1225 <get_battery_level+0xa5>
mov    -0x10(%rbp),%rdi
call   10a0 <getc@plt>
mov    %eax,%edi
mov    %edi,-0x18(%rbp)
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1205 <get_battery_level+0x85>
jmp    1225 <get_battery_level+0xa5>
mov    -0x18(%rbp),%eax
mov    %al,%cl
movslq -0x14(%rbp),%rax
mov    %cl,-0x1c(%rbp,%rax,1)
jmp    1217 <get_battery_level+0x97>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11da <get_battery_level+0x5a>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movslq -0x14(%rbp),%rax
movb   $0x0,-0x1c(%rbp,%rax,1)
lea    -0x1c(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0xa,-0x18(%rbp)
jne    125d <get_battery_level+0xdd>
cmpl   $0x0,-0x214(%rbp)
jge    1325 <get_battery_level+0x1a5>
lea    -0x210(%rbp),%rdi
mov    -0x18(%rbp),%ecx
mov    -0x214(%rbp),%r8d
mov    $0x1f4,%esi
lea    0xdce(%rip),%rdx
call   1060 <snprintf@plt>
movl   $0x0,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x1f4,-0x14(%rbp)
mov    %al,-0x215(%rbp)
jge    12b3 <get_battery_level+0x133>
movslq -0x14(%rbp),%rax
movsbl -0x210(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
setne  %al
mov    %al,-0x215(%rbp)
mov    -0x215(%rbp),%al
test   $0x1,%al
jne    12c6 <get_battery_level+0x146>
jmp    12d9 <get_battery_level+0x159>
jmp    12cb <get_battery_level+0x14b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1286 <get_battery_level+0x106>
movslq -0x14(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
lea    -0x210(%rbp),%rdi
mov    $0x1,%esi
call   1050 <write_to_log@plt>
mov    0x2d68(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2d5f(%rip)
cmpl   $0x3,0x2d58(%rip)
jne    1320 <get_battery_level+0x1a0>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    132f <get_battery_level+0x1af>
movl   $0x0,0x2d35(%rip)
mov    -0x214(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_battery_level>:
push   %rbp
push   %r14
push   %rbx
sub    $0x210,%rsp
lea    0xe7e(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1304 <get_battery_level+0x194>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <getc@plt>
mov    %eax,%ebp
add    $0xffffffd0,%eax
cmp    $0xa,%eax
jae    122a <get_battery_level+0xba>
mov    %bpl,0xc(%rsp)
mov    %rbx,%rdi
call   1090 <getc@plt>
mov    %eax,%ebp
add    $0xffffffd0,%eax
mov    $0x1,%r14d
cmp    $0x9,%eax
ja     122d <get_battery_level+0xbd>
mov    %bpl,0xd(%rsp)
mov    %rbx,%rdi
call   1090 <getc@plt>
mov    %eax,%ebp
add    $0xffffffd0,%eax
mov    $0x2,%r14d
cmp    $0x9,%eax
ja     122d <get_battery_level+0xbd>
mov    %bpl,0xe(%rsp)
mov    %rbx,%rdi
call   1090 <getc@plt>
mov    %eax,%ebp
add    $0xffffffd0,%eax
mov    $0x3,%r14d
cmp    $0x9,%eax
ja     122d <get_battery_level+0xbd>
mov    %bpl,0xf(%rsp)
mov    %rbx,%rdi
call   1090 <getc@plt>
mov    %eax,%ebp
add    $0xffffffd0,%eax
mov    $0x4,%r14d
cmp    $0x9,%eax
ja     122d <get_battery_level+0xbd>
mov    %bpl,0x10(%rsp)
mov    $0x5,%r14d
jmp    122d <get_battery_level+0xbd>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1030 <fclose@plt>
movb   $0x0,0xc(%rsp,%r14,1)
lea    0xc(%rsp),%rdi
call   1070 <atoi@plt>
mov    %eax,%ebx
cmp    $0xa,%ebp
jne    125f <get_battery_level+0xef>
test   %ebx,%ebx
js     125f <get_battery_level+0xef>
movl   $0x0,0x2e02(%rip)
jmp    12f6 <get_battery_level+0x186>
lea    0xde2(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x1f4,%esi
mov    %ebp,%ecx
mov    %ebx,%r8d
call   1050 <snprintf@plt>
mov    $0x4,%eax
nop
cmpb   $0x0,0xc(%rsp,%rax,1)
je     12b6 <get_battery_level+0x146>
cmpb   $0x0,0xd(%rsp,%rax,1)
je     12bc <get_battery_level+0x14c>
cmpb   $0x0,0xe(%rsp,%rax,1)
je     12c2 <get_battery_level+0x152>
cmpb   $0x0,0xf(%rsp,%rax,1)
je     12c8 <get_battery_level+0x158>
cmpb   $0x0,0x10(%rsp,%rax,1)
je     12cc <get_battery_level+0x15c>
add    $0x5,%rax
cmp    $0x1f8,%rax
jne    1280 <get_battery_level+0x110>
mov    $0x1f4,%eax
jmp    12cc <get_battery_level+0x15c>
add    $0xfffffffffffffffc,%rax
jmp    12cc <get_battery_level+0x15c>
add    $0xfffffffffffffffd,%rax
jmp    12cc <get_battery_level+0x15c>
add    $0xfffffffffffffffe,%rax
jmp    12cc <get_battery_level+0x15c>
add    $0xffffffffffffffff,%rax
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
call   1040 <write_to_log@plt>
mov    0x2d74(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2d6b(%rip)
cmp    $0x3,%eax
je     1315 <get_battery_level+0x1a5>
mov    %ebx,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xcf7(%rip),%rdi
mov    $0x1,%esi
call   1040 <write_to_log@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@get_battery_level.failure = internal global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [70 x i8] c"Error in get_battery_level: Battery capacity file could not be opened\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [140 x i8] c"Battery capacity file could not be read properly. Last read char has ascii value: %d while it should be newline. Battery level read as: %d.\00"
define i32 @get_battery_level(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4 x i8]
%7 = alloca [500 x i8]
%8 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
%9 = load i8*, i8** %2
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %3
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = 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 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %1
store i32 0, i32* %4
br label %17
17: ; preds = %33, %16
%18 = load i32, i32* %4
%19 = icmp sle i32 %18, 4
br i1 %19, label %20, label %36
20: ; preds = %17
%21 = load i32*, i32** %3
%22 = call i32 @getc(i32* %21)
store i32 %22, i32* %5
%23 = call i32 @isdigit(i32 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %20
br label %36
26: ; preds = %20
%27 = load i32, i32* %5
%28 = trunc i32 %27 to i8
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 %30
store i8 %28, i8* %31
br label %32
32: ; preds = %26
br label %33
33: ; preds = %32
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
store i32 %35, i32* %4
br label %17
36: ; preds = %25, %17
%37 = load i32*, i32** %3
%38 = call i32 @fclose(i32* %37)
%39 = load i32, i32* %4
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 %40
store i8 0, i8* %41
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%43 = call i32 @atoi(i8* %42)
store i32 %43, i32* %8
%44 = load i32, i32* %5
%45 = icmp ne i32 %44, 10
br i1 %45, label %49, label %46
46: ; preds = %36
%47 = load i32, i32* %8
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %84
49: ; preds = %46, %36
%50 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
%51 = load i32, i32* %5
%52 = load i32, i32* %8
%53 = call i32 @snprintf(i8* %50, i32 500, i8* getelementptr inbounds ([140 x i8], [140 x i8]* @.str.2, i64 0, i64 0), i32 %51, i32 %52)
store i32 0, i32* %4
br label %54
54: ; preds = %67, %49
%55 = load i32, i32* %4
%56 = icmp slt i32 %55, 500
br i1 %56, label %57, label %64
57: ; preds = %54
%58 = load i32, i32* %4
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 0
br label %64
64: ; preds = %57, %54
%65 = phi i1 [ false, %54 ], [ %63, %57 ]
br i1 %65, label %66, label %70
66: ; preds = %64
br label %67
67: ; preds = %66
%68 = load i32, i32* %4
%69 = add nsw i32 %68, 1
store i32 %69, i32* %4
br label %54
70: ; preds = %64
%71 = load i32, i32* %4
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 %72
store i8 0, i8* %73
%74 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
%75 = call i32 @write_to_log(i8* %74, i32 1)
%76 = load i32, i32* @get_battery_level.failure
%77 = add nsw i32 %76, 1
store i32 %77, i32* @get_battery_level.failure
%78 = load i32, i32* @get_battery_level.failure
%79 = icmp eq i32 %78, 3
br i1 %79, label %80, label %83
80: ; preds = %70
%81 = load i32, i32* @EXIT_FAILURE
%82 = call i32 @exit(i32 %81)
unreachable
83: ; preds = %70
br label %85
84: ; preds = %46
store i32 0, i32* @get_battery_level.failure
br label %85
85: ; preds = %84, %83
%86 = load i32, i32* %8
ret i32 %86
}

LLVM IR (-O2):
@get_battery_level.failure = internal unnamed_addr global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [70 x i8] c"Error in get_battery_level: Battery capacity file could not be opened\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [140 x i8] c"Battery capacity file could not be read properly. Last read char has ascii value: %d while it should be newline. Battery level read as: %d.\00"
define i32 @get_battery_level(i8* nocapture %0) {
%2 = alloca [4 x i8]
%3 = alloca [500 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = tail call i32 @getc(i32* %6)
%10 = add i32 %9, -48
%11 = icmp ult i32 %10, 10
br i1 %11, label %16, label %42
12: ; preds = %1
%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 = %8
%17 = trunc i32 %9 to i8
store i8 %17, i8* %4
%18 = tail call i32 @getc(i32* %6)
%19 = add i32 %18, -48
%20 = icmp ult i32 %19, 10
br i1 %20, label %21, label %42
21: ; preds = %16
%22 = trunc i32 %18 to i8
%23 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 1
store i8 %22, i8* %23
%24 = tail call i32 @getc(i32* %6)
%25 = add i32 %24, -48
%26 = icmp ult i32 %25, 10
br i1 %26, label %27, label %42
27: ; preds = %21
%28 = trunc i32 %24 to i8
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 2
store i8 %28, i8* %29
%30 = tail call i32 @getc(i32* %6)
%31 = add i32 %30, -48
%32 = icmp ult i32 %31, 10
br i1 %32, label %33, label %42
33: ; preds = %27
%34 = trunc i32 %30 to i8
%35 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 3
store i8 %34, i8* %35
%36 = tail call i32 @getc(i32* %6)
%37 = add i32 %36, -48
%38 = icmp ult i32 %37, 10
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = trunc i32 %36 to i8
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 4
store i8 %40, i8* %41
br label %42
42: ; preds = %39, %33, %27, %21, %16, %8
%43 = phi i32 [ %9, %8 ], [ %18, %16 ], [ %24, %21 ], [ %30, %27 ], [ %36, %33 ], [ %36, %39 ]
%44 = phi i64 [ 0, %8 ], [ 1, %16 ], [ 2, %21 ], [ 3, %27 ], [ 4, %33 ], [ 5, %39 ]
%45 = tail call i32 @fclose(i32* %6)
%46 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 %44
store i8 0, i8* %46
%47 = call i32 @atoi(i8* %4)
%48 = icmp ne i32 %43, 10
%49 = icmp slt i32 %47, 0
%50 = select i1 %48, i1 true, i1 %49
br i1 %50, label %51, label %92
51: ; preds = %42
%52 = call i32 @snprintf(i8* %5, i32 500, i8* getelementptr inbounds ([140 x i8], [140 x i8]* @.str.2, i64 0, i64 0), i32 %43, i32 %47)
br label %53
53: ; preds = %78, %51
%54 = phi i64 [ 0, %51 ], [ %79, %78 ]
%55 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = icmp eq i8 %56, 0
br i1 %57, label %81, label %58
58: ; preds = %53
%59 = add nuw nsw i64 %54, 1
%60 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = icmp eq i8 %61, 0
br i1 %62, label %81, label %63
63: ; preds = %58
%64 = add nuw nsw i64 %54, 2
%65 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 %64
%66 = load i8, i8* %65
%67 = icmp eq i8 %66, 0
br i1 %67, label %81, label %68
68: ; preds = %63
%69 = add nuw nsw i64 %54, 3
%70 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 %69
%71 = load i8, i8* %70
%72 = icmp eq i8 %71, 0
br i1 %72, label %81, label %73
73: ; preds = %68
%74 = add nuw nsw i64 %54, 4
%75 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 %74
%76 = load i8, i8* %75
%77 = icmp eq i8 %76, 0
br i1 %77, label %81, label %78
78: ; preds = %73
%79 = add nuw nsw i64 %54, 5
%80 = icmp eq i64 %79, 500
br i1 %80, label %81, label %53
81: ; preds = %78, %73, %68, %63, %58, %53
%82 = phi i64 [ %54, %53 ], [ %59, %58 ], [ %64, %63 ], [ %69, %68 ], [ %74, %73 ], [ 500, %78 ]
%83 = and i64 %82, 4294967295
%84 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 %83
store i8 0, i8* %84
%85 = call i32 @write_to_log(i8* %5, i32 1)
%86 = load i32, i32* @get_battery_level.failure
%87 = add nsw i32 %86, 1
store i32 %87, i32* @get_battery_level.failure
%88 = icmp eq i32 %87, 3
br i1 %88, label %89, label %93
89: ; preds = %81
%90 = load i32, i32* @EXIT_FAILURE
%91 = call i32 @exit(i32 %90)
unreachable
92: ; preds = %42
store i32 0, i32* @get_battery_level.failure
br label %93
93: ; preds = %81, %92
ret i32 %47
}