#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
float atof(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
scalar_t__ sprintf(char *, char const *, int);
float read_voltage_scale(int pin_number) {
  const char *scale_file_path =
      "/sys/bus/iio/devices/iio\:device0/in_voltage%d_scale";
  int fscale, a_err = -1;
  char scale_file_buffer[256];
  if (sprintf(scale_file_buffer, scale_file_path, pin_number) < 0) {
    printf("Can't create scale file path\n");
    return a_err;
  }
  fscale = open(scale_file_buffer, O_RDONLY);
  if (fscale < 0) {
    printf("Can't open scale device\n");
    return a_err;
  }
  char buf[1024];
  int ret = read(fscale, buf, sizeof(buf));
  if (ret == a_err) {
    printf("Couldn't get value from Analog Pin\n");
    return -1;
  }
  buf[ret] = '\0';
  return atof(buf);
}


x86 assembly (clang, -O0):
<read_voltage_scale>:
push   %rbp
mov    %rsp,%rbp
sub    $0x530,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe9f(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1070 <sprintf@plt>
cmp    $0x0,%rax
jge    11a8 <read_voltage_scale+0x58>
lea    0xea4(%rip),%rdi
call   1040 <printf@plt>
cvtsi2ssl -0x18(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    124c <read_voltage_scale+0xfc>
lea    -0x120(%rbp),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e5 <read_voltage_scale+0x95>
lea    0xe85(%rip),%rdi
call   1040 <printf@plt>
cvtsi2ssl -0x18(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    124c <read_voltage_scale+0xfc>
mov    -0x14(%rbp),%edi
lea    -0x520(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x524(%rbp)
mov    -0x524(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    122c <read_voltage_scale+0xdc>
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
movss  0xdde(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    124c <read_voltage_scale+0xfc>
movslq -0x524(%rbp),%rax
movb   $0x0,-0x520(%rbp,%rax,1)
lea    -0x520(%rbp),%rdi
call   1030 <atof@plt>
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x530,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_voltage_scale>:
sub    $0x508,%rsp
mov    %edi,%edx
lea    0xea4(%rip),%rsi
mov    %rsp,%rdi
call   1070 <sprintf@plt>
test   %rax,%rax
js     11ba <read_voltage_scale+0x6a>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11c3 <read_voltage_scale+0x73>
lea    0x100(%rsp),%rsi
mov    %eax,%edi
mov    $0x400,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11cc <read_voltage_scale+0x7c>
cltq
movb   $0x0,0x100(%rsp,%rax,1)
lea    0x100(%rsp),%rdi
call   1040 <atof@plt>
add    $0x508,%rsp
ret
lea    0xeb2(%rip),%rdi
jmp    11d3 <read_voltage_scale+0x83>
lea    0xe91(%rip),%rdi
jmp    11d3 <read_voltage_scale+0x83>
lea    0xe65(%rip),%rdi
call   1030 <puts@plt>
movss  0xe20(%rip),%xmm0
add    $0x508,%rsp
ret

LLVM IR (-O0):
@.str = [52 x i8] c"/sys/bus/iio/devices/iio:device0/in_voltage%d_scale\00"
@.str.1 = [30 x i8] c"Can't create scale file path\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [25 x i8] c"Can't open scale device\0A\00"
@.str.3 = [36 x i8] c"Couldn't get value from Analog Pin\0A\00"
define float @read_voltage_scale(i32 %0) {
%2 = alloca float
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca [1024 x i8]
%9 = alloca i32
store i32 %0, i32* %3
store i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8** %4
store i32 -1, i32* %6
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = load i32, i32* %3
%13 = call i64 @sprintf(i8* %10, i8* %11, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* %6
%18 = sitofp i32 %17 to float
store float %18, float* %2
br label %44
19: ; preds = %1
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* %6
%28 = sitofp i32 %27 to float
store float %28, float* %2
br label %44
29: ; preds = %19
%30 = load i32, i32* %5
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 1024)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = load i32, i32* %6
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
store float -1.000000e+00, float* %2
br label %44
38: ; preds = %29
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %40
store i8 0, i8* %41
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%43 = call float @atof(i8* %42)
store float %43, float* %2
br label %44
44: ; preds = %38, %36, %25, %15
%45 = load float, float* %2
ret float %45
}

LLVM IR (-O2):
@.str = [52 x i8] c"/sys/bus/iio/devices/iio:device0/in_voltage%d_scale\00"
@O_RDONLY = global i32 0
@str = [35 x i8] c"Couldn't get value from Analog Pin\00"
@str.4 = [24 x i8] c"Can't open scale device\00"
@str.5 = [29 x i8] c"Can't create scale file path\00"
define float @read_voltage_scale(i32 %0) {
%2 = alloca [256 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = call i64 @sprintf(i8* %4, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.5, i64 0, i64 0))
br label %27
9: ; preds = %1
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %4, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.4, i64 0, i64 0))
br label %27
15: ; preds = %9
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = call i32 @read(i32 %11, i8* %16, i32 1024)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %25
21: ; preds = %15
%22 = sext i32 %17 to i64
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %22
store i8 0, i8* %23
%24 = call float @atof(i8* %16)
br label %25
25: ; preds = %21, %19
%26 = phi float [ -1.000000e+00, %19 ], [ %24, %21 ]
br label %27
27: ; preds = %25, %13, %7
%28 = phi float [ -1.000000e+00, %7 ], [ -1.000000e+00, %13 ], [ %26, %25 ]
ret float %28
}