#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 ENODEV;
int ENOMEM;
int errno;
int /*<<< orphan*/ 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*/ 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, tmp;
  FILE *sysfsfp;
  char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  if (temp == NULL) {
    fprintf(stderr, "Memory allocation failed");
    return -ENOMEM;
  }
  sprintf(temp, "%s/%s", basedir, filename);
  sysfsfp = fopen(temp, "r");
  if (sysfsfp == NULL) {
    ret = -errno;
    goto error_free;
  }
  tmp = fscanf(sysfsfp, "%i\n", &ret);
  fclose(sysfsfp);
  if (tmp != 1)
    ret = -ENODEV;
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    11ed <read_sysfs_posint+0x6d>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    0x2dfd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <read_sysfs_posint+0x103>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe19(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x30(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1237 <read_sysfs_posint+0xb7>
xor    %eax,%eax
mov    0x2da3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    1274 <read_sysfs_posint+0xf4>
mov    -0x28(%rbp),%rdi
lea    0xddf(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <fscanf@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x1,-0x20(%rbp)
je     126f <read_sysfs_posint+0xef>
xor    %eax,%eax
mov    0x2d5e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    1274 <read_sysfs_posint+0xf4>
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   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
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     120f <read_sysfs_posint+0x8f>
mov    %rax,%r14
lea    0xe58(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r12,%rcx
call   10a0 <sprintf@plt>
lea    0xe49(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1231 <read_sysfs_posint+0xb1>
mov    %rax,%rbx
lea    0xe34(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    $0x1,%ebp
jne    123a <read_sysfs_posint+0xba>
mov    0xc(%rsp),%ebx
jmp    1245 <read_sysfs_posint+0xc5>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2db5(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    124d <read_sysfs_posint+0xcd>
mov    0x2d98(%rip),%rax
jmp    1241 <read_sysfs_posint+0xc1>
mov    0x2d87(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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"%i\0A\00"
@ENODEV = global i32 0
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 i32*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i64 @strlen(i8* %10)
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
%14 = add nsw i64 %11, %13
%15 = add nsw i64 %14, 2
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %9
%17 = load i8*, i8** %9
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, 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* %3
br label %51
24: ; preds = %2
%25 = load i8*, i8** %9
%26 = load i8*, i8** %5
%27 = load i8*, i8** %4
%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)
%29 = load i8*, i8** %9
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %8
%31 = load i32*, i32** %8
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %36
33: ; preds = %24
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
store i32 %35, i32* %6
br label %47
36: ; preds = %24
%37 = load i32*, i32** %8
%38 = call i32 @fscanf(i32* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %6)
store i32 %38, i32* %7
%39 = load i32*, i32** %8
%40 = call i32 @fclose(i32* %39)
%41 = load i32, i32* %7
%42 = icmp ne i32 %41, 1
br i1 %42, label %43, label %46
43: ; preds = %36
%44 = load i32, i32* @ENODEV
%45 = sub nsw i32 0, %44
store i32 %45, i32* %6
br label %46
46: ; preds = %43, %36
br label %47
47: ; preds = %46, %33
%48 = load i8*, i8** %9
%49 = call i32 @free(i8* %48)
%50 = load i32, i32* %6
store i32 %50, i32* %3
br label %51
51: ; preds = %47, %19
%52 = load i32, i32* %3
ret i32 %52
}

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"%i\0A\00"
@ENODEV = global i32 0
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 %35
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)
%18 = tail call i32* @fopen(i8* %9, 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 = %16
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
br label %32
23: ; preds = %16
%24 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%25 = call i32 @fclose(i32* %18)
%26 = icmp eq i32 %24, 1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i32, i32* %3
br label %32
29: ; preds = %23
%30 = load i32, i32* @ENODEV
%31 = sub nsw i32 0, %30
br label %32
32: ; preds = %27, %29, %20
%33 = phi i32 [ %28, %27 ], [ %31, %29 ], [ %22, %20 ]
%34 = call i32 @free(i8* %9)
br label %35
35: ; preds = %32, %11
%36 = phi i32 [ %15, %11 ], [ %33, %32 ]
ret i32 %36
}