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

/* Variables and functions */
int EINVAL;
int EOF;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char const *, void *, void *);
char *path_append(char *, char const *);
int /*<<< orphan*/ perror(char *);
int sysfs_read_attr(char *path, const char *attr, const char *fmt, void *val1,
                    void *val2, int verbose) {
  FILE *fp;
  int count, err = 0;
  char *pathtail = path_append(path, attr);
  fp = fopen(path, "r");
  if (!fp) {
    err = errno;
  } else {
    count = fscanf(fp, fmt, val1, val2);
    if (count != (val2 ? 2 : 1))
      err = (count == EOF) ? errno : EINVAL;
    fclose(fp);
  }
  if (err && verbose)
    perror(path);
  *pathtail = '\0';
  return err;
}


x86 assembly (clang, -O0):
<sysfs_read_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <path_append@plt>
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11b8 <sysfs_read_attr+0x68>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
jmp    122e <sysfs_read_attr+0xde>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <fscanf@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x28(%rbp),%rsi
mov    $0x1,%ecx
mov    $0x2,%edx
cmp    $0x0,%rsi
cmovne %edx,%ecx
cmp    %ecx,%eax
je     1225 <sysfs_read_attr+0xd5>
mov    -0x3c(%rbp),%eax
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%eax
jne    1213 <sysfs_read_attr+0xc3>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
jmp    121f <sysfs_read_attr+0xcf>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x40(%rbp)
je     124b <sysfs_read_attr+0xfb>
cmpl   $0x0,-0x2c(%rbp)
je     124b <sysfs_read_attr+0xfb>
mov    -0x8(%rbp),%rdi
call   1070 <perror@plt>
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read_attr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r15d
mov    %r8,%rbx
mov    %rcx,%r13
mov    %rdx,%r14
mov    %rdi,%r12
call   1030 <path_append@plt>
mov    %rax,(%rsp)
lea    0xe86(%rip),%rsi
mov    %r12,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11c1 <sysfs_read_attr+0x71>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1050 <fscanf@plt>
cmp    $0x1,%rbx
mov    $0x2,%ecx
sbb    $0x0,%ecx
xor    %ebx,%ebx
cmp    %ecx,%eax
je     11da <sysfs_read_attr+0x8a>
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%eax
jne    11d1 <sysfs_read_attr+0x81>
mov    0x2e19(%rip),%rax
jmp    11d8 <sysfs_read_attr+0x88>
mov    0x2e10(%rip),%rax
mov    (%rax),%ebx
test   %r15d,%r15d
jne    11e7 <sysfs_read_attr+0x97>
jmp    11eb <sysfs_read_attr+0x9b>
mov    0x2df8(%rip),%rax
mov    (%rax),%ebx
mov    %rbp,%rdi
call   1040 <fclose@plt>
test   %r15d,%r15d
je     11eb <sysfs_read_attr+0x9b>
test   %ebx,%ebx
jne    1203 <sysfs_read_attr+0xb3>
mov    (%rsp),%rax
movb   $0x0,(%rax)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1070 <perror@plt>
jmp    11eb <sysfs_read_attr+0x9b>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
@EOF = global i32 0
@EINVAL = global i32 0
define i32 @sysfs_read_attr(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
store i32 %5, i32* %12
store i32 0, i32* %15
%17 = load i8*, i8** %7
%18 = load i8*, i8** %8
%19 = call i8* @path_append(i8* %17, i8* %18)
store i8* %19, i8** %16
%20 = load i8*, i8** %7
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %13
%22 = load i32*, i32** %13
%23 = icmp ne i32* %22, null
br i1 %23, label %26, label %24
24: ; preds = %6
%25 = load i32, i32* @errno
store i32 %25, i32* %15
br label %51
26: ; preds = %6
%27 = load i32*, i32** %13
%28 = load i8*, i8** %9
%29 = load i8*, i8** %10
%30 = load i8*, i8** %11
%31 = call i32 @fscanf(i32* %27, i8* %28, i8* %29, i8* %30)
store i32 %31, i32* %14
%32 = load i32, i32* %14
%33 = load i8*, i8** %11
%34 = icmp ne i8* %33, null
%35 = zext i1 %34 to i64
%36 = select i1 %34, i32 2, i32 1
%37 = icmp ne i32 %32, %36
br i1 %37, label %38, label %48
38: ; preds = %26
%39 = load i32, i32* %14
%40 = load i32, i32* @EOF
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = load i32, i32* @errno
br label %46
44: ; preds = %38
%45 = load i32, i32* @EINVAL
br label %46
46: ; preds = %44, %42
%47 = phi i32 [ %43, %42 ], [ %45, %44 ]
store i32 %47, i32* %15
br label %48
48: ; preds = %46, %26
%49 = load i32*, i32** %13
%50 = call i32 @fclose(i32* %49)
br label %51
51: ; preds = %48, %24
%52 = load i32, i32* %15
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %51
%55 = load i32, i32* %12
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load i8*, i8** %7
%59 = call i32 @perror(i8* %58)
br label %60
60: ; preds = %57, %54, %51
%61 = load i8*, i8** %16
store i8 0, i8* %61
%62 = load i32, i32* %15
ret i32 %62
}

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