#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() {
  FILE *fd;
  int energy_now, energy_full, voltage_now;
  fd = fopen("/sys/class/power_supply/BAT0/energy_now", "r");
  if (fd == NULL) {
    fprintf(stderr, "Error opening energy_now.\n");
    return -1;
  }
  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);
}


x86 assembly (clang, -O0):
<getbattery>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xebd(%rip),%rdi
lea    0xede(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1191 <getbattery+0x51>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xebc(%rip),%rsi
call   1050 <fprintf@plt>
movss  0xe81(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    12c3 <getbattery+0x183>
mov    -0x10(%rbp),%rdi
lea    0xeb5(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe9f(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f7 <getbattery+0xb7>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe9d(%rip),%rsi
call   1050 <fprintf@plt>
movss  0xe1b(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    12c3 <getbattery+0x183>
mov    -0x10(%rbp),%rdi
lea    0xe4f(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe7e(%rip),%rdi
lea    0xe12(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    125d <getbattery+0x11d>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
call   1050 <fprintf@plt>
movss  0xdb5(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    12c3 <getbattery+0x183>
mov    -0x10(%rbp),%rdi
lea    0xde9(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cvtsi2ssl -0x14(%rbp),%xmm0
movss  0xd79(%rip),%xmm1
mulss  %xmm1,%xmm0
cvtsi2ssl -0x1c(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  0xd68(%rip),%xmm1
mulss  %xmm1,%xmm0
cvtsi2ssl -0x18(%rbp),%xmm1
movss  0xd53(%rip),%xmm2
mulss  %xmm2,%xmm1
cvtsi2ssl -0x1c(%rbp),%xmm2
divss  %xmm2,%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x20,%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     122e <getbattery+0xee>
mov    %rax,%rbx
lea    0xef2(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xed9(%rip),%rdi
lea    0xeb2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1240 <getbattery+0x100>
mov    %rax,%rbx
lea    0xeb7(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xee3(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1252 <getbattery+0x112>
mov    %rax,%rbx
lea    0xe80(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
cvtsi2ssl 0x4(%rsp),%xmm0
movss  0x8(%rsp),%xmm1
movss  0xc(%rsp),%xmm2
unpcklps %xmm1,%xmm2
cvtdq2ps %xmm2,%xmm1
mulps  0xdef(%rip),%xmm1
shufps $0x0,%xmm0,%xmm0
divps  %xmm0,%xmm1
movss  0xdf0(%rip),%xmm0
mulss  %xmm1,%xmm0
shufps $0x55,%xmm1,%xmm1
divss  %xmm1,%xmm0
jmp    126f <getbattery+0x12f>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
jmp    1262 <getbattery+0x122>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
jmp    1262 <getbattery+0x122>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1050 <fprintf@plt>
movss  0xda5(%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"
@stderr = global i32 0
@.str.2 = [27 x i8] c"Error opening energy_now.\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [41 x i8] c"/sys/class/power_supply/BAT0/energy_full\00"
@.str.5 = [28 x i8] c"Error opening energy_full.\0A\00"
@.str.6 = [41 x i8] c"/sys/class/power_supply/BAT0/voltage_now\00"
@.str.7 = [28 x i8] c"Error opening voltage_now.\0A\00"
define float @getbattery() {
%1 = alloca float
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = 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* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store float -1.000000e+00, float* %1
br label %53
12: ; preds = %0
%13 = load i32*, i32** %2
%14 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%15 = load i32*, i32** %2
%16 = call i32 @fclose(i32* %15)
%17 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %2
%18 = load i32*, i32** %2
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %12
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
store float -1.000000e+00, float* %1
br label %53
23: ; preds = %12
%24 = load i32*, i32** %2
%25 = call i32 @fscanf(i32* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%26 = load i32*, i32** %2
%27 = call i32 @fclose(i32* %26)
%28 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %2
%29 = load i32*, i32** %2
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
store float -1.000000e+00, float* %1
br label %53
34: ; preds = %23
%35 = load i32*, i32** %2
%36 = call i32 @fscanf(i32* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%37 = load i32*, i32** %2
%38 = call i32 @fclose(i32* %37)
%39 = load i32, i32* %3
%40 = sitofp i32 %39 to float
%41 = fmul float %40, 1.000000e+03
%42 = load i32, i32* %5
%43 = sitofp i32 %42 to float
%44 = fdiv float %41, %43
%45 = fmul float %44, 1.000000e+02
%46 = load i32, i32* %4
%47 = sitofp i32 %46 to float
%48 = fmul float %47, 1.000000e+03
%49 = load i32, i32* %5
%50 = sitofp i32 %49 to float
%51 = fdiv float %48, %50
%52 = fdiv float %45, %51
store float %52, float* %1
br label %53
53: ; preds = %34, %31, %20, %9
%54 = load float, float* %1
ret float %54
}

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