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


x86 assembly (clang, -O0):
<read_sysfs_posint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11fd <read_sysfs_posint+0x6d>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    0x2de5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1313 <read_sysfs_posint+0x183>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe09(%rip),%rsi
call   10b0 <sprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1227 <read_sysfs_posint+0x97>
jmp    1304 <read_sysfs_posint+0x174>
mov    -0x30(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1259 <read_sysfs_posint+0xc9>
xor    %eax,%eax
mov    0x2d79(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    1304 <read_sysfs_posint+0x174>
mov    0x2d68(%rip),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
lea    0xdae(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
je     12de <read_sysfs_posint+0x14e>
mov    0x2d3e(%rip),%rax
cmpl   $0x0,(%rax)
je     12a6 <read_sysfs_posint+0x116>
xor    %eax,%eax
mov    0x2d2c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x3c(%rbp)
jmp    12b4 <read_sysfs_posint+0x124>
xor    %eax,%eax
mov    0x2d39(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12d9 <read_sysfs_posint+0x149>
lea    0xd4f(%rip),%rdi
call   10a0 <perror@plt>
jmp    1304 <read_sysfs_posint+0x174>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12ff <read_sysfs_posint+0x16f>
xor    %eax,%eax
mov    0x2cce(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    1304 <read_sysfs_posint+0x174>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sysfs_posint>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     123b <read_sysfs_posint+0xab>
mov    %rax,%r15
lea    0xe4c(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
mov    %r12,%rcx
call   10b0 <sprintf@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
js     1290 <read_sysfs_posint+0x100>
lea    0xe5c(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     125d <read_sysfs_posint+0xcd>
mov    %rax,%r14
mov    0x2dc3(%rip),%rbx
movl   $0x0,(%rbx)
lea    0xe0d(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    126e <read_sysfs_posint+0xde>
mov    %r14,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     1290 <read_sysfs_posint+0x100>
xor    %eax,%eax
sub    (%rbx),%eax
mov    %eax,0x4(%rsp)
jmp    1290 <read_sysfs_posint+0x100>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d81(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    129c <read_sysfs_posint+0x10c>
mov    0x2d64(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    %ecx,0x4(%rsp)
jmp    1290 <read_sysfs_posint+0x100>
mov    (%rbx),%eax
test   %eax,%eax
jne    127d <read_sysfs_posint+0xed>
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
neg    %eax
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    12a8 <read_sysfs_posint+0x118>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd74(%rip),%rdi
call   10a0 <perror@plt>
jmp    1290 <read_sysfs_posint+0x100>

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

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