#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;
int 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    0xeb9(%rip),%rdi
lea    0xeda(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118b <getbattery+0x4b>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xeb8(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <getbattery+0x173>
mov    -0x10(%rbp),%rdi
lea    0xeb7(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xea1(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11eb <getbattery+0xab>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe9f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <getbattery+0x173>
mov    -0x10(%rbp),%rdi
lea    0xe57(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe86(%rip),%rdi
lea    0xe1a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    124b <getbattery+0x10b>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <getbattery+0x173>
mov    -0x10(%rbp),%rdi
lea    0xdf7(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cvtsi2ssl -0x14(%rbp),%xmm0
movss  0xd8b(%rip),%xmm1
mulss  %xmm1,%xmm0
cvtsi2ssl -0x1c(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  0xd7a(%rip),%xmm1
mulss  %xmm1,%xmm0
cvtsi2ssl -0x18(%rbp),%xmm1
movss  0xd65(%rip),%xmm2
mulss  %xmm2,%xmm1
cvtsi2ssl -0x1c(%rbp),%xmm2
divss  %xmm2,%xmm1
divss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbattery>:
push   %rbx
sub    $0x10,%rsp
lea    0xec8(%rip),%rdi
lea    0xee9(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1236 <getbattery+0xf6>
mov    %rax,%rbx
lea    0xeee(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xed5(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1248 <getbattery+0x108>
mov    %rax,%rbx
lea    0xeb3(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xedf(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     125a <getbattery+0x11a>
mov    %rax,%rbx
lea    0xe78(%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  0xdeb(%rip),%xmm1
shufps $0x0,%xmm0,%xmm0
divps  %xmm0,%xmm1
movss  0xdec(%rip),%xmm0
mulss  %xmm1,%xmm0
shufps $0x55,%xmm1,%xmm1
divss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
jmp    1274 <getbattery+0x134>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
jmp    126a <getbattery+0x12a>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
jmp    126a <getbattery+0x12a>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
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 i32 @getbattery() {
%1 = alloca i32
%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 i32 -1, i32* %1
br label %54
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 i32 -1, i32* %1
br label %54
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 i32 -1, i32* %1
br label %54
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
%53 = fptosi float %52 to i32
store i32 %53, i32* %1
br label %54
54: ; preds = %34, %31, %20, %9
%55 = load i32, i32* %1
ret i32 %55
}

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 i32 @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 %47
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 %47
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 %47
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
%46 = fptosi float %45 to i32
br label %47
47: ; preds = %28, %25, %17, %9
%48 = phi i32 [ -1, %9 ], [ -1, %17 ], [ -1, %25 ], [ %46, %28 ]
ret i32 %48
}