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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char **, int);
int sysfs_value_read(char *path) {
  char buffer[100];
  int value;
  int fd = -1;
  int rc;
  if (path == NULL)
    return -1;
  fd = open(path, O_RDONLY);
  if (fd < 0)
    goto error;
  rc = read(fd, &buffer, sizeof(buffer));
  if (rc <= 0)
    goto error;
  value = atoi(buffer);
  goto complete;
error:
  value = -1;
complete:
  if (fd >= 0)
    close(fd);
  return value;
}


x86 assembly (clang, -O0):
<sysfs_value_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x88(%rbp)
ff ff ff
cmpq   $0x0,-0x10(%rbp)
jne    1170 <sysfs_value_read+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <sysfs_value_read+0xc5>
mov    -0x10(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x88(%rbp)
cmpl   $0x0,-0x88(%rbp)
jge    119a <sysfs_value_read+0x5a>
jmp    11da <sysfs_value_read+0x9a>
mov    -0x88(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1040 <read@plt>
mov    %eax,-0x8c(%rbp)
cmpl   $0x0,-0x8c(%rbp)
jg     11c6 <sysfs_value_read+0x86>
jmp    11da <sysfs_value_read+0x9a>
lea    -0x80(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x84(%rbp)
jmp    11e4 <sysfs_value_read+0xa4>
movl   $0xffffffff,-0x84(%rbp)
ff ff ff
cmpl   $0x0,-0x88(%rbp)
jl     11fc <sysfs_value_read+0xbc>
mov    -0x88(%rbp),%edi
call   1030 <close@plt>
mov    -0x84(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_value_read>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     118f <sysfs_value_read+0x4f>
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     118f <sysfs_value_read+0x4f>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x64,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1183 <sysfs_value_read+0x43>
mov    %rsp,%rdi
call   1060 <atoi@plt>
mov    %eax,%ebp
jmp    1188 <sysfs_value_read+0x48>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @sysfs_value_read(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %6
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %37
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %11
br label %28
18: ; preds = %11
%19 = load i32, i32* %6
%20 = bitcast [100 x i8]* %4 to i8**
%21 = call i32 @read(i32 %19, i8** %20, i32 100)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18
br label %28
25: ; preds = %18
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %5
br label %29
28: ; preds = %24, %17
store i32 -1, i32* %5
br label %29
29: ; preds = %28, %25
%30 = load i32, i32* %6
%31 = icmp sge i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %32, %29
%36 = load i32, i32* %5
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %10
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @sysfs_value_read(i8* %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %18, label %5
5: ; preds = %1
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %18, label %9
9: ; preds = %5
%10 = bitcast [100 x i8]* %2 to i8**
%11 = call i32 @read(i32 %7, i8** %10, i32 100)
%12 = icmp slt i32 %11, 1
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = call i32 @atoi(i8* %3)
br label %15
15: ; preds = %9, %13
%16 = phi i32 [ -1, %9 ], [ %14, %13 ]
%17 = tail call i32 @close(i32 %7)
br label %18
18: ; preds = %5, %15, %1
%19 = phi i32 [ -1, %1 ], [ %16, %15 ], [ -1, %5 ]
ret i32 %19
}