#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*/ *fgets(char *, size_t, int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
long strtol(char *, int /*<<< orphan*/ *, int);
long int get_brightness(FILE *sysfs_fp) {
  rewind(sysfs_fp);
  size_t string_length = 5;
  char *brightness_string = (char *)malloc(string_length);
  if (brightness_string == NULL) {
    return -1;
  }
  if (fgets(brightness_string, string_length, sysfs_fp) == NULL) {
    return -1;
  }
  long int brightness = strtol(brightness_string, NULL, 10);
  return brightness;
}


x86 assembly (clang, -O0):
<get_brightness>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <rewind@plt>
movq   $0x5,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1182 <get_brightness+0x42>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c8 <get_brightness+0x88>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11aa <get_brightness+0x6a>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c8 <get_brightness+0x88>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_brightness>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <rewind@plt>
mov    $0x5,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1189 <get_brightness+0x49>
mov    %rax,%rbx
mov    $0x5,%esi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1189 <get_brightness+0x49>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <strtol@plt>
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @get_brightness(i32* %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
store i32* %0, i32** %3
%7 = load i32*, i32** %3
%8 = call i32 @rewind(i32* %7)
store i64 5, i64* %4
%9 = load i64, i64* %4
%10 = call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %1
store i64 -1, i64* %2
br label %26
15: ; preds = %1
%16 = load i8*, i8** %5
%17 = load i64, i64* %4
%18 = load i32*, i32** %3
%19 = call i32* @fgets(i8* %16, i64 %17, i32* %18)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %15
store i64 -1, i64* %2
br label %26
22: ; preds = %15
%23 = load i8*, i8** %5
%24 = call i64 @strtol(i8* %23, i32* null, i32 10)
store i64 %24, i64* %6
%25 = load i64, i64* %6
store i64 %25, i64* %2
br label %26
26: ; preds = %22, %21, %14
%27 = load i64, i64* %2
ret i64 %27
}

LLVM IR (-O2):
define i64 @get_brightness(i32* nocapture %0) {
%2 = tail call i32 @rewind(i32* %0)
%3 = tail call i64 @malloc(i64 5)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = tail call i32* @fgets(i8* %4, i64 5, i32* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i64 @strtol(i8* nocapture %4, i32* null, i32 10)
br label %11
11: ; preds = %6, %1, %9
%12 = phi i64 [ %10, %9 ], [ -1, %1 ], [ -1, %6 ]
ret i64 %12
}