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

/* Variables and functions */
int /*<<< orphan*/ abort();
char *alloca(size_t);
long readfile(char *);
int sprintf(char *, char *, char *, char const *);
scalar_t__ strlen(char const *);
double getbrightness(const char *device, long int *out_cur, long int *out_max) {
  size_t n = sizeof("//max_brightness") - 1;
  char *path;
  long int max, cur;
  n += strlen("/sys/class/backlight");
  n += strlen(device);
  path = alloca(n + 1);
  if (sprintf(path, "%s/%s/max_brightness", "/sys/class/backlight", device) !=
      (int)n)
    abort();
  max = readfile(path);
  if (max < 1)
    return -1;
  if (out_max)
    *out_max = max;
  if (sprintf(path, "%s/%s/brightness", "/sys/class/backlight", device) !=
      (int)n - 4)
    abort();
  cur = readfile(path);
  if (cur < 0)
    return -1;
  if (out_cur)
    *out_cur = cur;
  return (double)cur / (double)max;
}


x86 assembly (clang, -O0):
<getbrightness>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x10,-0x28(%rbp)
lea    0xe95(%rip),%rdi
call   1040 <strlen@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1050 <alloca@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xe6c(%rip),%rsi
lea    0xe50(%rip),%rdx
call   1060 <sprintf@plt>
mov    -0x28(%rbp),%rcx
cmp    %ecx,%eax
je     11d0 <getbrightness+0x80>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x30(%rbp),%rdi
call   1070 <readfile@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x1,-0x38(%rbp)
jge    11fa <getbrightness+0xaa>
movsd  0xe10(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1296 <getbrightness+0x146>
cmpq   $0x0,-0x20(%rbp)
je     1210 <getbrightness+0xc0>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xe13(%rip),%rsi
lea    0xde2(%rip),%rdx
call   1060 <sprintf@plt>
mov    -0x28(%rbp),%rcx
sub    $0x4,%ecx
cmp    %ecx,%eax
je     1241 <getbrightness+0xf1>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x30(%rbp),%rdi
call   1070 <readfile@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    126b <getbrightness+0x11b>
movsd  0xd9f(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1296 <getbrightness+0x146>
cmpq   $0x0,-0x18(%rbp)
je     1281 <getbrightness+0x131>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cvtsi2sdq -0x40(%rbp),%xmm0
cvtsi2sdq -0x38(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbrightness>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %rax,%rbx
lea    0x25(%rax),%rdi
call   1050 <alloca@plt>
mov    %rax,%r15
lea    0xe9e(%rip),%rsi
lea    0xe82(%rip),%rdx
mov    %rax,%rdi
mov    %r12,%rcx
call   1060 <sprintf@plt>
lea    0x24(%rbx),%ecx
cmp    %ecx,%eax
jne    1216 <getbrightness+0xc6>
mov    %r15,%rdi
call   1070 <readfile@plt>
movsd  0xe58(%rip),%xmm0
test   %rax,%rax
jle    1207 <getbrightness+0xb7>
mov    %rax,%rbp
test   %r13,%r13
je     11b9 <getbrightness+0x69>
mov    %rbp,0x0(%r13)
lea    0xe72(%rip),%rsi
lea    0xe41(%rip),%rdx
mov    %r15,%rdi
mov    %r12,%rcx
call   1060 <sprintf@plt>
add    $0x20,%ebx
cmp    %ebx,%eax
jne    1216 <getbrightness+0xc6>
mov    %r15,%rdi
call   1070 <readfile@plt>
test   %rax,%rax
movsd  0xe14(%rip),%xmm0
js     1207 <getbrightness+0xb7>
test   %r14,%r14
je     11f6 <getbrightness+0xa6>
mov    %rax,(%r14)
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
cvtsi2sd %rbp,%xmm1
divsd  %xmm1,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"/sys/class/backlight\00"
@.str.1 = [21 x i8] c"%s/%s/max_brightness\00"
@.str.2 = [17 x i8] c"%s/%s/brightness\00"
define double @getbrightness(i8* %0, i64* %1, i64* %2) {
%4 = alloca double
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %5
store i64* %1, i64** %6
store i64* %2, i64** %7
store i64 16, i64* %8
%12 = call i64 @strlen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%13 = load i64, i64* %8
%14 = add i64 %13, %12
store i64 %14, i64* %8
%15 = load i8*, i8** %5
%16 = call i64 @strlen(i8* %15)
%17 = load i64, i64* %8
%18 = add i64 %17, %16
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = add i64 %19, 1
%21 = call i8* @alloca(i64 %20)
store i8* %21, i8** %9
%22 = load i8*, i8** %9
%23 = load i8*, i8** %5
%24 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i64, i64* %8
%26 = trunc i64 %25 to i32
%27 = icmp ne i32 %24, %26
br i1 %27, label %28, label %30
28: ; preds = %3
%29 = call i32 (...) @abort()
unreachable
30: ; preds = %3
%31 = load i8*, i8** %9
%32 = call i64 @readfile(i8* %31)
store i64 %32, i64* %10
%33 = load i64, i64* %10
%34 = icmp slt i64 %33, 1
br i1 %34, label %35, label %36
35: ; preds = %30
store double -1.000000e+00, double* %4
br label %70
36: ; preds = %30
%37 = load i64*, i64** %7
%38 = icmp ne i64* %37, null
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i64, i64* %10
%41 = load i64*, i64** %7
store i64 %40, i64* %41
br label %42
42: ; preds = %39, %36
%43 = load i8*, i8** %9
%44 = load i8*, i8** %5
%45 = call i32 @sprintf(i8* %43, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %44)
%46 = load i64, i64* %8
%47 = trunc i64 %46 to i32
%48 = sub nsw i32 %47, 4
%49 = icmp ne i32 %45, %48
br i1 %49, label %50, label %52
50: ; preds = %42
%51 = call i32 (...) @abort()
unreachable
52: ; preds = %42
%53 = load i8*, i8** %9
%54 = call i64 @readfile(i8* %53)
store i64 %54, i64* %11
%55 = load i64, i64* %11
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %52
store double -1.000000e+00, double* %4
br label %70
58: ; preds = %52
%59 = load i64*, i64** %6
%60 = icmp ne i64* %59, null
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i64, i64* %11
%63 = load i64*, i64** %6
store i64 %62, i64* %63
br label %64
64: ; preds = %61, %58
%65 = load i64, i64* %11
%66 = sitofp i64 %65 to double
%67 = load i64, i64* %10
%68 = sitofp i64 %67 to double
%69 = fdiv double %66, %68
store double %69, double* %4
br label %70
70: ; preds = %64, %57, %35
%71 = load double, double* %4
ret double %71
}

LLVM IR (-O2):
@.str = [21 x i8] c"/sys/class/backlight\00"
@.str.1 = [21 x i8] c"%s/%s/max_brightness\00"
@.str.2 = [17 x i8] c"%s/%s/brightness\00"
define double @getbrightness(i8* %0, i64* writeonly %1, i64* writeonly %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = add i64 %4, 37
%6 = tail call i8* @alloca(i64 %5)
%7 = tail call i32 @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = trunc i64 %4 to i32
%9 = add i32 %8, 36
%10 = icmp eq i32 %7, %9
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = tail call i32 (...) @abort()
unreachable
13: ; preds = %3
%14 = tail call i64 @readfile(i8* %6)
%15 = icmp slt i64 %14, 1
br i1 %15, label %35, label %16
16: ; preds = %13
%17 = icmp eq i64* %2, null
br i1 %17, label %19, label %18
18: ; preds = %16
store i64 %14, i64* %2
br label %19
19: ; preds = %18, %16
%20 = tail call i32 @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%21 = add i32 %8, 32
%22 = icmp eq i32 %20, %21
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 (...) @abort()
unreachable
25: ; preds = %19
%26 = tail call i64 @readfile(i8* %6)
%27 = icmp slt i64 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %25
%29 = icmp eq i64* %1, null
br i1 %29, label %31, label %30
30: ; preds = %28
store i64 %26, i64* %1
br label %31
31: ; preds = %30, %28
%32 = sitofp i64 %26 to double
%33 = sitofp i64 %14 to double
%34 = fdiv double %32, %33
br label %35
35: ; preds = %25, %13, %31
%36 = phi double [ %34, %31 ], [ -1.000000e+00, %13 ], [ -1.000000e+00, %25 ]
ret double %36
}