#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 lnum1, lnum2 = 0;
  char *status = malloc(sizeof(char) * 12);
  char s = '?';
  FILE *fp = NULL;
  float level;
  if ((fp = fopen("/sys/class/power_supply/BAT0/charge_now", "r"))) {
    fscanf(fp, "%ld\n", &lnum1);
    fclose(fp);
    fp = fopen("/sys/class/power_supply/BAT0/charge_full", "r");
    fscanf(fp, "%ld\n", &lnum2);
    fclose(fp);
    fp = fopen("/sys/class/power_supply/BAT0/status", "r");
    fscanf(fp, "%s\n", status);
    fclose(fp);
    level = (lnum1 / (lnum2 / 100.0));
    if (level > 100.0)
      level = 100.0;
    if (strcmp(status, "Charging") == 0)
      s = '^';
    if (strcmp(status, "Discharging") == 0)
      s = 'v';
    if (strcmp(status, "Full") == 0)
      s = '=';
    return smprintf("%c %0.1f%%", s, level);
  } 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    0xe7b(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1301 <getbattery+0x1a1>
mov    -0x30(%rbp),%rdi
lea    0xe80(%rip),%rsi
lea    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe6a(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe4a(%rip),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe5d(%rip),%rdi
lea    0xe26(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cvtsi2sdq -0x10(%rbp),%xmm0
cvtsi2sdq -0x18(%rbp),%xmm1
movsd  0xdb6(%rip),%xmm2
divsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
movss  -0x34(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xd94(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1283 <getbattery+0x123>
movss  0xd8a(%rip),%xmm0
movss  %xmm0,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12a1 <getbattery+0x141>
movb   $0x5e,-0x21(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12bf <getbattery+0x15f>
movb   $0x76,-0x21(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdd7(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12dd <getbattery+0x17d>
movb   $0x3d,-0x21(%rbp)
movsbl -0x21(%rbp),%esi
movss  -0x34(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdb5(%rip),%rdi
mov    $0x1,%al
call   1030 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1313 <getbattery+0x1b3>
lea    0xd32(%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    $0x20,%rsp
movq   $0x0,0x10(%rsp)
mov    $0xc,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe83(%rip),%rdi
lea    0xea4(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12cb <getbattery+0x16b>
mov    %rax,%rbx
lea    0xe8e(%rip),%r15
lea    0x18(%rsp),%rdx
mov    %rax,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe72(%rip),%rdi
lea    0xe64(%rip),%r12
mov    %r12,%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe68(%rip),%rdi
mov    %r12,%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
lea    0xe7a(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
cvtsi2sdq 0x18(%rsp),%xmm0
cvtsi2sdq 0x10(%rsp),%xmm1
divsd  0xdc7(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  0xdbf(%rip),%xmm1
minss  %xmm0,%xmm1
movss  %xmm1,0xc(%rsp)
lea    0xe32(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
mov    $0x5e,%eax
mov    $0x3f,%ebx
cmove  %eax,%ebx
lea    0xe1c(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
mov    $0x76,%ebp
cmovne %ebx,%ebp
lea    0xe0e(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
mov    $0x3d,%esi
cmovne %ebp,%esi
movss  0xc(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdef(%rip),%rdi
mov    $0x1,%al
call   1030 <smprintf@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd68(%rip),%rdi
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <smprintf@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"/sys/class/power_supply/BAT0/charge_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/charge_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 = [11 x i8] c"%c %0.1f%%\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*
%7 = alloca float
store i64 0, i64* %3
%8 = call i8* @malloc(i32 12)
store i8* %8, i8** %4
store i8 63, i8* %5
store i32* null, i32** %6
%9 = 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* %9, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %59
11: ; preds = %0
%12 = load i32*, i32** %6
%13 = call i32 (i32*, i8*, ...) @fscanf(i32* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%14 = load i32*, i32** %6
%15 = call i32 @fclose(i32* %14)
%16 = 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* %16, i32** %6
%17 = load i32*, i32** %6
%18 = call i32 (i32*, i8*, ...) @fscanf(i32* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64* %3)
%19 = load i32*, i32** %6
%20 = call i32 @fclose(i32* %19)
%21 = 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* %21, i32** %6
%22 = load i32*, i32** %6
%23 = load i8*, i8** %4
%24 = call i32 (i32*, i8*, ...) @fscanf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %23)
%25 = load i32*, i32** %6
%26 = call i32 @fclose(i32* %25)
%27 = load i64, i64* %2
%28 = sitofp i64 %27 to double
%29 = load i64, i64* %3
%30 = sitofp i64 %29 to double
%31 = fdiv double %30, 1.000000e+02
%32 = fdiv double %28, %31
%33 = fptrunc double %32 to float
store float %33, float* %7
%34 = load float, float* %7
%35 = fpext float %34 to double
%36 = fcmp ogt double %35, 1.000000e+02
br i1 %36, label %37, label %38
37: ; preds = %11
store float 1.000000e+02, float* %7
br label %38
38: ; preds = %37, %11
%39 = load i8*, i8** %4
%40 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
store i8 94, i8* %5
br label %43
43: ; preds = %42, %38
%44 = load i8*, i8** %4
%45 = call i64 @strcmp(i8* %44, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %43
store i8 118, i8* %5
br label %48
48: ; preds = %47, %43
%49 = load i8*, i8** %4
%50 = call i64 @strcmp(i8* %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %48
store i8 61, i8* %5
br label %53
53: ; preds = %52, %48
%54 = load i8, i8* %5
%55 = sext i8 %54 to i32
%56 = load float, float* %7
%57 = fpext float %56 to double
%58 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i32 %55, double %57)
store i8* %58, i8** %1
br label %61
59: ; preds = %0
%60 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.10, i64 0, i64 0))
store i8* %60, i8** %1
br label %61
61: ; preds = %59, %53
%62 = load i8*, i8** %1
ret i8* %62
}

LLVM IR (-O2):
@.str = [40 x i8] c"/sys/class/power_supply/BAT0/charge_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/charge_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 = [11 x i8] c"%c %0.1f%%\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 %37, 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 = load i64, i64* %1
%18 = sitofp i64 %17 to double
%19 = load i64, i64* %2
%20 = sitofp i64 %19 to double
%21 = fdiv double %20, 1.000000e+02
%22 = fdiv double %18, %21
%23 = fptrunc double %22 to float
%24 = fcmp ogt float %23, 1.000000e+02
%25 = select i1 %24, float 1.000000e+02, float %23
%26 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
%28 = select i1 %27, i32 94, i32 63
%29 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
%31 = select i1 %30, i32 118, i32 %28
%32 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
%34 = select i1 %33, i32 61, i32 %31
%35 = fpext float %25 to double
%36 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i32 %34, double %35)
br label %39
37: ; preds = %0
%38 = tail call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.10, i64 0, i64 0))
br label %39
39: ; preds = %37, %8
%40 = phi i8* [ %36, %8 ], [ %38, %37 ]
ret i8* %40
}