#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 */

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *readfile(char *, char *);
char *smprintf(char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, int *);
char *getbattery(char *base) {
  char *co;
  int descap, remcap, charging;
  descap = -1;
  remcap = -1;
  charging = 1;
  co = readfile(base, "present");
  if (co == NULL || co[0] != '1') {
    if (co != NULL)
      free(co);
    return smprintf("not present");
  }
  free(co);
  co = readfile(base, "charge_full_design");
  if (co == NULL) {
    co = readfile(base, "energy_full_design");
    if (co == NULL)
      return smprintf("");
  }
  sscanf(co, "%d", &descap);
  free(co);
  co = readfile(base, "charge_now");
  if (co == NULL) {
    co = readfile(base, "energy_now");
    if (co == NULL)
      return smprintf("");
  }
  sscanf(co, "%d", &remcap);
  free(co);
  if (remcap < 0 || descap < 0)
    return smprintf("invalid");
  co = readfile(base, "status");
  if (co[0] == 'D')
    charging = 0;
  return smprintf("%.0f % (%s)", ((float)remcap / (float)descap) * 100,
                  charging ? "C" : "D");
}


x86 assembly (clang, -O0):
<getbattery>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1190 <getbattery+0x50>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
je     11bb <getbattery+0x7b>
cmpq   $0x0,-0x18(%rbp)
je     11a4 <getbattery+0x64>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1350 <getbattery+0x210>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121e <getbattery+0xde>
mov    -0x10(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1219 <getbattery+0xd9>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1350 <getbattery+0x210>
jmp    121e <getbattery+0xde>
mov    -0x18(%rbp),%rdi
lea    0xe0d(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1295 <getbattery+0x155>
mov    -0x10(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1290 <getbattery+0x150>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1350 <getbattery+0x210>
jmp    1295 <getbattery+0x155>
mov    -0x18(%rbp),%rdi
lea    0xd96(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x20(%rbp)
jl     12c6 <getbattery+0x186>
cmpl   $0x0,-0x1c(%rbp)
jge    12dd <getbattery+0x19d>
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1350 <getbattery+0x210>
mov    -0x10(%rbp),%rdi
lea    0xd6f(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x44,%eax
jne    1308 <getbattery+0x1c8>
movl   $0x0,-0x24(%rbp)
cvtsi2ssl -0x20(%rbp),%xmm0
cvtsi2ssl -0x1c(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  0xce2(%rip),%xmm1
mulss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
mov    -0x24(%rbp),%ecx
lea    0xd3c(%rip),%rsi
lea    0xd33(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xd19(%rip),%rdi
mov    $0x1,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbattery>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
movl   $0xffffffff,0x4(%rsp)
ff
movl   $0xffffffff,(%rsp)
lea    0xeab(%rip),%rsi
call   1060 <readfile@plt>
test   %rax,%rax
je     1274 <getbattery+0x134>
mov    (%rax),%bl
mov    %rax,%rdi
call   1030 <free@plt>
cmp    $0x31,%bl
jne    1274 <getbattery+0x134>
lea    0xe8b(%rip),%rsi
mov    %r14,%rdi
call   1060 <readfile@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b0 <getbattery+0x70>
lea    0xe87(%rip),%rsi
mov    %r14,%rdi
call   1060 <readfile@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1292 <getbattery+0x152>
lea    0xe7f(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1050 <sscanf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe66(%rip),%rsi
mov    %r14,%rdi
call   1060 <readfile@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11fe <getbattery+0xbe>
lea    0xe5a(%rip),%rsi
mov    %r14,%rdi
call   1060 <readfile@plt>
mov    %rax,%rbx
test   %rax,%rax
je     129b <getbattery+0x15b>
lea    0xe31(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
call   1050 <sscanf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
cmpl   $0x0,(%rsp)
js     1289 <getbattery+0x149>
cmpl   $0x0,0x4(%rsp)
js     1289 <getbattery+0x149>
lea    0xe2b(%rip),%rsi
mov    %r14,%rdi
call   1060 <readfile@plt>
cmpb   $0x44,(%rax)
lea    0xe2e(%rip),%rax
lea    0xe25(%rip),%rsi
cmove  %rax,%rsi
cvtsi2ssl (%rsp),%xmm0
cvtsi2ssl 0x4(%rsp),%xmm1
divss  %xmm1,%xmm0
mulss  0xda0(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdf3(%rip),%rdi
mov    $0x1,%al
call   1040 <smprintf@plt>
jmp    12a9 <getbattery+0x169>
lea    0xd89(%rip),%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <smprintf@plt>
lea    0xdbf(%rip),%rdi
jmp    12a2 <getbattery+0x162>
lea    0xdd0(%rip),%rdi
jmp    127b <getbattery+0x13b>
lea    0xdc7(%rip),%rdi
xor    %eax,%eax
call   1040 <smprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [8 x i8] c"present\00"
@.str.1 = [12 x i8] c"not present\00"
@.str.2 = [19 x i8] c"charge_full_design\00"
@.str.3 = [19 x i8] c"energy_full_design\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [11 x i8] c"charge_now\00"
@.str.7 = [11 x i8] c"energy_now\00"
@.str.8 = [8 x i8] c"invalid\00"
@.str.9 = [7 x i8] c"status\00"
@.str.10 = [12 x i8] c"%.0f % (%s)\00"
@.str.11 = [2 x i8] c"C\00"
@.str.12 = [2 x i8] c"D\00"
define i8* @getbattery(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %5
store i32 -1, i32* %6
store i32 1, i32* %7
%8 = load i8*, i8** %3
%9 = call i8* @readfile(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %18, label %12
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 49
br i1 %17, label %18, label %26
18: ; preds = %12, %1
%19 = load i8*, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = call i32 @free(i8* %22)
br label %24
24: ; preds = %21, %18
%25 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i8* %25, i8** %2
br label %92
26: ; preds = %12
%27 = load i8*, i8** %4
%28 = call i32 @free(i8* %27)
%29 = load i8*, i8** %3
%30 = call i8* @readfile(i8* %29, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %41
33: ; preds = %26
%34 = load i8*, i8** %3
%35 = call i8* @readfile(i8* %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i8* %35, i8** %4
%36 = load i8*, i8** %4
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
store i8* %39, i8** %2
br label %92
40: ; preds = %33
br label %41
41: ; preds = %40, %26
%42 = load i8*, i8** %4
%43 = call i32 @sscanf(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %5)
%44 = load i8*, i8** %4
%45 = call i32 @free(i8* %44)
%46 = load i8*, i8** %3
%47 = call i8* @readfile(i8* %46, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
store i8* %47, i8** %4
%48 = load i8*, i8** %4
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %58
50: ; preds = %41
%51 = load i8*, i8** %3
%52 = call i8* @readfile(i8* %51, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
store i8* %52, i8** %4
%53 = load i8*, i8** %4
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
store i8* %56, i8** %2
br label %92
57: ; preds = %50
br label %58
58: ; preds = %57, %41
%59 = load i8*, i8** %4
%60 = call i32 @sscanf(i8* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %6)
%61 = load i8*, i8** %4
%62 = call i32 @free(i8* %61)
%63 = load i32, i32* %6
%64 = icmp slt i32 %63, 0
br i1 %64, label %68, label %65
65: ; preds = %58
%66 = load i32, i32* %5
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %65, %58
%69 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
store i8* %69, i8** %2
br label %92
70: ; preds = %65
%71 = load i8*, i8** %3
%72 = call i8* @readfile(i8* %71, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
store i8* %72, i8** %4
%73 = load i8*, i8** %4
%74 = getelementptr inbounds i8, i8* %73, i64 0
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp eq i32 %76, 68
br i1 %77, label %78, label %79
78: ; preds = %70
store i32 0, i32* %7
br label %79
79: ; preds = %78, %70
%80 = load i32, i32* %6
%81 = sitofp i32 %80 to float
%82 = load i32, i32* %5
%83 = sitofp i32 %82 to float
%84 = fdiv float %81, %83
%85 = fmul float %84, 1.000000e+02
%86 = fpext float %85 to double
%87 = load i32, i32* %7
%88 = icmp ne i32 %87, 0
%89 = zext i1 %88 to i64
%90 = select i1 %88, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0)
%91 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), double %86, i8* %90)
store i8* %91, i8** %2
br label %92
92: ; preds = %79, %68, %55, %38, %24
%93 = load i8*, i8** %2
ret i8* %93
}

LLVM IR (-O2):
@.str = [8 x i8] c"present\00"
@.str.1 = [12 x i8] c"not present\00"
@.str.2 = [19 x i8] c"charge_full_design\00"
@.str.3 = [19 x i8] c"energy_full_design\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [11 x i8] c"charge_now\00"
@.str.7 = [11 x i8] c"energy_now\00"
@.str.8 = [8 x i8] c"invalid\00"
@.str.9 = [7 x i8] c"status\00"
@.str.10 = [12 x i8] c"%.0f % (%s)\00"
@.str.11 = [2 x i8] c"C\00"
@.str.12 = [2 x i8] c"D\00"
define i8* @getbattery(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 -1, i32* %2
store i32 -1, i32* %3
%6 = tail call i8* @readfile(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = load i8, i8* %6
%10 = icmp eq i8 %9, 49
%11 = tail call i32 @free(i8* %6)
br i1 %10, label %14, label %12
12: ; preds = %8, %1
%13 = tail call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %57
14: ; preds = %8
%15 = tail call i8* @readfile(i8* %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = tail call i8* @readfile(i8* %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
br label %57
22: ; preds = %17, %14
%23 = phi i8* [ %18, %17 ], [ %15, %14 ]
%24 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %2)
%25 = call i32 @free(i8* %23)
%26 = call i8* @readfile(i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = call i8* @readfile(i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
br label %57
33: ; preds = %28, %22
%34 = phi i8* [ %29, %28 ], [ %26, %22 ]
%35 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%36 = call i32 @free(i8* %34)
%37 = load i32, i32* %3
%38 = icmp slt i32 %37, 0
%39 = load i32, i32* %2
%40 = icmp slt i32 %39, 0
%41 = select i1 %38, i1 true, i1 %40
br i1 %41, label %42, label %44
42: ; preds = %33
%43 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
br label %57
44: ; preds = %33
%45 = call i8* @readfile(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
%46 = load i8, i8* %45
%47 = icmp eq i8 %46, 68
%48 = select i1 %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0)
%49 = load i32, i32* %3
%50 = sitofp i32 %49 to float
%51 = load i32, i32* %2
%52 = sitofp i32 %51 to float
%53 = fdiv float %50, %52
%54 = fmul float %53, 1.000000e+02
%55 = fpext float %54 to double
%56 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), double %55, i8* %48)
br label %57
57: ; preds = %44, %42, %31, %20, %12
%58 = phi i8* [ %13, %12 ], [ %21, %20 ], [ %32, %31 ], [ %43, %42 ], [ %56, %44 ]
ret i8* %58
}