#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 uint64_t;
typedef int uint32_t;

/* Variables and functions */
uint32_t calculate_polyphase_weight(int dx, int dy, int j, int i, int w, int h,
                                    int source_width, int source_height) {
  uint32_t weighty;
  if (source_height & 1)
    switch (i) {
    case 0:
      weighty = 65536 * (h - dy) / (2 * h + 1);
      break;
    case 1:
      weighty = 65536 * h / (2 * h + 1);
      break;
    case 2:
      weighty = 65536 * (1 + dy) / (2 * h + 1);
      break;
    }
  else if (i == 2)
    return 0;
  else
    weighty = 65536 / 2;
  uint32_t weightx;
  if (source_width & 1)
    switch (j) {
    case 0:
      weightx = 65536 * (w - dx) / (2 * w + 1);
      break;
    case 1:
      weightx = 65536 * w / (2 * w + 1);
      break;
    case 2:
      weightx = 65536 * (1 + dx) / (2 * w + 1);
      break;
    }
  else if (j == 2)
    return 0;
  else
    weightx = 65536 / 2;
  return ((uint64_t)weightx * (uint64_t)weighty) >> 16;
}


x86 assembly (clang, -O0):
<calculate_polyphase_weight>:
push   %rbp
mov    %rsp,%rbp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    %r9d,-0x1c(%rbp)
mov    0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11b6 <calculate_polyphase_weight+0xb6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
test   %eax,%eax
je     1162 <calculate_polyphase_weight+0x62>
jmp    1140 <calculate_polyphase_weight+0x40>
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
je     117f <calculate_polyphase_weight+0x7f>
jmp    1151 <calculate_polyphase_weight+0x51>
mov    -0x28(%rbp),%eax
sub    $0x2,%eax
je     1199 <calculate_polyphase_weight+0x99>
jmp    11b1 <calculate_polyphase_weight+0xb1>
mov    -0x1c(%rbp),%eax
sub    -0xc(%rbp),%eax
shl    $0x10,%eax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
jmp    11b1 <calculate_polyphase_weight+0xb1>
mov    -0x1c(%rbp),%eax
shl    $0x10,%eax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
jmp    11b1 <calculate_polyphase_weight+0xb1>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
shl    $0x10,%eax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
jmp    11d8 <calculate_polyphase_weight+0xd8>
cmpl   $0x2,-0x14(%rbp)
jne    11cc <calculate_polyphase_weight+0xcc>
movl   $0x0,-0x4(%rbp)
jmp    129f <calculate_polyphase_weight+0x19f>
movl   $0x8000,-0x20(%rbp)
jmp    11d8 <calculate_polyphase_weight+0xd8>
mov    0x10(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1270 <calculate_polyphase_weight+0x170>
mov    -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
test   %eax,%eax
je     121c <calculate_polyphase_weight+0x11c>
jmp    11fa <calculate_polyphase_weight+0xfa>
mov    -0x2c(%rbp),%eax
sub    $0x1,%eax
je     1239 <calculate_polyphase_weight+0x139>
jmp    120b <calculate_polyphase_weight+0x10b>
mov    -0x2c(%rbp),%eax
sub    $0x2,%eax
je     1253 <calculate_polyphase_weight+0x153>
jmp    126b <calculate_polyphase_weight+0x16b>
mov    -0x18(%rbp),%eax
sub    -0x8(%rbp),%eax
shl    $0x10,%eax
mov    -0x18(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
jmp    126b <calculate_polyphase_weight+0x16b>
mov    -0x18(%rbp),%eax
shl    $0x10,%eax
mov    -0x18(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
jmp    126b <calculate_polyphase_weight+0x16b>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
shl    $0x10,%eax
mov    -0x18(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
jmp    1292 <calculate_polyphase_weight+0x192>
cmpl   $0x2,-0x10(%rbp)
jne    1286 <calculate_polyphase_weight+0x186>
movl   $0x0,-0x4(%rbp)
jmp    129f <calculate_polyphase_weight+0x19f>
movl   $0x8000,-0x24(%rbp)
jmp    1292 <calculate_polyphase_weight+0x192>
mov    -0x24(%rbp),%eax
imul   -0x20(%rbp),%eax
sar    $0x10,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_polyphase_weight>:
mov    %edx,%r11d
testb  $0x1,0x10(%rsp)
jne    111a <calculate_polyphase_weight+0x1a>
mov    $0x8000,%r10d
xor    %edx,%edx
cmp    $0x2,%ecx
jne    114f <calculate_polyphase_weight+0x4f>
mov    %edx,%eax
ret
cmp    $0x2,%ecx
je     112f <calculate_polyphase_weight+0x2f>
cmp    $0x1,%ecx
je     113c <calculate_polyphase_weight+0x3c>
test   %ecx,%ecx
jne    114f <calculate_polyphase_weight+0x4f>
mov    %r9d,%eax
sub    %esi,%eax
jmp    113f <calculate_polyphase_weight+0x3f>
shl    $0x10,%esi
add    $0x10000,%esi
mov    %esi,%eax
jmp    1142 <calculate_polyphase_weight+0x42>
mov    %r9d,%eax
shl    $0x10,%eax
lea    (%r9,%r9,1),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %eax,%r10d
mov    0x8(%rsp),%eax
test   $0x1,%al
jne    1166 <calculate_polyphase_weight+0x66>
mov    $0x8000,%eax
xor    %edx,%edx
cmp    $0x2,%r11d
je     1117 <calculate_polyphase_weight+0x17>
jmp    119b <calculate_polyphase_weight+0x9b>
cmp    $0x2,%r11d
je     117e <calculate_polyphase_weight+0x7e>
cmp    $0x1,%r11d
je     118b <calculate_polyphase_weight+0x8b>
test   %r11d,%r11d
jne    119b <calculate_polyphase_weight+0x9b>
mov    %r8d,%eax
sub    %edi,%eax
jmp    118e <calculate_polyphase_weight+0x8e>
shl    $0x10,%edi
add    $0x10000,%edi
mov    %edi,%eax
jmp    1191 <calculate_polyphase_weight+0x91>
mov    %r8d,%eax
shl    $0x10,%eax
lea    (%r8,%r8,1),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
imul   %r10d,%eax
sar    $0x10,%eax
mov    %eax,%edx
mov    %edx,%eax
ret

LLVM IR (-O0):
define i32 @calculate_polyphase_weight(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32 %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 %0, i32* %10
store i32 %1, i32* %11
store i32 %2, i32* %12
store i32 %3, i32* %13
store i32 %4, i32* %14
store i32 %5, i32* %15
store i32 %6, i32* %16
store i32 %7, i32* %17
%20 = load i32, i32* %17
%21 = and i32 %20, 1
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %50
23: ; preds = %8
%24 = load i32, i32* %13
switch i32 %24, label %49 [
i32 0, label %25
i32 1, label %34
i32 2, label %41
]
25: ; preds = %23
%26 = load i32, i32* %15
%27 = load i32, i32* %11
%28 = sub nsw i32 %26, %27
%29 = mul nsw i32 65536, %28
%30 = load i32, i32* %15
%31 = mul nsw i32 2, %30
%32 = add nsw i32 %31, 1
%33 = sdiv i32 %29, %32
store i32 %33, i32* %18
br label %49
34: ; preds = %23
%35 = load i32, i32* %15
%36 = mul nsw i32 65536, %35
%37 = load i32, i32* %15
%38 = mul nsw i32 2, %37
%39 = add nsw i32 %38, 1
%40 = sdiv i32 %36, %39
store i32 %40, i32* %18
br label %49
41: ; preds = %23
%42 = load i32, i32* %11
%43 = add nsw i32 1, %42
%44 = mul nsw i32 65536, %43
%45 = load i32, i32* %15
%46 = mul nsw i32 2, %45
%47 = add nsw i32 %46, 1
%48 = sdiv i32 %44, %47
store i32 %48, i32* %18
br label %49
49: ; preds = %23, %41, %34, %25
br label %56
50: ; preds = %8
%51 = load i32, i32* %13
%52 = icmp eq i32 %51, 2
br i1 %52, label %53, label %54
53: ; preds = %50
store i32 0, i32* %9
br label %98
54: ; preds = %50
store i32 32768, i32* %18
br label %55
55: ; preds = %54
br label %56
56: ; preds = %55, %49
%57 = load i32, i32* %16
%58 = and i32 %57, 1
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %87
60: ; preds = %56
%61 = load i32, i32* %12
switch i32 %61, label %86 [
i32 0, label %62
i32 1, label %71
i32 2, label %78
]
62: ; preds = %60
%63 = load i32, i32* %14
%64 = load i32, i32* %10
%65 = sub nsw i32 %63, %64
%66 = mul nsw i32 65536, %65
%67 = load i32, i32* %14
%68 = mul nsw i32 2, %67
%69 = add nsw i32 %68, 1
%70 = sdiv i32 %66, %69
store i32 %70, i32* %19
br label %86
71: ; preds = %60
%72 = load i32, i32* %14
%73 = mul nsw i32 65536, %72
%74 = load i32, i32* %14
%75 = mul nsw i32 2, %74
%76 = add nsw i32 %75, 1
%77 = sdiv i32 %73, %76
store i32 %77, i32* %19
br label %86
78: ; preds = %60
%79 = load i32, i32* %10
%80 = add nsw i32 1, %79
%81 = mul nsw i32 65536, %80
%82 = load i32, i32* %14
%83 = mul nsw i32 2, %82
%84 = add nsw i32 %83, 1
%85 = sdiv i32 %81, %84
store i32 %85, i32* %19
br label %86
86: ; preds = %60, %78, %71, %62
br label %93
87: ; preds = %56
%88 = load i32, i32* %12
%89 = icmp eq i32 %88, 2
br i1 %89, label %90, label %91
90: ; preds = %87
store i32 0, i32* %9
br label %98
91: ; preds = %87
store i32 32768, i32* %19
br label %92
92: ; preds = %91
br label %93
93: ; preds = %92, %86
%94 = load i32, i32* %19
%95 = load i32, i32* %18
%96 = mul nsw i32 %94, %95
%97 = ashr i32 %96, 16
store i32 %97, i32* %9
br label %98
98: ; preds = %93, %90, %53
%99 = load i32, i32* %9
ret i32 %99
}

LLVM IR (-O2):
define i32 @calculate_polyphase_weight(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32 %7) {
%9 = and i32 %7, 1
%10 = icmp eq i32 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %8
switch i32 %3, label %27 [
i32 0, label %12
i32 1, label %15
i32 2, label %17
]
12: ; preds = %11
%13 = sub nsw i32 %5, %1
%14 = shl nsw i32 %13, 16
br label %22
15: ; preds = %11
%16 = shl nsw i32 %5, 16
br label %22
17: ; preds = %11
%18 = shl i32 %1, 16
%19 = add i32 %18, 65536
br label %22
20: ; preds = %8
%21 = icmp eq i32 %3, 2
br i1 %21, label %51, label %27
22: ; preds = %17, %15, %12
%23 = phi i32 [ %14, %12 ], [ %16, %15 ], [ %19, %17 ]
%24 = shl nsw i32 %5, 1
%25 = or i32 %24, 1
%26 = sdiv i32 %23, %25
br label %27
27: ; preds = %22, %20, %11
%28 = phi i32 [ undef, %11 ], [ 32768, %20 ], [ %26, %22 ]
%29 = and i32 %6, 1
%30 = icmp eq i32 %29, 0
br i1 %30, label %40, label %31
31: ; preds = %27
switch i32 %2, label %47 [
i32 0, label %32
i32 1, label %35
i32 2, label %37
]
32: ; preds = %31
%33 = sub nsw i32 %4, %0
%34 = shl nsw i32 %33, 16
br label %42
35: ; preds = %31
%36 = shl nsw i32 %4, 16
br label %42
37: ; preds = %31
%38 = shl i32 %0, 16
%39 = add i32 %38, 65536
br label %42
40: ; preds = %27
%41 = icmp eq i32 %2, 2
br i1 %41, label %51, label %47
42: ; preds = %37, %35, %32
%43 = phi i32 [ %34, %32 ], [ %36, %35 ], [ %39, %37 ]
%44 = shl nsw i32 %4, 1
%45 = or i32 %44, 1
%46 = sdiv i32 %43, %45
br label %47
47: ; preds = %42, %40, %31
%48 = phi i32 [ undef, %31 ], [ 32768, %40 ], [ %46, %42 ]
%49 = mul nsw i32 %48, %28
%50 = ashr i32 %49, 16
br label %51
51: ; preds = %47, %40, %20
%52 = phi i32 [ 0, %20 ], [ %50, %47 ], [ 0, %40 ]
ret i32 %52
}