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


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    122d <getbattery+0xed>
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    122d <getbattery+0xed>
mov    -0x10(%rbp),%rdi
lea    0xe57(%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  0xdde(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %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    0xebc(%rip),%rdi
lea    0xedd(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d5 <getbattery+0x95>
mov    %rax,%rbx
lea    0xee6(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xecd(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e7 <getbattery+0xa7>
mov    %rax,%rbx
lea    0xeaf(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
cvtsi2ssl 0xc(%rsp),%xmm0
cvtsi2ssl 0x8(%rsp),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  0xe31(%rip),%xmm0
cvttsd2si %xmm0,%eax
jmp    1201 <getbattery+0xc1>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
jmp    11f7 <getbattery+0xb7>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%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/charge_now\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [27 x i8] c"Error opening charge_now.\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [41 x i8] c"/sys/class/power_supply/BAT0/charge_full\00"
@.str.5 = [28 x i8] c"Error opening charge_full.\0A\00"
define i32 @getbattery() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = 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* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %35
11: ; preds = %0
%12 = load i32*, i32** %2
%13 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%14 = load i32*, i32** %2
%15 = call i32 @fclose(i32* %14)
%16 = 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* %16, i32** %2
%17 = load i32*, i32** %2
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
br label %35
22: ; preds = %11
%23 = load i32*, i32** %2
%24 = call i32 @fscanf(i32* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%25 = load i32*, i32** %2
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* %3
%28 = sitofp i32 %27 to float
%29 = load i32, i32* %4
%30 = sitofp i32 %29 to float
%31 = fdiv float %28, %30
%32 = fpext float %31 to double
%33 = fmul double %32, 1.000000e+02
%34 = fptosi double %33 to i32
store i32 %34, i32* %1
br label %35
35: ; preds = %22, %19, %8
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [40 x i8] c"/sys/class/power_supply/BAT0/charge_now\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [27 x i8] c"Error opening charge_now.\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [41 x i8] c"/sys/class/power_supply/BAT0/charge_full\00"
@.str.5 = [28 x i8] c"Error opening charge_full.\0A\00"
define i32 @getbattery() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %29
10: ; preds = %0
%11 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%12 = call i32 @fclose(i32* %5)
%13 = 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))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %29
18: ; preds = %10
%19 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%20 = call i32 @fclose(i32* %13)
%21 = load i32, i32* %1
%22 = sitofp i32 %21 to float
%23 = load i32, i32* %2
%24 = sitofp i32 %23 to float
%25 = fdiv float %22, %24
%26 = fpext float %25 to double
%27 = fmul double %26, 1.000000e+02
%28 = fptosi double %27 to i32
br label %29
29: ; preds = %18, %15, %7
%30 = phi i32 [ -1, %7 ], [ -1, %15 ], [ %28, %18 ]
ret i32 %30
}