#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 *smprintf(char *, float, char);
char *getbattery(void) {
  FILE *charge_full_design, *charge_now, *status;
  int full = 0, now = 0;
  float battery = 0.0;
  char stat = '\0';
  charge_full_design =
      fopen("/sys/class/power_supply/BAT0/charge_full_design", "r");
  charge_now = fopen("/sys/class/power_supply/BAT0/charge_now", "r");
  status = fopen("/sys/class/power_supply/BAT0/status", "r");
  fscanf(charge_full_design, "%i", &full);
  fscanf(charge_now, "%i", &now);
  fscanf(status, "%c", &stat);
  battery = (float)((now * 100) / full);
  fclose(status);
  fclose(charge_now);
  fclose(charge_full_design);
  return smprintf("%.2f%%[%c]", battery, stat);
}


x86 assembly (clang, -O0):
<getbattery>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x24(%rbp)
movb   $0x0,-0x25(%rbp)
lea    0xe97(%rip),%rdi
lea    0xec0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
lea    0xeb2(%rip),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xec3(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xecc(%rip),%rsi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x10(%rbp),%rdi
lea    0xeb6(%rip),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x18(%rbp),%rdi
lea    0xea3(%rip),%rsi
lea    -0x25(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
imul   $0x64,-0x20(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
cvtsi2ss %eax,%xmm0
movss  %xmm0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
movss  -0x24(%rbp),%xmm0
lea    0xe63(%rip),%rdi
movsbl -0x25(%rbp),%esi
call   1030 <smprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbattery>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
movb   $0x0,0xb(%rsp)
lea    0xe99(%rip),%rdi
lea    0xec2(%rip),%rbx
mov    %rbx,%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
lea    0xeb2(%rip),%rdi
mov    %rbx,%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
lea    0xec8(%rip),%rdi
mov    %rbx,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0xeda(%rip),%r12
lea    0x10(%rsp),%rdx
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <fscanf@plt>
lea    0xc(%rsp),%rdx
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <fscanf@plt>
lea    0xeb2(%rip),%rsi
lea    0xb(%rsp),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <fscanf@plt>
imul   $0x64,0xc(%rsp),%eax
cltd
idivl  0x10(%rsp)
cvtsi2ss %eax,%xmm0
movss  %xmm0,0x14(%rsp)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
movsbl 0xb(%rsp),%esi
lea    0xe6e(%rip),%rdi
movss  0x14(%rsp),%xmm0
call   1030 <smprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [48 x i8] c"/sys/class/power_supply/BAT0/charge_full_design\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [40 x i8] c"/sys/class/power_supply/BAT0/charge_now\00"
@.str.3 = [36 x i8] c"/sys/class/power_supply/BAT0/status\00"
@.str.4 = [3 x i8] c"%i\00"
@.str.5 = [3 x i8] c"%c\00"
@.str.6 = [11 x i8] c"%.2f%%[%c]\00"
define i8* @getbattery() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca float
%7 = alloca i8
store i32 0, i32* %4
store i32 0, i32* %5
store float 0.000000e+00, float* %6
store i8 0, i8* %7
%8 = call i32* @fopen(i8* getelementptr inbounds ([48 x i8], [48 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** %1
%9 = call i32* @fopen(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %2
%10 = call i32* @fopen(i8* getelementptr inbounds ([36 x i8], [36 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* %10, i32** %3
%11 = load i32*, i32** %1
%12 = call i32 (i32*, i8*, ...) @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %4)
%13 = load i32*, i32** %2
%14 = call i32 (i32*, i8*, ...) @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %5)
%15 = load i32*, i32** %3
%16 = call i32 (i32*, i8*, ...) @fscanf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %7)
%17 = load i32, i32* %5
%18 = mul nsw i32 %17, 100
%19 = load i32, i32* %4
%20 = sdiv i32 %18, %19
%21 = sitofp i32 %20 to float
store float %21, float* %6
%22 = load i32*, i32** %3
%23 = call i32 @fclose(i32* %22)
%24 = load i32*, i32** %2
%25 = call i32 @fclose(i32* %24)
%26 = load i32*, i32** %1
%27 = call i32 @fclose(i32* %26)
%28 = load float, float* %6
%29 = load i8, i8* %7
%30 = call i8* @smprintf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), float %28, i8 signext %29)
ret i8* %30
}

LLVM IR (-O2):
@.str = [48 x i8] c"/sys/class/power_supply/BAT0/charge_full_design\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [40 x i8] c"/sys/class/power_supply/BAT0/charge_now\00"
@.str.3 = [36 x i8] c"/sys/class/power_supply/BAT0/status\00"
@.str.4 = [3 x i8] c"%i\00"
@.str.5 = [3 x i8] c"%c\00"
@.str.6 = [11 x i8] c"%.2f%%[%c]\00"
define i8* @getbattery() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%5 = bitcast i32* %2 to i8*
store i32 0, i32* %2
store i8 0, i8* %3
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 (i32*, i8*, ...) @fscanf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%10 = call i32 (i32*, i8*, ...) @fscanf(i32* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%11 = call i32 (i32*, i8*, ...) @fscanf(i32* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %3)
%12 = load i32, i32* %2
%13 = mul nsw i32 %12, 100
%14 = load i32, i32* %1
%15 = sdiv i32 %13, %14
%16 = sitofp i32 %15 to float
%17 = call i32 @fclose(i32* %8)
%18 = call i32 @fclose(i32* %7)
%19 = call i32 @fclose(i32* %6)
%20 = load i8, i8* %3
%21 = call i8* @smprintf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), float %16, i8 signext %20)
ret i8* %21
}