#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 */
scalar_t__ EOF;
int /*<<< orphan*/ err(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *, int *);
int read_ambient_light() {
  FILE *f = fopen("/sys/devices/platform/applesmc.768/light", "r");
  int level = 0;
  int tmp = 0;
  if (f == NULL)
    err("can't open sensor file");
  if (fscanf(f, "(%d,%d)", &level, &tmp) == EOF)
    err("can't scan ambient light level");
  fclose(f);
  return level;
}


x86 assembly (clang, -O0):
<read_ambient_light>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
lea    0xed3(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1184 <read_ambient_light+0x44>
lea    0xeac(%rip),%rdi
call   1050 <err@plt>
mov    -0x8(%rbp),%rdi
lea    0xeb3(%rip),%rsi
lea    -0xc(%rbp),%rdx
lea    -0x10(%rbp),%rcx
call   1040 <fscanf@plt>
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
jne    11b8 <read_ambient_light+0x78>
lea    0xe97(%rip),%rdi
call   1050 <err@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_ambient_light>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb4(%rip),%rdi
lea    0xed6(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
movl   $0x0,0x8(%rsp)
movl   $0x0,0xc(%rsp)
test   %rax,%rax
jne    117c <read_ambient_light+0x3c>
lea    0xeb4(%rip),%rdi
call   1050 <err@plt>
lea    0xebf(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %rbx,%rdi
call   1040 <fscanf@plt>
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
jne    11ad <read_ambient_light+0x6d>
lea    0xea2(%rip),%rdi
call   1050 <err@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [41 x i8] c"/sys/devices/platform/applesmc.768/light\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"can't open sensor file\00"
@.str.3 = [8 x i8] c"(%d,%d)\00"
@EOF = global i64 0
@.str.4 = [31 x i8] c"can't scan ambient light level\00"
define i32 @read_ambient_light() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 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** %1
store i32 0, i32* %2
store i32 0, i32* %3
%5 = load i32*, i32** %1
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = load i32*, i32** %1
%11 = call i64 @fscanf(i32* %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* %2, i32* %3)
%12 = load i64, i64* @EOF
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @err(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = load i32*, i32** %1
%18 = call i32 @fclose(i32* %17)
%19 = load i32, i32* %2
ret i32 %19
}

LLVM IR (-O2):
@.str = [41 x i8] c"/sys/devices/platform/applesmc.768/light\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"can't open sensor file\00"
@.str.3 = [8 x i8] c"(%d,%d)\00"
@EOF = global i64 0
@.str.4 = [31 x i8] c"can't scan ambient light level\00"
define i32 @read_ambient_light() {
%1 = alloca i32
%2 = alloca i32
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%5 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%6 = icmp eq i32* %3, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = call i64 @fscanf(i32* %3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* %1, i32* %2)
%11 = load i64, i64* @EOF
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @err(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = call i32 @fclose(i32* %3)
%17 = load i32, i32* %1
ret i32 %17
}