#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 *, int *);
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_posint(const char *filename, const char *basedir) {
  int ret;
  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, "%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    11f4 <read_sysfs_posint+0x64>
lea    0xe24(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    0x2dee(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    130a <read_sysfs_posint+0x17a>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe12(%rip),%rsi
call   10b0 <sprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    121e <read_sysfs_posint+0x8e>
jmp    12fb <read_sysfs_posint+0x16b>
mov    -0x30(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1250 <read_sysfs_posint+0xc0>
xor    %eax,%eax
mov    0x2d8a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    12fb <read_sysfs_posint+0x16b>
mov    0x2d79(%rip),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
lea    0xdb7(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <fscanf@plt>
cmp    $0x1,%eax
je     12d5 <read_sysfs_posint+0x145>
mov    0x2d4f(%rip),%rax
cmpl   $0x0,(%rax)
je     129d <read_sysfs_posint+0x10d>
xor    %eax,%eax
mov    0x2d3d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x3c(%rbp)
jmp    12ab <read_sysfs_posint+0x11b>
xor    %eax,%eax
mov    0x2d42(%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     12d0 <read_sysfs_posint+0x140>
lea    0xd58(%rip),%rdi
call   10a0 <perror@plt>
jmp    12fb <read_sysfs_posint+0x16b>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12f6 <read_sysfs_posint+0x166>
xor    %eax,%eax
mov    0x2cdf(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    12fb <read_sysfs_posint+0x16b>
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     1287 <read_sysfs_posint+0xf7>
lea    0xe5c(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1254 <read_sysfs_posint+0xc4>
mov    %rax,%r14
mov    0x2dcb(%rip),%rbx
movl   $0x0,(%rbx)
lea    0xe0d(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1070 <fscanf@plt>
cmp    $0x1,%eax
jne    1265 <read_sysfs_posint+0xd5>
mov    %r14,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     1287 <read_sysfs_posint+0xf7>
xor    %eax,%eax
sub    (%rbx),%eax
mov    %eax,0x4(%rsp)
jmp    1287 <read_sysfs_posint+0xf7>
lea    0xdbe(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d8a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    1293 <read_sysfs_posint+0x103>
mov    0x2d75(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    %ecx,0x4(%rsp)
jmp    1287 <read_sysfs_posint+0xf7>
mov    (%rbx),%eax
test   %eax,%eax
jne    1274 <read_sysfs_posint+0xe4>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
neg    %eax
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    129f <read_sysfs_posint+0x10f>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd7d(%rip),%rdi
call   10a0 <perror@plt>
jmp    1287 <read_sysfs_posint+0xf7>

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"%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 eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @ENOMEM
%21 = sub nsw i32 0, %20
store i32 %21, i32* %3
br label %71
22: ; preds = %2
%23 = load i8*, i8** %8
%24 = load i8*, i8** %5
%25 = load i8*, i8** %4
%26 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %22
br label %67
30: ; preds = %22
%31 = load i8*, i8** %8
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %7
%33 = load i32*, i32** %7
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
store i32 %37, i32* %6
br label %67
38: ; preds = %30
store i32 0, i32* @errno
%39 = load i32*, i32** %7
%40 = call i32 @fscanf(i32* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %6)
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %59
42: ; preds = %38
%43 = load i32, i32* @errno
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* @errno
%47 = sub nsw i32 0, %46
br label %51
48: ; preds = %42
%49 = load i32, i32* @ENODATA
%50 = sub nsw i32 0, %49
br label %51
51: ; preds = %48, %45
%52 = phi i32 [ %47, %45 ], [ %50, %48 ]
store i32 %52, i32* %6
%53 = load i32*, i32** %7
%54 = call i64 @fclose(i32* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %56, %51
br label %67
59: ; preds = %38
%60 = load i32*, i32** %7
%61 = call i64 @fclose(i32* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %59
%64 = load i32, i32* @errno
%65 = sub nsw i32 0, %64
store i32 %65, i32* %6
br label %66
66: ; preds = %63, %59
br label %67
67: ; preds = %66, %58, %35, %29
%68 = load i8*, i8** %8
%69 = call i32 @free(i8* %68)
%70 = load i32, i32* %6
store i32 %70, i32* %3
br label %71
71: ; preds = %67, %18
%72 = load i32, i32* %3
ret i32 %72
}

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"%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 %15
11: ; preds = %2
%12 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @ENOMEM
%14 = sub nsw i32 0, %13
br label %46
15: ; preds = %2
%16 = 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 %16, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %43, label %18
18: ; preds = %15
%19 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
store i32 %23, i32* %3
br label %43
24: ; preds = %18
store i32 0, i32* @errno
%25 = call i32 @fscanf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%26 = icmp eq i32 %25, 1
br i1 %26, label %37, label %27
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = icmp eq i32 %28, 0
%30 = load i32, i32* @ENODATA
%31 = select i1 %29, i32 %30, i32 %28
%32 = sub nsw i32 0, %31
store i32 %32, i32* %3
%33 = call i64 @fclose(i32* %19)
%34 = icmp eq i64 %33, 0
br i1 %34, label %43, label %35
35: ; preds = %27
%36 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %43
37: ; preds = %24
%38 = call i64 @fclose(i32* %19)
%39 = icmp eq i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = sub nsw i32 0, %41
store i32 %42, i32* %3
br label %43
43: ; preds = %37, %40, %27, %35, %15, %21
%44 = call i32 @free(i8* %9)
%45 = load i32, i32* %3
br label %46
46: ; preds = %43, %11
%47 = phi i32 [ %14, %11 ], [ %45, %43 ]
ret i32 %47
}