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

/* Variables and functions */
int O_APPEND;
int O_RDWR;
char *bbb_wildCardPath(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int snprintf(char *, int, char *, int, int);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int write(int, char *, int);
int bbb_enablePwm(int header, int pin) {
  int fd = -1;
  int ret = -1;
  char buffer[4096];
  char ptr[128];
  char *pwm = "am33xx_pwm";
  int size = strlen(pwm);
  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) {
      close(fd);
      return -1;
    }
    if (strncmp(buffer, pwm, size) != 0) {
      printf("Enabling PWM\n");
      ;
      if (write(fd, pwm, size) != size) {
        close(fd);
        return -1;
      }
    }
    memset(ptr, 0, sizeof(ptr));
    ret = snprintf(ptr, sizeof(ptr), "bone_pwm_P%d_%d", header, pin);
    if (strncmp(buffer, ptr, ret) != 0) {
      printf("Enabling PWM P%d_%d\n", header, pin);
      ;
      if (write(fd, ptr, ret) != ret) {
        close(fd);
        return -1;
      }
    }
    close(fd);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<bbb_enablePwm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10c0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
lea    0xe3a(%rip),%rax
mov    %rax,-0x10a8(%rbp)
mov    -0x10a8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x10ac(%rbp)
movq   $0x0,-0x10b8(%rbp)
lea    0xe1a(%rip),%rdi
call   10c0 <bbb_wildCardPath@plt>
mov    %rax,-0x10b8(%rbp)
cmp    $0x0,%rax
je     13a0 <bbb_enablePwm+0x200>
mov    -0x10b8(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    0x2db2(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    123d <bbb_enablePwm+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <bbb_enablePwm+0x207>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
mov    -0x10(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   10a0 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1284 <bbb_enablePwm+0xe4>
mov    -0x10(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <bbb_enablePwm+0x207>
lea    -0x1020(%rbp),%rdi
mov    -0x10a8(%rbp),%rsi
mov    -0x10ac(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     12ef <bbb_enablePwm+0x14f>
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%edi
mov    -0x10a8(%rbp),%rsi
mov    -0x10ac(%rbp),%edx
call   1040 <write@plt>
cmp    -0x10ac(%rbp),%eax
je     12ea <bbb_enablePwm+0x14a>
mov    -0x10(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <bbb_enablePwm+0x207>
jmp    12ef <bbb_enablePwm+0x14f>
lea    -0x10a0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1080 <memset@plt>
lea    -0x10a0(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    $0x80,%esi
lea    0xd1f(%rip),%rdx
call   1070 <snprintf@plt>
mov    %eax,-0x14(%rbp)
lea    -0x1020(%rbp),%rdi
lea    -0x10a0(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     138c <bbb_enablePwm+0x1ec>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xcfa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%edi
lea    -0x10a0(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
cmp    -0x14(%rbp),%eax
je     1387 <bbb_enablePwm+0x1e7>
mov    -0x10(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <bbb_enablePwm+0x207>
jmp    138c <bbb_enablePwm+0x1ec>
mov    -0x10(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a7 <bbb_enablePwm+0x207>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_enablePwm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1088,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
lea    0xe32(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%r12d
lea    0xe2e(%rip),%rdi
call   10d0 <bbb_wildCardPath@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     12fa <bbb_enablePwm+0x14a>
mov    0x2df1(%rip),%rcx
mov    0x2dd2(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %rax,%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     12fa <bbb_enablePwm+0x14a>
mov    %eax,%ebp
lea    0x80(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1090 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   10b0 <read@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     12f3 <bbb_enablePwm+0x143>
lea    0xdb2(%rip),%rsi
lea    0x80(%rsp),%rdi
mov    %r12d,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1285 <bbb_enablePwm+0xd5>
lea    0xde8(%rip),%rdi
call   1040 <puts@plt>
lea    0xd8a(%rip),%rsi
mov    %ebp,%edi
mov    %r12d,%edx
call   1050 <write@plt>
cmp    %r12d,%eax
jne    12f3 <bbb_enablePwm+0x143>
mov    %rsp,%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1090 <memset@plt>
lea    0xd8f(%rip),%rdx
mov    %r13,%rdi
mov    $0x80,%esi
mov    %r15d,%ecx
mov    %r14d,%r8d
call   1080 <snprintf@plt>
mov    %eax,%r12d
lea    0x80(%rsp),%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     12f1 <bbb_enablePwm+0x141>
lea    0xd6b(%rip),%rdi
mov    %r15d,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %r12d,%edx
call   1050 <write@plt>
cmp    %r12d,%eax
jne    12f3 <bbb_enablePwm+0x143>
xor    %ebx,%ebx
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %ebx,%eax
add    $0x1088,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"am33xx_pwm\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 PWM\0A\00"
@.str.3 = [16 x i8] c"bone_pwm_P%d_%d\00"
@.str.4 = [21 x i8] c"Enabling PWM P%d_%d\0A\00"
define i32 @bbb_enablePwm(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca [128 x i8]
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %10
%13 = load i8*, i8** %10
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %11
store i8* null, i8** %12
%15 = call i8* @bbb_wildCardPath(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i8* %15, i8** %12
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %82
17: ; preds = %2
%18 = load i8*, i8** %12
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_APPEND
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %18, i32 %21)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -1, i32* %3
br label %83
25: ; preds = %17
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 0, i32 4096)
%28 = load i32, i32* %6
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 4096)
store i32 %30, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %3
br label %83
35: ; preds = %25
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%37 = load i8*, i8** %10
%38 = load i32, i32* %11
%39 = call i64 @strncmp(i8* %36, i8* %37, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %53
41: ; preds = %35
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* %6
%44 = load i8*, i8** %10
%45 = load i32, i32* %11
%46 = call i32 @write(i32 %43, i8* %44, i32 %45)
%47 = load i32, i32* %11
%48 = icmp ne i32 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %41
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %3
br label %83
52: ; preds = %41
br label %53
53: ; preds = %52, %35
%54 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%55 = call i32 @memset(i8* %54, i32 0, i32 128)
%56 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%57 = load i32, i32* %4
%58 = load i32, i32* %5
%59 = call i32 @snprintf(i8* %56, i32 128, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %57, i32 %58)
store i32 %59, i32* %7
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%61 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%62 = load i32, i32* %7
%63 = call i64 @strncmp(i8* %60, i8* %61, i32 %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %79
65: ; preds = %53
%66 = load i32, i32* %4
%67 = load i32, i32* %5
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %66, i32 %67)
%69 = load i32, i32* %6
%70 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%71 = load i32, i32* %7
%72 = call i32 @write(i32 %69, i8* %70, i32 %71)
%73 = load i32, i32* %7
%74 = icmp ne i32 %72, %73
br i1 %74, label %75, label %78
75: ; preds = %65
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
store i32 -1, i32* %3
br label %83
78: ; preds = %65
br label %79
79: ; preds = %78, %53
%80 = load i32, i32* %6
%81 = call i32 @close(i32 %80)
store i32 0, i32* %3
br label %83
82: ; preds = %2
store i32 -1, i32* %3
br label %83
83: ; preds = %82, %79, %75, %49, %32, %24
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@.str = [11 x i8] c"am33xx_pwm\00"
@.str.1 = [34 x i8] c"/sys/devices/bone_capemgr.*/slots\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.3 = [16 x i8] c"bone_pwm_P%d_%d\00"
@.str.4 = [21 x i8] c"Enabling PWM P%d_%d\0A\00"
@str = [13 x i8] c"Enabling PWM\00"
define i32 @bbb_enablePwm(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = tail call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%8 = tail call i8* @bbb_wildCardPath(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %40, label %10
10: ; preds = %2
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_APPEND
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* %8, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %40, label %16
16: ; preds = %10
%17 = call i32 @memset(i8* %5, i32 0, i32 4096)
%18 = call i32 @read(i32 %14, i8* %5, i32 4096)
%19 = icmp slt i32 %18, 0
br i1 %19, label %37, label %20
20: ; preds = %16
%21 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%25 = call i32 @write(i32 %14, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
%26 = icmp eq i32 %25, %7
br i1 %26, label %27, label %37
27: ; preds = %23, %20
%28 = call i32 @memset(i8* %6, i32 0, i32 128)
%29 = call i32 @snprintf(i8* %6, i32 128, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %0, i32 %1)
%30 = call i64 @strncmp(i8* %5, i8* %6, i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %27
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %0, i32 %1)
%34 = call i32 @write(i32 %14, i8* %6, i32 %29)
%35 = icmp eq i32 %34, %29
br i1 %35, label %36, label %37
36: ; preds = %32, %27
br label %37
37: ; preds = %32, %23, %16, %36
%38 = phi i32 [ 0, %36 ], [ -1, %16 ], [ -1, %23 ], [ -1, %32 ]
%39 = call i32 @close(i32 %14)
br label %40
40: ; preds = %37, %2, %10
%41 = phi i32 [ -1, %10 ], [ -1, %2 ], [ %38, %37 ]
ret i32 %41
}