#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 uint8_t;

/* Variables and functions */
unsigned short ehrpwm_read(int, int);
int /*<<< orphan*/ ehrpwm_tz_clr_evt_status(int);
int /*<<< orphan*/ ehrpwm_tz_set_action(int, int, int);
int /*<<< orphan*/ ehrpwm_write(int, int, unsigned short);
int ehrpwm_pwm_start(uint8_t module) {
  unsigned short val;
  unsigned short read_val1;
  unsigned short read_val2;
  int chan;
  chan = module & 0x1;
  val = ehrpwm_read(module, 0x0);
  val = (val & ~((1 << 1) | (1 << 0))) | (0x0 | 0x2 << 14);
  ehrpwm_write(module, 0x0, val);
  ehrpwm_tz_set_action(module, chan, 0x3);
  read_val1 = ehrpwm_read(module, 0x2C);
  read_val2 = ehrpwm_read(module, 0x28);
  read_val2 = read_val2 & (chan ? 0x3 : (0x3 << 2));
  read_val2 = chan ? read_val2 : (read_val2 >> 2);
  if (!(read_val1 & 0x4) || (read_val2 == 0x3))
    ehrpwm_tz_clr_evt_status(module);
  return 0;
}


x86 assembly (clang, -O0):
<ehrpwm_pwm_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1030 <ehrpwm_read@plt>
mov    %ax,-0x6(%rbp)
movzwl -0x6(%rbp),%eax
and    $0xfffffffc,%eax
or     $0x8000,%eax
mov    %ax,-0x6(%rbp)
mov    -0x4(%rbp),%edi
xor    %esi,%esi
movzwl -0x6(%rbp),%edx
call   1060 <ehrpwm_write@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    $0x3,%edx
call   1050 <ehrpwm_tz_set_action@plt>
mov    -0x4(%rbp),%edi
mov    $0x2c,%esi
call   1030 <ehrpwm_read@plt>
mov    %ax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x28,%esi
call   1030 <ehrpwm_read@plt>
mov    %ax,-0xa(%rbp)
movzwl -0xa(%rbp),%eax
mov    -0x10(%rbp),%esi
mov    $0xc,%ecx
mov    $0x3,%edx
cmp    $0x0,%esi
cmovne %edx,%ecx
and    %ecx,%eax
mov    %ax,-0xa(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11e5 <ehrpwm_pwm_start+0xa5>
movzwl -0xa(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11ef <ehrpwm_pwm_start+0xaf>
movzwl -0xa(%rbp),%eax
sar    $0x2,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %ax,-0xa(%rbp)
movzwl -0x8(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     1213 <ehrpwm_pwm_start+0xd3>
movzwl -0xa(%rbp),%eax
cmp    $0x3,%eax
jne    121b <ehrpwm_pwm_start+0xdb>
mov    -0x4(%rbp),%edi
call   1040 <ehrpwm_tz_clr_evt_status@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ehrpwm_pwm_start>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
xor    %esi,%esi
call   1030 <ehrpwm_read@plt>
and    $0x7ffc,%eax
lea    0x8000(%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <ehrpwm_write@plt>
xor    %ebp,%ebp
mov    %ebx,%esi
and    $0x1,%esi
sete   %bpl
mov    %ebx,%edi
mov    $0x3,%edx
call   1050 <ehrpwm_tz_set_action@plt>
mov    %ebx,%edi
mov    $0x2c,%esi
call   1030 <ehrpwm_read@plt>
mov    %eax,%r14d
mov    %ebx,%edi
mov    $0x28,%esi
call   1030 <ehrpwm_read@plt>
lea    0x3(%rbp,%rbp,8),%ecx
and    %eax,%ecx
movzwl %cx,%ecx
mov    %ecx,%eax
shr    $0x2,%eax
mov    %ebx,%edx
and    $0x1,%edx
cmovne %ecx,%eax
test   $0x4,%r14b
je     11b5 <ehrpwm_pwm_start+0x75>
cmp    $0x3,%ax
jne    11bc <ehrpwm_pwm_start+0x7c>
mov    %ebx,%edi
call   1040 <ehrpwm_tz_clr_evt_status@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ehrpwm_pwm_start(i32 %0) {
%2 = alloca i32
%3 = alloca i16
%4 = alloca i16
%5 = alloca i16
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = and i32 %7, 1
store i32 %8, i32* %6
%9 = load i32, i32* %2
%10 = call zeroext i16 @ehrpwm_read(i32 %9, i32 0)
store i16 %10, i16* %3
%11 = load i16, i16* %3
%12 = zext i16 %11 to i32
%13 = and i32 %12, -4
%14 = or i32 %13, 32768
%15 = trunc i32 %14 to i16
store i16 %15, i16* %3
%16 = load i32, i32* %2
%17 = load i16, i16* %3
%18 = call i32 @ehrpwm_write(i32 %16, i32 0, i16 zeroext %17)
%19 = load i32, i32* %2
%20 = load i32, i32* %6
%21 = call i32 @ehrpwm_tz_set_action(i32 %19, i32 %20, i32 3)
%22 = load i32, i32* %2
%23 = call zeroext i16 @ehrpwm_read(i32 %22, i32 44)
store i16 %23, i16* %4
%24 = load i32, i32* %2
%25 = call zeroext i16 @ehrpwm_read(i32 %24, i32 40)
store i16 %25, i16* %5
%26 = load i16, i16* %5
%27 = zext i16 %26 to i32
%28 = load i32, i32* %6
%29 = icmp ne i32 %28, 0
%30 = zext i1 %29 to i64
%31 = select i1 %29, i32 3, i32 12
%32 = and i32 %27, %31
%33 = trunc i32 %32 to i16
store i16 %33, i16* %5
%34 = load i32, i32* %6
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %1
%37 = load i16, i16* %5
%38 = zext i16 %37 to i32
br label %43
39: ; preds = %1
%40 = load i16, i16* %5
%41 = zext i16 %40 to i32
%42 = ashr i32 %41, 2
br label %43
43: ; preds = %39, %36
%44 = phi i32 [ %38, %36 ], [ %42, %39 ]
%45 = trunc i32 %44 to i16
store i16 %45, i16* %5
%46 = load i16, i16* %4
%47 = zext i16 %46 to i32
%48 = and i32 %47, 4
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %43
%51 = load i16, i16* %5
%52 = zext i16 %51 to i32
%53 = icmp eq i32 %52, 3
br i1 %53, label %54, label %57
54: ; preds = %50, %43
%55 = load i32, i32* %2
%56 = call i32 @ehrpwm_tz_clr_evt_status(i32 %55)
br label %57
57: ; preds = %54, %50
ret i32 0
}

LLVM IR (-O2):
define i32 @ehrpwm_pwm_start(i32 %0) {
%2 = and i32 %0, 1
%3 = tail call zeroext i16 @ehrpwm_read(i32 %0, i32 0)
%4 = and i16 %3, 32764
%5 = or i16 %4, -32768
%6 = tail call i32 @ehrpwm_write(i32 %0, i32 0, i16 zeroext %5)
%7 = tail call i32 @ehrpwm_tz_set_action(i32 %0, i32 %2, i32 3)
%8 = tail call zeroext i16 @ehrpwm_read(i32 %0, i32 44)
%9 = tail call zeroext i16 @ehrpwm_read(i32 %0, i32 40)
%10 = icmp eq i32 %2, 0
%11 = select i1 %10, i16 12, i16 3
%12 = and i16 %9, %11
%13 = lshr i16 %12, 2
%14 = select i1 %10, i16 %13, i16 %12
%15 = and i16 %8, 4
%16 = icmp eq i16 %15, 0
%17 = icmp eq i16 %14, 3
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = tail call i32 @ehrpwm_tz_clr_evt_status(i32 %0)
br label %21
21: ; preds = %1, %19
ret i32 0
}