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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int PWMChannel_SetDutyCycle(char *chnl, char *duty) {
  int fchnl;
  char *buff = (char *)malloc(255);
  if (buff == (char *)NULL) {
    fprintf(stderr, "Cannot allocate memory\n");
    return -1;
  }
  snprintf(buff, 255, "/sys/class/pwm/pwmchip0/pwm%s/enable", chnl);
  fchnl = open(buff, O_WRONLY);
  if (fchnl < 0) {
    fprintf(stderr, "Cannot open %s to disable the channel \n", buff);
    free(buff);
    return -1;
  }
  write(fchnl, "0", 2);
  close(fchnl);
  snprintf(buff, 255, "/sys/class/pwm/pwmchip0/pwm%s/duty_cycle", chnl);
  fchnl = open(buff, O_WRONLY);
  if (fchnl < 0) {
    fprintf(stderr, "Cannot open %s to set duty_cycle \n", buff);
    free(buff);
    return -1;
  }
  write(fchnl, duty, strlen(duty));
  close(fchnl);
  snprintf(buff, 255, "/sys/class/pwm/pwmchip0/pwm%s/enable", chnl);
  fchnl = open(buff, O_WRONLY);
  if (fchnl < 0) {
    fprintf(stderr, "Cannot open %s to re-enable the channel \n", buff);
    free(buff);
    return -1;
  }
  write(fchnl, "1", 2);
  close(fchnl);
  free(buff);
  return 0;
}


x86 assembly (clang, -O0):
<PWMChannel_SetDutyCycle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0xff,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cc <PWMChannel_SetDutyCycle+0x4c>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <PWMChannel_SetDutyCycle+0x1f9>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xe38(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1234 <PWMChannel_SetDutyCycle+0xb4>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <PWMChannel_SetDutyCycle+0x1f9>
mov    -0x1c(%rbp),%edi
lea    0xe27(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xe03(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12b8 <PWMChannel_SetDutyCycle+0x138>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdf4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <PWMChannel_SetDutyCycle+0x1f9>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xd1f(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2cc7(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    134d <PWMChannel_SetDutyCycle+0x1cd>
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <PWMChannel_SetDutyCycle+0x1f9>
mov    -0x1c(%rbp),%edi
lea    0xd86(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PWMChannel_SetDutyCycle>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0xff,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1289 <PWMChannel_SetDutyCycle+0x109>
mov    %rax,%rbx
lea    0xe6d(%rip),%rdx
mov    %rax,%rdi
mov    $0xff,%esi
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    0x2e0e(%rip),%r12
mov    (%r12),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12a2 <PWMChannel_SetDutyCycle+0x122>
mov    %eax,%ebp
lea    0xe86(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xe6e(%rip),%rdx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    (%r12),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12b4 <PWMChannel_SetDutyCycle+0x134>
mov    %eax,%ebp
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xdd7(%rip),%rdx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    (%r12),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12c6 <PWMChannel_SetDutyCycle+0x146>
mov    %eax,%ebp
lea    0xe73(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    12ed <PWMChannel_SetDutyCycle+0x16d>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12e8 <PWMChannel_SetDutyCycle+0x168>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    0xd8b(%rip),%rsi
jmp    12d6 <PWMChannel_SetDutyCycle+0x156>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
lea    0xdcc(%rip),%rsi
jmp    12d6 <PWMChannel_SetDutyCycle+0x156>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Cannot allocate memory\0A\00"
@.str.1 = [37 x i8] c"/sys/class/pwm/pwmchip0/pwm%s/enable\00"
@O_WRONLY = global i32 0
@.str.2 = [40 x i8] c"Cannot open %s to disable the channel \0A\00"
@.str.3 = [2 x i8] c"0\00"
@.str.4 = [41 x i8] c"/sys/class/pwm/pwmchip0/pwm%s/duty_cycle\00"
@.str.5 = [35 x i8] c"Cannot open %s to set duty_cycle \0A\00"
@.str.6 = [42 x i8] c"Cannot open %s to re-enable the channel \0A\00"
@.str.7 = [2 x i8] c"1\00"
define i32 @PWMChannel_SetDutyCycle(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = call i64 @malloc(i32 255)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %7
%10 = load i8*, i8** %7
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %78
15: ; preds = %2
%16 = load i8*, i8** %7
%17 = load i8*, i8** %4
%18 = call i32 @snprintf(i8* %16, i32 255, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %7
%20 = load i32, i32* @O_WRONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %15
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %7
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = load i8*, i8** %7
%29 = call i32 @free(i8* %28)
store i32 -1, i32* %3
br label %78
30: ; preds = %15
%31 = load i32, i32* %6
%32 = call i32 @write(i32 %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 2)
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
%35 = load i8*, i8** %7
%36 = load i8*, i8** %4
%37 = call i32 @snprintf(i8* %35, i32 255, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%38 = load i8*, i8** %7
%39 = load i32, i32* @O_WRONLY
%40 = call i32 @open(i8* %38, i32 %39)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %30
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %7
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i8* %45)
%47 = load i8*, i8** %7
%48 = call i32 @free(i8* %47)
store i32 -1, i32* %3
br label %78
49: ; preds = %30
%50 = load i32, i32* %6
%51 = load i8*, i8** %5
%52 = load i8*, i8** %5
%53 = call i32 @strlen(i8* %52)
%54 = call i32 @write(i32 %50, i8* %51, i32 %53)
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
%57 = load i8*, i8** %7
%58 = load i8*, i8** %4
%59 = call i32 @snprintf(i8* %57, i32 255, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %58)
%60 = load i8*, i8** %7
%61 = load i32, i32* @O_WRONLY
%62 = call i32 @open(i8* %60, i32 %61)
store i32 %62, i32* %6
%63 = load i32, i32* %6
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %71
65: ; preds = %49
%66 = load i32, i32* @stderr
%67 = load i8*, i8** %7
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i8* %67)
%69 = load i8*, i8** %7
%70 = call i32 @free(i8* %69)
store i32 -1, i32* %3
br label %78
71: ; preds = %49
%72 = load i32, i32* %6
%73 = call i32 @write(i32 %72, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 2)
%74 = load i32, i32* %6
%75 = call i32 @close(i32 %74)
%76 = load i8*, i8** %7
%77 = call i32 @free(i8* %76)
store i32 0, i32* %3
br label %78
78: ; preds = %71, %65, %43, %24, %12
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Cannot allocate memory\0A\00"
@.str.1 = [37 x i8] c"/sys/class/pwm/pwmchip0/pwm%s/enable\00"
@O_WRONLY = global i32 0
@.str.2 = [40 x i8] c"Cannot open %s to disable the channel \0A\00"
@.str.3 = [2 x i8] c"0\00"
@.str.4 = [41 x i8] c"/sys/class/pwm/pwmchip0/pwm%s/duty_cycle\00"
@.str.5 = [35 x i8] c"Cannot open %s to set duty_cycle \0A\00"
@.str.6 = [42 x i8] c"Cannot open %s to re-enable the channel \0A\00"
@.str.7 = [2 x i8] c"1\00"
define i32 @PWMChannel_SetDutyCycle(i8* %0, i8* %1) {
%3 = tail call i64 @malloc(i32 255)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %45
9: ; preds = %2
%10 = tail call i32 @snprintf(i8* %4, i32 255, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%11 = load i32, i32* @O_WRONLY
%12 = tail call i32 @open(i8* %4, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%17 = tail call i32 @free(i8* %4)
br label %45
18: ; preds = %9
%19 = tail call i32 @write(i32 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 2)
%20 = tail call i32 @close(i32 %12)
%21 = tail call i32 @snprintf(i8* %4, i32 255, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%22 = load i32, i32* @O_WRONLY
%23 = tail call i32 @open(i8* %4, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i8* %4)
%28 = tail call i32 @free(i8* %4)
br label %45
29: ; preds = %18
%30 = tail call i32 @strlen(i8* %1)
%31 = tail call i32 @write(i32 %23, i8* %1, i32 %30)
%32 = tail call i32 @close(i32 %23)
%33 = tail call i32 @snprintf(i8* %4, i32 255, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%34 = load i32, i32* @O_WRONLY
%35 = tail call i32 @open(i8* %4, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %29
%38 = load i32, i32* @stderr
%39 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%40 = tail call i32 @free(i8* %4)
br label %45
41: ; preds = %29
%42 = tail call i32 @write(i32 %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 2)
%43 = tail call i32 @close(i32 %35)
%44 = tail call i32 @free(i8* %4)
br label %45
45: ; preds = %41, %37, %25, %14, %6
%46 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %25 ], [ -1, %37 ], [ 0, %41 ]
ret i32 %46
}