#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;
float errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, float *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int read_sysfs_float(char *filename, char *basedir, float *val) {
  float ret = 0;
  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, "%f\n", val);
  fclose(sysfsfp);
error_free:
  free(temp);
  return ret;
}


x86 assembly (clang, -O0):
<read_sysfs_float>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-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   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f9 <read_sysfs_float+0x79>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1080 <syslog@plt>
xor    %eax,%eax
mov    0x2df1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1280 <read_sysfs_float+0x100>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe0d(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xe03(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1252 <read_sysfs_float+0xd2>
mov    0x2da1(%rip),%rax
movss  (%rax),%xmm0
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
movss  %xmm0,-0x24(%rbp)
jmp    126f <read_sysfs_float+0xef>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xdc0(%rip),%rsi
call   1060 <fscanf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
cvttss2si -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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,%r12
mov    %rdi,%r15
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1203 <read_sysfs_float+0x83>
mov    %rax,%rbx
lea    0xe69(%rip),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
mov    %r15,%rcx
call   10a0 <sprintf@plt>
lea    0xe5a(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1225 <read_sysfs_float+0xa5>
mov    %rax,%rbp
lea    0xe45(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1060 <fscanf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
jmp    123b <read_sysfs_float+0xbb>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
call   1080 <syslog@plt>
mov    0x2dc1(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    1243 <read_sysfs_float+0xc3>
mov    0x2dac(%rip),%rax
movss  (%rax),%xmm0
xorps  0xdc9(%rip),%xmm0
cvttss2si %xmm0,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
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 float 0.000000e+00
@.str.3 = [4 x i8] c"%f\0A\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 float
%9 = alloca i32*
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store float* %2, float** %7
store float 0.000000e+00, float* %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 eq i8* %18, null
br i1 %19, label %20, label %25
20: ; preds = %3
%21 = load i32, i32* @LOG_ERR
%22 = call i32 @syslog(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 %48
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)
%30 = load i8*, i8** %10
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %9
%32 = load i32*, i32** %9
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = load float, float* @errno
%36 = fneg float %35
store float %36, float* %8
br label %43
37: ; preds = %25
%38 = load i32*, i32** %9
%39 = load float*, float** %7
%40 = call i32 @fscanf(i32* %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), float* %39)
%41 = load i32*, i32** %9
%42 = call i32 @fclose(i32* %41)
br label %43
43: ; preds = %37, %34
%44 = load i8*, i8** %10
%45 = call i32 @free(i8* %44)
%46 = load float, float* %8
%47 = fptosi float %46 to i32
store i32 %47, i32* %4
br label %48
48: ; preds = %43, %20
%49 = load i32, i32* %4
ret i32 %49
}

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 float 0.000000e+00
@.str.3 = [4 x i8] c"%f\0A\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* @LOG_ERR
%12 = tail call i32 @syslog(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 %29
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 = tail call i32* @fopen(i8* %8, 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 %23
19: ; preds = %15
%20 = load float, float* @errno
%21 = fneg float %20
%22 = fptosi float %21 to i32
br label %26
23: ; preds = %15
%24 = tail call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), float* %2)
%25 = tail call i32 @fclose(i32* %17)
br label %26
26: ; preds = %23, %19
%27 = phi i32 [ %22, %19 ], [ 0, %23 ]
%28 = tail call i32 @free(i8* %8)
br label %29
29: ; preds = %26, %10
%30 = phi i32 [ %14, %10 ], [ %27, %26 ]
ret i32 %30
}