#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 uint8_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int readint(char *);
char *readstring(char *);
char *smprintf(char *, char *, int);
char *getbat(void) {
  char *status = readstring("/sys/class/power_supply/BAT0"
                            "/status");
  char *result = NULL;
  int now_state = readint("/sys/class/power_supply/BAT0"
                          "/charge_now") /
                  10000;
  int now_full = readint("/sys/class/power_supply/BAT0"
                         "/charge_full") /
                 10000;
  int now_design = readint("/sys/class/power_supply/BAT0"
                           "/charge_full_design") /
                   10000;
  uint8_t bat_proc = 0;
  if (now_state > now_full)
    bat_proc = ((now_state * 100) / now_design);
  else
    bat_proc = ((now_state * 100) / now_full);
  result = smprintf("bat: %s %d%%", status, bat_proc);
  free(status);
  return result;
}


x86 assembly (clang, -O0):
<getbat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
call   1060 <readstring@plt>
mov    %rax,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
lea    0xebd(%rip),%rdi
call   1050 <readint@plt>
mov    $0x2710,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
lea    0xece(%rip),%rdi
call   1050 <readint@plt>
mov    $0x2710,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
lea    0xee0(%rip),%rdi
call   1050 <readint@plt>
mov    $0x2710,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    11c8 <getbat+0x88>
imul   $0x64,-0x14(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
mov    %eax,-0x20(%rbp)
jmp    11d3 <getbat+0x93>
imul   $0x64,-0x14(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%edx
lea    0xec4(%rip),%rdi
call   1040 <smprintf@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbat>:
push   %r15
push   %r14
push   %rbx
lea    0xeb4(%rip),%rdi
call   1060 <readstring@plt>
mov    %rax,%r14
lea    0xec9(%rip),%rdi
call   1050 <readint@plt>
cltq
imul   $0x68db8bad,%rax,%r15
mov    %r15,%rax
shr    $0x3f,%rax
sar    $0x2c,%r15
add    %eax,%r15d
lea    0xece(%rip),%rdi
call   1050 <readint@plt>
cltq
imul   $0x68db8bad,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x2c,%rbx
add    %eax,%ebx
lea    0xed5(%rip),%rdi
call   1050 <readint@plt>
cmp    %ebx,%r15d
jle    11c0 <getbat+0x80>
cltq
imul   $0x68db8bad,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x2c,%rbx
add    %eax,%ebx
imul   $0x64,%r15d,%eax
cltd
idiv   %ebx
lea    0xed7(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <smprintf@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [36 x i8] c"/sys/class/power_supply/BAT0/status\00"
@.str.1 = [40 x i8] c"/sys/class/power_supply/BAT0/charge_now\00"
@.str.2 = [41 x i8] c"/sys/class/power_supply/BAT0/charge_full\00"
@.str.3 = [48 x i8] c"/sys/class/power_supply/BAT0/charge_full_design\00"
@.str.4 = [13 x i8] c"bat: %s %d%%\00"
define i8* @getbat() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = call i8* @readstring(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %1
store i8* null, i8** %2
%8 = call i32 @readint(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%9 = sdiv i32 %8, 10000
store i32 %9, i32* %3
%10 = call i32 @readint(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%11 = sdiv i32 %10, 10000
store i32 %11, i32* %4
%12 = call i32 @readint(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
%13 = sdiv i32 %12, 10000
store i32 %13, i32* %5
store i32 0, i32* %6
%14 = load i32, i32* %3
%15 = load i32, i32* %4
%16 = icmp sgt i32 %14, %15
br i1 %16, label %17, label %22
17: ; preds = %0
%18 = load i32, i32* %3
%19 = mul nsw i32 %18, 100
%20 = load i32, i32* %5
%21 = sdiv i32 %19, %20
store i32 %21, i32* %6
br label %27
22: ; preds = %0
%23 = load i32, i32* %3
%24 = mul nsw i32 %23, 100
%25 = load i32, i32* %4
%26 = sdiv i32 %24, %25
store i32 %26, i32* %6
br label %27
27: ; preds = %22, %17
%28 = load i8*, i8** %1
%29 = load i32, i32* %6
%30 = call i8* @smprintf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %28, i32 %29)
store i8* %30, i8** %2
%31 = load i8*, i8** %1
%32 = call i32 @free(i8* %31)
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
@.str = [36 x i8] c"/sys/class/power_supply/BAT0/status\00"
@.str.1 = [40 x i8] c"/sys/class/power_supply/BAT0/charge_now\00"
@.str.2 = [41 x i8] c"/sys/class/power_supply/BAT0/charge_full\00"
@.str.3 = [48 x i8] c"/sys/class/power_supply/BAT0/charge_full_design\00"
@.str.4 = [13 x i8] c"bat: %s %d%%\00"
define i8* @getbat() {
%1 = tail call i8* @readstring(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @readint(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%3 = sdiv i32 %2, 10000
%4 = tail call i32 @readint(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%5 = sdiv i32 %4, 10000
%6 = tail call i32 @readint(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
%7 = icmp sgt i32 %3, %5
%8 = sdiv i32 %6, 10000
%9 = select i1 %7, i32 %8, i32 %5
%10 = mul nsw i32 %3, 100
%11 = sdiv i32 %10, %9
%12 = tail call i8* @smprintf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %1, i32 %11)
%13 = tail call i32 @free(i8* %1)
ret i8* %12
}