#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 O_APPEND;
int O_RDWR;
char *bbb_wildCardPath(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int snprintf(char *, int, char *, int);
scalar_t__ strncmp(char *, char *, int);
int write(int, char *, int);
int bbb_enableSerial(int index) {
  int fd = -1;
  static int ret = -1;
  char buffer[1024];
  char path[128];
  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;
    }
    memset(path, 0, sizeof(path));
    ret = snprintf(path, sizeof(path), "BB-UART%d", index);
    if (strncmp(buffer, path, ret) != 0) {
      printf("Enabling Serial %s\n", path);
      ;
      if (write(fd, path, ret) != ret) {
        return -1;
      }
    }
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<bbb_enableSerial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4a0,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movq   $0x0,-0x498(%rbp)
lea    0xe59(%rip),%rdi
call   10a0 <bbb_wildCardPath@plt>
mov    %rax,-0x498(%rbp)
cmp    $0x0,%rax
je     12dd <bbb_enableSerial+0x15d>
mov    -0x498(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11f3 <bbb_enableSerial+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e4 <bbb_enableSerial+0x164>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,0x2e40(%rip)
cmp    $0x0,%eax
jge    1235 <bbb_enableSerial+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e4 <bbb_enableSerial+0x164>
lea    -0x490(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
lea    -0x490(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x80,%esi
lea    0xdc4(%rip),%rdx
call   1060 <snprintf@plt>
mov    %eax,0x2df7(%rip)
lea    -0x410(%rbp),%rdi
lea    -0x490(%rbp),%rsi
mov    0x2de3(%rip),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     12d1 <bbb_enableSerial+0x151>
lea    -0x490(%rbp),%rsi
lea    0xd92(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x490(%rbp),%rsi
mov    0x2db1(%rip),%edx
call   1040 <write@plt>
cmp    0x2da6(%rip),%eax
je     12cc <bbb_enableSerial+0x14c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e4 <bbb_enableSerial+0x164>
jmp    12d1 <bbb_enableSerial+0x151>
movl   $0x0,-0x4(%rbp)
jmp    12e4 <bbb_enableSerial+0x164>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_enableSerial>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x488,%rsp
mov    %edi,%r14d
lea    0xe69(%rip),%rdi
call   10a0 <bbb_wildCardPath@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1270 <bbb_enableSerial+0xf0>
mov    0x2e37(%rip),%rcx
mov    0x2e18(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %rax,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1270 <bbb_enableSerial+0xf0>
mov    %eax,%ebp
lea    0x80(%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>
mov    %eax,0x2e66(%rip)
test   %eax,%eax
js     1270 <bbb_enableSerial+0xf0>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
lea    0xe0b(%rip),%rdx
mov    %r15,%rdi
mov    $0x80,%esi
mov    %r14d,%ecx
call   1060 <snprintf@plt>
mov    %eax,0x2e33(%rip)
lea    0x80(%rsp),%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     126e <bbb_enableSerial+0xee>
lea    0xde1(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
call   1050 <printf@plt>
mov    0x2e04(%rip),%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <write@plt>
cmp    0x2df4(%rip),%eax
jne    1270 <bbb_enableSerial+0xf0>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x488,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@bbb_enableSerial.ret = internal unnamed_addr global i32 -1
@.str = [34 x i8] c"/sys/devices/bone_capemgr.*/slots\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [10 x i8] c"BB-UART%d\00"
@.str.2 = [20 x i8] c"Enabling Serial %s\0A\00"
define i32 @bbb_enableSerial(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = tail call i8* @bbb_wildCardPath(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %30, label %8
8: ; preds = %1
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_APPEND
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %6, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %30, label %14
14: ; preds = %8
%15 = call i32 @memset(i8* %4, i32 0, i32 1024)
%16 = call i32 @read(i32 %12, i8* %4, i32 1024)
store i32 %16, i32* @bbb_enableSerial.ret
%17 = icmp slt i32 %16, 0
br i1 %17, label %30, label %18
18: ; preds = %14
%19 = call i32 @memset(i8* %5, i32 0, i32 128)
%20 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %0)
store i32 %20, i32* @bbb_enableSerial.ret
%21 = call i64 @strncmp(i8* %4, i8* %5, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %18
%24 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%25 = load i32, i32* @bbb_enableSerial.ret
%26 = call i32 @write(i32 %12, i8* %5, i32 %25)
%27 = load i32, i32* @bbb_enableSerial.ret
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %23, %18
br label %30
30: ; preds = %1, %23, %14, %8, %29
%31 = phi i32 [ 0, %29 ], [ -1, %8 ], [ -1, %14 ], [ -1, %23 ], [ -1, %1 ]
ret i32 %31
}