#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_setAutoInc(int);
void VDP_doDMA(unsigned char location, unsigned long from, unsigned short to,
               unsigned short len) {
  volatile unsigned short *pw;
  volatile unsigned long *pl;
  unsigned long newlen;
  unsigned long banklimit;
  banklimit = 0x20000 - (from & 0x1FFFF);
  if (len > banklimit) {
    VDP_doDMA(location, from + banklimit, to + banklimit, len - banklimit);
    newlen = banklimit;
  } else
    newlen = len;
  VDP_setAutoInc(2);
  pw = (unsigned short *)0xC00004;
  newlen >>= 1;
  *pw = 0x9300 + (newlen & 0xff);
  newlen >>= 8;
  *pw = 0x9400 + (newlen & 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_doDMA>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %cx,%ax
mov    %dx,%cx
mov    %dil,%dl
mov    %dl,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %cx,-0x12(%rbp)
mov    %ax,-0x14(%rbp)
mov    -0x10(%rbp),%rcx
and    $0x1ffff,%rcx
mov    $0x20000,%eax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
movzwl -0x14(%rbp),%eax
cmp    -0x38(%rbp),%rax
jbe    119e <VDP_doDMA+0x7e>
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    -0x38(%rbp),%rax
movzbl %dl,%edi
movzwl %cx,%edx
movzwl %ax,%ecx
call   1030 <VDP_doDMA@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11a6 <VDP_doDMA+0x86>
movzwl -0x14(%rbp),%eax
mov    %rax,-0x30(%rbp)
mov    $0x2,%edi
call   1040 <VDP_setAutoInc@plt>
movq   $0xc00004,-0x20(%rbp)
mov    -0x30(%rbp),%rax
shr    %rax
mov    %rax,-0x30(%rbp)
movzbl -0x30(%rbp),%eax
or     $0x9300,%eax
mov    %ax,%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
mov    -0x30(%rbp),%rax
shr    $0x8,%rax
mov    %rax,-0x30(%rbp)
movzbl -0x30(%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,-0x3c(%rbp)
test   %eax,%eax
je     1292 <VDP_doDMA+0x172>
jmp    1270 <VDP_doDMA+0x150>
mov    -0x3c(%rbp),%eax
sub    $0x1,%eax
je     12c1 <VDP_doDMA+0x1a1>
jmp    1281 <VDP_doDMA+0x161>
mov    -0x3c(%rbp),%eax
sub    $0x2,%eax
je     12f0 <VDP_doDMA+0x1d0>
jmp    131a <VDP_doDMA+0x1fa>
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    131a <VDP_doDMA+0x1fa>
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    131a <VDP_doDMA+0x1fa>
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    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<VDP_doDMA>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r12d
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %ebx,%eax
and    $0x1ffff,%eax
mov    $0x20000,%ebp
sub    %rax,%rbp
mov    %ecx,%eax
cmp    %rax,%rbp
jae    116a <VDP_doDMA+0x4a>
lea    (%rbx,%rbp,1),%rsi
lea    (%r14,%rbp,1),%eax
sub    %ebp,%r12d
movzwl %ax,%edx
movzwl %r12w,%ecx
movzbl %r15b,%edi
call   1030 <VDP_doDMA@plt>
mov    %ebp,%r12d
jmp    116d <VDP_doDMA+0x4d>
mov    %rax,%rbp
mov    $0x2,%edi
call   1040 <VDP_setAutoInc@plt>
shr    %r12d
movzbl %r12b,%eax
or     $0x9300,%eax
mov    %ax,0xc00004
shr    $0x9,%ebp
movzbl %bpl,%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     1208 <VDP_doDMA+0xe8>
cmp    $0x1,%r15b
je     1223 <VDP_doDMA+0x103>
test   %r15b,%r15b
jne    1240 <VDP_doDMA+0x120>
movzwl %r14w,%eax
mov    %eax,%ecx
shl    $0x10,%ecx
and    $0x3fff0000,%ecx
shr    $0xe,%eax
add    %ecx,%eax
add    $0x40000080,%eax
jmp    1238 <VDP_doDMA+0x118>
movzwl %r14w,%eax
mov    %eax,%ecx
shl    $0x10,%ecx
and    $0x3fff0000,%ecx
shr    $0xe,%eax
add    %ecx,%eax
add    $0x40000090,%eax
jmp    1238 <VDP_doDMA+0x118>
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_doDMA(i8 zeroext %0, i64 %1, i16 zeroext %2, i16 zeroext %3) {
%5 = alloca i8
%6 = alloca i64
%7 = alloca i16
%8 = alloca i16
%9 = alloca i16*
%10 = alloca i64*
%11 = alloca i64
%12 = alloca i64
store i8 %0, i8* %5
store i64 %1, i64* %6
store i16 %2, i16* %7
store i16 %3, i16* %8
%13 = load i64, i64* %6
%14 = and i64 %13, 131071
%15 = sub i64 131072, %14
store i64 %15, i64* %12
%16 = load i16, i16* %8
%17 = zext i16 %16 to i64
%18 = load i64, i64* %12
%19 = icmp ugt i64 %17, %18
br i1 %19, label %20, label %36
20: ; preds = %4
%21 = load i8, i8* %5
%22 = load i64, i64* %6
%23 = load i64, i64* %12
%24 = add i64 %22, %23
%25 = load i16, i16* %7
%26 = zext i16 %25 to i64
%27 = load i64, i64* %12
%28 = add i64 %26, %27
%29 = trunc i64 %28 to i16
%30 = load i16, i16* %8
%31 = zext i16 %30 to i64
%32 = load i64, i64* %12
%33 = sub i64 %31, %32
%34 = trunc i64 %33 to i16
%35 = load i64, i64* %12
store i64 %35, i64* %11
br label %39
36: ; preds = %4
%37 = load i16, i16* %8
%38 = zext i16 %37 to i64
store i64 %38, i64* %11
br label %39
39: ; preds = %36, %20
%40 = call i32 @VDP_setAutoInc(i32 2)
store i16* inttoptr (i64 12582916 to i16*), i16** %9
%41 = load i64, i64* %11
%42 = lshr i64 %41, 1
store i64 %42, i64* %11
%43 = load i64, i64* %11
%44 = and i64 %43, 255
%45 = add i64 37632, %44
%46 = trunc i64 %45 to i16
%47 = load i16*, i16** %9
store volatile i16 %46, i16* %47
%48 = load i64, i64* %11
%49 = lshr i64 %48, 8
store i64 %49, i64* %11
%50 = load i64, i64* %11
%51 = and i64 %50, 255
%52 = add i64 37888, %51
%53 = trunc i64 %52 to i16
%54 = load i16*, i16** %9
store volatile i16 %53, i16* %54
%55 = load i64, i64* %6
%56 = lshr i64 %55, 1
store i64 %56, i64* %6
%57 = load i64, i64* %6
%58 = and i64 %57, 255
%59 = add i64 38144, %58
%60 = trunc i64 %59 to i16
%61 = load i16*, i16** %9
store volatile i16 %60, i16* %61
%62 = load i64, i64* %6
%63 = lshr i64 %62, 8
store i64 %63, i64* %6
%64 = load i64, i64* %6
%65 = and i64 %64, 255
%66 = add i64 38400, %65
%67 = trunc i64 %66 to i16
%68 = load i16*, i16** %9
store volatile i16 %67, i16* %68
%69 = load i64, i64* %6
%70 = lshr i64 %69, 8
store i64 %70, i64* %6
%71 = load i64, i64* %6
%72 = and i64 %71, 127
%73 = add i64 38656, %72
%74 = trunc i64 %73 to i16
%75 = load i16*, i16** %9
store volatile i16 %74, i16* %75
store i64* inttoptr (i64 12582916 to i64*), i64** %10
%76 = load i8, i8* %5
%77 = zext i8 %76 to i32
switch i32 %77, label %117 [
i32 0, label %78
i32 1, label %91
i32 2, label %104
]
78: ; preds = %39
%79 = load i16, i16* %7
%80 = zext i16 %79 to i32
%81 = and i32 %80, 16383
%82 = add nsw i32 16384, %81
%83 = shl i32 %82, 16
%84 = load i16, i16* %7
%85 = zext i16 %84 to i32
%86 = ashr i32 %85, 14
%87 = or i32 %86, 128
%88 = add nsw i32 %83, %87
%89 = sext i32 %88 to i64
%90 = load i64*, i64** %10
store volatile i64 %89, i64* %90
br label %117
91: ; preds = %39
%92 = load i16, i16* %7
%93 = zext i16 %92 to i32
%94 = and i32 %93, 16383
%95 = add nsw i32 49152, %94
%96 = shl i32 %95, 16
%97 = load i16, i16* %7
%98 = zext i16 %97 to i32
%99 = ashr i32 %98, 14
%100 = or i32 %99, 128
%101 = add nsw i32 %96, %100
%102 = sext i32 %101 to i64
%103 = load i64*, i64** %10
store volatile i64 %102, i64* %103
br label %117
104: ; preds = %39
%105 = load i16, i16* %7
%106 = zext i16 %105 to i32
%107 = and i32 %106, 16383
%108 = add nsw i32 16384, %107
%109 = shl i32 %108, 16
%110 = load i16, i16* %7
%111 = zext i16 %110 to i32
%112 = ashr i32 %111, 14
%113 = or i32 %112, 144
%114 = add nsw i32 %109, %113
%115 = sext i32 %114 to i64
%116 = load i64*, i64** %10
store volatile i64 %115, i64* %116
br label %117
117: ; preds = %39, %104, %91, %78
ret void
}

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