#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*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ stderr;
float getbattery(void) {
  FILE *fd;
  int charge_now, charge_full, energy_now, energy_full, voltage_now;
  if ((fd = fopen("/sys/class/power_supply/BAT0/energy_now", "r")) != NULL) {
    fscanf(fd, "%d", &energy_now);
    fclose(fd);
    fd = fopen("/sys/class/power_supply/BAT0/energy_full", "r");
    if (fd == NULL) {
      fprintf(stderr, "Error opening energy_full.\n");
      return -1;
    }
    fscanf(fd, "%d", &energy_full);
    fclose(fd);
    fd = fopen("/sys/class/power_supply/BAT0/voltage_now", "r");
    if (fd == NULL) {
      fprintf(stderr, "Error opening voltage_now.\n");
      return -1;
    }
    fscanf(fd, "%d", &voltage_now);
    fclose(fd);
    return ((float)energy_now * 1000 / (float)voltage_now) * 100 /
           ((float)energy_full * 1000 / (float)voltage_now);
  } else if ((fd = fopen("/sys/class/power_supply/BAT1/charge_now", "r")) !=
             NULL) {
    fscanf(fd, "%d", &charge_now);
    fclose(fd);
    fd = fopen("/sys/class/power_supply/BAT1/charge_full", "r");
    if (fd == NULL) {
      fprintf(stderr, "Error opening charge_full.\n");
      return -1;
    }
    fscanf(fd, "%d", &charge_full);
    fclose(fd);
    return (((float)charge_now / (float)charge_full) * 100.0);
  } else {
    return -1.0;
  }
}


x86 assembly (clang, -O0):
<getbattery>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xec9(%rip),%rdi
lea    0xeea(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     12a0 <getbattery+0x160>
mov    -0x10(%rbp),%rdi
lea    0xece(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xeb8(%rip),%rdi
lea    0xeac(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cf <getbattery+0x8f>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xeb6(%rip),%rsi
call   1050 <fprintf@plt>
movss  0xe3b(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    137d <getbattery+0x23d>
mov    -0x10(%rbp),%rdi
lea    0xe68(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe97(%rip),%rdi
lea    0xe46(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1235 <getbattery+0xf5>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xe95(%rip),%rsi
call   1050 <fprintf@plt>
movss  0xdd5(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    137d <getbattery+0x23d>
mov    -0x10(%rbp),%rdi
lea    0xe02(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cvtsi2ssl -0x1c(%rbp),%xmm0
movss  0xda5(%rip),%xmm1
mulss  %xmm1,%xmm0
cvtsi2ssl -0x24(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  0xd94(%rip),%xmm1
mulss  %xmm1,%xmm0
cvtsi2ssl -0x20(%rbp),%xmm1
movss  0xd7f(%rip),%xmm2
mulss  %xmm2,%xmm1
cvtsi2ssl -0x24(%rbp),%xmm2
divss  %xmm2,%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    137d <getbattery+0x23d>
lea    0xe28(%rip),%rdi
lea    0xd92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1370 <getbattery+0x230>
mov    -0x10(%rbp),%rdi
lea    0xd76(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe12(%rip),%rdi
lea    0xd54(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1327 <getbattery+0x1e7>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1050 <fprintf@plt>
movss  0xce3(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    137d <getbattery+0x23d>
mov    -0x10(%rbp),%rdi
lea    0xd10(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cvtsi2ssl -0x14(%rbp),%xmm0
cvtsi2ssl -0x18(%rbp),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xcb2(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    137d <getbattery+0x23d>
movss  0xc88(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbattery>:
push   %rbx
sub    $0x10,%rsp
lea    0xecc(%rip),%rdi
lea    0xeed(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1235 <getbattery+0xf5>
mov    %rax,%rbx
lea    0xed7(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xebe(%rip),%rdi
lea    0xeb2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12cc <getbattery+0x18c>
mov    %rax,%rbx
lea    0xe9c(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xec8(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12de <getbattery+0x19e>
mov    %rax,%rbx
lea    0xe61(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
cvtsi2ssl 0xc(%rsp),%xmm0
movss  0x4(%rsp),%xmm1
movss  0x8(%rsp),%xmm2
unpcklps %xmm1,%xmm2
cvtdq2ps %xmm2,%xmm1
mulps  0xdeb(%rip),%xmm1
shufps $0x0,%xmm0,%xmm0
divps  %xmm0,%xmm1
movss  0xdec(%rip),%xmm0
mulss  %xmm1,%xmm0
shufps $0x55,%xmm1,%xmm1
divss  %xmm1,%xmm0
jmp    130d <getbattery+0x1cd>
lea    0xe93(%rip),%rdi
lea    0xdfd(%rip),%rsi
call   1060 <fopen@plt>
movss  0xdc4(%rip),%xmm0
test   %rax,%rax
je     130d <getbattery+0x1cd>
mov    %rax,%rbx
lea    0xddf(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe78(%rip),%rdi
lea    0xdba(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12f0 <getbattery+0x1b0>
mov    %rax,%rbx
lea    0xda8(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xorps  %xmm0,%xmm0
cvtsi2ssl 0x8(%rsp),%xmm0
cvtsi2ssl 0x4(%rsp),%xmm1
divss  %xmm1,%xmm0
mulss  0xd46(%rip),%xmm0
jmp    130d <getbattery+0x1cd>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
lea    0xd92(%rip),%rsi
jmp    1300 <getbattery+0x1c0>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
jmp    1300 <getbattery+0x1c0>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1050 <fprintf@plt>
movss  0xd07(%rip),%xmm0
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [40 x i8] c"/sys/class/power_supply/BAT0/energy_now\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [41 x i8] c"/sys/class/power_supply/BAT0/energy_full\00"
@stderr = global i32 0
@.str.4 = [28 x i8] c"Error opening energy_full.\0A\00"
@.str.5 = [41 x i8] c"/sys/class/power_supply/BAT0/voltage_now\00"
@.str.6 = [28 x i8] c"Error opening voltage_now.\0A\00"
@.str.7 = [40 x i8] c"/sys/class/power_supply/BAT1/charge_now\00"
@.str.8 = [41 x i8] c"/sys/class/power_supply/BAT1/charge_full\00"
@.str.9 = [28 x i8] c"Error opening charge_full.\0A\00"
define float @getbattery() {
%1 = alloca float
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = call i32* @fopen(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %51
10: ; preds = %0
%11 = load i32*, i32** %2
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%13 = load i32*, i32** %2
%14 = call i32 @fclose(i32* %13)
%15 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %2
%16 = load i32*, i32** %2
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store float -1.000000e+00, float* %1
br label %79
21: ; preds = %10
%22 = load i32*, i32** %2
%23 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%24 = load i32*, i32** %2
%25 = call i32 @fclose(i32* %24)
%26 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %2
%27 = load i32*, i32** %2
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
store float -1.000000e+00, float* %1
br label %79
32: ; preds = %21
%33 = load i32*, i32** %2
%34 = call i32 @fscanf(i32* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%35 = load i32*, i32** %2
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* %5
%38 = sitofp i32 %37 to float
%39 = fmul float %38, 1.000000e+03
%40 = load i32, i32* %7
%41 = sitofp i32 %40 to float
%42 = fdiv float %39, %41
%43 = fmul float %42, 1.000000e+02
%44 = load i32, i32* %6
%45 = sitofp i32 %44 to float
%46 = fmul float %45, 1.000000e+03
%47 = load i32, i32* %7
%48 = sitofp i32 %47 to float
%49 = fdiv float %46, %48
%50 = fdiv float %43, %49
store float %50, float* %1
br label %79
51: ; preds = %0
%52 = call i32* @fopen(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %52, i32** %2
%53 = icmp ne i32* %52, null
br i1 %53, label %54, label %78
54: ; preds = %51
%55 = load i32*, i32** %2
%56 = call i32 @fscanf(i32* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%57 = load i32*, i32** %2
%58 = call i32 @fclose(i32* %57)
%59 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %59, i32** %2
%60 = load i32*, i32** %2
%61 = icmp eq i32* %60, null
br i1 %61, label %62, label %65
62: ; preds = %54
%63 = load i32, i32* @stderr
%64 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
store float -1.000000e+00, float* %1
br label %79
65: ; preds = %54
%66 = load i32*, i32** %2
%67 = call i32 @fscanf(i32* %66, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%68 = load i32*, i32** %2
%69 = call i32 @fclose(i32* %68)
%70 = load i32, i32* %3
%71 = sitofp i32 %70 to float
%72 = load i32, i32* %4
%73 = sitofp i32 %72 to float
%74 = fdiv float %71, %73
%75 = fpext float %74 to double
%76 = fmul double %75, 1.000000e+02
%77 = fptrunc double %76 to float
store float %77, float* %1
br label %79
78: ; preds = %51
store float -1.000000e+00, float* %1
br label %79
79: ; preds = %78, %65, %62, %32, %29, %18
%80 = load float, float* %1
ret float %80
}

LLVM IR (-O2):
@.str = [40 x i8] c"/sys/class/power_supply/BAT0/energy_now\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [41 x i8] c"/sys/class/power_supply/BAT0/energy_full\00"
@stderr = global i32 0
@.str.4 = [28 x i8] c"Error opening energy_full.\0A\00"
@.str.5 = [41 x i8] c"/sys/class/power_supply/BAT0/voltage_now\00"
@.str.6 = [28 x i8] c"Error opening voltage_now.\0A\00"
@.str.7 = [40 x i8] c"/sys/class/power_supply/BAT1/charge_now\00"
@.str.8 = [41 x i8] c"/sys/class/power_supply/BAT1/charge_full\00"
@.str.9 = [28 x i8] c"Error opening charge_full.\0A\00"
define float @getbattery() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %1 to i8*
%7 = bitcast i32* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %47, label %13
13: ; preds = %0
%14 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%15 = call i32 @fclose(i32* %11)
%16 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %67
21: ; preds = %13
%22 = call i32 @fscanf(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%23 = call i32 @fclose(i32* %16)
%24 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
br label %67
29: ; preds = %21
%30 = call i32 @fscanf(i32* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%31 = call i32 @fclose(i32* %24)
%32 = load i32, i32* %3
%33 = load i32, i32* %5
%34 = sitofp i32 %33 to float
%35 = load i32, i32* %4
%36 = insertelement <2 x i32> poison, i32 %32, i64 0
%37 = insertelement <2 x i32> %36, i32 %35, i64 1
%38 = sitofp <2 x i32> %37 to <2 x float>
%39 = fmul <2 x float> %38, <float 1.000000e+03, float 1.000000e+03>
%40 = insertelement <2 x float> poison, float %34, i64 0
%41 = shufflevector <2 x float> %40, <2 x float> poison, <2 x i32> zeroinitializer
%42 = fdiv <2 x float> %39, %41
%43 = extractelement <2 x float> %42, i64 0
%44 = fmul float %43, 1.000000e+02
%45 = extractelement <2 x float> %42, i64 1
%46 = fdiv float %44, %45
br label %67
47: ; preds = %0
%48 = tail call i32* @fopen(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%49 = icmp eq i32* %48, null
br i1 %49, label %67, label %50
50: ; preds = %47
%51 = call i32 @fscanf(i32* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%52 = call i32 @fclose(i32* %48)
%53 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%54 = icmp eq i32* %53, null
br i1 %54, label %55, label %58
55: ; preds = %50
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
br label %67
58: ; preds = %50
%59 = call i32 @fscanf(i32* %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%60 = call i32 @fclose(i32* %53)
%61 = load i32, i32* %1
%62 = sitofp i32 %61 to float
%63 = load i32, i32* %2
%64 = sitofp i32 %63 to float
%65 = fdiv float %62, %64
%66 = fmul float %65, 1.000000e+02
br label %67
67: ; preds = %47, %58, %55, %29, %26, %18
%68 = phi float [ -1.000000e+00, %18 ], [ -1.000000e+00, %26 ], [ %46, %29 ], [ -1.000000e+00, %55 ], [ %66, %58 ], [ -1.000000e+00, %47 ]
ret float %68
}