#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *, char *, ...);
int main(void) {
  char status[32];
  int curr_charge, full_charge;
  double percent;
  FILE *batt_file;
  if (batt_file = fopen("/sys/class/power_supply/BAT0/status", "r")) {
    fgets(status, 32, batt_file);
    fclose(batt_file);
    batt_file = fopen("/sys/class/power_supply/BAT0/charge_now", "r");
    fscanf(batt_file, "%d", &curr_charge);
    fclose(batt_file);
    batt_file = fopen("/sys/class/power_supply/BAT0/charge_full", "r");
    fscanf(batt_file, "%d", &full_charge);
    fclose(batt_file);
    percent = ((double)curr_charge / full_charge) * 100;
    printf("%s%s", "Battery status: ", status);
    printf("%s%2.1f%%\n", "Current charge: ", percent);
  } else {
    printf("%s\n", "Error: No battery detected.");
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea2(%rip),%rdi
lea    0xebf(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     125a <main+0x10a>
lea    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rdx
mov    $0x20,%esi
call   1060 <fgets@plt>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe8c(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
lea    0xe99(%rip),%rsi
lea    -0x34(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe83(%rip),%rdi
lea    0xe4f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
lea    0xe65(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
cvtsi2sdl -0x34(%rbp),%xmm0
cvtsi2sdl -0x38(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  0xde7(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
lea    -0x30(%rbp),%rdx
lea    0xe55(%rip),%rdi
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movsd  -0x40(%rbp),%xmm0
lea    0xe51(%rip),%rdi
lea    0xe55(%rip),%rsi
mov    $0x1,%al
call   1040 <printf@plt>
jmp    1280 <main+0x130>
lea    0xe53(%rip),%rdi
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    128c <main+0x13c>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    0xea6(%rip),%rdi
lea    0xec3(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1250 <main+0x100>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x20,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe90(%rip),%rdi
lea    0xe87(%rip),%r15
mov    %r15,%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xe9f(%rip),%r12
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
mov    %r12,%rsi
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe83(%rip),%rdi
mov    %r15,%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %rsp,%rdx
mov    %rax,%rdi
mov    %r12,%rsi
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
cvtsi2sdl 0x4(%rsp),%xmm1
cvtsi2sdl (%rsp),%xmm0
divsd  %xmm0,%xmm1
mulsd  0xdf2(%rip),%xmm1
movsd  %xmm1,0x8(%rsp)
lea    0xe67(%rip),%rdi
lea    0xe65(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe65(%rip),%rdi
lea    0xe69(%rip),%rsi
movsd  0x8(%rsp),%xmm0
mov    $0x1,%al
call   1040 <printf@plt>
mov    0x2d8a(%rip),%rax
jmp    126c <main+0x11c>
lea    0xe5d(%rip),%rdi
lea    0xe5a(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [36 x i8] c"/sys/class/power_supply/BAT0/status\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 = [3 x i8] c"%d\00"
@.str.4 = [41 x i8] c"/sys/class/power_supply/BAT0/charge_full\00"
@.str.5 = [5 x i8] c"%s%s\00"
@.str.6 = [17 x i8] c"Battery status: \00"
@.str.7 = [11 x i8] c"%s%2.1f%%\0A\00"
@.str.8 = [17 x i8] c"Current charge: \00"
@.str.9 = [4 x i8] c"%s\0A\00"
@.str.10 = [28 x i8] c"Error: No battery detected.\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [32 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca i32*
store i32 0, i32* %1
%7 = call i32* @fopen(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %6
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %35
9: ; preds = %0
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%11 = load i32*, i32** %6
%12 = call i32 @fgets(i8* %10, i32 32, i32* %11)
%13 = load i32*, i32** %6
%14 = call i32 @fclose(i32* %13)
%15 = 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* %15, i32** %6
%16 = load i32*, i32** %6
%17 = call i32 @fscanf(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%18 = load i32*, i32** %6
%19 = call i32 @fclose(i32* %18)
%20 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 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 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%23 = load i32*, i32** %6
%24 = call i32 @fclose(i32* %23)
%25 = load i32, i32* %3
%26 = sitofp i32 %25 to double
%27 = load i32, i32* %4
%28 = sitofp i32 %27 to double
%29 = fdiv double %26, %28
%30 = fmul double %29, 1.000000e+02
store double %30, double* %5
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%32 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i8* %31)
%33 = load double, double* %5
%34 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), double %33)
br label %38
35: ; preds = %0
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %1
br label %40
38: ; preds = %9
%39 = load i32, i32* @EXIT_SUCCESS
store i32 %39, i32* %1
br label %40
40: ; preds = %38, %35
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str = [36 x i8] c"/sys/class/power_supply/BAT0/status\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 = [3 x i8] c"%d\00"
@.str.4 = [41 x i8] c"/sys/class/power_supply/BAT0/charge_full\00"
@.str.5 = [5 x i8] c"%s%s\00"
@.str.6 = [17 x i8] c"Battery status: \00"
@.str.7 = [11 x i8] c"%s%2.1f%%\0A\00"
@.str.8 = [17 x i8] c"Current charge: \00"
@.str.9 = [4 x i8] c"%s\0A\00"
@.str.10 = [28 x i8] c"Error: No battery detected.\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [32 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %26, label %9
9: ; preds = %0
%10 = call i32 @fgets(i8* %4, i32 32, i32* %7)
%11 = call i32 @fclose(i32* %7)
%12 = 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))
%13 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%14 = call i32 @fclose(i32* %12)
%15 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%17 = call i32 @fclose(i32* %15)
%18 = load i32, i32* %2
%19 = sitofp i32 %18 to double
%20 = load i32, i32* %3
%21 = sitofp i32 %20 to double
%22 = fdiv double %19, %21
%23 = fmul double %22, 1.000000e+02
%24 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%25 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), double %23)
br label %28
26: ; preds = %0
%27 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
br label %28
28: ; preds = %9, %26
%29 = phi i32* [ @EXIT_SUCCESS, %9 ], [ @EXIT_FAILURE, %26 ]
%30 = load i32, i32* %29
ret i32 %30
}