#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 ENOMEM;
int /*<<< orphan*/ LOG_ERR;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int read_sysfs_posint(char *filename, char *basedir) {
  int ret;
  FILE *sysfsfp;
  char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  if (temp == NULL) {
    syslog(LOG_ERR, "Memory allocation failed");
    return -ENOMEM;
  }
  sprintf(temp, "%s/%s", basedir, filename);
  sysfsfp = fopen(temp, "r");
  if (sysfsfp == NULL) {
    ret = -errno;
    goto error_free;
  }
  fscanf(sysfsfp, "%i\n", &ret);
  fclose(sysfsfp);
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   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ed <read_sysfs_posint+0x6d>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
call   1080 <syslog@plt>
xor    %eax,%eax
mov    0x2dfd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1263 <read_sysfs_posint+0xe3>
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    0x2dab(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    1254 <read_sysfs_posint+0xd4>
mov    -0x28(%rbp),%rdi
lea    0xddf(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
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   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1203 <read_sysfs_posint+0x83>
mov    %rax,%rbx
lea    0xe5d(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   10a0 <sprintf@plt>
lea    0xe4e(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1225 <read_sysfs_posint+0xa5>
mov    %rax,%rbp
lea    0xe39(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%ebp
jmp    1230 <read_sysfs_posint+0xb0>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
call   1080 <syslog@plt>
mov    0x2dc1(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    1238 <read_sysfs_posint+0xb8>
mov    0x2dac(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@LOG_ERR = 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"
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 %23
18: ; preds = %2
%19 = load i32, i32* @LOG_ERR
%20 = call i32 @syslog(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 %44
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)
%28 = load i8*, i8** %8
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %29, i32** %7
%30 = load i32*, i32** %7
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i32, i32* @errno
%34 = sub nsw i32 0, %33
store i32 %34, i32* %6
br label %40
35: ; preds = %23
%36 = load i32*, i32** %7
%37 = call i32 @fscanf(i32* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %6)
%38 = load i32*, i32** %7
%39 = call i32 @fclose(i32* %38)
br label %40
40: ; preds = %35, %32
%41 = load i8*, i8** %8
%42 = call i32 @free(i8* %41)
%43 = load i32, i32* %6
store i32 %43, i32* %3
br label %44
44: ; preds = %40, %18
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@LOG_ERR = 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"
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* @LOG_ERR
%13 = tail call i32 @syslog(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 %30
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 %27
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 = load i32, i32* %3
br label %27
27: ; preds = %23, %20
%28 = phi i32 [ %26, %23 ], [ %22, %20 ]
%29 = call i32 @free(i8* %9)
br label %30
30: ; preds = %27, %11
%31 = phi i32 [ %15, %11 ], [ %28, %27 ]
ret i32 %31
}