#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*/ printf(char *, ...);
int read_file(char *, char *, int);
int string_compare(char *, char *);
long string_to_int(char *);
int print_info() {
  char content[16];
  long current;
  long maximum;
  long percent;
  int ret;
  ret = read_file("/sys/class/power_supply/"
                  "BAT0"
                  "/"
                  "charge"
                  "_now",
                  content, 16);
  if (ret == 1)
    read_file("/sys/class/power_supply/"
              "BAT0"
              "/"
              "energy"
              "_now",
              content, 16);
  current = string_to_int(content);
  ret = read_file("/sys/class/power_supply/"
                  "BAT0"
                  "/"
                  "charge"
                  "_full",
                  content, 16);
  if (ret == 1)
    read_file("/sys/class/power_supply/"
              "BAT0"
              "/"
              "energy"
              "_full",
              content, 16);
  maximum = string_to_int(content);
  read_file("/sys/class/power_supply/"
            "BAT0"
            "/status",
            content, 16);
  if (string_compare(content, "Charging\0") == -1)
    printf("  up"
           ", ");
  else if (string_compare(content, "Discharging\0") == -1)
    printf("down"
           ", ");
  else
    printf("full"
           ", ");
  percent = 100 * current / maximum;
  printf("%ld%%\n", percent);
  return 0;
}


x86 assembly (clang, -O0):
<print_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x10(%rbp),%rsi
lea    0xead(%rip),%rdi
mov    $0x10,%edx
call   1060 <read_file@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jne    117f <print_info+0x3f>
lea    -0x10(%rbp),%rsi
lea    0xeb3(%rip),%rdi
mov    $0x10,%edx
call   1060 <read_file@plt>
lea    -0x10(%rbp),%rdi
call   1050 <string_to_int@plt>
mov    %rax,-0x18(%rbp)
lea    -0x10(%rbp),%rsi
lea    0xeb9(%rip),%rdi
mov    $0x10,%edx
call   1060 <read_file@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jne    11c3 <print_info+0x83>
lea    -0x10(%rbp),%rsi
lea    0xec0(%rip),%rdi
mov    $0x10,%edx
call   1060 <read_file@plt>
lea    -0x10(%rbp),%rdi
call   1050 <string_to_int@plt>
mov    %rax,-0x20(%rbp)
lea    -0x10(%rbp),%rsi
lea    0xec7(%rip),%rdi
mov    $0x10,%edx
call   1060 <read_file@plt>
lea    -0x10(%rbp),%rdi
lea    0xef2(%rip),%rsi
call   1030 <string_compare@plt>
cmp    $0xffffffff,%eax
jne    1211 <print_info+0xd1>
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1250 <print_info+0x110>
lea    -0x10(%rbp),%rdi
lea    0xed0(%rip),%rsi
call   1030 <string_compare@plt>
cmp    $0xffffffff,%eax
jne    123d <print_info+0xfd>
lea    0xe9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    124b <print_info+0x10b>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1250 <print_info+0x110>
imul   $0x64,-0x18(%rbp),%rax
cqto
idivq  -0x20(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_info>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xeb0(%rip),%rdi
mov    %rsp,%rsi
mov    $0x10,%edx
call   1060 <read_file@plt>
cmp    $0x1,%eax
jne    1176 <print_info+0x36>
lea    0xebf(%rip),%rdi
mov    %rsp,%rsi
mov    $0x10,%edx
call   1060 <read_file@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <string_to_int@plt>
mov    %rax,%r14
lea    0xec5(%rip),%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1060 <read_file@plt>
cmp    $0x1,%eax
jne    11b1 <print_info+0x71>
lea    0xed5(%rip),%rdi
mov    %rsp,%rsi
mov    $0x10,%edx
call   1060 <read_file@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <string_to_int@plt>
mov    %rax,%r15
lea    0xedc(%rip),%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1060 <read_file@plt>
lea    0xf08(%rip),%rsi
mov    %rbx,%rdi
call   1030 <string_compare@plt>
cmp    $0xffffffff,%eax
je     120d <print_info+0xcd>
lea    0xefe(%rip),%rsi
mov    %rsp,%rdi
call   1030 <string_compare@plt>
cmp    $0xffffffff,%eax
lea    0xecd(%rip),%rax
lea    0xecd(%rip),%rdi
cmove  %rax,%rdi
jmp    1214 <print_info+0xd4>
lea    0xeb2(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
imul   $0x64,%r14,%rax
mov    %r15,%rcx
or     %rax,%rcx
shr    $0x20,%rcx
je     1232 <print_info+0xf2>
cqto
idiv   %r15
jmp    1237 <print_info+0xf7>
xor    %edx,%edx
div    %r15d
lea    0xe9d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [40 x i8] c"/sys/class/power_supply/BAT0/charge_now\00"
@.str.1 = [40 x i8] c"/sys/class/power_supply/BAT0/energy_now\00"
@.str.2 = [41 x i8] c"/sys/class/power_supply/BAT0/charge_full\00"
@.str.3 = [41 x i8] c"/sys/class/power_supply/BAT0/energy_full\00"
@.str.4 = [36 x i8] c"/sys/class/power_supply/BAT0/status\00"
@.str.5 = [10 x i8] c"Charging\00\00"
@.str.6 = [7 x i8] c" up, \00"
@.str.7 = [13 x i8] c"Discharging\00\00"
@.str.8 = [7 x i8] c"down, \00"
@.str.9 = [7 x i8] c"full, \00"
@.str.10 = [7 x i8] c"%ld%%\0A\00"
define i32 @print_info() {
%1 = alloca [16 x i8]
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%7 = call i32 @read_file(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %6, i32 16)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%12 = call i32 @read_file(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %11, i32 16)
br label %13
13: ; preds = %10, %0
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%15 = call i64 @string_to_int(i8* %14)
store i64 %15, i64* %2
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%17 = call i32 @read_file(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %16, i32 16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%22 = call i32 @read_file(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %21, i32 16)
br label %23
23: ; preds = %20, %13
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%25 = call i64 @string_to_int(i8* %24)
store i64 %25, i64* %3
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%27 = call i32 @read_file(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %26, i32 16)
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%29 = call i32 @string_compare(i8* %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %42
33: ; preds = %23
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%35 = call i32 @string_compare(i8* %34, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %41
39: ; preds = %33
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
br label %41
41: ; preds = %39, %37
br label %42
42: ; preds = %41, %31
%43 = load i64, i64* %2
%44 = mul nsw i64 100, %43
%45 = load i64, i64* %3
%46 = sdiv i64 %44, %45
store i64 %46, i64* %4
%47 = load i64, i64* %4
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0), i64 %47)
ret i32 0
}

LLVM IR (-O2):
@.str = [40 x i8] c"/sys/class/power_supply/BAT0/charge_now\00"
@.str.1 = [40 x i8] c"/sys/class/power_supply/BAT0/energy_now\00"
@.str.2 = [41 x i8] c"/sys/class/power_supply/BAT0/charge_full\00"
@.str.3 = [41 x i8] c"/sys/class/power_supply/BAT0/energy_full\00"
@.str.4 = [36 x i8] c"/sys/class/power_supply/BAT0/status\00"
@.str.5 = [10 x i8] c"Charging\00\00"
@.str.6 = [7 x i8] c" up, \00"
@.str.7 = [13 x i8] c"Discharging\00\00"
@.str.8 = [7 x i8] c"down, \00"
@.str.9 = [7 x i8] c"full, \00"
@.str.10 = [7 x i8] c"%ld%%\0A\00"
define i32 @print_info() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = call i32 @read_file(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %2, i32 16)
%4 = icmp eq i32 %3, 1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @read_file(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %2, i32 16)
br label %7
7: ; preds = %5, %0
%8 = call i64 @string_to_int(i8* %2)
%9 = call i32 @read_file(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %2, i32 16)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @read_file(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %2, i32 16)
br label %13
13: ; preds = %11, %7
%14 = call i64 @string_to_int(i8* %2)
%15 = call i32 @read_file(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %2, i32 16)
%16 = call i32 @string_compare(i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%17 = icmp eq i32 %16, -1
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = call i32 @string_compare(i8* %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%20 = icmp eq i32 %19, -1
%21 = select i1 %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0)
br label %22
22: ; preds = %18, %13
%23 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), %13 ], [ %21, %18 ]
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %23)
%25 = mul nsw i64 %8, 100
%26 = sdiv i64 %25, %14
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0), i64 %26)
ret i32 0
}