#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 /*<<< orphan*/ temp;
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int const);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int read(int const, char *, int);
int /*<<< orphan*/ stderr;
char *strdup(char *);
char *battery_level() {
  char temp[1025];
  const int fd = open("/sys/class/power_supply/BAT0/capacity", O_RDONLY);
  if (-1 == fd) {
    fprintf(stderr, "open failed!\n");
    return NULL;
  }
  const ssize_t r = read(fd, temp, sizeof(temp) - 1);
  if (-1 == r || r == sizeof(temp) - 1) {
    fprintf(stderr, "read failed\n");
    close(fd);
    return NULL;
  }
  assert(r >= 0 && r < sizeof(temp) - 1);
  if (0 == r) {
    return strdup("");
  }
  assert(r > 1);
  assert('\n' == temp[r - 1]);
  temp[r - 1] = 0;
  close(fd);
  return strdup(temp);
}


x86 assembly (clang, -O0):
<battery_level>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x414(%rbp)
mov    $0xffffffff,%eax
cmp    -0x414(%rbp),%eax
jne    11b9 <battery_level+0x59>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe7f(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ee <battery_level+0x18e>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x418(%rbp)
mov    $0xffffffff,%eax
cmp    -0x418(%rbp),%eax
je     11fa <battery_level+0x9a>
movslq -0x418(%rbp),%rax
cmp    $0x400,%rax
jne    1227 <battery_level+0xc7>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x414(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ee <battery_level+0x18e>
xor    %eax,%eax
cmpl   $0x0,-0x418(%rbp)
mov    %al,-0x419(%rbp)
jl     1252 <battery_level+0xf2>
movslq -0x418(%rbp),%rax
cmp    $0x400,%rax
setb   %al
mov    %al,-0x419(%rbp)
mov    -0x419(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
xor    %eax,%eax
cmp    -0x418(%rbp),%eax
jne    1285 <battery_level+0x125>
lea    0xdbc(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12ee <battery_level+0x18e>
cmpl   $0x1,-0x418(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x418(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%ecx
ff
mov    $0xa,%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x418(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x414(%rbp),%edi
call   1030 <close@plt>
lea    -0x410(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<battery_level>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11c7 <battery_level+0x67>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
cmp    $0x400,%eax
je     11a7 <battery_level+0x47>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11e0 <battery_level+0x80>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
jmp    122c <battery_level+0xcc>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
jmp    122c <battery_level+0xcc>
xor    %edi,%edi
cmp    $0x400,%ebp
setb   %dil
call   1050 <assert@plt>
test   %ebp,%ebp
je     1238 <battery_level+0xd8>
xor    %edi,%edi
cmp    $0x2,%ebp
setge  %dil
call   1050 <assert@plt>
movslq %ebp,%rbp
xor    %edi,%edi
cmpb   $0xa,-0x1(%rbp,%r14,1)
sete   %dil
call   1050 <assert@plt>
movb   $0x0,-0x1(%rbp,%r14,1)
mov    %ebx,%edi
call   1030 <close@plt>
mov    %r14,%rdi
call   1080 <strdup@plt>
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf4(%rip),%rdi
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <strdup@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"/sys/class/power_supply/BAT0/capacity\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [14 x i8] c"open failed!\0A\00"
@.str.2 = [13 x i8] c"read failed\0A\00"
@.str.3 = [1 x i8] zeroinitializer
define i8* @battery_level() {
%1 = alloca i8*
%2 = alloca [1025 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp eq i32 -1, %7
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %1
br label %64
12: ; preds = %0
%13 = load i32, i32* %3
%14 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 1024)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp eq i32 -1, %16
br i1 %17, label %22, label %18
18: ; preds = %12
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = icmp eq i64 %20, 1024
br i1 %21, label %22, label %27
22: ; preds = %18, %12
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
store i8* null, i8** %1
br label %64
27: ; preds = %18
%28 = load i32, i32* %4
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = icmp ult i64 %32, 1024
br label %34
34: ; preds = %30, %27
%35 = phi i1 [ false, %27 ], [ %33, %30 ]
%36 = zext i1 %35 to i32
%37 = call i32 @assert(i32 %36)
%38 = load i32, i32* %4
%39 = icmp eq i32 0, %38
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
store i8* %41, i8** %1
br label %64
42: ; preds = %34
%43 = load i32, i32* %4
%44 = icmp sgt i32 %43, 1
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
%47 = load i32, i32* %4
%48 = sub nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 10, %52
%54 = zext i1 %53 to i32
%55 = call i32 @assert(i32 %54)
%56 = load i32, i32* %4
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 %58
store i8 0, i8* %59
%60 = load i32, i32* %3
%61 = call i32 @close(i32 %60)
%62 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%63 = call i8* @strdup(i8* %62)
store i8* %63, i8** %1
br label %64
64: ; preds = %42, %40, %22, %9
%65 = load i8*, i8** %1
ret i8* %65
}

LLVM IR (-O2):
@.str = [38 x i8] c"/sys/class/power_supply/BAT0/capacity\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [14 x i8] c"open failed!\0A\00"
@.str.2 = [13 x i8] c"read failed\0A\00"
@.str.3 = [1 x i8] zeroinitializer
define noalias i8* @battery_level() {
%1 = alloca [1025 x i8]
%2 = getelementptr inbounds [1025 x i8], [1025 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %35
9: ; preds = %0
%10 = call i32 @read(i32 %4, i8* %2, i32 1024)
switch i32 %10, label %15 [
i32 -1, label %11
i32 1024, label %11
]
11: ; preds = %9, %9
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @close(i32 %4)
br label %35
15: ; preds = %9
%16 = icmp ult i32 %10, 1024
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = icmp eq i32 %10, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
br label %35
22: ; preds = %15
%23 = icmp sgt i32 %10, 1
%24 = zext i1 %23 to i32
%25 = tail call i32 @assert(i32 %24)
%26 = add nsw i32 %10, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1025 x i8], [1025 x i8]* %1, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 10
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
store i8 0, i8* %28
%33 = tail call i32 @close(i32 %4)
%34 = call i8* @strdup(i8* %2)
br label %35
35: ; preds = %11, %20, %22, %6
%36 = phi i8* [ null, %6 ], [ null, %11 ], [ %21, %20 ], [ %34, %22 ]
ret i8* %36
}