#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*/ path;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
char *bbb_wildCardPath(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
int bbb_getADC(int id) {
  int fd = -1;
  static int ret = -1;
  char buffer[128];
  char path[128];
  char *wpath = NULL;
  memset(path, 0, sizeof(path));
  snprintf(path, sizeof(path), "%s/AIN%d", "/sys/devices/ocp.*/helper.*", id);
  if ((wpath = bbb_wildCardPath(path)) != NULL) {
    if ((fd = open(wpath, O_RDONLY)) < 0) {
      return -1;
    }
    memset(buffer, 0, sizeof(buffer));
    if ((ret = read(fd, buffer, sizeof(buffer))) < 0) {
      return -1;
    }
    ret = atoi(buffer);
  }
  return ret;
}


x86 assembly (clang, -O0):
<bbb_getADC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movq   $0x0,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    $0x80,%esi
lea    0xe56(%rip),%rdx
lea    0xe58(%rip),%rcx
call   1030 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1080 <bbb_wildCardPath@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
je     1254 <bbb_getADC+0xf4>
mov    -0x118(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1200 <bbb_getADC+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <bbb_getADC+0xfd>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,0x2e23(%rip)
cmp    $0x0,%eax
jge    1242 <bbb_getADC+0xe2>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <bbb_getADC+0xfd>
lea    -0x90(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,0x2dfc(%rip)
mov    0x2df6(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_getADC>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %edi,%ebx
lea    0x80(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
lea    0xe75(%rip),%rdx
lea    0xe77(%rip),%rcx
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %ebx,%r8d
call   1030 <snprintf@plt>
mov    %rbp,%rdi
call   1080 <bbb_wildCardPath@plt>
test   %rax,%rax
je     1208 <bbb_getADC+0xa8>
mov    0x2e32(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1060 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     120e <bbb_getADC+0xae>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,0x2e5e(%rip)
test   %eax,%eax
js     120e <bbb_getADC+0xae>
mov    %rsp,%rdi
call   1070 <atoi@plt>
mov    %eax,%ebx
mov    %eax,0x2e4a(%rip)
jmp    120e <bbb_getADC+0xae>
mov    0x2e42(%rip),%ebx
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@bbb_getADC.ret = internal global i32 -1
@.str = [9 x i8] c"%s/AIN%d\00"
@.str.1 = [28 x i8] c"/sys/devices/ocp.*/helper.*\00"
@O_RDONLY = global i32 0
define i32 @bbb_getADC(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = alloca [128 x i8]
%7 = alloca i8*
store i32 %0, i32* %3
store i32 -1, i32* %4
store i8* null, i8** %7
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 128)
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* %3
%12 = call i32 @snprintf(i8* %10, i32 128, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%14 = call i8* @bbb_wildCardPath(i8* %13)
store i8* %14, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %33
16: ; preds = %1
%17 = load i8*, i8** %7
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -1, i32* %2
br label %35
22: ; preds = %16
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 128)
%25 = load i32, i32* %4
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 128)
store i32 %27, i32* @bbb_getADC.ret
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 -1, i32* %2
br label %35
30: ; preds = %22
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* @bbb_getADC.ret
br label %33
33: ; preds = %30, %1
%34 = load i32, i32* @bbb_getADC.ret
store i32 %34, i32* %2
br label %35
35: ; preds = %33, %29, %21
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@bbb_getADC.ret = internal unnamed_addr global i32 -1
@.str = [9 x i8] c"%s/AIN%d\00"
@.str.1 = [28 x i8] c"/sys/devices/ocp.*/helper.*\00"
@O_RDONLY = global i32 0
define i32 @bbb_getADC(i32 %0) {
%2 = alloca [128 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 0, i32 128)
%7 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%8 = call i8* @bbb_wildCardPath(i8* %5)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i32, i32* @bbb_getADC.ret
br label %22
12: ; preds = %1
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %8, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = call i32 @memset(i8* %4, i32 0, i32 128)
%18 = call i32 @read(i32 %14, i8* %4, i32 128)
store i32 %18, i32* @bbb_getADC.ret
%19 = icmp slt i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = call i32 @atoi(i8* %4)
store i32 %21, i32* @bbb_getADC.ret
br label %22
22: ; preds = %20, %10, %16, %12
%23 = phi i32 [ -1, %12 ], [ -1, %16 ], [ %11, %10 ], [ %21, %20 ]
ret i32 %23
}