#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned long *);
int get_battery_lights(int *urgent) {
  FILE *in;
  unsigned long current, max;
  unsigned per256;
  static int blink = 0;
  in = fopen("/sys/class/power_supply/BAT0/energy_full", "r");
  if (!in)
    goto blink;
  fscanf(in, "%ld", &max);
  fclose(in);
  in = fopen("/sys/class/power_supply/BAT0/energy_now", "r");
  if (!in)
    goto blink;
  fscanf(in, "%ld", &current);
  fclose(in);
  per256 = current / (max >> 8);
  *urgent = 0;
  if (per256 == 256)
    return 0x7;
  else if (per256 > 16)
    return per256 >> 5;
  else {
  blink:
    *urgent = 1;
    blink = !blink;
    return blink ? 0x7 : 0x0;
  }
}


x86 assembly (clang, -O0):
<get_battery_lights>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xebd(%rip),%rdi
lea    0xedf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1163 <get_battery_lights+0x33>
jmp    1218 <get_battery_lights+0xe8>
mov    -0x18(%rbp),%rdi
lea    0xebd(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xea8(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a7 <get_battery_lights+0x77>
jmp    1218 <get_battery_lights+0xe8>
mov    -0x18(%rbp),%rdi
lea    0xe79(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
shr    $0x8,%rcx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0x100,-0x2c(%rbp)
jne    11fb <get_battery_lights+0xcb>
movl   $0x7,-0x4(%rbp)
jmp    124f <get_battery_lights+0x11f>
cmpl   $0x10,-0x2c(%rbp)
jbe    1213 <get_battery_lights+0xe3>
mov    -0x2c(%rbp),%eax
shr    $0x5,%eax
mov    %eax,-0x4(%rbp)
jmp    124f <get_battery_lights+0x11f>
jmp    1218 <get_battery_lights+0xe8>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
cmpl   $0x0,0x2e13(%rip)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,0x2e03(%rip)
mov    0x2dfd(%rip),%edx
xor    %eax,%eax
mov    $0x7,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_battery_lights>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0xebf(%rip),%rdi
lea    0xee1(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11f0 <get_battery_lights+0xc0>
mov    %rax,%rbx
lea    0xecb(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xeb3(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11f0 <get_battery_lights+0xc0>
mov    %rax,%rbx
lea    0xe94(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x10(%rsp),%rax
mov    0x8(%rsp),%rcx
shr    $0x8,%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     11cd <get_battery_lights+0x9d>
xor    %edx,%edx
div    %rcx
jmp    11d1 <get_battery_lights+0xa1>
xor    %edx,%edx
div    %ecx
movl   $0x0,(%r14)
mov    $0x7,%ecx
cmp    $0x100,%eax
je     1213 <get_battery_lights+0xe3>
cmp    $0x11,%eax
jb     11f0 <get_battery_lights+0xc0>
shr    $0x5,%eax
mov    %eax,%ecx
jmp    1213 <get_battery_lights+0xe3>
movl   $0x1,(%r14)
xor    %eax,%eax
xor    %ecx,%ecx
cmpl   $0x0,0x2e3a(%rip)
sete   %cl
mov    %ecx,0x2e31(%rip)
mov    $0x7,%ecx
cmovne %eax,%ecx
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@get_battery_lights.blink = internal global i32 0
@.str = [41 x i8] c"/sys/class/power_supply/BAT0/energy_full\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%ld\00"
@.str.3 = [40 x i8] c"/sys/class/power_supply/BAT0/energy_now\00"
define i32 @get_battery_lights(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i32* %0, i32** %3
%8 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
br label %42
12: ; preds = %1
%13 = load i32*, i32** %4
%14 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %6)
%15 = load i32*, i32** %4
%16 = call i32 @fclose(i32* %15)
%17 = call i32* @fopen(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %4
%18 = load i32*, i32** %4
%19 = icmp ne i32* %18, null
br i1 %19, label %21, label %20
20: ; preds = %12
br label %42
21: ; preds = %12
%22 = load i32*, i32** %4
%23 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %5)
%24 = load i32*, i32** %4
%25 = call i32 @fclose(i32* %24)
%26 = load i64, i64* %5
%27 = load i64, i64* %6
%28 = lshr i64 %27, 8
%29 = udiv i64 %26, %28
%30 = trunc i64 %29 to i32
store i32 %30, i32* %7
%31 = load i32*, i32** %3
store i32 0, i32* %31
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, 256
br i1 %33, label %34, label %35
34: ; preds = %21
store i32 7, i32* %2
br label %52
35: ; preds = %21
%36 = load i32, i32* %7
%37 = icmp ugt i32 %36, 16
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* %7
%40 = lshr i32 %39, 5
store i32 %40, i32* %2
br label %52
41: ; preds = %35
br label %42
42: ; preds = %41, %20, %11
%43 = load i32*, i32** %3
store i32 1, i32* %43
%44 = load i32, i32* @get_battery_lights.blink
%45 = icmp ne i32 %44, 0
%46 = xor i1 %45, true
%47 = zext i1 %46 to i32
store i32 %47, i32* @get_battery_lights.blink
%48 = load i32, i32* @get_battery_lights.blink
%49 = icmp ne i32 %48, 0
%50 = zext i1 %49 to i64
%51 = select i1 %49, i32 7, i32 0
store i32 %51, i32* %2
br label %52
52: ; preds = %42, %38, %34
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
@get_battery_lights.blink = internal unnamed_addr global i32 0
@.str = [41 x i8] c"/sys/class/power_supply/BAT0/energy_full\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%ld\00"
@.str.3 = [40 x i8] c"/sys/class/power_supply/BAT0/energy_now\00"
define i32 @get_battery_lights(i32* nocapture writeonly %0) {
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i64* %2 to i8*
%5 = bitcast i64* %3 to i8*
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %26, label %8
8: ; preds = %1
%9 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %3)
%10 = call i32 @fclose(i32* %6)
%11 = call i32* @fopen(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %26, label %13
13: ; preds = %8
%14 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%15 = call i32 @fclose(i32* %11)
%16 = load i64, i64* %2
%17 = load i64, i64* %3
%18 = lshr i64 %17, 8
%19 = udiv i64 %16, %18
%20 = trunc i64 %19 to i32
store i32 0, i32* %0
%21 = icmp eq i32 %20, 256
br i1 %21, label %31, label %22
22: ; preds = %13
%23 = icmp ugt i32 %20, 16
br i1 %23, label %24, label %26
24: ; preds = %22
%25 = lshr i32 %20, 5
br label %31
26: ; preds = %22, %8, %1
store i32 1, i32* %0
%27 = load i32, i32* @get_battery_lights.blink
%28 = icmp eq i32 %27, 0
%29 = zext i1 %28 to i32
store i32 %29, i32* @get_battery_lights.blink
%30 = select i1 %28, i32 7, i32 0
br label %31
31: ; preds = %13, %26, %24
%32 = phi i32 [ %25, %24 ], [ %30, %26 ], [ 7, %13 ]
ret i32 %32
}