#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, size_t);
unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) {
  int fd;
  ssize_t numread;
  fd = open(path, O_RDONLY);
  if (fd == -1)
    return 0;
  numread = read(fd, buf, buflen - 1);
  if (numread < 1) {
    close(fd);
    return 0;
  }
  buf[numread] = '\0';
  close(fd);
  return (unsigned int)numread;
}


x86 assembly (clang, -O0):
<sysfs_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    116f <sysfs_read_file+0x3f>
movl   $0x0,-0x4(%rbp)
jmp    11be <sysfs_read_file+0x8e>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
jge    11a4 <sysfs_read_file+0x74>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11be <sysfs_read_file+0x8e>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     116f <sysfs_read_file+0x3f>
mov    %eax,%ebp
add    $0xffffffffffffffff,%rbx
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
test   %eax,%eax
jle    1173 <sysfs_read_file+0x43>
mov    %eax,%ebx
mov    %eax,%eax
movb   $0x0,(%r14,%rax,1)
jmp    1175 <sysfs_read_file+0x45>
xor    %ebx,%ebx
jmp    117c <sysfs_read_file+0x4c>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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