#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 atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, char *);
int get_battery_percent(char *forbattery) {
  char *path = "/sys/class/power_supply";
  FILE *fp;
  char battery_read[512];
  char tmp[64];
  int energy_now = 1;
  int energy_full = 1;
  snprintf(battery_read, sizeof battery_read, "%s/%s/%s", path, forbattery,
           "energy_now");
  fp = fopen(battery_read, "r");
  if (fp != NULL) {
    if (fgets(tmp, sizeof tmp, fp))
      energy_now = atoi(tmp);
    fclose(fp);
  } else {
    snprintf(battery_read, sizeof battery_read, "%s/%s/%s", path, forbattery,
             "charge_now");
    fp = fopen(battery_read, "r");
    if (fp != NULL) {
      if (fgets(tmp, sizeof tmp, fp))
        energy_now = atoi(tmp);
      fclose(fp);
    } else {
      perror(battery_read);
      return -1;
    }
  }
  snprintf(battery_read, sizeof battery_read, "%s/%s/%s", path, forbattery,
           "energy_full");
  fp = fopen(battery_read, "r");
  if (fp != NULL) {
    if (fgets(tmp, sizeof tmp, fp))
      energy_full = atoi(tmp);
    fclose(fp);
  } else {
    snprintf(battery_read, sizeof battery_read, "%s/%s/%s", path, forbattery,
             "charge_full");
    fp = fopen(battery_read, "r");
    if (fp != NULL) {
      if (fgets(tmp, sizeof tmp, fp))
        energy_full = atoi(tmp);
      fclose(fp);
    } else {
      return -2;
    }
  }
  return energy_now / (energy_full / 100);
}


x86 assembly (clang, -O0):
<get_battery_percent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe8a(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x264(%rbp)
movl   $0x1,-0x268(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x200,%esi
lea    0xe6f(%rip),%rdx
lea    0xe71(%rip),%r9
call   1040 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1216 <get_battery_percent+0xb6>
lea    -0x260(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x40,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1208 <get_battery_percent+0xa8>
lea    -0x260(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x264(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    12bb <get_battery_percent+0x15b>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x200,%esi
lea    0xde7(%rip),%rdx
lea    0xdf6(%rip),%r9
call   1040 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xde1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     129e <get_battery_percent+0x13e>
lea    -0x260(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x40,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1290 <get_battery_percent+0x130>
lea    -0x260(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x264(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    12b6 <get_battery_percent+0x156>
lea    -0x220(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1404 <get_battery_percent+0x2a4>
jmp    12bb <get_battery_percent+0x15b>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x200,%esi
lea    0xd42(%rip),%rdx
lea    0xd5c(%rip),%r9
call   1040 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xd3c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1343 <get_battery_percent+0x1e3>
lea    -0x260(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x40,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1335 <get_battery_percent+0x1d5>
lea    -0x260(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x268(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    13dc <get_battery_percent+0x27c>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x200,%esi
lea    0xcba(%rip),%rdx
lea    0xce0(%rip),%r9
call   1040 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xcb4(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     13cb <get_battery_percent+0x26b>
lea    -0x260(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x40,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     13bd <get_battery_percent+0x25d>
lea    -0x260(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x268(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    13d7 <get_battery_percent+0x277>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1404 <get_battery_percent+0x2a4>
jmp    13dc <get_battery_percent+0x27c>
mov    -0x264(%rbp),%eax
mov    %eax,-0x26c(%rbp)
mov    -0x268(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x26c(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x270,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_battery_percent>:
push   %rbp
push   %r14
push   %rbx
sub    $0x240,%rsp
mov    %rdi,%rbx
lea    0xea3(%rip),%rdx
lea    0xe84(%rip),%rcx
lea    0xe9e(%rip),%r9
lea    0x40(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%r8
call   1040 <snprintf@plt>
lea    0xe8d(%rip),%rsi
mov    %rbp,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
jne    11ee <get_battery_percent+0x8e>
lea    0xe65(%rip),%rdx
lea    0xe46(%rip),%rcx
lea    0xe6d(%rip),%r9
lea    0x40(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%r8
call   1040 <snprintf@plt>
lea    0xe4f(%rip),%rsi
mov    %rbp,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     12f8 <get_battery_percent+0x198>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x40,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1213 <get_battery_percent+0xb3>
mov    %rsp,%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
jmp    1219 <get_battery_percent+0xb9>
mov    $0x1,%r14d
mov    %rbp,%rdi
call   1030 <fclose@plt>
lea    0xdf0(%rip),%rdx
lea    0xdd1(%rip),%rcx
lea    0xe03(%rip),%r9
lea    0x40(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%r8
call   1040 <snprintf@plt>
lea    0xdda(%rip),%rsi
mov    %rbp,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
jne    129d <get_battery_percent+0x13d>
lea    0xdb2(%rip),%rdx
lea    0xd93(%rip),%rcx
lea    0xdd1(%rip),%r9
lea    0x40(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%r8
call   1040 <snprintf@plt>
lea    0xd9c(%rip),%rsi
mov    %rbp,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     12f1 <get_battery_percent+0x191>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x40,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12d5 <get_battery_percent+0x175>
mov    %rsp,%rdi
call   1080 <atoi@plt>
cltq
imul   $0x51eb851f,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x25,%rbx
add    %eax,%ebx
jmp    12d7 <get_battery_percent+0x177>
xor    %ebx,%ebx
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    %r14d,%eax
cltd
idiv   %ebx
add    $0x240,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xfffffffe,%eax
jmp    12e5 <get_battery_percent+0x185>
lea    0x40(%rsp),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    12e5 <get_battery_percent+0x185>

LLVM IR (-O0):
@.str = [24 x i8] c"/sys/class/power_supply\00"
@.str.1 = [9 x i8] c"%s/%s/%s\00"
@.str.2 = [11 x i8] c"energy_now\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [11 x i8] c"charge_now\00"
@.str.5 = [12 x i8] c"energy_full\00"
@.str.6 = [12 x i8] c"charge_full\00"
define i32 @get_battery_percent(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [512 x i8]
%7 = alloca [64 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8** %4
store i32 1, i32* %8
store i32 1, i32* %9
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = load i8*, i8** %3
%13 = call i32 @snprintf(i8* %10, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %11, i8* %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %15, i32** %5
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %29
18: ; preds = %1
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%20 = load i32*, i32** %5
%21 = call i64 @fgets(i8* %19, i32 64, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %8
br label %26
26: ; preds = %23, %18
%27 = load i32*, i32** %5
%28 = call i32 @fclose(i32* %27)
br label %53
29: ; preds = %1
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%31 = load i8*, i8** %4
%32 = load i8*, i8** %3
%33 = call i32 @snprintf(i8* %30, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %35, i32** %5
%36 = load i32*, i32** %5
%37 = icmp ne i32* %36, null
br i1 %37, label %38, label %49
38: ; preds = %29
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%40 = load i32*, i32** %5
%41 = call i64 @fgets(i8* %39, i32 64, i32* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%45 = call i32 @atoi(i8* %44)
store i32 %45, i32* %8
br label %46
46: ; preds = %43, %38
%47 = load i32*, i32** %5
%48 = call i32 @fclose(i32* %47)
br label %52
49: ; preds = %29
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%51 = call i32 @perror(i8* %50)
store i32 -1, i32* %2
br label %100
52: ; preds = %46
br label %53
53: ; preds = %52, %26
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%55 = load i8*, i8** %4
%56 = load i8*, i8** %3
%57 = call i32 @snprintf(i8* %54, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %55, i8* %56, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%59 = call i32* @fopen(i8* %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %59, i32** %5
%60 = load i32*, i32** %5
%61 = icmp ne i32* %60, null
br i1 %61, label %62, label %73
62: ; preds = %53
%63 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%64 = load i32*, i32** %5
%65 = call i64 @fgets(i8* %63, i32 64, i32* %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %62
%68 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%69 = call i32 @atoi(i8* %68)
store i32 %69, i32* %9
br label %70
70: ; preds = %67, %62
%71 = load i32*, i32** %5
%72 = call i32 @fclose(i32* %71)
br label %95
73: ; preds = %53
%74 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%75 = load i8*, i8** %4
%76 = load i8*, i8** %3
%77 = call i32 @snprintf(i8* %74, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %75, i8* %76, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%78 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%79 = call i32* @fopen(i8* %78, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %79, i32** %5
%80 = load i32*, i32** %5
%81 = icmp ne i32* %80, null
br i1 %81, label %82, label %93
82: ; preds = %73
%83 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%84 = load i32*, i32** %5
%85 = call i64 @fgets(i8* %83, i32 64, i32* %84)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %90
87: ; preds = %82
%88 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%89 = call i32 @atoi(i8* %88)
store i32 %89, i32* %9
br label %90
90: ; preds = %87, %82
%91 = load i32*, i32** %5
%92 = call i32 @fclose(i32* %91)
br label %94
93: ; preds = %73
store i32 -2, i32* %2
br label %100
94: ; preds = %90
br label %95
95: ; preds = %94, %70
%96 = load i32, i32* %8
%97 = load i32, i32* %9
%98 = sdiv i32 %97, 100
%99 = sdiv i32 %96, %98
store i32 %99, i32* %2
br label %100
100: ; preds = %95, %93, %49
%101 = load i32, i32* %2
ret i32 %101
}

LLVM IR (-O2):
@.str = [24 x i8] c"/sys/class/power_supply\00"
@.str.1 = [9 x i8] c"%s/%s/%s\00"
@.str.2 = [11 x i8] c"energy_now\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [11 x i8] c"charge_now\00"
@.str.5 = [12 x i8] c"energy_full\00"
@.str.6 = [12 x i8] c"charge_full\00"
define i32 @get_battery_percent(i8* %0) {
%2 = alloca [512 x i8]
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = call i64 @fgets(i8* %5, i32 64, i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %24, label %21
12: ; preds = %1
%13 = call i32 @snprintf(i8* %4, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%14 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = call i64 @fgets(i8* %5, i32 64, i32* %14)
%18 = icmp eq i64 %17, 0
br i1 %18, label %24, label %21
19: ; preds = %12
%20 = call i32 @perror(i8* %4)
br label %50
21: ; preds = %16, %9
%22 = phi i32* [ %7, %9 ], [ %14, %16 ]
%23 = call i32 @atoi(i8* %5)
br label %24
24: ; preds = %21, %16, %9
%25 = phi i32* [ %7, %9 ], [ %14, %16 ], [ %22, %21 ]
%26 = phi i32 [ 1, %9 ], [ 1, %16 ], [ %23, %21 ]
%27 = call i32 @fclose(i32* %25)
%28 = call i32 @snprintf(i8* %4, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%29 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %34, label %31
31: ; preds = %24
%32 = call i64 @fgets(i8* %5, i32 64, i32* %29)
%33 = icmp eq i64 %32, 0
br i1 %33, label %45, label %41
34: ; preds = %24
%35 = call i32 @snprintf(i8* %4, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%36 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp eq i32* %36, null
br i1 %37, label %50, label %38
38: ; preds = %34
%39 = call i64 @fgets(i8* %5, i32 64, i32* %36)
%40 = icmp eq i64 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %38, %31
%42 = phi i32* [ %29, %31 ], [ %36, %38 ]
%43 = call i32 @atoi(i8* %5)
%44 = sdiv i32 %43, 100
br label %45
45: ; preds = %41, %38, %31
%46 = phi i32* [ %29, %31 ], [ %36, %38 ], [ %42, %41 ]
%47 = phi i32 [ 0, %31 ], [ 0, %38 ], [ %44, %41 ]
%48 = call i32 @fclose(i32* %46)
%49 = sdiv i32 %26, %47
br label %50
50: ; preds = %34, %45, %19
%51 = phi i32 [ %49, %45 ], [ -1, %19 ], [ -2, %34 ]
ret i32 %51
}