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

/* Variables and functions */
int /*<<< orphan*/ VIM_HIF_SetReg8(int, unsigned char);
unsigned long VIM_HIF_WriteSramOnWord(unsigned long adr, unsigned long dat,
                                      unsigned long size) {
  unsigned long actSize;
  if (adr > 0x20000) {
    return 0;
  }
  if (size > (0x20000 - adr)) {
    size = 0x20000 - adr;
  }
  size >>= 2;
  actSize = size << 2;
  adr += 0x100000;
  VIM_HIF_SetReg8((0x1800 + 0x086), 0x1);
  VIM_HIF_SetReg8((0x1800 + 0x044), (unsigned char)adr);
  VIM_HIF_SetReg8((0x1800 + 0x045), (unsigned char)(adr >> 8));
  VIM_HIF_SetReg8((0x1800 + 0x048), (unsigned char)(adr >> 16));
  ;
  *(volatile unsigned short *)(0x8000000) = (unsigned short)0x1850;
  ;
  while (size--) {
    *(volatile unsigned short *)(0x8000100) = (unsigned short)dat;
    *(volatile unsigned short *)(0x8000100) = (unsigned short)(dat >> 16);
  }
  return actSize;
}


x86 assembly (clang, -O0):
<VIM_HIF_WriteSramOnWord>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x20000,-0x10(%rbp)
jbe    113f <VIM_HIF_WriteSramOnWord+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    1224 <VIM_HIF_WriteSramOnWord+0x114>
mov    -0x20(%rbp),%rax
mov    $0x20000,%ecx
sub    -0x10(%rbp),%rcx
cmp    %rcx,%rax
jbe    1162 <VIM_HIF_WriteSramOnWord+0x52>
mov    $0x20000,%eax
sub    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shr    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    $0x100000,%rax
mov    %rax,-0x10(%rbp)
mov    $0x1886,%edi
mov    $0x1,%esi
call   1030 <VIM_HIF_SetReg8@plt>
mov    -0x10(%rbp),%rax
mov    $0x1844,%edi
movzbl %al,%esi
call   1030 <VIM_HIF_SetReg8@plt>
mov    -0x10(%rbp),%rax
shr    $0x8,%rax
mov    $0x1845,%edi
movzbl %al,%esi
call   1030 <VIM_HIF_SetReg8@plt>
mov    -0x10(%rbp),%rax
shr    $0x10,%rax
mov    $0x1848,%edi
movzbl %al,%esi
call   1030 <VIM_HIF_SetReg8@plt>
mov    $0x8000000,%eax
movw   $0x1850,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
cmp    $0x0,%rax
je     121c <VIM_HIF_WriteSramOnWord+0x10c>
mov    -0x18(%rbp),%rax
mov    %ax,%cx
mov    $0x8000100,%eax
mov    %cx,(%rax)
mov    -0x18(%rbp),%rax
shr    $0x10,%rax
mov    %ax,%cx
mov    $0x8000100,%eax
mov    %cx,(%rax)
jmp    11dc <VIM_HIF_WriteSramOnWord+0xcc>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<VIM_HIF_WriteSramOnWord>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x20000,%rdi
jbe    1129 <VIM_HIF_WriteSramOnWord+0x19>
xor    %r14d,%r14d
jmp    127a <VIM_HIF_WriteSramOnWord+0x16a>
mov    %rsi,%rbx
mov    %rdi,%rax
mov    $0x20000,%r15d
sub    %rdi,%r15
cmp    %rdx,%r15
cmovae %rdx,%r15
mov    %r15,%r14
and    $0xfffffffffffffffc,%r14
add    $0x100000,%eax
mov    $0x1886,%edi
mov    $0x1,%esi
mov    %rax,%r12
call   1030 <VIM_HIF_SetReg8@plt>
movzbl %r12b,%esi
mov    $0x1844,%edi
call   1030 <VIM_HIF_SetReg8@plt>
mov    %r12,%rax
movzbl %ah,%esi
mov    $0x1845,%edi
call   1030 <VIM_HIF_SetReg8@plt>
mov    %r12,%rax
shr    $0x10,%eax
movzbl %al,%esi
mov    $0x1848,%edi
call   1030 <VIM_HIF_SetReg8@plt>
movw   $0x1850,0x8000000
08 50 18
cmp    $0x4,%r15
jb     127a <VIM_HIF_WriteSramOnWord+0x16a>
shr    $0x2,%r15
mov    %rbx,%rax
shr    $0x10,%rax
lea    -0x1(%r15),%rcx
mov    %r15,%rdx
and    $0x7,%rdx
je     11dc <VIM_HIF_WriteSramOnWord+0xcc>
xor    %esi,%esi
nopl   0x0(%rax)
mov    %bx,0x8000100
08
mov    %ax,0x8000100
08
add    $0x1,%rsi
cmp    %rsi,%rdx
jne    11c0 <VIM_HIF_WriteSramOnWord+0xb0>
sub    %rsi,%r15
cmp    $0x7,%rcx
jb     127a <VIM_HIF_WriteSramOnWord+0x16a>
cs nopw 0x0(%rax,%rax,1)
mov    %bx,0x8000100
08
mov    %ax,0x8000100
08
mov    %bx,0x8000100
08
mov    %ax,0x8000100
08
mov    %bx,0x8000100
08
mov    %ax,0x8000100
08
mov    %bx,0x8000100
08
mov    %ax,0x8000100
08
mov    %bx,0x8000100
08
mov    %ax,0x8000100
08
mov    %bx,0x8000100
08
mov    %ax,0x8000100
08
mov    %bx,0x8000100
08
mov    %ax,0x8000100
08
mov    %bx,0x8000100
08
mov    %ax,0x8000100
08
add    $0xfffffffffffffff8,%r15
jne    11f0 <VIM_HIF_WriteSramOnWord+0xe0>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @VIM_HIF_WriteSramOnWord(i64 %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i64 %0, i64* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%9 = load i64, i64* %5
%10 = icmp ugt i64 %9, 131072
br i1 %10, label %11, label %12
11: ; preds = %3
store i64 0, i64* %4
br label %51
12: ; preds = %3
%13 = load i64, i64* %7
%14 = load i64, i64* %5
%15 = sub i64 131072, %14
%16 = icmp ugt i64 %13, %15
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i64, i64* %5
%19 = sub i64 131072, %18
store i64 %19, i64* %7
br label %20
20: ; preds = %17, %12
%21 = load i64, i64* %7
%22 = lshr i64 %21, 2
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = shl i64 %23, 2
store i64 %24, i64* %8
%25 = load i64, i64* %5
%26 = add i64 %25, 1048576
store i64 %26, i64* %5
%27 = call i32 @VIM_HIF_SetReg8(i32 6278, i8 zeroext 1)
%28 = load i64, i64* %5
%29 = trunc i64 %28 to i8
%30 = call i32 @VIM_HIF_SetReg8(i32 6212, i8 zeroext %29)
%31 = load i64, i64* %5
%32 = lshr i64 %31, 8
%33 = trunc i64 %32 to i8
%34 = call i32 @VIM_HIF_SetReg8(i32 6213, i8 zeroext %33)
%35 = load i64, i64* %5
%36 = lshr i64 %35, 16
%37 = trunc i64 %36 to i8
%38 = call i32 @VIM_HIF_SetReg8(i32 6216, i8 zeroext %37)
store volatile i16 6224, i16* inttoptr (i64 134217728 to i16*)
br label %39
39: ; preds = %43, %20
%40 = load i64, i64* %7
%41 = add i64 %40, -1
store i64 %41, i64* %7
%42 = icmp ne i64 %40, 0
br i1 %42, label %43, label %49
43: ; preds = %39
%44 = load i64, i64* %6
%45 = trunc i64 %44 to i16
store volatile i16 %45, i16* inttoptr (i64 134217984 to i16*)
%46 = load i64, i64* %6
%47 = lshr i64 %46, 16
%48 = trunc i64 %47 to i16
store volatile i16 %48, i16* inttoptr (i64 134217984 to i16*)
br label %39
49: ; preds = %39
%50 = load i64, i64* %8
store i64 %50, i64* %4
br label %51
51: ; preds = %49, %11
%52 = load i64, i64* %4
ret i64 %52
}

LLVM IR (-O2):
define i64 @VIM_HIF_WriteSramOnWord(i64 %0, i64 %1, i64 %2) {
%4 = icmp ugt i64 %0, 131072
br i1 %4, label %42, label %5
5: ; preds = %3
%6 = sub nuw nsw i64 131072, %0
%7 = icmp ult i64 %6, %2
%8 = select i1 %7, i64 %6, i64 %2
%9 = and i64 %8, -4
%10 = add nuw nsw i64 %0, 1048576
%11 = tail call i32 @VIM_HIF_SetReg8(i32 6278, i8 zeroext 1)
%12 = trunc i64 %10 to i8
%13 = tail call i32 @VIM_HIF_SetReg8(i32 6212, i8 zeroext %12)
%14 = lshr i64 %10, 8
%15 = trunc i64 %14 to i8
%16 = tail call i32 @VIM_HIF_SetReg8(i32 6213, i8 zeroext %15)
%17 = lshr i64 %10, 16
%18 = trunc i64 %17 to i8
%19 = tail call i32 @VIM_HIF_SetReg8(i32 6216, i8 zeroext %18)
store volatile i16 6224, i16* inttoptr (i64 134217728 to i16*)
%20 = icmp ult i64 %8, 4
br i1 %20, label %42, label %21
21: ; preds = %5
%22 = lshr i64 %8, 2
%23 = trunc i64 %1 to i16
%24 = lshr i64 %1, 16
%25 = trunc i64 %24 to i16
%26 = add nsw i64 %22, -1
%27 = and i64 %22, 7
%28 = icmp eq i64 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %21, %29
%30 = phi i64 [ %32, %29 ], [ %22, %21 ]
%31 = phi i64 [ %33, %29 ], [ 0, %21 ]
%32 = add i64 %30, -1
store volatile i16 %23, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %25, i16* inttoptr (i64 134217984 to i16*)
%33 = add i64 %31, 1
%34 = icmp eq i64 %33, %27
br i1 %34, label %35, label %29
35: ; preds = %29, %21
%36 = phi i64 [ %22, %21 ], [ %32, %29 ]
%37 = icmp ult i64 %26, 7
br i1 %37, label %42, label %38
38: ; preds = %35, %38
%39 = phi i64 [ %40, %38 ], [ %36, %35 ]
store volatile i16 %23, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %25, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %23, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %25, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %23, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %25, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %23, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %25, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %23, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %25, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %23, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %25, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %23, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %25, i16* inttoptr (i64 134217984 to i16*)
%40 = add i64 %39, -8
store volatile i16 %23, i16* inttoptr (i64 134217984 to i16*)
store volatile i16 %25, i16* inttoptr (i64 134217984 to i16*)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %38
42: ; preds = %35, %38, %5, %3
%43 = phi i64 [ 0, %3 ], [ %9, %5 ], [ %9, %38 ], [ %9, %35 ]
ret i64 %43
}