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

/* Variables and functions */
int32_t phasestep(int32_t val) {
  static const int32_t table[13] = {
      815363806,  863847861,  915214928,  969636440,  1027294023,
      1088380104, 1153098553, 1221665361, 1294309364, 1371273004,
      1452813140, 1539201904, 1630727612,
  };
  int shift = (val / 65536) / 12;
  int num = (val / 65536) % 12;
  int denom = val & 0xffff;
  if (num < 0) {
    num += 12;
    shift -= 1;
  }
  int val1 = table[num] >> (21 - shift);
  int val2 = table[num] >> (21 - shift);
  return val1 + (((val2 - val1) * (denom >> 8)) >> 8);
}


x86 assembly (clang, -O0):
<phasestep>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x10000,%ecx
cltd
idiv   %ecx
mov    $0xc,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x10000,%ecx
cltd
idiv   %ecx
mov    $0xc,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    115a <phasestep+0x5a>
mov    -0xc(%rbp),%eax
add    $0xc,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x8(%rbp)
movslq -0xc(%rbp),%rcx
lea    0xe9b(%rip),%rax
mov    (%rax,%rcx,4),%eax
mov    $0x15,%ecx
sub    -0x8(%rbp),%ecx
sar    %cl,%eax
mov    %eax,-0x14(%rbp)
movslq -0xc(%rbp),%rcx
lea    0xe80(%rip),%rax
mov    (%rax,%rcx,4),%eax
mov    $0x15,%ecx
sub    -0x8(%rbp),%ecx
sar    %cl,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%edx
sar    $0x8,%edx
imul   %edx,%ecx
sar    $0x8,%ecx
add    %ecx,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<phasestep>:
lea    0xffff(%rdi),%ecx
test   %edi,%edi
cmovns %edi,%ecx
sar    $0x10,%ecx
movslq %edi,%rax
imul   $0xffffffffd5555555,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
shr    $0x31,%rax
add    %edx,%eax
imul   $0x2aab,%ecx,%edx
mov    %edx,%esi
shr    $0x1f,%esi
sar    $0x11,%edx
add    %esi,%edx
shl    $0x2,%edx
lea    (%rdx,%rdx,2),%edx
sub    %edx,%ecx
movzwl %cx,%ecx
movswl %cx,%edx
lea    0xc(%rdx),%esi
test   %cx,%cx
cmovns %edx,%esi
shr    $0xf,%ecx
add    %eax,%ecx
lea    0xea8(%rip),%rax
mov    (%rax,%rsi,4),%eax
add    $0x15,%cl
sar    %cl,%eax
ret

LLVM IR (-O0):
@phasestep.table = internal constant [13 x i32] [i32 815363806, i32 863847861, i32 915214928, i32 969636440, i32 1027294023, i32 1088380104, i32 1153098553, i32 1221665361, i32 1294309364, i32 1371273004, i32 1452813140, i32 1539201904, i32 1630727612]
define i32 @phasestep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = sdiv i32 %8, 65536
%10 = sdiv i32 %9, 12
store i32 %10, i32* %3
%11 = load i32, i32* %2
%12 = sdiv i32 %11, 65536
%13 = srem i32 %12, 12
store i32 %13, i32* %4
%14 = load i32, i32* %2
%15 = and i32 %14, 65535
store i32 %15, i32* %5
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %1
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 12
store i32 %20, i32* %4
%21 = load i32, i32* %3
%22 = sub nsw i32 %21, 1
store i32 %22, i32* %3
br label %23
23: ; preds = %18, %1
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [13 x i32], [13 x i32]* @phasestep.table, i64 0, i64 %25
%27 = load i32, i32* %26
%28 = load i32, i32* %3
%29 = sub nsw i32 21, %28
%30 = ashr i32 %27, %29
store i32 %30, i32* %6
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [13 x i32], [13 x i32]* @phasestep.table, i64 0, i64 %32
%34 = load i32, i32* %33
%35 = load i32, i32* %3
%36 = sub nsw i32 21, %35
%37 = ashr i32 %34, %36
store i32 %37, i32* %7
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = load i32, i32* %6
%41 = sub nsw i32 %39, %40
%42 = load i32, i32* %5
%43 = ashr i32 %42, 8
%44 = mul nsw i32 %41, %43
%45 = ashr i32 %44, 8
%46 = add nsw i32 %38, %45
ret i32 %46
}

LLVM IR (-O2):
@phasestep.table = internal unnamed_addr constant [13 x i32] [i32 815363806, i32 863847861, i32 915214928, i32 969636440, i32 1027294023, i32 1088380104, i32 1153098553, i32 1221665361, i32 1294309364, i32 1371273004, i32 1452813140, i32 1539201904, i32 1630727612]
define i32 @phasestep(i32 %0) {
%2 = sdiv i32 %0, 65536
%3 = sdiv i32 %0, -786432
%4 = trunc i32 %2 to i16
%5 = srem i16 %4, 12
%6 = sext i16 %5 to i32
%7 = icmp slt i16 %5, 0
%8 = add nsw i32 %6, 12
%9 = select i1 %7, i32 %8, i32 %6
%10 = lshr i16 %5, 15
%11 = zext i32 %9 to i64
%12 = getelementptr inbounds [13 x i32], [13 x i32]* @phasestep.table, i64 0, i64 %11
%13 = load i32, i32* %12
%14 = zext i16 %10 to i32
%15 = add nsw i32 %3, 21
%16 = add nsw i32 %15, %14
%17 = ashr i32 %13, %16
ret i32 %17
}