#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_ReadSram(unsigned long adr, unsigned char *buf,
                               unsigned long size) {
  unsigned long actSize;
  unsigned short uTemp;
  if (adr > 0x20000) {
    return 0;
  }
  if (size > (0x20000 - adr)) {
    size = 0x20000 - adr;
  }
  size >>= 1;
  actSize = size << 1;
  adr += 0x100000;
  VIM_HIF_SetReg8((0x1800 + 0x086), 0x0);
  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--) {
    uTemp = *(volatile unsigned short *)(0x8000100);
    *buf++ = (unsigned char)uTemp;
    *buf++ = (unsigned char)(uTemp >> 8);
  }
  return actSize;
}


x86 assembly (clang, -O0):
<VIM_HIF_ReadSram>:
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_ReadSram+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    123c <VIM_HIF_ReadSram+0x12c>
mov    -0x20(%rbp),%rax
mov    $0x20000,%ecx
sub    -0x10(%rbp),%rcx
cmp    %rcx,%rax
jbe    1162 <VIM_HIF_ReadSram+0x52>
mov    $0x20000,%eax
sub    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    $0x100000,%rax
mov    %rax,-0x10(%rbp)
mov    $0x1886,%edi
xor    %esi,%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     1234 <VIM_HIF_ReadSram+0x124>
mov    $0x8000100,%eax
mov    (%rax),%ax
mov    %ax,-0x2a(%rbp)
mov    -0x2a(%rbp),%ax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movzwl -0x2a(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    11d9 <VIM_HIF_ReadSram+0xc9>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<VIM_HIF_ReadSram>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x20000,%rdi
jbe    1129 <VIM_HIF_ReadSram+0x19>
xor    %r14d,%r14d
jmp    122c <VIM_HIF_ReadSram+0x11c>
mov    %rsi,%rbx
mov    %rdi,%rax
mov    $0x20000,%r15d
sub    %rdi,%r15
cmp    %rdx,%r15
cmovae %rdx,%r15
mov    %r15,%r14
and    $0xfffffffffffffffe,%r14
add    $0x100000,%eax
mov    $0x1886,%edi
xor    %esi,%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    $0x2,%r15
jb     122c <VIM_HIF_ReadSram+0x11c>
shr    %r15
lea    -0x1(%r15),%rax
mov    %r15,%rcx
and    $0x3,%rcx
je     11db <VIM_HIF_ReadSram+0xcb>
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzwl 0x8000100,%esi
08
mov    %si,(%rbx)
add    $0x2,%rbx
add    $0x1,%rdx
cmp    %rdx,%rcx
jne    11c0 <VIM_HIF_ReadSram+0xb0>
sub    %rdx,%r15
cmp    $0x3,%rax
jb     122c <VIM_HIF_ReadSram+0x11c>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzwl 0x8000100,%ecx
08
mov    %cx,(%rbx,%rax,2)
movzwl 0x8000100,%ecx
08
mov    %cx,0x2(%rbx,%rax,2)
movzwl 0x8000100,%ecx
08
mov    %cx,0x4(%rbx,%rax,2)
movzwl 0x8000100,%ecx
08
mov    %cx,0x6(%rbx,%rax,2)
add    $0x4,%rax
cmp    %rax,%r15
jne    11f0 <VIM_HIF_ReadSram+0xe0>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i64 @VIM_HIF_ReadSram(i64 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = icmp ugt i64 %0, 131072
br i1 %4, label %72, 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, -2
%10 = add nuw nsw i64 %0, 1048576
%11 = tail call i32 @VIM_HIF_SetReg8(i32 6278, i8 zeroext 0)
%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, 2
br i1 %20, label %72, label %21
21: ; preds = %5
%22 = lshr i64 %8, 1
%23 = add nsw i64 %22, -1
%24 = and i64 %22, 3
%25 = icmp eq i64 %24, 0
br i1 %25, label %39, label %26
26: ; preds = %21, %26
%27 = phi i64 [ %30, %26 ], [ %22, %21 ]
%28 = phi i8* [ %36, %26 ], [ %1, %21 ]
%29 = phi i64 [ %37, %26 ], [ 0, %21 ]
%30 = add i64 %27, -1
%31 = load volatile i16, i16* inttoptr (i64 134217984 to i16*)
%32 = trunc i16 %31 to i8
%33 = getelementptr inbounds i8, i8* %28, i64 1
store i8 %32, i8* %28
%34 = lshr i16 %31, 8
%35 = trunc i16 %34 to i8
%36 = getelementptr inbounds i8, i8* %28, i64 2
store i8 %35, i8* %33
%37 = add i64 %29, 1
%38 = icmp eq i64 %37, %24
br i1 %38, label %39, label %26
39: ; preds = %26, %21
%40 = phi i64 [ %22, %21 ], [ %30, %26 ]
%41 = phi i8* [ %1, %21 ], [ %36, %26 ]
%42 = icmp ult i64 %23, 3
br i1 %42, label %72, label %43
43: ; preds = %39, %43
%44 = phi i64 [ %64, %43 ], [ %40, %39 ]
%45 = phi i8* [ %70, %43 ], [ %41, %39 ]
%46 = load volatile i16, i16* inttoptr (i64 134217984 to i16*)
%47 = trunc i16 %46 to i8
%48 = getelementptr inbounds i8, i8* %45, i64 1
store i8 %47, i8* %45
%49 = lshr i16 %46, 8
%50 = trunc i16 %49 to i8
%51 = getelementptr inbounds i8, i8* %45, i64 2
store i8 %50, i8* %48
%52 = load volatile i16, i16* inttoptr (i64 134217984 to i16*)
%53 = trunc i16 %52 to i8
%54 = getelementptr inbounds i8, i8* %45, i64 3
store i8 %53, i8* %51
%55 = lshr i16 %52, 8
%56 = trunc i16 %55 to i8
%57 = getelementptr inbounds i8, i8* %45, i64 4
store i8 %56, i8* %54
%58 = load volatile i16, i16* inttoptr (i64 134217984 to i16*)
%59 = trunc i16 %58 to i8
%60 = getelementptr inbounds i8, i8* %45, i64 5
store i8 %59, i8* %57
%61 = lshr i16 %58, 8
%62 = trunc i16 %61 to i8
%63 = getelementptr inbounds i8, i8* %45, i64 6
store i8 %62, i8* %60
%64 = add i64 %44, -4
%65 = load volatile i16, i16* inttoptr (i64 134217984 to i16*)
%66 = trunc i16 %65 to i8
%67 = getelementptr inbounds i8, i8* %45, i64 7
store i8 %66, i8* %63
%68 = lshr i16 %65, 8
%69 = trunc i16 %68 to i8
%70 = getelementptr inbounds i8, i8* %45, i64 8
store i8 %69, i8* %67
%71 = icmp eq i64 %64, 0
br i1 %71, label %72, label %43
72: ; preds = %39, %43, %5, %3
%73 = phi i64 [ 0, %3 ], [ %9, %5 ], [ %9, %43 ], [ %9, %39 ]
ret i64 %73
}