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

/* Variables and functions */
int O_APPEND;
int O_RDWR;
char *bbb_wildCardPath(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int write(int, char *, int);
int bbb_enableADC() {
  int fd = -1;
  static int ret = -1;
  char buffer[1024];
  char *ptr = "cape-bone-iio";
  int size = strlen(ptr);
  char *wpath = NULL;
  if ((wpath = bbb_wildCardPath("/sys/devices/bone_capemgr.*/slots")) != NULL) {
    if ((fd = open(wpath, O_RDWR | O_APPEND)) < 0) {
      return -1;
    }
    memset(buffer, 0, sizeof(buffer));
    if ((ret = read(fd, buffer, sizeof(buffer))) < 0) {
      return -1;
    }
    if (strncmp(buffer, ptr, size) != 0) {
      printf("Enabling ADC\n");
      ;
      if (write(fd, ptr, size) != size) {
        return -1;
      }
    }
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<bbb_enableADC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0xffffffff,-0x8(%rbp)
lea    0xe67(%rip),%rax
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x41c(%rbp)
movq   $0x0,-0x428(%rbp)
lea    0xe4a(%rip),%rdi
call   10a0 <bbb_wildCardPath@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
je     12bf <bbb_enableADC+0x13f>
mov    -0x428(%rbp),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    0x2ddf(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1210 <bbb_enableADC+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <bbb_enableADC+0x146>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,0x2e23(%rip)
cmp    $0x0,%eax
jge    1252 <bbb_enableADC+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <bbb_enableADC+0x146>
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     12b3 <bbb_enableADC+0x133>
lea    0xdb4(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x418(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1040 <write@plt>
cmp    -0x41c(%rbp),%eax
je     12ae <bbb_enableADC+0x12e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <bbb_enableADC+0x146>
jmp    12b3 <bbb_enableADC+0x133>
movl   $0x0,-0x4(%rbp)
jmp    12c6 <bbb_enableADC+0x146>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_enableADC>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
lea    0xe6c(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
lea    0xe6b(%rip),%rdi
call   10a0 <bbb_wildCardPath@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1236 <bbb_enableADC+0xb6>
mov    0x2e2b(%rip),%rcx
mov    0x2e0c(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %rax,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1236 <bbb_enableADC+0xb6>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
js     1236 <bbb_enableADC+0xb6>
lea    0xdfe(%rip),%rsi
mov    %rsp,%rdi
mov    %r14d,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1234 <bbb_enableADC+0xb4>
lea    0xe17(%rip),%rdi
call   1040 <puts@plt>
lea    0xddb(%rip),%rsi
mov    %ebp,%edi
mov    %r14d,%edx
call   1050 <write@plt>
cmp    %r14d,%eax
jne    1236 <bbb_enableADC+0xb6>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@bbb_enableADC.ret = internal global i32 -1
@.str = [14 x i8] c"cape-bone-iio\00"
@.str.1 = [34 x i8] c"/sys/devices/bone_capemgr.*/slots\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.2 = [14 x i8] c"Enabling ADC\0A\00"
define i32 @bbb_enableADC() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i32 -1, i32* %2
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %4
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %5
store i8* null, i8** %6
%9 = call i8* @bbb_wildCardPath(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i8* %9, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %44
11: ; preds = %0
%12 = load i8*, i8** %6
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_APPEND
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %12, i32 %15)
store i32 %16, i32* %2
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %11
store i32 -1, i32* %1
br label %45
19: ; preds = %11
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 1024)
%22 = load i32, i32* %2
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 1024)
store i32 %24, i32* @bbb_enableADC.ret
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %19
store i32 -1, i32* %1
br label %45
27: ; preds = %19
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%29 = load i8*, i8** %4
%30 = load i32, i32* %5
%31 = call i64 @strncmp(i8* %28, i8* %29, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %43
33: ; preds = %27
%34 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* %2
%36 = load i8*, i8** %4
%37 = load i32, i32* %5
%38 = call i32 @write(i32 %35, i8* %36, i32 %37)
%39 = load i32, i32* %5
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %33
store i32 -1, i32* %1
br label %45
42: ; preds = %33
br label %43
43: ; preds = %42, %27
store i32 0, i32* %1
br label %45
44: ; preds = %0
store i32 -1, i32* %1
br label %45
45: ; preds = %44, %43, %41, %26, %18
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
@.str = [14 x i8] c"cape-bone-iio\00"
@.str.1 = [34 x i8] c"/sys/devices/bone_capemgr.*/slots\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@str = [13 x i8] c"Enabling ADC\00"
define i32 @bbb_enableADC() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%4 = tail call i8* @bbb_wildCardPath(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %24, label %6
6: ; preds = %0
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_APPEND
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %4, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %6
%13 = call i32 @memset(i8* %2, i32 0, i32 1024)
%14 = call i32 @read(i32 %10, i8* %2, i32 1024)
%15 = icmp slt i32 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %12
%17 = call i64 @strncmp(i8* %2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%21 = call i32 @write(i32 %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3)
%22 = icmp eq i32 %21, %3
br i1 %22, label %23, label %24
23: ; preds = %19, %16
br label %24
24: ; preds = %0, %19, %12, %6, %23
%25 = phi i32 [ 0, %23 ], [ -1, %6 ], [ -1, %12 ], [ -1, %19 ], [ -1, %0 ]
ret i32 %25
}