#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
int sysfs_read(const char *path, const char *filename, char *value) {
  FILE *fd;
  char buf[100];
  snprintf(buf, sizeof(buf), "%s%s", path, filename);
  fd = fopen(buf, "r");
  if (fd == NULL) {
    perror(buf);
    return -1;
  }
  fscanf(fd, "%s", value);
  if (fclose(fd) != 0)
    perror(buf);
  return 0;
}


x86 assembly (clang, -O0):
<sysfs_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x64,%esi
lea    0xe7e(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xe70(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c1 <sysfs_read+0x71>
lea    -0x90(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <sysfs_read+0xab>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe32(%rip),%rsi
call   1050 <fscanf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     11f4 <sysfs_read+0xa4>
lea    -0x90(%rbp),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rdx,%r14
mov    %rsi,%r8
mov    %rdi,%rcx
lea    0xe99(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1040 <snprintf@plt>
lea    0xe87(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b9 <sysfs_read+0x69>
mov    %rax,%rbx
lea    0xe6d(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    11be <sysfs_read+0x6e>
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret
mov    $0xffffffff,%ebx
mov    %rsp,%rdi
call   1070 <perror@plt>
jmp    11af <sysfs_read+0x5f>

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @sysfs_read(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [100 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%11 = load i8*, i8** %5
%12 = load i8*, i8** %6
%13 = call i32 @snprintf(i8* %10, i32 100, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %12)
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%20 = call i32 @perror(i8* %19)
store i32 -1, i32* %4
br label %32
21: ; preds = %3
%22 = load i32*, i32** %8
%23 = load i8*, i8** %7
%24 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i32*, i32** %8
%26 = call i64 @fclose(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%30 = call i32 @perror(i8* %29)
br label %31
31: ; preds = %28, %21
store i32 0, i32* %4
br label %32
32: ; preds = %31, %18
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @sysfs_read(i8* %0, i8* %1, i8* %2) {
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 100, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%7 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%11 = call i64 @fclose(i32* %7)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %9, %3
%14 = phi i32 [ -1, %3 ], [ 0, %9 ]
%15 = call i32 @perror(i8* %5)
br label %16
16: ; preds = %13, %9
%17 = phi i32 [ 0, %9 ], [ %14, %13 ]
ret i32 %17
}