#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_WRONLY;
scalar_t__ bbb_setPwmDuty(int, int, int /*<<< orphan*/);
char *bbb_wildCardPath(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, ...);
scalar_t__ write(int, char *, int);
int bbb_setPwmPeriod(int header, int pin, int time) {
  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_WRONLY)) < 0) {
        return -1;
      }
      if (bbb_setPwmDuty(header, pin, 0) < 0) {
        close(fd);
        return -1;
      }
      memset(ptr, 0, sizeof(ptr));
      ret = snprintf(ptr, sizeof(ptr), "%d", time);
      if (write(fd, ptr, ret) < 0) {
        ret = -1;
      } else {
        ret = 0;
      }
      close(fd);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<bbb_setPwmPeriod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x128(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    12d6 <bbb_setPwmPeriod+0x166>
cmpl   $0x0,-0xc(%rbp)
jle    12d6 <bbb_setPwmPeriod+0x166>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    -0xc(%rbp),%r9d
mov    $0x80,%esi
lea    0xe28(%rip),%rdx
lea    0xe3d(%rip),%rcx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
call   1090 <bbb_wildCardPath@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
je     12d1 <bbb_setPwmPeriod+0x161>
mov    -0x128(%rbp),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1230 <bbb_setPwmPeriod+0xc0>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <bbb_setPwmPeriod+0x16f>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
call   1070 <bbb_setPwmDuty@plt>
cmp    $0x0,%rax
jge    125b <bbb_setPwmPeriod+0xeb>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <bbb_setPwmPeriod+0x16f>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%ecx
mov    $0x80,%esi
lea    0xdab(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,0x2dc7(%rip)
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    0x2db7(%rip),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12bf <bbb_setPwmPeriod+0x14f>
movl   $0xffffffff,0x2d9e(%rip)
ff ff ff
jmp    12c9 <bbb_setPwmPeriod+0x159>
movl   $0x0,0x2d8f(%rip)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    12d6 <bbb_setPwmPeriod+0x166>
mov    0x2d7c(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_setPwmPeriod>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
test   %edi,%edi
jle    125c <bbb_setPwmPeriod+0xec>
mov    %esi,%r12d
test   %esi,%esi
jle    125c <bbb_setPwmPeriod+0xec>
mov    %edx,%r14d
mov    %edi,%ebx
lea    0x80(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0xe4b(%rip),%rdx
lea    0xe60(%rip),%rcx
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %ebx,%r8d
mov    %r12d,%r9d
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %rbp,%rdi
call   1090 <bbb_wildCardPath@plt>
test   %rax,%rax
je     125c <bbb_setPwmPeriod+0xec>
mov    0x2df3(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1080 <open@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1262 <bbb_setPwmPeriod+0xf2>
mov    %eax,%r15d
mov    %ebx,%edi
mov    %r12d,%esi
xor    %edx,%edx
call   1070 <bbb_setPwmDuty@plt>
test   %rax,%rax
js     1274 <bbb_setPwmPeriod+0x104>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0xe0a(%rip),%rdx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %eax,0x2e1b(%rip)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
sar    $0x3f,%rax
mov    %eax,0x2e04(%rip)
mov    %r15d,%edi
call   1060 <close@plt>
mov    0x2df6(%rip),%ebp
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
call   1060 <close@plt>
jmp    1262 <bbb_setPwmPeriod+0xf2>

LLVM IR (-O0):
@bbb_setPwmPeriod.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_WRONLY = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @bbb_setPwmPeriod(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [128 x i8]
%10 = alloca [128 x i8]
%11 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 -1, i32* %8
store i8* null, i8** %11
%12 = load i32, i32* %5
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %58
14: ; preds = %3
%15 = load i32, i32* %6
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %58
17: ; preds = %14
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%19 = call i32 @memset(i8* %18, i32 0, i32 128)
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%21 = load i32, i32* %5
%22 = load i32, i32* %6
%23 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %20, 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 %21, i32 %22)
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%25 = call i8* @bbb_wildCardPath(i8* %24)
store i8* %25, i8** %11
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %57
27: ; preds = %17
%28 = load i8*, i8** %11
%29 = load i32, i32* @O_WRONLY
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 -1, i32* %4
br label %60
33: ; preds = %27
%34 = load i32, i32* %5
%35 = load i32, i32* %6
%36 = call i64 @bbb_setPwmDuty(i32 %34, i32 %35, i32 0)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* %8
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %4
br label %60
41: ; preds = %33
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%43 = call i32 @memset(i8* %42, i32 0, i32 128)
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%45 = load i32, i32* %7
%46 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %44, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %45)
store i32 %46, i32* @bbb_setPwmPeriod.ret
%47 = load i32, i32* %8
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%49 = load i32, i32* @bbb_setPwmPeriod.ret
%50 = call i64 @write(i32 %47, i8* %48, i32 %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %41
store i32 -1, i32* @bbb_setPwmPeriod.ret
br label %54
53: ; preds = %41
store i32 0, i32* @bbb_setPwmPeriod.ret
br label %54
54: ; preds = %53, %52
%55 = load i32, i32* %8
%56 = call i32 @close(i32 %55)
br label %57
57: ; preds = %54, %17
br label %58
58: ; preds = %57, %14, %3
%59 = load i32, i32* @bbb_setPwmPeriod.ret
store i32 %59, i32* %4
br label %60
60: ; preds = %58, %38, %32
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
@bbb_setPwmPeriod.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_WRONLY = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @bbb_setPwmPeriod(i32 %0, i32 %1, i32 %2) {
%4 = alloca [128 x i8]
%5 = alloca [128 x i8]
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%8 = icmp sgt i32 %0, 0
%9 = icmp sgt i32 %1, 0
%10 = and i1 %8, %9
br i1 %10, label %11, label %32
11: ; preds = %3
%12 = call i32 @memset(i8* %6, i32 0, i32 128)
%13 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, 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)
%14 = call i8* @bbb_wildCardPath(i8* %6)
%15 = icmp eq i8* %14, null
br i1 %15, label %32, label %16
16: ; preds = %11
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* %14, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %34, label %20
20: ; preds = %16
%21 = call i64 @bbb_setPwmDuty(i32 %0, i32 %1, i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @close(i32 %18)
br label %34
25: ; preds = %20
%26 = call i32 @memset(i8* %7, i32 0, i32 128)
%27 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %2)
store i32 %27, i32* @bbb_setPwmPeriod.ret
%28 = call i64 @write(i32 %18, i8* %7, i32 %27)
%29 = ashr i64 %28, 63
%30 = trunc i64 %29 to i32
store i32 %30, i32* @bbb_setPwmPeriod.ret
%31 = call i32 @close(i32 %18)
br label %32
32: ; preds = %11, %25, %3
%33 = load i32, i32* @bbb_setPwmPeriod.ret
br label %34
34: ; preds = %16, %32, %23
%35 = phi i32 [ -1, %23 ], [ %33, %32 ], [ -1, %16 ]
ret i32 %35
}