#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int sysfs_string_read(char *path, char *buffer, int length) {
  int fd = -1;
  int rc;
  if (path == NULL || buffer == NULL || length <= 0)
    return -1;
  fd = open(path, O_RDONLY);
  if (fd < 0)
    goto error;
  rc = read(fd, buffer, length);
  if (rc <= 0)
    goto error;
  rc = 0;
  goto complete;
error:
  rc = -1;
complete:
  if (fd >= 0)
    close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<sysfs_string_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116a <sysfs_string_read+0x3a>
cmpq   $0x0,-0x18(%rbp)
je     116a <sysfs_string_read+0x3a>
cmpl   $0x0,-0x1c(%rbp)
jg     1176 <sysfs_string_read+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <sysfs_string_read+0xb6>
mov    -0x10(%rbp),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    119a <sysfs_string_read+0x6a>
jmp    11c7 <sysfs_string_read+0x97>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     11bb <sysfs_string_read+0x8b>
jmp    11c7 <sysfs_string_read+0x97>
movl   $0x0,-0x24(%rbp)
jmp    11ce <sysfs_string_read+0x9e>
movl   $0xffffffff,-0x24(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     11e0 <sysfs_string_read+0xb0>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_string_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     1183 <sysfs_string_read+0x53>
mov    %rsi,%rbp
test   %rsi,%rsi
je     1183 <sysfs_string_read+0x53>
mov    %edx,%r14d
test   %edx,%edx
jle    1183 <sysfs_string_read+0x53>
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1183 <sysfs_string_read+0x53>
mov    %eax,%r15d
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <read@plt>
xor    %ebx,%ebx
test   %eax,%eax
setle  %bl
neg    %ebx
mov    %r15d,%edi
call   1030 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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