#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;
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_setPwmDuty(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.*/duty",
             "/sys/devices/ocp.*", header, pin);
    if ((wpath = bbb_wildCardPath(path)) != NULL) {
      if ((fd = open(wpath, O_WRONLY)) < 0) {
        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_setPwmDuty>:
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    129b <bbb_setPwmDuty+0x13b>
cmpl   $0x0,-0xc(%rbp)
jle    129b <bbb_setPwmDuty+0x13b>
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    0xe38(%rip),%rdx
lea    0xe4b(%rip),%rcx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
call   1080 <bbb_wildCardPath@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
je     1296 <bbb_setPwmDuty+0x136>
mov    -0x128(%rbp),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1220 <bbb_setPwmDuty+0xc0>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <bbb_setPwmDuty+0x144>
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    0xde4(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,0x2dfa(%rip)
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    0x2dea(%rip),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1284 <bbb_setPwmDuty+0x124>
movl   $0xffffffff,0x2dd1(%rip)
ff ff ff
jmp    128e <bbb_setPwmDuty+0x12e>
movl   $0x0,0x2dc2(%rip)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    129b <bbb_setPwmDuty+0x13b>
mov    0x2daf(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_setPwmDuty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
test   %edi,%edi
jle    1230 <bbb_setPwmDuty+0xd0>
mov    %esi,%ebp
test   %esi,%esi
jle    1230 <bbb_setPwmDuty+0xd0>
mov    %edx,%r14d
mov    %edi,%ebx
lea    0x80(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0xe5e(%rip),%rdx
lea    0xe71(%rip),%rcx
mov    %r15,%rdi
mov    $0x80,%esi
mov    %ebx,%r8d
mov    %ebp,%r9d
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %r15,%rdi
call   1080 <bbb_wildCardPath@plt>
test   %rax,%rax
je     1230 <bbb_setPwmDuty+0xd0>
mov    0x2e06(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1244 <bbb_setPwmDuty+0xe4>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0xe32(%rip),%rdx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %eax,0x2e3d(%rip)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
sar    $0x3f,%rax
mov    %eax,0x2e27(%rip)
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2e1a(%rip),%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    1236 <bbb_setPwmDuty+0xd6>

LLVM IR (-O0):
@bbb_setPwmDuty.ret = internal global i32 -1
@.str = [26 x i8] c"%s/pwm_test_P%d_%d.*/duty\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_setPwmDuty(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 %50
14: ; preds = %3
%15 = load i32, i32* %6
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %50
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 ([26 x i8], [26 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 %49
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 %52
33: ; preds = %27
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%35 = call i32 @memset(i8* %34, i32 0, i32 128)
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%37 = load i32, i32* %7
%38 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %36, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %37)
store i32 %38, i32* @bbb_setPwmDuty.ret
%39 = load i32, i32* %8
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%41 = load i32, i32* @bbb_setPwmDuty.ret
%42 = call i64 @write(i32 %39, i8* %40, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %33
store i32 -1, i32* @bbb_setPwmDuty.ret
br label %46
45: ; preds = %33
store i32 0, i32* @bbb_setPwmDuty.ret
br label %46
46: ; preds = %45, %44
%47 = load i32, i32* %8
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %46, %17
br label %50
50: ; preds = %49, %14, %3
%51 = load i32, i32* @bbb_setPwmDuty.ret
store i32 %51, i32* %4
br label %52
52: ; preds = %50, %32
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@bbb_setPwmDuty.ret = internal unnamed_addr global i32 -1
@.str = [26 x i8] c"%s/pwm_test_P%d_%d.*/duty\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_setPwmDuty(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 %27
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 ([26 x i8], [26 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 %27, 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 %29, label %20
20: ; preds = %16
%21 = call i32 @memset(i8* %7, i32 0, i32 128)
%22 = 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 %22, i32* @bbb_setPwmDuty.ret
%23 = call i64 @write(i32 %18, i8* %7, i32 %22)
%24 = ashr i64 %23, 63
%25 = trunc i64 %24 to i32
store i32 %25, i32* @bbb_setPwmDuty.ret
%26 = call i32 @close(i32 %18)
br label %27
27: ; preds = %11, %20, %3
%28 = load i32, i32* @bbb_setPwmDuty.ret
br label %29
29: ; preds = %16, %27
%30 = phi i32 [ %28, %27 ], [ -1, %16 ]
ret i32 %30
}