#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*/ pwm_write(char *, char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned long);
int pwm_set_duty_cycle(char *pwm_key, double duty_cycle) {
  if (pwm_key == NULL)
    return -1;
  if (duty_cycle < 0.0)
    duty_cycle = 0.0;
  if (duty_cycle > 1.0)
    duty_cycle = 1.0;
  char value[64];
  snprintf(value, 64, "%lu", (unsigned long)(duty_cycle * 500000));
  pwm_write(pwm_key, "duty", value);
  return 0;
}


x86 assembly (clang, -O0):
<pwm_set_duty_cycle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1148 <pwm_set_duty_cycle+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <pwm_set_duty_cycle+0xc9>
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    115e <pwm_set_duty_cycle+0x3e>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xe95(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1182 <pwm_set_duty_cycle+0x62>
movsd  0xe83(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
movsd  -0x18(%rbp),%xmm0
movsd  0xe75(%rip),%xmm1
mulsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rcx
mov    %rcx,%rdx
sar    $0x3f,%rdx
movsd  0xe62(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
and    %rdx,%rax
or     %rax,%rcx
mov    $0x40,%esi
lea    0xe4f(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rdx
lea    0xe3f(%rip),%rsi
call   1030 <pwm_write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pwm_set_duty_cycle>:
test   %rdi,%rdi
je     119d <pwm_set_duty_cycle+0x7d>
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rbx
xorpd  %xmm1,%xmm1
maxsd  %xmm0,%xmm1
movsd  0xec1(%rip),%xmm0
minsd  %xmm1,%xmm0
mulsd  0xebd(%rip),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
subsd  0xeb1(%rip),%xmm0
cvttsd2si %xmm0,%rcx
and    %rdx,%rcx
or     %rax,%rcx
lea    0xea7(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
lea    0xe94(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1030 <pwm_write@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%lu\00"
@.str.1 = [5 x i8] c"duty\00"
define i32 @pwm_set_duty_cycle(i8* %0, double %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca double
%6 = alloca [64 x i8]
store i8* %0, i8** %4
store double %1, double* %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %2
store i32 -1, i32* %3
br label %27
10: ; preds = %2
%11 = load double, double* %5
%12 = fcmp olt double %11, 0.000000e+00
br i1 %12, label %13, label %14
13: ; preds = %10
store double 0.000000e+00, double* %5
br label %14
14: ; preds = %13, %10
%15 = load double, double* %5
%16 = fcmp ogt double %15, 1.000000e+00
br i1 %16, label %17, label %18
17: ; preds = %14
store double 1.000000e+00, double* %5
br label %18
18: ; preds = %17, %14
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%20 = load double, double* %5
%21 = fmul double %20, 5.000000e+05
%22 = fptoui double %21 to i64
%23 = call i32 @snprintf(i8* %19, i32 64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %22)
%24 = load i8*, i8** %4
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%26 = call i32 @pwm_write(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i32 0, i32* %3
br label %27
27: ; preds = %18, %9
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@.str = [4 x i8] c"%lu\00"
@.str.1 = [5 x i8] c"duty\00"
define i32 @pwm_set_duty_cycle(i8* %0, double %1) {
%3 = alloca [64 x i8]
%4 = icmp eq i8* %0, null
br i1 %4, label %15, label %5
5: ; preds = %2
%6 = fcmp olt double %1, 0.000000e+00
%7 = select i1 %6, double 0.000000e+00, double %1
%8 = fcmp ogt double %7, 1.000000e+00
%9 = select i1 %8, double 1.000000e+00, double %7
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%11 = fmul double %9, 5.000000e+05
%12 = fptoui double %11 to i64
%13 = call i32 @snprintf(i8* %10, i32 64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %12)
%14 = call i32 @pwm_write(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %15
15: ; preds = %2, %5
%16 = phi i32 [ 0, %5 ], [ -1, %2 ]
ret i32 %16
}