#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;
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*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(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) {
    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, "%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   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f0 <read_sysfs_float+0x70>
lea    0xe28(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    0x2dfa(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1277 <read_sysfs_float+0xf7>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe16(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1249 <read_sysfs_float+0xc9>
mov    0x2daa(%rip),%rax
movss  (%rax),%xmm0
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
movss  %xmm0,-0x24(%rbp)
jmp    1266 <read_sysfs_float+0xe6>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xdc9(%rip),%rsi
call   1070 <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   1080 <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     121c <read_sysfs_float+0x9c>
mov    %rax,%rbp
lea    0xe45(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1070 <fscanf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
jmp    1232 <read_sysfs_float+0xb2>
lea    0xe06(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dca(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    123a <read_sysfs_float+0xba>
mov    0x2db5(%rip),%rax
movss  (%rax),%xmm0
xorps  0xdd2(%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):
@.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 %24
20: ; preds = %3
%21 = call i32 @printf(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* %4
br label %47
24: ; preds = %3
%25 = load i8*, i8** %10
%26 = load i8*, i8** %6
%27 = load i8*, i8** %5
%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** %10
%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** %9
%31 = load i32*, i32** %9
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %36
33: ; preds = %24
%34 = load float, float* @errno
%35 = fneg float %34
store float %35, float* %8
br label %42
36: ; preds = %24
%37 = load i32*, i32** %9
%38 = load float*, float** %7
%39 = call i32 @fscanf(i32* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), float* %38)
%40 = load i32*, i32** %9
%41 = call i32 @fclose(i32* %40)
br label %42
42: ; preds = %36, %33
%43 = load i8*, i8** %10
%44 = call i32 @free(i8* %43)
%45 = load float, float* %8
%46 = fptosi float %45 to i32
store i32 %46, i32* %4
br label %47
47: ; preds = %42, %20
%48 = load i32, i32* %4
ret i32 %48
}

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