#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;
  descap = -1;
  remcap = -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");
  return smprintf("%.0f", ((float)remcap / (float)descap) * 100);
}


x86 assembly (clang, -O0):
<getbattery>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea3(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1189 <getbattery+0x49>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
je     11b4 <getbattery+0x74>
cmpq   $0x0,-0x18(%rbp)
je     119d <getbattery+0x5d>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1306 <getbattery+0x1c6>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1217 <getbattery+0xd7>
mov    -0x10(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1212 <getbattery+0xd2>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1306 <getbattery+0x1c6>
jmp    1217 <getbattery+0xd7>
mov    -0x18(%rbp),%rdi
lea    0xe14(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    128e <getbattery+0x14e>
mov    -0x10(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1289 <getbattery+0x149>
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1306 <getbattery+0x1c6>
jmp    128e <getbattery+0x14e>
mov    -0x18(%rbp),%rdi
lea    0xd9d(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x20(%rbp)
jl     12bf <getbattery+0x17f>
cmpl   $0x0,-0x1c(%rbp)
jge    12d6 <getbattery+0x196>
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1306 <getbattery+0x1c6>
cvtsi2ssl -0x20(%rbp),%xmm0
cvtsi2ssl -0x1c(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  0xd14(%rip),%xmm1
mulss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xd5c(%rip),%rdi
mov    $0x1,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%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     124b <getbattery+0x10b>
mov    (%rax),%bl
mov    %rax,%rdi
call   1030 <free@plt>
cmp    $0x31,%bl
jne    124b <getbattery+0x10b>
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     1269 <getbattery+0x129>
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    11fa <getbattery+0xba>
lea    0xe5a(%rip),%rsi
mov    %r14,%rdi
call   1060 <readfile@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1272 <getbattery+0x132>
lea    0xe35(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
call   1050 <sscanf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%rsp),%eax
test   %eax,%eax
js     1260 <getbattery+0x120>
mov    0x4(%rsp),%ecx
test   %ecx,%ecx
js     1260 <getbattery+0x120>
cvtsi2ss %eax,%xmm0
cvtsi2ss %ecx,%xmm1
divss  %xmm1,%xmm0
mulss  0xdc9(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe15(%rip),%rdi
mov    $0x1,%al
call   1040 <smprintf@plt>
jmp    1280 <getbattery+0x140>
lea    0xdb2(%rip),%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <smprintf@plt>
lea    0xde8(%rip),%rdi
jmp    1279 <getbattery+0x139>
lea    0xdc8(%rip),%rdi
jmp    1252 <getbattery+0x112>
lea    0xdbf(%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 = [5 x i8] c"%.0f\00"
define i8* @getbattery(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %5
store i32 -1, i32* %6
%7 = load i8*, i8** %3
%8 = call i8* @readfile(i8* %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %17, label %11
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 49
br i1 %16, label %17, label %25
17: ; preds = %11, %1
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i8*, i8** %4
%22 = call i32 @free(i8* %21)
br label %23
23: ; preds = %20, %17
%24 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i8* %24, i8** %2
br label %78
25: ; preds = %11
%26 = load i8*, i8** %4
%27 = call i32 @free(i8* %26)
%28 = load i8*, i8** %3
%29 = call i8* @readfile(i8* %28, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i8* %29, i8** %4
%30 = load i8*, i8** %4
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %40
32: ; preds = %25
%33 = load i8*, i8** %3
%34 = call i8* @readfile(i8* %33, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i8* %34, i8** %4
%35 = load i8*, i8** %4
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
store i8* %38, i8** %2
br label %78
39: ; preds = %32
br label %40
40: ; preds = %39, %25
%41 = load i8*, i8** %4
%42 = call i32 @sscanf(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %5)
%43 = load i8*, i8** %4
%44 = call i32 @free(i8* %43)
%45 = load i8*, i8** %3
%46 = call i8* @readfile(i8* %45, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
store i8* %46, i8** %4
%47 = load i8*, i8** %4
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %57
49: ; preds = %40
%50 = load i8*, i8** %3
%51 = call i8* @readfile(i8* %50, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
store i8* %51, i8** %4
%52 = load i8*, i8** %4
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
store i8* %55, i8** %2
br label %78
56: ; preds = %49
br label %57
57: ; preds = %56, %40
%58 = load i8*, i8** %4
%59 = call i32 @sscanf(i8* %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %6)
%60 = load i8*, i8** %4
%61 = call i32 @free(i8* %60)
%62 = load i32, i32* %6
%63 = icmp slt i32 %62, 0
br i1 %63, label %67, label %64
64: ; preds = %57
%65 = load i32, i32* %5
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %64, %57
%68 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
store i8* %68, i8** %2
br label %78
69: ; preds = %64
%70 = load i32, i32* %6
%71 = sitofp i32 %70 to float
%72 = load i32, i32* %5
%73 = sitofp i32 %72 to float
%74 = fdiv float %71, %73
%75 = fmul float %74, 1.000000e+02
%76 = fpext float %75 to double
%77 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), double %76)
store i8* %77, i8** %2
br label %78
78: ; preds = %69, %67, %54, %37, %23
%79 = load i8*, i8** %2
ret i8* %79
}

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 = [5 x i8] c"%.0f\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 %51
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 %51
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 %51
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 %51
44: ; preds = %33
%45 = sitofp i32 %37 to float
%46 = sitofp i32 %39 to float
%47 = fdiv float %45, %46
%48 = fmul float %47, 1.000000e+02
%49 = fpext float %48 to double
%50 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), double %49)
br label %51
51: ; preds = %44, %42, %31, %20, %12
%52 = phi i8* [ %13, %12 ], [ %21, %20 ], [ %32, %31 ], [ %43, %42 ], [ %50, %44 ]
ret i8* %52
}