#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 *, ...);
char *getbattery(char *base) {
  char *co;
  int capacity;
  char status;
  int wattage;
  if ((co = readfile(base, "capacity")) == NULL) {
    return smprintf("");
  }
  sscanf(co, "%d", &capacity);
  free(co);
  if ((co = readfile(base, "status")) == NULL) {
    return smprintf("");
  }
  sscanf(co, "%c", &status);
  free(co);
  if ((co = readfile(base, "power_now")) == NULL) {
    return smprintf("");
  }
  sscanf(co, "%d", &wattage);
  free(co);
  return smprintf("%c %d%% (%.1fW)", status, capacity,
                  (double)wattage / 1000000.0);
}


x86 assembly (clang, -O0):
<getbattery>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1181 <getbattery+0x41>
lea    0xec6(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1272 <getbattery+0x132>
mov    -0x18(%rbp),%rdi
lea    0xe85(%rip),%rsi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1050 <sscanf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d5 <getbattery+0x95>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1272 <getbattery+0x132>
mov    -0x18(%rbp),%rdi
lea    0xe3b(%rip),%rsi
lea    -0x1d(%rbp),%rdx
mov    $0x0,%al
call   1050 <sscanf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   1060 <readfile@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1229 <getbattery+0xe9>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1272 <getbattery+0x132>
mov    -0x18(%rbp),%rdi
lea    0xddd(%rip),%rsi
lea    -0x24(%rbp),%rdx
mov    $0x0,%al
call   1050 <sscanf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movsbl -0x1d(%rbp),%esi
mov    -0x1c(%rbp),%edx
cvtsi2sdl -0x24(%rbp),%xmm0
movsd  0xda4(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xdc1(%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
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0xeb7(%rip),%rsi
call   1060 <readfile@plt>
test   %rax,%rax
je     1215 <getbattery+0xd5>
mov    %rax,%rbx
lea    0xea8(%rip),%rsi
lea    0x14(%rsp),%rdx
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <sscanf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe8d(%rip),%rsi
mov    %r14,%rdi
call   1060 <readfile@plt>
test   %rax,%rax
je     122a <getbattery+0xea>
mov    %rax,%rbx
lea    0xe79(%rip),%rsi
lea    0xf(%rsp),%rdx
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <sscanf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe5e(%rip),%rsi
mov    %r14,%rdi
call   1060 <readfile@plt>
test   %rax,%rax
je     122a <getbattery+0xea>
mov    %rax,%rbx
lea    0xe3a(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <sscanf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
movsbl 0xf(%rsp),%esi
mov    0x14(%rsp),%edx
cvtsi2sdl 0x10(%rsp),%xmm0
divsd  0xdfb(%rip),%xmm0
lea    0xe1c(%rip),%rdi
mov    $0x1,%al
call   1040 <smprintf@plt>
jmp    1238 <getbattery+0xf8>
lea    0xe1b(%rip),%rdi
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
jmp    1040 <smprintf@plt>
lea    0xe06(%rip),%rdi
xor    %eax,%eax
call   1040 <smprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [9 x i8] c"capacity\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [7 x i8] c"status\00"
@.str.4 = [3 x i8] c"%c\00"
@.str.5 = [10 x i8] c"power_now\00"
@.str.6 = [16 x i8] c"%c %d%% (%.1fW)\00"
define i8* @getbattery(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @readfile(i8* %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i8* %12, i8** %2
br label %45
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = call i32 (i8*, i8*, ...) @sscanf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%16 = load i8*, i8** %4
%17 = call i32 @free(i8* %16)
%18 = load i8*, i8** %3
%19 = call i8* @readfile(i8* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i8* %19, i8** %4
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i8* %22, i8** %2
br label %45
23: ; preds = %13
%24 = load i8*, i8** %4
%25 = call i32 (i8*, i8*, ...) @sscanf(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%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 ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
store i8* %29, i8** %4
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i8* %32, i8** %2
br label %45
33: ; preds = %23
%34 = load i8*, i8** %4
%35 = call i32 (i8*, i8*, ...) @sscanf(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%36 = load i8*, i8** %4
%37 = call i32 @free(i8* %36)
%38 = load i8, i8* %6
%39 = sext i8 %38 to i32
%40 = load i32, i32* %5
%41 = load i32, i32* %7
%42 = sitofp i32 %41 to double
%43 = fdiv double %42, 1.000000e+06
%44 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %39, i32 %40, double %43)
store i8* %44, i8** %2
br label %45
45: ; preds = %33, %31, %21, %11
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@.str = [9 x i8] c"capacity\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [7 x i8] c"status\00"
@.str.4 = [3 x i8] c"%c\00"
@.str.5 = [10 x i8] c"power_now\00"
@.str.6 = [16 x i8] c"%c %d%% (%.1fW)\00"
define i8* @getbattery(i8* %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %4 to i8*
%7 = tail call i8* @readfile(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %35
11: ; preds = %1
%12 = call i32 (i8*, i8*, ...) @sscanf(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%13 = call i32 @free(i8* %7)
%14 = call i8* @readfile(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %35
18: ; preds = %11
%19 = call i32 (i8*, i8*, ...) @sscanf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %3)
%20 = call i32 @free(i8* %14)
%21 = call i8* @readfile(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %35
25: ; preds = %18
%26 = call i32 (i8*, i8*, ...) @sscanf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%27 = call i32 @free(i8* %21)
%28 = load i8, i8* %3
%29 = sext i8 %28 to i32
%30 = load i32, i32* %2
%31 = load i32, i32* %4
%32 = sitofp i32 %31 to double
%33 = fdiv double %32, 1.000000e+06
%34 = call i8* (i8*, ...) @smprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %29, i32 %30, double %33)
br label %35
35: ; preds = %25, %23, %16, %9
%36 = phi i8* [ %10, %9 ], [ %17, %16 ], [ %24, %23 ], [ %34, %25 ]
ret i8* %36
}