#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 *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int led_reset(void) {
  FILE *r = fopen("/sys/class/leds/"
                  "power"
                  "/max_brightness",
                  "r");
  if (!r)
    return -1;
  char buf[16];
  size_t read = fread(buf, 1, 16, r);
  fclose(r);
  FILE *w = fopen("/sys/class/leds/"
                  "power"
                  "/brightness",
                  "w");
  if (!w)
    return -1;
  fwrite(buf, 1, read, w);
  fclose(w);
  return 0;
}


x86 assembly (clang, -O0):
<led_reset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xeb1(%rip),%rdi
lea    0xecf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1176 <led_reset+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <led_reset+0xae>
lea    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe86(%rip),%rdi
lea    0xea0(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c8 <led_reset+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <led_reset+0xae>
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<led_reset>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xeb2(%rip),%rdi
lea    0xed0(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11bc <led_reset+0x7c>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe9e(%rip),%rdi
lea    0xeb8(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11bc <led_reset+0x7c>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    11c1 <led_reset+0x81>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [37 x i8] c"/sys/class/leds/power/max_brightness\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"/sys/class/leds/power/brightness\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @led_reset() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [16 x i8]
%4 = alloca i64
%5 = alloca i32*
%6 = call i32* @fopen(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp ne i32* %7, null
br i1 %8, label %10, label %9
9: ; preds = %0
store i32 -1, i32* %1
br label %27
10: ; preds = %0
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%12 = load i32*, i32** %2
%13 = call i64 @fread(i8* %11, i32 1, i32 16, i32* %12)
store i64 %13, i64* %4
%14 = load i32*, i32** %2
%15 = call i32 @fclose(i32* %14)
%16 = call i32* @fopen(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %16, i32** %5
%17 = load i32*, i32** %5
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %10
store i32 -1, i32* %1
br label %27
20: ; preds = %10
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%22 = load i64, i64* %4
%23 = load i32*, i32** %5
%24 = call i32 @fwrite(i8* %21, i32 1, i64 %22, i32* %23)
%25 = load i32*, i32** %5
%26 = call i32 @fclose(i32* %25)
store i32 0, i32* %1
br label %27
27: ; preds = %20, %19, %9
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [37 x i8] c"/sys/class/leds/power/max_brightness\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"/sys/class/leds/power/brightness\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @led_reset() {
%1 = alloca [16 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([37 x i8], [37 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 %15, label %4
4: ; preds = %0
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%6 = call i64 @fread(i8* %5, i32 1, i32 16, i32* %2)
%7 = tail call i32 @fclose(i32* %2)
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %4
%11 = call i32 @fwrite(i8* %5, i32 1, i64 %6, i32* %8)
%12 = tail call i32 @fclose(i32* %8)
br label %13
13: ; preds = %4, %10
%14 = phi i32 [ 0, %10 ], [ -1, %4 ]
br label %15
15: ; preds = %0, %13
%16 = phi i32 [ %14, %13 ], [ -1, %0 ]
ret i32 %16
}