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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ read(int, char *, unsigned int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, unsigned int);
int adc_read(unsigned int ain, char *value, unsigned int length) {
  int fd, len;
  char filepath[64];
  snprintf(filepath, sizeof(filepath), "%s/in_voltage%d_raw",
           "/sys/bus/iio/devices/iio:device0", ain);
  if ((fd = open(filepath, O_RDONLY | O_NONBLOCK)) < 0)
    return -1;
  lseek(fd, 0, SEEK_SET);
  read(fd, value, length);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<adc_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    $0x40,%esi
lea    0xe8a(%rip),%rdx
lea    0xe97(%rip),%rcx
call   1030 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e52(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11b5 <adc_read+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <adc_read+0x96>
mov    -0x18(%rbp),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <read@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adc_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%r8d
lea    0xe96(%rip),%rdx
lea    0xea3(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e58(%rip),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11cb <adc_read+0x7b>
mov    %eax,%ebx
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1060 <read@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11d0 <adc_read+0x80>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"%s/in_voltage%d_raw\00"
@.str.1 = [33 x i8] c"/sys/bus/iio/devices/iio:device0\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@SEEK_SET = global i32 0
define i32 @adc_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [64 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%12 = load i32, i32* %5
%13 = call i32 @snprintf(i8* %11, i32 64, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%15 = load i32, i32* @O_RDONLY
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17)
store i32 %18, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %31
21: ; preds = %3
%22 = load i32, i32* %8
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @lseek(i32 %22, i32 0, i32 %23)
%25 = load i32, i32* %8
%26 = load i8*, i8** %6
%27 = load i32, i32* %7
%28 = call i32 @read(i32 %25, i8* %26, i32 %27)
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
store i32 0, i32* %4
br label %31
31: ; preds = %21, %20
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [20 x i8] c"%s/in_voltage%d_raw\00"
@.str.1 = [33 x i8] c"/sys/bus/iio/devices/iio:device0\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@SEEK_SET = global i32 0
define i32 @adc_read(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 64, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %8, %7
%10 = call i32 @open(i8* %5, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %3
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @lseek(i32 %10, i32 0, i32 %13)
%15 = call i32 @read(i32 %10, i8* %1, i32 %2)
%16 = call i32 @close(i32 %10)
br label %17
17: ; preds = %3, %12
%18 = phi i32 [ 0, %12 ], [ -1, %3 ]
ret i32 %18
}