#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 */
float EXIT_FAILURE;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ stderr;
float getbattery(void) {
  FILE *fd;
  int capacity_bat0, capacity_bat1;
  if (!(fd = fopen("/sys/class/power_supply/BAT0/capacity", "r"))) {
    fputs("Error opening energy_now.\n", stderr);
    return EXIT_FAILURE;
  }
  if (fscanf(fd, "%d", &capacity_bat0) != 1) {
    fputs("Error in fscanf.\n", stderr);
    fclose(fd);
  }
  fclose(fd);
  if (!(fd = fopen("/sys/class/power_supply/BAT1/capacity", "r"))) {
    fputs("Error opening energy_full.\n", stderr);
    return EXIT_FAILURE;
  }
  if (fscanf(fd, "%d", &capacity_bat1) != 1) {
    fputs("Error in fscanf.\n", stderr);
    fclose(fd);
  }
  fclose(fd);
  return ((capacity_bat0 + capacity_bat1) / 2);
}


x86 assembly (clang, -O0):
<getbattery>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
lea    0xed0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1193 <getbattery+0x53>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1040 <fputs@plt>
mov    0x2e53(%rip),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    127d <getbattery+0x13d>
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     11ce <getbattery+0x8e>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0xe86(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe7a(%rip),%rdi
lea    0xe41(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1222 <getbattery+0xe2>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdi
call   1040 <fputs@plt>
mov    0x2dc4(%rip),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    127d <getbattery+0x13d>
mov    -0x10(%rbp),%rdi
lea    0xe16(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     125d <getbattery+0x11d>
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
lea    0xdf7(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cvtsi2ss %eax,%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    0xeb4(%rip),%rdi
lea    0xed3(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1212 <getbattery+0xd2>
mov    %rax,%rbx
lea    0xed8(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     119a <getbattery+0x5a>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xeb9(%rip),%rdi
call   1040 <fputs@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xeaf(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1224 <getbattery+0xe4>
mov    %rax,%rbx
lea    0xe7f(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     11f3 <getbattery+0xb3>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
lea    0xe60(%rip),%rdi
call   1040 <fputs@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%eax
add    0xc(%rsp),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
cvtsi2ss %ecx,%xmm0
jmp    1244 <getbattery+0x104>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    0xe06(%rip),%rdi
jmp    1234 <getbattery+0xf4>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    0xe4a(%rip),%rdi
call   1040 <fputs@plt>
mov    0x2d98(%rip),%rax
movss  (%rax),%xmm0
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [38 x i8] c"/sys/class/power_supply/BAT0/capacity\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"Error opening energy_now.\0A\00"
@stderr = global i32 0
@EXIT_FAILURE = global float 0.000000e+00
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [18 x i8] c"Error in fscanf.\0A\00"
@.str.5 = [38 x i8] c"/sys/class/power_supply/BAT1/capacity\00"
@.str.6 = [28 x i8] c"Error opening energy_full.\0A\00"
define float @getbattery() {
%1 = alloca float
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = call i32* @fopen(i8* getelementptr inbounds ([38 x i8], [38 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 = icmp ne i32* %5, null
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 @fputs(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = load float, float* @EXIT_FAILURE
store float %10, float* %1
br label %46
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 = icmp ne i32 %13, 1
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %16)
%18 = load i32*, i32** %2
%19 = call i32 @fclose(i32* %18)
br label %20
20: ; preds = %15, %11
%21 = load i32*, i32** %2
%22 = call i32 @fclose(i32* %21)
%23 = call i32* @fopen(i8* getelementptr inbounds ([38 x i8], [38 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* %23, i32** %2
%24 = icmp ne i32* %23, null
br i1 %24, label %29, label %25
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %26)
%28 = load float, float* @EXIT_FAILURE
store float %28, float* %1
br label %46
29: ; preds = %20
%30 = load i32*, i32** %2
%31 = call i32 @fscanf(i32* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = load i32*, i32** %2
%37 = call i32 @fclose(i32* %36)
br label %38
38: ; preds = %33, %29
%39 = load i32*, i32** %2
%40 = call i32 @fclose(i32* %39)
%41 = load i32, i32* %3
%42 = load i32, i32* %4
%43 = add nsw i32 %41, %42
%44 = sdiv i32 %43, 2
%45 = sitofp i32 %44 to float
store float %45, float* %1
br label %46
46: ; preds = %38, %25, %7
%47 = load float, float* %1
ret float %47
}

LLVM IR (-O2):
@.str = [38 x i8] c"/sys/class/power_supply/BAT0/capacity\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"Error opening energy_now.\0A\00"
@stderr = global i32 0
@EXIT_FAILURE = global float 0.000000e+00
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [18 x i8] c"Error in fscanf.\0A\00"
@.str.5 = [38 x i8] c"/sys/class/power_supply/BAT1/capacity\00"
@.str.6 = [28 x i8] c"Error opening energy_full.\0A\00"
define float @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 ([38 x i8], [38 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 %11
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = tail call i32 @fputs(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = load float, float* @EXIT_FAILURE
br label %40
11: ; preds = %0
%12 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%13 = icmp eq i32 %12, 1
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%17 = call i32 @fclose(i32* %5)
br label %18
18: ; preds = %14, %11
%19 = call i32 @fclose(i32* %5)
%20 = call i32* @fopen(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %23)
%25 = load float, float* @EXIT_FAILURE
br label %40
26: ; preds = %18
%27 = call i32 @fscanf(i32* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%28 = icmp eq i32 %27, 1
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = call i32 @fclose(i32* %20)
br label %33
33: ; preds = %29, %26
%34 = call i32 @fclose(i32* %20)
%35 = load i32, i32* %1
%36 = load i32, i32* %2
%37 = add nsw i32 %36, %35
%38 = sdiv i32 %37, 2
%39 = sitofp i32 %38 to float
br label %40
40: ; preds = %33, %22, %7
%41 = phi float [ %39, %33 ], [ %25, %22 ], [ %10, %7 ]
ret float %41
}