#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 *, int *);
int /*<<< orphan*/ perror(char *);
int power_online(void) {
  FILE *f = fopen("/sys/class/power_supply/usb/online", "r");
  if (!f) {
    perror("Unable to open USB sysfs file");
    return -1;
  }
  int online;
  fscanf(f, "%i", &online);
  fclose(f);
  if (online)
    return online;
  f = fopen("/sys/class/power_supply/dc/online", "r");
  if (!f) {
    perror("Unable to open USB sysfs file");
    return -1;
  }
  fscanf(f, "%i", &online);
  fclose(f);
  return online;
}


x86 assembly (clang, -O0):
<power_online>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
lea    0xecd(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1182 <power_online+0x42>
lea    0xeb4(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <power_online+0xd1>
mov    -0x10(%rbp),%rdi
lea    0xeb6(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x14(%rbp)
je     11b4 <power_online+0x74>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1211 <power_online+0xd1>
lea    0xe8b(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ee <power_online+0xae>
lea    0xe48(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <power_online+0xd1>
mov    -0x10(%rbp),%rdi
lea    0xe4a(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<power_online>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb4(%rip),%rdi
lea    0xed0(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11c1 <power_online+0x81>
mov    %rax,%rbx
lea    0xedc(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%eax
test   %eax,%eax
jne    11d2 <power_online+0x92>
lea    0xebb(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11c1 <power_online+0x81>
mov    %rax,%rbx
lea    0xe9d(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%eax
jmp    11d2 <power_online+0x92>
lea    0xe5d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [35 x i8] c"/sys/class/power_supply/usb/online\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"Unable to open USB sysfs file\00"
@.str.3 = [3 x i8] c"%i\00"
@.str.4 = [34 x i8] c"/sys/class/power_supply/dc/online\00"
define i32 @power_online() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = call i32* @fopen(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %30
9: ; preds = %0
%10 = load i32*, i32** %2
%11 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%12 = load i32*, i32** %2
%13 = call i32 @fclose(i32* %12)
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = load i32, i32* %3
store i32 %17, i32* %1
br label %30
18: ; preds = %9
%19 = call i32* @fopen(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %2
%20 = load i32*, i32** %2
%21 = icmp ne i32* %20, null
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %30
24: ; preds = %18
%25 = load i32*, i32** %2
%26 = call i32 @fscanf(i32* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%27 = load i32*, i32** %2
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* %3
store i32 %29, i32* %1
br label %30
30: ; preds = %24, %22, %16, %7
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [35 x i8] c"/sys/class/power_supply/usb/online\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"Unable to open USB sysfs file\00"
@.str.3 = [3 x i8] c"%i\00"
@.str.4 = [34 x i8] c"/sys/class/power_supply/dc/online\00"
define i32 @power_online() {
%1 = alloca i32
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %23
6: ; preds = %0
%7 = bitcast i32* %1 to i8*
%8 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%9 = call i32 @fclose(i32* %2)
%10 = load i32, i32* %1
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %6
%13 = call i32* @fopen(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %21
17: ; preds = %12
%18 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%19 = call i32 @fclose(i32* %13)
%20 = load i32, i32* %1
br label %21
21: ; preds = %6, %17, %15
%22 = phi i32 [ %20, %17 ], [ -1, %15 ], [ %10, %6 ]
br label %23
23: ; preds = %21, %4
%24 = phi i32 [ %22, %21 ], [ -1, %4 ]
ret i32 %24
}