#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 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*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
int read_sysfs_posint(char *filename, char *basedir) {
  int ret;
  FILE *sysfsfp;
  char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  if (temp == NULL) {
    printf("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, "%d\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   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e4 <read_sysfs_posint+0x64>
lea    0xe34(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    0x2e06(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    125a <read_sysfs_posint+0xda>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe22(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x30(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122e <read_sysfs_posint+0xae>
xor    %eax,%eax
mov    0x2db4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    124b <read_sysfs_posint+0xcb>
mov    -0x28(%rbp),%rdi
lea    0xde8(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <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   1080 <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     121c <read_sysfs_posint+0x9c>
mov    %rax,%rbp
lea    0xe39(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1070 <fscanf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%ebp
jmp    1227 <read_sysfs_posint+0xa7>
lea    0xdf6(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dca(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    122f <read_sysfs_posint+0xaf>
mov    0x2db5(%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):
@.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"
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 %43
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)
%27 = load i8*, i8** %8
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %28, i32** %7
%29 = load i32*, i32** %7
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = load i32, i32* @errno
%33 = sub nsw i32 0, %32
store i32 %33, i32* %6
br label %39
34: ; preds = %22
%35 = load i32*, i32** %7
%36 = call i32 @fscanf(i32* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %6)
%37 = load i32*, i32** %7
%38 = call i32 @fclose(i32* %37)
br label %39
39: ; preds = %34, %31
%40 = load i8*, i8** %8
%41 = call i32 @free(i8* %40)
%42 = load i32, i32* %6
store i32 %42, i32* %3
br label %43
43: ; preds = %39, %18
%44 = load i32, i32* %3
ret i32 %44
}

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"
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 %29
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)
%17 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
br label %26
22: ; preds = %15
%23 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%24 = call i32 @fclose(i32* %17)
%25 = load i32, i32* %3
br label %26
26: ; preds = %22, %19
%27 = phi i32 [ %25, %22 ], [ %21, %19 ]
%28 = call i32 @free(i8* %9)
br label %29
29: ; preds = %26, %11
%30 = phi i32 [ %14, %11 ], [ %27, %26 ]
ret i32 %30
}