#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int main() {
  FILE *f;
  char level;
  f = fopen("/sys/devices/virtual/backlight/acpi_video0/brightness", "r+");
  if (f <= 0) {
    perror("Error: ");
    return 0;
  }
  fscanf(f, "%c", &level);
  printf("Current level: %c\n", level);
  printf("Enter level (0-5):");
  scanf("%c", &level);
  if (level < '0' || level > '5') {
    printf("Level must be between 0 and 5!\n");
    return 0;
  }
  fseek(f, 0, SEEK_SET);
  fprintf(f, "%c\n", level);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
ja     11c9 <main+0x49>
lea    0xe81(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1279 <main+0xf9>
mov    -0x10(%rbp),%rdi
lea    0xe6d(%rip),%rsi
lea    -0x11(%rbp),%rdx
call   1050 <fscanf@plt>
movsbl -0x11(%rbp),%esi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe3d(%rip),%rdi
lea    -0x11(%rbp),%rsi
call   1080 <scanf@plt>
movsbl -0x11(%rbp),%eax
cmp    $0x30,%eax
jl     1227 <main+0xa7>
movsbl -0x11(%rbp),%eax
cmp    $0x35,%eax
jle    1241 <main+0xc1>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1279 <main+0xf9>
mov    -0x10(%rbp),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
lea    0xdf3(%rip),%rsi
movsbl -0x11(%rbp),%edx
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe60(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1251 <main+0xc1>
mov    %rax,%rbx
lea    0xe82(%rip),%r14
lea    0xf(%rsp),%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <fscanf@plt>
movsbl 0xf(%rsp),%esi
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe6b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <scanf@plt>
mov    0xf(%rsp),%al
add    $0xca,%al
cmp    $0xf9,%al
ja     1216 <main+0x86>
lea    0xe5b(%rip),%rdi
call   1030 <puts@plt>
jmp    1245 <main+0xb5>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
movsbl 0xf(%rsp),%edx
lea    0xe1e(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xde1(%rip),%rdi
call   10b0 <perror@plt>
jmp    1245 <main+0xb5>

LLVM IR (-O0):
@.str = [54 x i8] c"/sys/devices/virtual/backlight/acpi_video0/brightness\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [8 x i8] c"Error: \00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [19 x i8] c"Current level: %c\0A\00"
@.str.5 = [19 x i8] c"Enter level (0-5):\00"
@.str.6 = [32 x i8] c"Level must be between 0 and 5!\0A\00"
@SEEK_SET = global i32 0
@.str.7 = [4 x i8] c"%c\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp ule i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %35
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), i8* %3)
%12 = load i8, i8* %3
%13 = sext i8 %12 to i32
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%17 = load i8, i8* %3
%18 = sext i8 %17 to i32
%19 = icmp slt i32 %18, 48
br i1 %19, label %24, label %20
20: ; preds = %9
%21 = load i8, i8* %3
%22 = sext i8 %21 to i32
%23 = icmp sgt i32 %22, 53
br i1 %23, label %24, label %26
24: ; preds = %20, %9
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %35
26: ; preds = %20
%27 = load i32*, i32** %2
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
%30 = load i32*, i32** %2
%31 = load i8, i8* %3
%32 = call i32 @fprintf(i32* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8 signext %31)
%33 = load i32*, i32** %2
%34 = call i32 @fclose(i32* %33)
store i32 0, i32* %1
br label %35
35: ; preds = %26, %24, %7
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [54 x i8] c"/sys/devices/virtual/backlight/acpi_video0/brightness\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [8 x i8] c"Error: \00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [19 x i8] c"Current level: %c\0A\00"
@.str.5 = [19 x i8] c"Enter level (0-5):\00"
@SEEK_SET = global i32 0
@.str.7 = [4 x i8] c"%c\0A\00"
@str = [31 x i8] c"Level must be between 0 and 5!\00"
define i32 @main() {
%1 = alloca i8
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 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 ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %24
6: ; preds = %0
%7 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%8 = load i8, i8* %1
%9 = sext i8 %8 to i32
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %9)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%13 = load i8, i8* %1
%14 = add i8 %13, -54
%15 = icmp ult i8 %14, -6
br i1 %15, label %16, label %18
16: ; preds = %6
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %24
18: ; preds = %6
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %2, i32 0, i32 %19)
%21 = load i8, i8* %1
%22 = call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8 signext %21)
%23 = call i32 @fclose(i32* %2)
br label %24
24: ; preds = %18, %16, %4
ret i32 0
}