#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*/ uint8_t;
typedef unsigned short uint32_t;
typedef long uint16_t;

/* Variables and functions */
int /*<<< orphan*/ ehrpwm_write(int /*<<< orphan*/, int, int);
int ehrpwm_hr_duty_config(uint8_t module, uint16_t tick_hz, uint32_t duty_ns) {
  unsigned char no_of_mepsteps;
  unsigned short cmphr_val;
  if (!tick_hz) {
    return -1;
  }
  no_of_mepsteps = 1000000L / ((tick_hz / 1000000L) * 63);
  cmphr_val = tick_hz / 1000000L;
  cmphr_val = (duty_ns * cmphr_val) % 1000L;
  cmphr_val = (cmphr_val * no_of_mepsteps) / 1000;
  cmphr_val = (cmphr_val << 8) + 0x180;
  ehrpwm_write(module, 0x10, cmphr_val);
  ehrpwm_write(module, 0x1040, 0x2);
  return 0;
}


x86 assembly (clang, -O0):
<ehrpwm_hr_duty_config>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dx,%ax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    113d <ehrpwm_hr_duty_config+0x2d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <ehrpwm_hr_duty_config+0xcd>
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
imul   $0x3f,%rax,%rcx
mov    $0xf4240,%eax
cqto
idiv   %rcx
mov    %al,-0x13(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %ax,-0x16(%rbp)
movzwl -0x12(%rbp),%eax
movzwl -0x16(%rbp),%ecx
imul   %ecx,%eax
cltq
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %dx,%ax
mov    %ax,-0x16(%rbp)
movzwl -0x16(%rbp),%eax
movzbl -0x13(%rbp),%ecx
imul   %ecx,%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %ax,-0x16(%rbp)
movzwl -0x16(%rbp),%eax
shl    $0x8,%eax
add    $0x180,%eax
mov    %ax,-0x16(%rbp)
mov    -0x8(%rbp),%edi
movzwl -0x16(%rbp),%edx
mov    $0x10,%esi
call   1030 <ehrpwm_write@plt>
mov    -0x8(%rbp),%edi
mov    $0x1040,%esi
mov    $0x2,%edx
call   1030 <ehrpwm_write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ehrpwm_hr_duty_config>:
test   %rsi,%rsi
je     115a <ehrpwm_hr_duty_config+0x4a>
push   %rbx
mov    %edx,%ecx
mov    %edi,%ebx
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mov    %rsi,%rax
imul   %rdx
mov    %rdx,%rsi
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x12,%rsi
add    %rax,%rsi
mov    %rsi,%rdi
shl    $0x6,%rdi
sub    %rsi,%rdi
mov    %rdi,%rax
shr    $0x20,%rax
je     1160 <ehrpwm_hr_duty_config+0x50>
mov    $0xf4240,%eax
xor    %edx,%edx
idiv   %rdi
jmp    1169 <ehrpwm_hr_duty_config+0x59>
mov    $0xffffffff,%eax
ret
mov    $0xf4240,%eax
xor    %edx,%edx
div    %edi
movzwl %cx,%ecx
movzwl %si,%edx
imul   %rcx,%rdx
imul   $0x10624dd3,%rdx,%rcx
shr    $0x26,%rcx
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%edx
movzbl %al,%eax
imul   %edx,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x20,%rax
and    $0x3fc0,%eax
lea    0x180(,%rax,4),%eax
movzwl %ax,%edx
mov    %ebx,%edi
mov    $0x10,%esi
call   1030 <ehrpwm_write@plt>
mov    %ebx,%edi
mov    $0x1040,%esi
mov    $0x2,%edx
call   1030 <ehrpwm_write@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @ehrpwm_hr_duty_config(i32 %0, i64 %1, i16 zeroext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i16
%8 = alloca i8
%9 = alloca i16
store i32 %0, i32* %5
store i64 %1, i64* %6
store i16 %2, i16* %7
%10 = load i64, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %3
store i32 -1, i32* %4
br label %48
13: ; preds = %3
%14 = load i64, i64* %6
%15 = sdiv i64 %14, 1000000
%16 = mul nsw i64 %15, 63
%17 = sdiv i64 1000000, %16
%18 = trunc i64 %17 to i8
store i8 %18, i8* %8
%19 = load i64, i64* %6
%20 = sdiv i64 %19, 1000000
%21 = trunc i64 %20 to i16
store i16 %21, i16* %9
%22 = load i16, i16* %7
%23 = zext i16 %22 to i32
%24 = load i16, i16* %9
%25 = zext i16 %24 to i32
%26 = mul nsw i32 %23, %25
%27 = sext i32 %26 to i64
%28 = srem i64 %27, 1000
%29 = trunc i64 %28 to i16
store i16 %29, i16* %9
%30 = load i16, i16* %9
%31 = zext i16 %30 to i32
%32 = load i8, i8* %8
%33 = zext i8 %32 to i32
%34 = mul nsw i32 %31, %33
%35 = sdiv i32 %34, 1000
%36 = trunc i32 %35 to i16
store i16 %36, i16* %9
%37 = load i16, i16* %9
%38 = zext i16 %37 to i32
%39 = shl i32 %38, 8
%40 = add nsw i32 %39, 384
%41 = trunc i32 %40 to i16
store i16 %41, i16* %9
%42 = load i32, i32* %5
%43 = load i16, i16* %9
%44 = zext i16 %43 to i32
%45 = call i32 @ehrpwm_write(i32 %42, i32 16, i32 %44)
%46 = load i32, i32* %5
%47 = call i32 @ehrpwm_write(i32 %46, i32 4160, i32 2)
store i32 0, i32* %4
br label %48
48: ; preds = %13, %12
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
define i32 @ehrpwm_hr_duty_config(i32 %0, i64 %1, i16 zeroext %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %23, label %5
5: ; preds = %3
%6 = sdiv i64 %1, 1000000
%7 = mul nsw i64 %6, 63
%8 = sdiv i64 1000000, %7
%9 = trunc i64 %8 to i32
%10 = zext i16 %2 to i64
%11 = and i64 %6, 65535
%12 = mul nuw nsw i64 %11, %10
%13 = trunc i64 %12 to i32
%14 = urem i32 %13, 1000
%15 = and i32 %9, 255
%16 = mul nuw nsw i32 %15, %14
%17 = udiv i32 %16, 1000
%18 = shl nuw nsw i32 %17, 8
%19 = add nuw nsw i32 %18, 384
%20 = and i32 %19, 65408
%21 = tail call i32 @ehrpwm_write(i32 %0, i32 16, i32 %20)
%22 = tail call i32 @ehrpwm_write(i32 %0, i32 4160, i32 2)
br label %23
23: ; preds = %3, %5
%24 = phi i32 [ 0, %5 ], [ -1, %3 ]
ret i32 %24
}