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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
char *bbb_wildCardPath(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int, int);
int bbb_getPwmPolarity(int header, int pin) {
  int ret = -1;
  int fd = -1;
  char path[128];
  char ptr[128];
  char *wpath = NULL;
  if (header > 0 && pin > 0) {
    memset(path, 0, sizeof(path));
    snprintf(path, sizeof(path), "%s/pwm_test_P%d_%d.*/polarity",
             "/sys/devices/ocp.*", header, pin);
    if ((wpath = bbb_wildCardPath(path)) != NULL) {
      if ((fd = open(wpath, O_RDONLY)) < 0) {
        return -1;
      }
      memset(ptr, 0, sizeof(ptr));
      if (read(fd, ptr, sizeof(ptr)) <= 0) {
        ret = -1;
      } else {
        ret = atoi(ptr);
      }
      close(fd);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<bbb_getPwmPolarity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x128(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    128b <bbb_getPwmPolarity+0x11b>
cmpl   $0x0,-0xc(%rbp)
jle    128b <bbb_getPwmPolarity+0x11b>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    -0xc(%rbp),%r9d
mov    $0x80,%esi
lea    0xe24(%rip),%rdx
lea    0xe3b(%rip),%rcx
call   1030 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
call   1090 <bbb_wildCardPath@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
je     1286 <bbb_getPwmPolarity+0x116>
mov    -0x128(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1232 <bbb_getPwmPolarity+0xc2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <bbb_getPwmPolarity+0x121>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jg     126f <bbb_getPwmPolarity+0xff>
movl   $0xffffffff,-0x10(%rbp)
jmp    127e <bbb_getPwmPolarity+0x10e>
lea    -0x120(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    128b <bbb_getPwmPolarity+0x11b>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_getPwmPolarity>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    $0xffffffff,%r14d
test   %edi,%edi
jle    1233 <bbb_getPwmPolarity+0xc3>
mov    %esi,%ebp
test   %esi,%esi
jle    1233 <bbb_getPwmPolarity+0xc3>
mov    %edi,%ebx
lea    0x80(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
lea    0xe4b(%rip),%rdx
lea    0xe62(%rip),%rcx
mov    %r15,%rdi
mov    $0x80,%esi
mov    %ebx,%r8d
mov    %ebp,%r9d
call   1030 <snprintf@plt>
mov    %r15,%rdi
call   1090 <bbb_wildCardPath@plt>
test   %rax,%rax
je     1233 <bbb_getPwmPolarity+0xc3>
mov    0x2e05(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1233 <bbb_getPwmPolarity+0xc3>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    1226 <bbb_getPwmPolarity+0xb6>
mov    %rsp,%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
jmp    122c <bbb_getPwmPolarity+0xbc>
mov    $0xffffffff,%r14d
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"%s/pwm_test_P%d_%d.*/polarity\00"
@.str.1 = [19 x i8] c"/sys/devices/ocp.*\00"
@O_RDONLY = global i32 0
define i32 @bbb_getPwmPolarity(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [128 x i8]
%9 = alloca [128 x i8]
%10 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i8* null, i8** %10
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %47
13: ; preds = %2
%14 = load i32, i32* %5
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %47
16: ; preds = %13
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%18 = call i32 @memset(i8* %17, i32 0, i32 128)
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%20 = load i32, i32* %4
%21 = load i32, i32* %5
%22 = call i32 @snprintf(i8* %19, i32 128, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %21)
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%24 = call i8* @bbb_wildCardPath(i8* %23)
store i8* %24, i8** %10
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %46
26: ; preds = %16
%27 = load i8*, i8** %10
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 -1, i32* %3
br label %49
32: ; preds = %26
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%34 = call i32 @memset(i8* %33, i32 0, i32 128)
%35 = load i32, i32* %7
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%37 = call i64 @read(i32 %35, i8* %36, i32 128)
%38 = icmp sle i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %32
store i32 -1, i32* %6
br label %43
40: ; preds = %32
%41 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%42 = call i32 @atoi(i8* %41)
store i32 %42, i32* %6
br label %43
43: ; preds = %40, %39
%44 = load i32, i32* %7
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %43, %16
br label %47
47: ; preds = %46, %13, %2
%48 = load i32, i32* %6
store i32 %48, i32* %3
br label %49
49: ; preds = %47, %31
%50 = load i32, i32* %3
ret i32 %50
}

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