#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*/ to;

/* Variables and functions */
int /*<<< orphan*/ VDP_doDMA(unsigned char, unsigned long, unsigned short,
                             unsigned short);
int /*<<< orphan*/ VDP_setAutoInc(short);
void VDP_doDMAEx(unsigned char location, unsigned long from, unsigned short to,
                 unsigned short len, short vramStep) {
  volatile unsigned short *pw;
  volatile unsigned long *pl;
  unsigned long newlen;
  unsigned long banklimitb;
  unsigned long banklimitw;
  banklimitb = 0x20000 - (from & 0x1FFFF);
  banklimitw = banklimitb >> 1;
  if (len > banklimitw) {
    VDP_doDMA(location, from + banklimitb, to + banklimitb, len - banklimitw);
    newlen = banklimitw;
  } else
    newlen = len;
  if (vramStep != -1)
    VDP_setAutoInc(vramStep);
  pw = (unsigned short *)0xC00004;
  *pw = 0x9300 + (newlen & 0xff);
  *pw = 0x9400 + ((newlen >> 8) & 0xff);
  from >>= 1;
  *pw = 0x9500 + (from & 0xff);
  from >>= 8;
  *pw = 0x9600 + (from & 0xff);
  from >>= 8;
  *pw = 0x9700 + (from & 0x7f);
  pl = (unsigned long *)0xC00004;
  switch (location) {
  case 0:
    *pl = ((0x4000 + ((to)&0x3FFF)) << 16) + (((to) >> 14) | 0x80);
    break;
  case 1:
    *pl = ((0xC000 + ((to)&0x3FFF)) << 16) + (((to) >> 14) | 0x80);
    break;
  case 2:
    *pl = ((0x4000 + ((to)&0x3FFF)) << 16) + (((to) >> 14) | 0x90);
    break;
  }
}


x86 assembly (clang, -O0):
<VDP_doDMAEx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %r8w,%ax
mov    %dil,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %dx,-0x12(%rbp)
mov    %cx,-0x14(%rbp)
mov    %ax,-0x16(%rbp)
mov    -0x10(%rbp),%rcx
and    $0x1ffff,%rcx
mov    $0x20000,%eax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x40(%rbp)
movzwl -0x14(%rbp),%eax
cmp    -0x40(%rbp),%rax
jbe    11aa <VDP_doDMAEx+0x8a>
mov    -0x1(%rbp),%dl
mov    -0x10(%rbp),%rsi
add    -0x38(%rbp),%rsi
movzwl -0x12(%rbp),%eax
add    -0x38(%rbp),%rax
mov    %ax,%cx
movzwl -0x14(%rbp),%eax
sub    -0x40(%rbp),%rax
movzbl %dl,%edi
movzwl %cx,%edx
movzwl %ax,%ecx
call   1030 <VDP_doDMA@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11b2 <VDP_doDMAEx+0x92>
movzwl -0x14(%rbp),%eax
mov    %rax,-0x30(%rbp)
movswl -0x16(%rbp),%eax
cmp    $0xffffffff,%eax
je     11c8 <VDP_doDMAEx+0xa8>
movswl -0x16(%rbp),%edi
call   1040 <VDP_setAutoInc@plt>
movq   $0xc00004,-0x20(%rbp)
movzbl -0x30(%rbp),%eax
or     $0x9300,%eax
mov    %ax,%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
movzbl -0x2f(%rbp),%eax
or     $0x9400,%eax
mov    %ax,%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
mov    -0x10(%rbp),%rax
shr    %rax
mov    %rax,-0x10(%rbp)
movzbl -0x10(%rbp),%eax
or     $0x9500,%eax
mov    %ax,%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
mov    -0x10(%rbp),%rax
shr    $0x8,%rax
mov    %rax,-0x10(%rbp)
movzbl -0x10(%rbp),%eax
or     $0x9600,%eax
mov    %ax,%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
mov    -0x10(%rbp),%rax
shr    $0x8,%rax
mov    %rax,-0x10(%rbp)
movzwl -0x10(%rbp),%eax
and    $0x7f,%eax
or     $0x9700,%eax
mov    %ax,%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
movq   $0xc00004,-0x28(%rbp)
movzbl -0x1(%rbp),%eax
mov    %eax,-0x44(%rbp)
test   %eax,%eax
je     1293 <VDP_doDMAEx+0x173>
jmp    1271 <VDP_doDMAEx+0x151>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
je     12c2 <VDP_doDMAEx+0x1a2>
jmp    1282 <VDP_doDMAEx+0x162>
mov    -0x44(%rbp),%eax
sub    $0x2,%eax
je     12f1 <VDP_doDMAEx+0x1d1>
jmp    131b <VDP_doDMAEx+0x1fb>
movzwl -0x12(%rbp),%eax
and    $0x3fff,%eax
add    $0x4000,%eax
shl    $0x10,%eax
movzwl -0x12(%rbp),%ecx
sar    $0xe,%ecx
or     $0x80,%ecx
add    %ecx,%eax
movslq %eax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
jmp    131b <VDP_doDMAEx+0x1fb>
movzwl -0x12(%rbp),%eax
and    $0x3fff,%eax
add    $0xc000,%eax
shl    $0x10,%eax
movzwl -0x12(%rbp),%ecx
sar    $0xe,%ecx
or     $0x80,%ecx
add    %ecx,%eax
movslq %eax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
jmp    131b <VDP_doDMAEx+0x1fb>
movzwl -0x12(%rbp),%eax
and    $0x3fff,%eax
add    $0x4000,%eax
shl    $0x10,%eax
movzwl -0x12(%rbp),%ecx
sar    $0xe,%ecx
or     $0x90,%ecx
add    %ecx,%eax
movslq %eax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<VDP_doDMAEx>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r12d
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %ebx,%edx
and    $0x1ffff,%edx
mov    $0x20000,%eax
sub    %rdx,%rax
mov    %rax,%rbp
shr    %rbp
mov    %ecx,%edx
cmp    %rdx,%rbp
jae    116c <VDP_doDMAEx+0x4c>
lea    (%rax,%rbx,1),%rsi
add    %r14d,%eax
sub    %ebp,%ecx
movzwl %ax,%edx
movzwl %cx,%ecx
movzbl %r15b,%edi
call   1030 <VDP_doDMA@plt>
mov    %rbp,%rdx
cmp    $0xffff,%r12w
je     1182 <VDP_doDMAEx+0x62>
movswl %r12w,%edi
mov    %rdx,%rbp
call   1040 <VDP_setAutoInc@plt>
mov    %rbp,%rdx
movzbl %dl,%eax
or     $0x9300,%eax
mov    %ax,0xc00004
movzbl %dh,%eax
or     $0x9400,%eax
mov    %ax,0xc00004
mov    %ebx,%eax
shr    %eax
movzbl %al,%eax
or     $0x9500,%eax
mov    %ax,0xc00004
mov    %ebx,%eax
shr    $0x9,%eax
movzbl %al,%eax
or     $0x9600,%eax
mov    %ax,0xc00004
shr    $0x11,%ebx
and    $0x7f,%ebx
or     $0x9700,%ebx
mov    %bx,0xc00004
cmp    $0x2,%r15b
je     120b <VDP_doDMAEx+0xeb>
cmp    $0x1,%r15b
je     1226 <VDP_doDMAEx+0x106>
test   %r15b,%r15b
jne    1243 <VDP_doDMAEx+0x123>
movzwl %r14w,%eax
mov    %eax,%ecx
shl    $0x10,%ecx
and    $0x3fff0000,%ecx
shr    $0xe,%eax
add    %ecx,%eax
add    $0x40000080,%eax
jmp    123b <VDP_doDMAEx+0x11b>
movzwl %r14w,%eax
mov    %eax,%ecx
shl    $0x10,%ecx
and    $0x3fff0000,%ecx
shr    $0xe,%eax
add    %ecx,%eax
add    $0x40000090,%eax
jmp    123b <VDP_doDMAEx+0x11b>
movzwl %r14w,%eax
mov    %eax,%ecx
shl    $0x10,%ecx
shr    $0xe,%eax
or     %ecx,%eax
or     $0xc0000080,%eax
cltq
mov    %rax,0xc00004
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @VDP_doDMAEx(i8 zeroext %0, i64 %1, i16 zeroext %2, i16 zeroext %3, i16 signext %4) {
%6 = alloca i8
%7 = alloca i64
%8 = alloca i16
%9 = alloca i16
%10 = alloca i16
%11 = alloca i16*
%12 = alloca i64*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
store i8 %0, i8* %6
store i64 %1, i64* %7
store i16 %2, i16* %8
store i16 %3, i16* %9
store i16 %4, i16* %10
%16 = load i64, i64* %7
%17 = and i64 %16, 131071
%18 = sub i64 131072, %17
store i64 %18, i64* %14
%19 = load i64, i64* %14
%20 = lshr i64 %19, 1
store i64 %20, i64* %15
%21 = load i16, i16* %9
%22 = zext i16 %21 to i64
%23 = load i64, i64* %15
%24 = icmp ugt i64 %22, %23
br i1 %24, label %25, label %42
25: ; preds = %5
%26 = load i8, i8* %6
%27 = load i64, i64* %7
%28 = load i64, i64* %14
%29 = add i64 %27, %28
%30 = load i16, i16* %8
%31 = zext i16 %30 to i64
%32 = load i64, i64* %14
%33 = add i64 %31, %32
%34 = trunc i64 %33 to i16
%35 = load i16, i16* %9
%36 = zext i16 %35 to i64
%37 = load i64, i64* %15
%38 = sub i64 %36, %37
%39 = trunc i64 %38 to i16
%40 = call i32 @VDP_doDMA(i8 zeroext %26, i64 %29, i16 zeroext %34, i16 zeroext %39)
%41 = load i64, i64* %15
store i64 %41, i64* %13
br label %45
42: ; preds = %5
%43 = load i16, i16* %9
%44 = zext i16 %43 to i64
store i64 %44, i64* %13
br label %45
45: ; preds = %42, %25
%46 = load i16, i16* %10
%47 = sext i16 %46 to i32
%48 = icmp ne i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i16, i16* %10
%51 = call i32 @VDP_setAutoInc(i16 signext %50)
br label %52
52: ; preds = %49, %45
store i16* inttoptr (i64 12582916 to i16*), i16** %11
%53 = load i64, i64* %13
%54 = and i64 %53, 255
%55 = add i64 37632, %54
%56 = trunc i64 %55 to i16
%57 = load i16*, i16** %11
store volatile i16 %56, i16* %57
%58 = load i64, i64* %13
%59 = lshr i64 %58, 8
%60 = and i64 %59, 255
%61 = add i64 37888, %60
%62 = trunc i64 %61 to i16
%63 = load i16*, i16** %11
store volatile i16 %62, i16* %63
%64 = load i64, i64* %7
%65 = lshr i64 %64, 1
store i64 %65, i64* %7
%66 = load i64, i64* %7
%67 = and i64 %66, 255
%68 = add i64 38144, %67
%69 = trunc i64 %68 to i16
%70 = load i16*, i16** %11
store volatile i16 %69, i16* %70
%71 = load i64, i64* %7
%72 = lshr i64 %71, 8
store i64 %72, i64* %7
%73 = load i64, i64* %7
%74 = and i64 %73, 255
%75 = add i64 38400, %74
%76 = trunc i64 %75 to i16
%77 = load i16*, i16** %11
store volatile i16 %76, i16* %77
%78 = load i64, i64* %7
%79 = lshr i64 %78, 8
store i64 %79, i64* %7
%80 = load i64, i64* %7
%81 = and i64 %80, 127
%82 = add i64 38656, %81
%83 = trunc i64 %82 to i16
%84 = load i16*, i16** %11
store volatile i16 %83, i16* %84
store i64* inttoptr (i64 12582916 to i64*), i64** %12
%85 = load i8, i8* %6
%86 = zext i8 %85 to i32
switch i32 %86, label %126 [
i32 0, label %87
i32 1, label %100
i32 2, label %113
]
87: ; preds = %52
%88 = load i16, i16* %8
%89 = zext i16 %88 to i32
%90 = and i32 %89, 16383
%91 = add nsw i32 16384, %90
%92 = shl i32 %91, 16
%93 = load i16, i16* %8
%94 = zext i16 %93 to i32
%95 = ashr i32 %94, 14
%96 = or i32 %95, 128
%97 = add nsw i32 %92, %96
%98 = sext i32 %97 to i64
%99 = load i64*, i64** %12
store volatile i64 %98, i64* %99
br label %126
100: ; preds = %52
%101 = load i16, i16* %8
%102 = zext i16 %101 to i32
%103 = and i32 %102, 16383
%104 = add nsw i32 49152, %103
%105 = shl i32 %104, 16
%106 = load i16, i16* %8
%107 = zext i16 %106 to i32
%108 = ashr i32 %107, 14
%109 = or i32 %108, 128
%110 = add nsw i32 %105, %109
%111 = sext i32 %110 to i64
%112 = load i64*, i64** %12
store volatile i64 %111, i64* %112
br label %126
113: ; preds = %52
%114 = load i16, i16* %8
%115 = zext i16 %114 to i32
%116 = and i32 %115, 16383
%117 = add nsw i32 16384, %116
%118 = shl i32 %117, 16
%119 = load i16, i16* %8
%120 = zext i16 %119 to i32
%121 = ashr i32 %120, 14
%122 = or i32 %121, 144
%123 = add nsw i32 %118, %122
%124 = sext i32 %123 to i64
%125 = load i64*, i64** %12
store volatile i64 %124, i64* %125
br label %126
126: ; preds = %52, %113, %100, %87
ret void
}

LLVM IR (-O2):
define void @VDP_doDMAEx(i8 zeroext %0, i64 %1, i16 zeroext %2, i16 zeroext %3, i16 signext %4) {
%6 = and i64 %1, 131071
%7 = sub nuw nsw i64 131072, %6
%8 = lshr i64 %7, 1
%9 = zext i16 %3 to i64
%10 = icmp ult i64 %8, %9
br i1 %10, label %11, label %18
11: ; preds = %5
%12 = add i64 %7, %1
%13 = trunc i64 %7 to i16
%14 = add i16 %13, %2
%15 = trunc i64 %8 to i16
%16 = sub i16 %3, %15
%17 = tail call i32 @VDP_doDMA(i8 zeroext %0, i64 %12, i16 zeroext %14, i16 zeroext %16)
br label %18
18: ; preds = %5, %11
%19 = phi i64 [ %8, %11 ], [ %9, %5 ]
%20 = icmp eq i16 %4, -1
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @VDP_setAutoInc(i16 signext %4)
br label %23
23: ; preds = %21, %18
%24 = trunc i64 %19 to i16
%25 = and i16 %24, 255
%26 = or i16 %25, -27904
store volatile i16 %26, i16* inttoptr (i64 12582916 to i16*)
%27 = lshr i16 %24, 8
%28 = or i16 %27, -27648
store volatile i16 %28, i16* inttoptr (i64 12582916 to i16*)
%29 = trunc i64 %1 to i16
%30 = lshr i16 %29, 1
%31 = and i16 %30, 255
%32 = or i16 %31, -27392
store volatile i16 %32, i16* inttoptr (i64 12582916 to i16*)
%33 = lshr i64 %1, 9
%34 = trunc i64 %33 to i16
%35 = and i16 %34, 255
%36 = or i16 %35, -27136
store volatile i16 %36, i16* inttoptr (i64 12582916 to i16*)
%37 = lshr i64 %1, 17
%38 = trunc i64 %37 to i16
%39 = and i16 %38, 127
%40 = or i16 %39, -26880
store volatile i16 %40, i16* inttoptr (i64 12582916 to i16*)
switch i8 %0, label %66 [
i8 0, label %41
i8 1, label %49
i8 2, label %56
]
41: ; preds = %23
%42 = zext i16 %2 to i32
%43 = shl nuw i32 %42, 16
%44 = and i32 %43, 1073676288
%45 = lshr i32 %42, 14
%46 = or i32 %45, %44
%47 = or i32 %46, 1073741952
%48 = zext i32 %47 to i64
br label %64
49: ; preds = %23
%50 = zext i16 %2 to i32
%51 = shl nuw i32 %50, 16
%52 = lshr i32 %50, 14
%53 = or i32 %51, %52
%54 = or i32 %53, -1073741696
%55 = sext i32 %54 to i64
br label %64
56: ; preds = %23
%57 = zext i16 %2 to i32
%58 = shl nuw i32 %57, 16
%59 = and i32 %58, 1073676288
%60 = lshr i32 %57, 14
%61 = or i32 %60, %59
%62 = or i32 %61, 1073741968
%63 = zext i32 %62 to i64
br label %64
64: ; preds = %41, %49, %56
%65 = phi i64 [ %63, %56 ], [ %55, %49 ], [ %48, %41 ]
store volatile i64 %65, i64* inttoptr (i64 12582916 to i64*)
br label %66
66: ; preds = %64, %23
ret void
}