#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_getPwmPeriod(int header, int pin) {
  static 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.*/period",
             "/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_getPwmPeriod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movq   $0x0,-0x118(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    128a <bbb_getPwmPeriod+0x11a>
cmpl   $0x0,-0xc(%rbp)
jle    128a <bbb_getPwmPeriod+0x11a>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    -0xc(%rbp),%r9d
mov    $0x80,%esi
lea    0xe2b(%rip),%rdx
lea    0xe40(%rip),%rcx
call   1030 <snprintf@plt>
lea    -0x90(%rbp),%rdi
call   1090 <bbb_wildCardPath@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
je     1285 <bbb_getPwmPeriod+0x115>
mov    -0x118(%rbp),%rdi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    122b <bbb_getPwmPeriod+0xbb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <bbb_getPwmPeriod+0x123>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jg     126b <bbb_getPwmPeriod+0xfb>
movl   $0xffffffff,0x2df2(%rip)
ff ff ff
jmp    127d <bbb_getPwmPeriod+0x10d>
lea    -0x110(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,0x2ddb(%rip)
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
jmp    128a <bbb_getPwmPeriod+0x11a>
mov    0x2dc8(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_getPwmPeriod>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
test   %edi,%edi
jle    1234 <bbb_getPwmPeriod+0xc4>
mov    %esi,%ebx
test   %esi,%esi
jle    1234 <bbb_getPwmPeriod+0xc4>
mov    %edi,%ebp
lea    0x80(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
lea    0xe53(%rip),%rdx
lea    0xe68(%rip),%rcx
mov    %r14,%rdi
mov    $0x80,%esi
mov    %ebp,%r8d
mov    %ebx,%r9d
call   1030 <snprintf@plt>
mov    %r14,%rdi
call   1090 <bbb_wildCardPath@plt>
test   %rax,%rax
je     1234 <bbb_getPwmPeriod+0xc4>
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     121b <bbb_getPwmPeriod+0xab>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    1222 <bbb_getPwmPeriod+0xb2>
mov    %rsp,%rdi
call   1080 <atoi@plt>
jmp    1227 <bbb_getPwmPeriod+0xb7>
mov    $0xffffffff,%eax
jmp    123a <bbb_getPwmPeriod+0xca>
mov    $0xffffffff,%eax
mov    %eax,0x2e2b(%rip)
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2e1e(%rip),%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@bbb_getPwmPeriod.ret = internal unnamed_addr global i32 -1
@.str = [28 x i8] c"%s/pwm_test_P%d_%d.*/period\00"
@.str.1 = [19 x i8] c"/sys/devices/ocp.*\00"
@O_RDONLY = global i32 0
define i32 @bbb_getPwmPeriod(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 ([28 x i8], [28 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 %30, 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 ]
store i32 %26, i32* @bbb_getPwmPeriod.ret
%27 = call i32 @close(i32 %17)
br label %28
28: ; preds = %10, %25, %2
%29 = load i32, i32* @bbb_getPwmPeriod.ret
br label %30
30: ; preds = %15, %28
%31 = phi i32 [ %29, %28 ], [ -1, %15 ]
ret i32 %31
}