#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 ENODATA;
int ENOMEM;
int errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sprintf(char *, char *, char const *, char const *);
scalar_t__ strlen(char const *);
int read_sysfs_string(const char *filename, const char *basedir, char *str) {
  int ret = 0;
  FILE *sysfsfp;
  char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  if (temp == NULL) {
    printf("Memory allocation failed");
    return -ENOMEM;
  }
  ret = sprintf(temp, "%s/%s", basedir, filename);
  if (ret < 0)
    goto error_free;
  sysfsfp = fopen(temp, "r");
  if (sysfsfp == NULL) {
    ret = -errno;
    goto error_free;
  }
  errno = 0;
  if (fscanf(sysfsfp, "%s\n", str) != 1) {
    ret = errno ? -errno : -ENODATA;
    if (fclose(sysfsfp))
      perror("read_sysfs_string(): Failed to close dir");
    goto error_free;
  }
  if (fclose(sysfsfp))
    ret = -errno;
error_free:
  free(temp);
  return ret;
}


x86 assembly (clang, -O0):
<read_sysfs_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ff <read_sysfs_string+0x6f>
lea    0xe19(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    0x2de3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1315 <read_sysfs_string+0x185>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe07(%rip),%rsi
call   10b0 <sprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1229 <read_sysfs_string+0x99>
jmp    1306 <read_sysfs_string+0x176>
mov    -0x38(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    125b <read_sysfs_string+0xcb>
xor    %eax,%eax
mov    0x2d7f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    1306 <read_sysfs_string+0x176>
mov    0x2d6e(%rip),%rax
movl   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xda8(%rip),%rsi
call   1070 <fscanf@plt>
cmp    $0x1,%eax
je     12e0 <read_sysfs_string+0x150>
mov    0x2d44(%rip),%rax
cmpl   $0x0,(%rax)
je     12a8 <read_sysfs_string+0x118>
xor    %eax,%eax
mov    0x2d32(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x44(%rbp)
jmp    12b6 <read_sysfs_string+0x126>
xor    %eax,%eax
mov    0x2d37(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12db <read_sysfs_string+0x14b>
lea    0xd4d(%rip),%rdi
call   10a0 <perror@plt>
jmp    1306 <read_sysfs_string+0x176>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1301 <read_sysfs_string+0x171>
xor    %eax,%eax
mov    0x2cd4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    1306 <read_sysfs_string+0x176>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sysfs_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1236 <read_sysfs_string+0xa6>
mov    %rax,%r12
lea    0xe49(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %rbp,%rcx
call   10b0 <sprintf@plt>
mov    %eax,%ebp
test   %eax,%eax
js     127a <read_sysfs_string+0xea>
lea    0xe5b(%rip),%rsi
mov    %r12,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     124f <read_sysfs_string+0xbf>
mov    %rax,%r15
mov    0x2dca(%rip),%rbx
movl   $0x0,(%rbx)
lea    0xe0c(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1070 <fscanf@plt>
cmp    $0x1,%eax
jne    125c <read_sysfs_string+0xcc>
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     127a <read_sysfs_string+0xea>
xor    %ebp,%ebp
sub    (%rbx),%ebp
jmp    127a <read_sysfs_string+0xea>
lea    0xdc3(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d8f(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    1282 <read_sysfs_string+0xf2>
mov    0x2d7a(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    127a <read_sysfs_string+0xea>
mov    (%rbx),%ebp
test   %ebp,%ebp
jne    126b <read_sysfs_string+0xdb>
mov    0x2d7f(%rip),%rax
mov    (%rax),%ebp
neg    %ebp
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    128d <read_sysfs_string+0xfd>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8f(%rip),%rdi
call   10a0 <perror@plt>
jmp    127a <read_sysfs_string+0xea>

LLVM IR (-O0):
@.str = [25 x i8] c"Memory allocation failed\00"
@ENOMEM = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
@ENODATA = global i32 0
@.str.4 = [41 x i8] c"read_sysfs_string(): Failed to close dir\00"
define i32 @read_sysfs_string(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %8
%11 = load i8*, i8** %6
%12 = call i64 @strlen(i8* %11)
%13 = load i8*, i8** %5
%14 = call i64 @strlen(i8* %13)
%15 = add nsw i64 %12, %14
%16 = add nsw i64 %15, 2
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %10
%18 = load i8*, i8** %10
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %3
%21 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @ENOMEM
%23 = sub nsw i32 0, %22
store i32 %23, i32* %4
br label %74
24: ; preds = %3
%25 = load i8*, i8** %10
%26 = load i8*, i8** %6
%27 = load i8*, i8** %5
%28 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %24
br label %70
32: ; preds = %24
%33 = load i8*, i8** %10
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %9
%35 = load i32*, i32** %9
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* @errno
%39 = sub nsw i32 0, %38
store i32 %39, i32* %8
br label %70
40: ; preds = %32
store i32 0, i32* @errno
%41 = load i32*, i32** %9
%42 = load i8*, i8** %7
%43 = call i32 @fscanf(i32* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = icmp ne i32 %43, 1
br i1 %44, label %45, label %62
45: ; preds = %40
%46 = load i32, i32* @errno
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* @errno
%50 = sub nsw i32 0, %49
br label %54
51: ; preds = %45
%52 = load i32, i32* @ENODATA
%53 = sub nsw i32 0, %52
br label %54
54: ; preds = %51, %48
%55 = phi i32 [ %50, %48 ], [ %53, %51 ]
store i32 %55, i32* %8
%56 = load i32*, i32** %9
%57 = call i64 @fclose(i32* %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %54
%60 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %61
61: ; preds = %59, %54
br label %70
62: ; preds = %40
%63 = load i32*, i32** %9
%64 = call i64 @fclose(i32* %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = load i32, i32* @errno
%68 = sub nsw i32 0, %67
store i32 %68, i32* %8
br label %69
69: ; preds = %66, %62
br label %70
70: ; preds = %69, %61, %37, %31
%71 = load i8*, i8** %10
%72 = call i32 @free(i8* %71)
%73 = load i32, i32* %8
store i32 %73, i32* %4
br label %74
74: ; preds = %70, %20
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
@.str = [25 x i8] c"Memory allocation failed\00"
@ENOMEM = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
@ENODATA = global i32 0
@.str.4 = [41 x i8] c"read_sysfs_string(): Failed to close dir\00"
define i32 @read_sysfs_string(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add i64 %4, 2
%7 = add i64 %6, %5
%8 = tail call i8* @malloc(i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @ENOMEM
%13 = sub nsw i32 0, %12
br label %45
14: ; preds = %3
%15 = tail call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %42, label %17
17: ; preds = %14
%18 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
br label %42
23: ; preds = %17
store i32 0, i32* @errno
%24 = tail call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%25 = icmp eq i32 %24, 1
br i1 %25, label %36, label %26
26: ; preds = %23
%27 = load i32, i32* @errno
%28 = icmp eq i32 %27, 0
%29 = load i32, i32* @ENODATA
%30 = select i1 %28, i32 %29, i32 %27
%31 = sub nsw i32 0, %30
%32 = tail call i64 @fclose(i32* %18)
%33 = icmp eq i64 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %26
%35 = tail call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %42
36: ; preds = %23
%37 = tail call i64 @fclose(i32* %18)
%38 = icmp eq i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = load i32, i32* @errno
%41 = sub nsw i32 0, %40
br label %42
42: ; preds = %36, %39, %26, %34, %14, %20
%43 = phi i32 [ %15, %14 ], [ %22, %20 ], [ %31, %34 ], [ %31, %26 ], [ %41, %39 ], [ %15, %36 ]
%44 = tail call i32 @free(i8* %8)
br label %45
45: ; preds = %42, %10
%46 = phi i32 [ %13, %10 ], [ %43, %42 ]
ret i32 %46
}