#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 char uint8_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, char *, size_t);
scalar_t__ strcmp(char *, char *);
int sysfs_read_file(const char *path, uint8_t *buff, size_t buff_size) {
  int fd = -1;
  int errno_save = 0;
  ssize_t readed = 0;
  ssize_t i = 0;
  assert(path != NULL);
  assert(buff != NULL);
  assert(buff_size != 0);
  memset(buff, 0, buff_size);
  fd = open(path, O_RDONLY);
  if (fd < 0)
    return errno;
  readed = read(fd, buff, buff_size);
  errno_save = errno;
  close(fd);
  if (readed < 0) {
    buff[0] = '\0';
    return errno_save;
  }
  buff[buff_size - 1] = '\0';
  for (i = readed - 1; i >= 0; --i) {
    if (buff[i] == '\n') {
      buff[i] = '\0';
      break;
    }
  }
  if (strcmp((char *)buff, "(null)") == 0)
    buff[0] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<sysfs_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1207 <sysfs_read_file+0xa7>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d7 <sysfs_read_file+0x177>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x30(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x30(%rbp)
jge    124c <sysfs_read_file+0xec>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d7 <sysfs_read_file+0x177>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jl     12af <sysfs_read_file+0x14f>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1299 <sysfs_read_file+0x139>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12af <sysfs_read_file+0x14f>
jmp    129e <sysfs_read_file+0x13e>
mov    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
jmp    1268 <sysfs_read_file+0x108>
mov    -0x18(%rbp),%rdi
lea    0xd46(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12d0 <sysfs_read_file+0x170>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1223 <sysfs_read_file+0xc3>
mov    %eax,%r12d
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
mov    %rax,%rbp
mov    0x2e01(%rip),%rax
mov    (%rax),%r15d
mov    %r12d,%edi
call   1040 <close@plt>
test   %rbp,%rbp
js     121e <sysfs_read_file+0xbe>
movb   $0x0,-0x1(%r14,%rbx,1)
nopl   (%rax)
test   %rbp,%rbp
jle    1207 <sysfs_read_file+0xa7>
lea    -0x1(%rbp),%rax
cmpb   $0xa,-0x1(%rbx,%rbp,1)
mov    %rax,%rbp
jne    11f0 <sysfs_read_file+0x90>
movb   $0x0,(%rbx,%rax,1)
lea    0xdf2(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
xor    %r15d,%r15d
test   %rax,%rax
jne    122d <sysfs_read_file+0xcd>
movb   $0x0,(%rbx)
jmp    122d <sysfs_read_file+0xcd>
mov    0x2dae(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i32 0
@.str = [7 x i8] c"(null)\00"
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
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 -1, i32* %8
store i32 0, i32* %9
store i64 0, i64* %10
store i64 0, i64* %11
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i64, i64* %7
%21 = icmp ne i64 %20, 0
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i8*, i8** %6
%25 = load i64, i64* %7
%26 = call i32 @memset(i8* %24, i32 0, i64 %25)
%27 = load i8*, i8** %5
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %3
%33 = load i32, i32* @errno
store i32 %33, i32* %4
br label %81
34: ; preds = %3
%35 = load i32, i32* %8
%36 = load i8*, i8** %6
%37 = load i64, i64* %7
%38 = call i64 @read(i32 %35, i8* %36, i64 %37)
store i64 %38, i64* %10
%39 = load i32, i32* @errno
store i32 %39, i32* %9
%40 = load i32, i32* %8
%41 = call i32 @close(i32 %40)
%42 = load i64, i64* %10
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %34
%45 = load i8*, i8** %6
%46 = getelementptr inbounds i8, i8* %45, i64 0
store i8 0, i8* %46
%47 = load i32, i32* %9
store i32 %47, i32* %4
br label %81
48: ; preds = %34
%49 = load i8*, i8** %6
%50 = load i64, i64* %7
%51 = sub i64 %50, 1
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i64, i64* %10
%54 = sub nsw i64 %53, 1
store i64 %54, i64* %11
br label %55
55: ; preds = %70, %48
%56 = load i64, i64* %11
%57 = icmp sge i64 %56, 0
br i1 %57, label %58, label %73
58: ; preds = %55
%59 = load i8*, i8** %6
%60 = load i64, i64* %11
%61 = getelementptr inbounds i8, i8* %59, i64 %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 10
br i1 %64, label %65, label %69
65: ; preds = %58
%66 = load i8*, i8** %6
%67 = load i64, i64* %11
%68 = getelementptr inbounds i8, i8* %66, i64 %67
store i8 0, i8* %68
br label %73
69: ; preds = %58
br label %70
70: ; preds = %69
%71 = load i64, i64* %11
%72 = add nsw i64 %71, -1
store i64 %72, i64* %11
br label %55
73: ; preds = %65, %55
%74 = load i8*, i8** %6
%75 = call i64 @strcmp(i8* %74, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%76 = icmp eq i64 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %73
%78 = load i8*, i8** %6
%79 = getelementptr inbounds i8, i8* %78, i64 0
store i8 0, i8* %79
br label %80
80: ; preds = %77, %73
store i32 0, i32* %4
br label %81
81: ; preds = %80, %44, %32
%82 = load i32, i32* %4
ret i32 %82
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
@.str = [7 x i8] c"(null)\00"
define i32 @sysfs_read_file(i8* %0, i8* %1, i64 %2) {
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i8* %1, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i64 %2, 0
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = tail call i32 @memset(i8* %1, i32 0, i64 %2)
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %0, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* @errno
br label %42
19: ; preds = %3
%20 = tail call i64 @read(i32 %15, i8* %1, i64 %2)
%21 = load i32, i32* @errno
%22 = tail call i32 @close(i32 %15)
%23 = icmp slt i64 %20, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i8 0, i8* %1
br label %42
25: ; preds = %19
%26 = add i64 %2, -1
%27 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %32, %25
%29 = phi i64 [ %20, %25 ], [ %30, %32 ]
%30 = add nsw i64 %29, -1
%31 = icmp sgt i64 %29, 0
br i1 %31, label %32, label %38
32: ; preds = %28
%33 = getelementptr inbounds i8, i8* %1, i64 %30
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 10
br i1 %35, label %36, label %28
36: ; preds = %32
%37 = getelementptr inbounds i8, i8* %1, i64 %30
store i8 0, i8* %37
br label %38
38: ; preds = %28, %36
%39 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %38
store i8 0, i8* %1
br label %42
42: ; preds = %38, %41, %24, %17
%43 = phi i32 [ %18, %17 ], [ %21, %24 ], [ 0, %41 ], [ 0, %38 ]
ret i32 %43
}