#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/BAT1/capacity", "r");
  if (fd == NULL) {
    fprintf(stderr, "Error opening energy_now.\n");
    return -1;
  }
  fscanf(fd, "%d", &energy_now);
  fclose(fd);
  return (float)energy_now;
}


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)
cmpq   $0x0,-0x10(%rbp)
jne    118b <getbattery+0x4b>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xeae(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <getbattery+0x74>
mov    -0x10(%rbp),%rdi
lea    0xead(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cvtsi2ssl -0x14(%rbp),%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    0xeb4(%rip),%rdi
lea    0xed3(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1188 <getbattery+0x48>
mov    %rax,%rbx
lea    0xedc(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
cvtsi2ssl 0xc(%rsp),%xmm0
cvttss2si %xmm0,%eax
jmp    11a2 <getbattery+0x62>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [38 x i8] c"/sys/class/power_supply/BAT1/capacity\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"
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 ([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* %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 %20
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 = load i32, i32* %3
%18 = sitofp i32 %17 to float
%19 = fptosi float %18 to i32
store i32 %19, i32* %1
br label %20
20: ; preds = %12, %9
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@.str = [38 x i8] c"/sys/class/power_supply/BAT1/capacity\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"
define i32 @getbattery() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = 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))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %14
8: ; preds = %0
%9 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%10 = call i32 @fclose(i32* %3)
%11 = load i32, i32* %1
%12 = sitofp i32 %11 to float
%13 = fptosi float %12 to i32
br label %14
14: ; preds = %8, %5
%15 = phi i32 [ -1, %5 ], [ %13, %8 ]
ret i32 %15
}