#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 *, float *);
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_float(const char *filename, const char *basedir, float *val) {
  int ret = 0;
  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, "%f\n", val) != 1) {
    ret = errno ? -errno : -ENODATA;
    if (fclose(sysfsfp))
      perror("read_sysfs_float(): Failed to close dir");
    goto error_free;
  }
  if (fclose(sysfsfp))
    ret = -errno;
error_free:
  free(temp);
  return ret;
}


x86 assembly (clang, -O0):
<read_sysfs_float>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1208 <read_sysfs_float+0x78>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    0x2dda(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    131e <read_sysfs_float+0x18e>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdfe(%rip),%rsi
call   10b0 <sprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1232 <read_sysfs_float+0xa2>
jmp    130f <read_sysfs_float+0x17f>
mov    -0x38(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1264 <read_sysfs_float+0xd4>
xor    %eax,%eax
mov    0x2d6e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    130f <read_sysfs_float+0x17f>
mov    0x2d5d(%rip),%rax
movl   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd9f(%rip),%rsi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
je     12e9 <read_sysfs_float+0x159>
mov    0x2d33(%rip),%rax
cmpl   $0x0,(%rax)
je     12b1 <read_sysfs_float+0x121>
xor    %eax,%eax
mov    0x2d21(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x44(%rbp)
jmp    12bf <read_sysfs_float+0x12f>
xor    %eax,%eax
mov    0x2d2e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12e4 <read_sysfs_float+0x154>
lea    0xd44(%rip),%rdi
call   10a0 <perror@plt>
jmp    130f <read_sysfs_float+0x17f>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     130a <read_sysfs_float+0x17a>
xor    %eax,%eax
mov    0x2cc3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    130f <read_sysfs_float+0x17f>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sysfs_float>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1236 <read_sysfs_float+0xa6>
mov    %rax,%r12
lea    0xe49(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %rbp,%rcx
call   10b0 <sprintf@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1283 <read_sysfs_float+0xf3>
lea    0xe5a(%rip),%rsi
mov    %r12,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1258 <read_sysfs_float+0xc8>
mov    %rax,%r15
mov    0x2dc2(%rip),%rbx
movl   $0x0,(%rbx)
lea    0xe0c(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    1265 <read_sysfs_float+0xd5>
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     1283 <read_sysfs_float+0xf3>
xor    %ebp,%ebp
sub    (%rbx),%ebp
jmp    1283 <read_sysfs_float+0xf3>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d86(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    128b <read_sysfs_float+0xfb>
mov    0x2d69(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    1283 <read_sysfs_float+0xf3>
mov    (%rbx),%ebp
test   %ebp,%ebp
jne    1274 <read_sysfs_float+0xe4>
mov    0x2d76(%rip),%rax
mov    (%rax),%ebp
neg    %ebp
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    1296 <read_sysfs_float+0x106>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd86(%rip),%rdi
call   10a0 <perror@plt>
jmp    1283 <read_sysfs_float+0xf3>

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

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