#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*/ fscanf(int /*<<< orphan*/ *, char *, ...);
char *malloc(int);
char *smprintf(char *, ...);
scalar_t__ strcmp(char *, char *);
char *getbattery() {
  long current, full = 0;
  char *status = malloc(sizeof(char) * 12);
  char s = '?';
  FILE *fp = NULL;
  if ((fp = fopen("/sys/class/power_supply/BAT0/energy_now", "r"))) {
    fscanf(fp, "%ld\n", &current);
    fclose(fp);
    fp = fopen("/sys/class/power_supply/BAT0/energy_full", "r");
    fscanf(fp, "%ld\n", &full);
    fclose(fp);
    fp = fopen("/sys/class/power_supply/BAT0/status", "r");
    fscanf(fp, "%s\n", status);
    fclose(fp);
    if (strcmp(status, "Charging") == 0)
      s = '+';
    if (strcmp(status, "Discharging") == 0)
      s = '-';
    if (strcmp(status, "Full") == 0)
      s = '=';
    return smprintf("%c%ld%%", s, (current / (full / 100)));
  } else
    return smprintf("");
}


x86 assembly (clang, -O0):
<getbattery>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movq   $0x0,-0x18(%rbp)
mov    $0xc,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movb   $0x3f,-0x21(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xe6f(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     12d0 <getbattery+0x170>
mov    -0x30(%rbp),%rdi
lea    0xe74(%rip),%rsi
lea    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe5e(%rip),%rdi
lea    0xe50(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe3e(%rip),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe51(%rip),%rdi
lea    0xe1a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1254 <getbattery+0xf4>
movb   $0x2b,-0x21(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1272 <getbattery+0x112>
movb   $0x2d,-0x21(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1290 <getbattery+0x130>
movb   $0x3d,-0x21(%rbp)
movsbl -0x21(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x64,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cqto
idiv   %rcx
mov    %rax,%rdx
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1030 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    12e2 <getbattery+0x182>
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1030 <smprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbattery>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
mov    $0xc,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe78(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12b6 <getbattery+0x156>
mov    %rax,%rbx
lea    0xe83(%rip),%r15
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe67(%rip),%rdi
lea    0xe59(%rip),%r12
mov    %r12,%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %rsp,%rdx
mov    %rax,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe5f(%rip),%rdi
mov    %r12,%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
lea    0xe71(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe59(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
mov    $0x2b,%eax
mov    $0x3f,%ebx
cmove  %eax,%ebx
lea    0xe43(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
mov    $0x2d,%ebp
cmovne %ebx,%ebp
lea    0xe35(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
mov    $0x3d,%esi
cmovne %ebp,%esi
mov    0x8(%rsp),%rcx
mov    (%rsp),%rbx
movabs $0xa3d70a3d70a3d70b,%rdx
0a d7 a3
mov    %rbx,%rax
imul   %rdx
mov    %rdx,%rdi
add    %rbx,%rdi
mov    %rdi,%rax
shr    $0x3f,%rax
sar    $0x6,%rdi
add    %rax,%rdi
mov    %rcx,%rax
or     %rdi,%rax
shr    $0x20,%rax
je     12d0 <getbattery+0x170>
mov    %rcx,%rax
cqto
idiv   %rdi
jmp    12d6 <getbattery+0x176>
lea    0xd71(%rip),%rdi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <smprintf@plt>
mov    %ecx,%eax
xor    %edx,%edx
div    %edi
lea    0xdbd(%rip),%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <smprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
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"
@.str.2 = [5 x i8] c"%ld\0A\00"
@.str.3 = [41 x i8] c"/sys/class/power_supply/BAT0/energy_full\00"
@.str.4 = [36 x i8] c"/sys/class/power_supply/BAT0/status\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [9 x i8] c"Charging\00"
@.str.7 = [12 x i8] c"Discharging\00"
@.str.8 = [5 x i8] c"Full\00"
@.str.9 = [8 x i8] c"%c%ld%%\00"
@.str.10 = [1 x i8] zeroinitializer
define i8* @getbattery() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32*
store i64 0, i64* %3
%7 = call i8* @malloc(i32 12)
store i8* %7, i8** %4
store i8 63, i8* %5
store i32* null, i32** %6
%8 = 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* %8, i32** %6
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %48
10: ; preds = %0
%11 = load i32*, i32** %6
%12 = call i32 (i32*, i8*, ...) @fscanf(i32* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%13 = load i32*, i32** %6
%14 = call i32 @fclose(i32* %13)
%15 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %6
%16 = load i32*, i32** %6
%17 = call i32 (i32*, i8*, ...) @fscanf(i32* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64* %3)
%18 = load i32*, i32** %6
%19 = call i32 @fclose(i32* %18)
%20 = call i32* @fopen(i8* getelementptr inbounds ([36 x i8], [36 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* %20, i32** %6
%21 = load i32*, i32** %6
%22 = load i8*, i8** %4
%23 = call i32 (i32*, i8*, ...) @fscanf(i32* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %22)
%24 = load i32*, i32** %6
%25 = call i32 @fclose(i32* %24)
%26 = load i8*, i8** %4
%27 = call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %10
store i8 43, i8* %5
br label %30
30: ; preds = %29, %10
%31 = load i8*, i8** %4
%32 = call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
store i8 45, i8* %5
br label %35
35: ; preds = %34, %30
%36 = load i8*, i8** %4
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
store i8 61, i8* %5
br label %40
40: ; preds = %39, %35
%41 = load i8, i8* %5
%42 = sext i8 %41 to i32
%43 = load i64, i64* %2
%44 = load i64, i64* %3
%45 = sdiv i64 %44, 100
%46 = sdiv i64 %43, %45
%47 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 %42, i64 %46)
store i8* %47, i8** %1
br label %50
48: ; preds = %0
%49 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.10, i64 0, i64 0))
store i8* %49, i8** %1
br label %50
50: ; preds = %48, %40
%51 = load i8*, i8** %1
ret i8* %51
}

LLVM IR (-O2):
@.str = [40 x i8] c"/sys/class/power_supply/BAT0/energy_now\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [5 x i8] c"%ld\0A\00"
@.str.3 = [41 x i8] c"/sys/class/power_supply/BAT0/energy_full\00"
@.str.4 = [36 x i8] c"/sys/class/power_supply/BAT0/status\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [9 x i8] c"Charging\00"
@.str.7 = [12 x i8] c"Discharging\00"
@.str.8 = [5 x i8] c"Full\00"
@.str.9 = [8 x i8] c"%c%ld%%\00"
@.str.10 = [1 x i8] zeroinitializer
define i8* @getbattery() {
%1 = alloca i64
%2 = alloca i64
%3 = bitcast i64* %1 to i8*
%4 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%5 = tail call dereferenceable_or_null(12) i8* @malloc(i32 12)
%6 = 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))
%7 = icmp eq i32* %6, null
br i1 %7, label %31, label %8
8: ; preds = %0
%9 = call i32 (i32*, i8*, ...) @fscanf(i32* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%10 = call i32 @fclose(i32* %6)
%11 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (i32*, i8*, ...) @fscanf(i32* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%13 = call i32 @fclose(i32* %11)
%14 = call i32* @fopen(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 (i32*, i8*, ...) @fscanf(i32* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %5)
%16 = call i32 @fclose(i32* %14)
%17 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
%19 = select i1 %18, i32 43, i32 63
%20 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
%22 = select i1 %21, i32 45, i32 %19
%23 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
%25 = select i1 %24, i32 61, i32 %22
%26 = load i64, i64* %1
%27 = load i64, i64* %2
%28 = sdiv i64 %27, 100
%29 = sdiv i64 %26, %28
%30 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 %25, i64 %29)
br label %33
31: ; preds = %0
%32 = tail call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.10, i64 0, i64 0))
br label %33
33: ; preds = %31, %8
%34 = phi i8* [ %30, %8 ], [ %32, %31 ]
ret i8* %34
}