#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*/ FlashReset();
int /*<<< orphan*/ FlashResetRelease();
int /*<<< orphan*/ RamRead32A(int, unsigned long *);
int /*<<< orphan*/ RamWrite32A(int, int);
void FlashNVR_ReadData_Byte(unsigned char Sel, unsigned char SetAddress,
                            unsigned char *ReadPtr, unsigned short Num) {
  unsigned short UsNum;
  unsigned long UlReadVal;
  if (Sel >= 3)
    return;
  if (Num == 0 || Num > 256)
    return;
  if (SetAddress + Num > 256)
    return;
  FlashResetRelease();
  RamWrite32A(0xC000, (0xE07000 + 0x14));
  RamWrite32A(0xD000, 1);
  RamWrite32A(0xC000, (0xE07000 + 0x1C));
  RamWrite32A(0xD000, 7);
  RamWrite32A(0xC000, (0xE07000 + 0x08));
  RamWrite32A(0xD000, 0x00010100);
  RamWrite32A(0xC000, (0xE07000 + 0x0C));
  RamWrite32A(0xD000, 7);
  RamWrite32A(0xC000, (0xE07000 + 0x10));
  RamWrite32A(0xD000, 7);
  RamWrite32A(0xC000, (0xE07000 + 0x0C));
  RamWrite32A(0xD000, Num - 1);
  RamWrite32A(0xC000, (0xE07000 + 0x1C));
  RamWrite32A(0xD000, (1 << Sel));
  RamWrite32A(0xC000, (0xE07000 + 0x08));
  RamWrite32A(0xD000, 0x00010000 + SetAddress);
  RamWrite32A(0xC000, (0xE07000 + 0x10));
  RamWrite32A(0xD000, 1);
  RamWrite32A(0xC000, (0xE07000 + 0x00));
  for (UsNum = 0; UsNum < Num; UsNum++) {
    RamRead32A(0xD000, &UlReadVal);
    ReadPtr[UsNum] = (unsigned char)(UlReadVal >> (Sel * 8));
  }
  FlashReset();
}


x86 assembly (clang, -O0):
<FlashNVR_ReadData_Byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cx,%ax
mov    %sil,%cl
mov    %dil,%sil
mov    %sil,-0x1(%rbp)
mov    %cl,-0x2(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movzbl -0x1(%rbp),%eax
cmp    $0x3,%eax
jl     1172 <FlashNVR_ReadData_Byte+0x32>
jmp    1337 <FlashNVR_ReadData_Byte+0x1f7>
movzwl -0x12(%rbp),%eax
cmp    $0x0,%eax
je     118e <FlashNVR_ReadData_Byte+0x4e>
movzwl -0x12(%rbp),%eax
cmp    $0x100,%eax
jle    1193 <FlashNVR_ReadData_Byte+0x53>
jmp    1337 <FlashNVR_ReadData_Byte+0x1f7>
movzbl -0x2(%rbp),%eax
movzwl -0x12(%rbp),%ecx
add    %ecx,%eax
cmp    $0x100,%eax
jle    11ad <FlashNVR_ReadData_Byte+0x6d>
jmp    1337 <FlashNVR_ReadData_Byte+0x1f7>
mov    $0x0,%al
call   1040 <FlashResetRelease@plt>
mov    $0xc000,%edi
mov    $0xe07014,%esi
call   1060 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x1,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1060 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1060 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x10100,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0700c,%esi
call   1060 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1060 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0700c,%esi
call   1060 <RamWrite32A@plt>
movzwl -0x12(%rbp),%esi
sub    $0x1,%esi
mov    $0xd000,%edi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1060 <RamWrite32A@plt>
movzbl -0x1(%rbp),%ecx
mov    $0x1,%esi
shl    %cl,%esi
mov    $0xd000,%edi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1060 <RamWrite32A@plt>
movzbl -0x2(%rbp),%esi
add    $0x10000,%esi
mov    $0xd000,%edi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1060 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x1,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07000,%esi
call   1060 <RamWrite32A@plt>
movw   $0x0,-0x14(%rbp)
movzwl -0x14(%rbp),%eax
movzwl -0x12(%rbp),%ecx
cmp    %ecx,%eax
jge    1330 <FlashNVR_ReadData_Byte+0x1f0>
mov    $0xd000,%edi
lea    -0x20(%rbp),%rsi
call   1050 <RamRead32A@plt>
mov    -0x20(%rbp),%rax
movzbl -0x1(%rbp),%ecx
shl    $0x3,%ecx
mov    %ecx,%ecx
shr    %cl,%rax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movzwl -0x14(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x14(%rbp)
jmp    12e4 <FlashNVR_ReadData_Byte+0x1a4>
mov    $0x0,%al
call   1030 <FlashReset@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FlashNVR_ReadData_Byte>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r13d
cmp    $0x2,%r13b
ja     12eb <FlashNVR_ReadData_Byte+0x1ab>
lea    -0x101(%rcx),%eax
movzwl %ax,%eax
cmp    $0xff00,%eax
jb     12eb <FlashNVR_ReadData_Byte+0x1ab>
movzwl %cx,%r12d
movzbl %sil,%r15d
lea    (%r12,%r15,1),%eax
cmp    $0x100,%eax
ja     12eb <FlashNVR_ReadData_Byte+0x1ab>
mov    %rdx,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <FlashResetRelease@plt>
mov    $0xc000,%edi
mov    $0xe07014,%esi
call   1060 <RamWrite32A@plt>
mov    $0x1,%ebp
mov    $0xd000,%edi
mov    $0x1,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1060 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1060 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x10100,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0700c,%esi
call   1060 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1060 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0700c,%esi
call   1060 <RamWrite32A@plt>
lea    -0x1(%r12),%esi
mov    $0xd000,%edi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1060 <RamWrite32A@plt>
mov    %r13d,%ecx
shl    %cl,%ebp
mov    $0xd000,%edi
mov    %ebp,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1060 <RamWrite32A@plt>
or     $0x10000,%r15d
mov    $0xd000,%edi
mov    %r15d,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1060 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x1,%esi
call   1060 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07000,%esi
call   1060 <RamWrite32A@plt>
shl    $0x3,%r13d
mov    %rsp,%rbp
nop
mov    $0xd000,%edi
mov    %rbp,%rsi
call   1050 <RamRead32A@plt>
mov    (%rsp),%rax
mov    %r13d,%ecx
shr    %cl,%rax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    12c0 <FlashNVR_ReadData_Byte+0x180>
xor    %eax,%eax
call   1030 <FlashReset@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @FlashNVR_ReadData_Byte(i8 zeroext %0, i8 zeroext %1, i8* %2, i16 zeroext %3) {
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i16
%9 = alloca i16
%10 = alloca i64
store i8 %0, i8* %5
store i8 %1, i8* %6
store i8* %2, i8** %7
store i16 %3, i16* %8
%11 = load i8, i8* %5
%12 = zext i8 %11 to i32
%13 = icmp sge i32 %12, 3
br i1 %13, label %14, label %15
14: ; preds = %4
br label %86
15: ; preds = %4
%16 = load i16, i16* %8
%17 = zext i16 %16 to i32
%18 = icmp eq i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i16, i16* %8
%21 = zext i16 %20 to i32
%22 = icmp sgt i32 %21, 256
br i1 %22, label %23, label %24
23: ; preds = %19, %15
br label %86
24: ; preds = %19
%25 = load i8, i8* %6
%26 = zext i8 %25 to i32
%27 = load i16, i16* %8
%28 = zext i16 %27 to i32
%29 = add nsw i32 %26, %28
%30 = icmp sgt i32 %29, 256
br i1 %30, label %31, label %32
31: ; preds = %24
br label %86
32: ; preds = %24
%33 = call i32 (...) @FlashResetRelease()
%34 = call i32 @RamWrite32A(i32 49152, i32 14708756)
%35 = call i32 @RamWrite32A(i32 53248, i32 1)
%36 = call i32 @RamWrite32A(i32 49152, i32 14708764)
%37 = call i32 @RamWrite32A(i32 53248, i32 7)
%38 = call i32 @RamWrite32A(i32 49152, i32 14708744)
%39 = call i32 @RamWrite32A(i32 53248, i32 65792)
%40 = call i32 @RamWrite32A(i32 49152, i32 14708748)
%41 = call i32 @RamWrite32A(i32 53248, i32 7)
%42 = call i32 @RamWrite32A(i32 49152, i32 14708752)
%43 = call i32 @RamWrite32A(i32 53248, i32 7)
%44 = call i32 @RamWrite32A(i32 49152, i32 14708748)
%45 = load i16, i16* %8
%46 = zext i16 %45 to i32
%47 = sub nsw i32 %46, 1
%48 = call i32 @RamWrite32A(i32 53248, i32 %47)
%49 = call i32 @RamWrite32A(i32 49152, i32 14708764)
%50 = load i8, i8* %5
%51 = zext i8 %50 to i32
%52 = shl i32 1, %51
%53 = call i32 @RamWrite32A(i32 53248, i32 %52)
%54 = call i32 @RamWrite32A(i32 49152, i32 14708744)
%55 = load i8, i8* %6
%56 = zext i8 %55 to i32
%57 = add nsw i32 65536, %56
%58 = call i32 @RamWrite32A(i32 53248, i32 %57)
%59 = call i32 @RamWrite32A(i32 49152, i32 14708752)
%60 = call i32 @RamWrite32A(i32 53248, i32 1)
%61 = call i32 @RamWrite32A(i32 49152, i32 14708736)
store i16 0, i16* %9
br label %62
62: ; preds = %81, %32
%63 = load i16, i16* %9
%64 = zext i16 %63 to i32
%65 = load i16, i16* %8
%66 = zext i16 %65 to i32
%67 = icmp slt i32 %64, %66
br i1 %67, label %68, label %84
68: ; preds = %62
%69 = call i32 @RamRead32A(i32 53248, i64* %10)
%70 = load i64, i64* %10
%71 = load i8, i8* %5
%72 = zext i8 %71 to i32
%73 = mul nsw i32 %72, 8
%74 = zext i32 %73 to i64
%75 = lshr i64 %70, %74
%76 = trunc i64 %75 to i8
%77 = load i8*, i8** %7
%78 = load i16, i16* %9
%79 = zext i16 %78 to i64
%80 = getelementptr inbounds i8, i8* %77, i64 %79
store i8 %76, i8* %80
br label %81
81: ; preds = %68
%82 = load i16, i16* %9
%83 = add i16 %82, 1
store i16 %83, i16* %9
br label %62
84: ; preds = %62
%85 = call i32 (...) @FlashReset()
br label %86
86: ; preds = %84, %31, %23, %14
ret void
}

LLVM IR (-O2):
define void @FlashNVR_ReadData_Byte(i8 zeroext %0, i8 zeroext %1, i8* nocapture writeonly %2, i16 zeroext %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to i8*
%7 = zext i8 %0 to i32
%8 = icmp ugt i8 %0, 2
br i1 %8, label %55, label %9
9: ; preds = %4
%10 = zext i16 %3 to i32
%11 = add i16 %3, -257
%12 = icmp ult i16 %11, -256
br i1 %12, label %55, label %13
13: ; preds = %9
%14 = zext i8 %1 to i32
%15 = add nuw nsw i32 %10, %14
%16 = icmp ugt i32 %15, 256
br i1 %16, label %55, label %17
17: ; preds = %13
%18 = tail call i32 (...) @FlashResetRelease()
%19 = tail call i32 @RamWrite32A(i32 49152, i32 14708756)
%20 = tail call i32 @RamWrite32A(i32 53248, i32 1)
%21 = tail call i32 @RamWrite32A(i32 49152, i32 14708764)
%22 = tail call i32 @RamWrite32A(i32 53248, i32 7)
%23 = tail call i32 @RamWrite32A(i32 49152, i32 14708744)
%24 = tail call i32 @RamWrite32A(i32 53248, i32 65792)
%25 = tail call i32 @RamWrite32A(i32 49152, i32 14708748)
%26 = tail call i32 @RamWrite32A(i32 53248, i32 7)
%27 = tail call i32 @RamWrite32A(i32 49152, i32 14708752)
%28 = tail call i32 @RamWrite32A(i32 53248, i32 7)
%29 = tail call i32 @RamWrite32A(i32 49152, i32 14708748)
%30 = add nsw i32 %10, -1
%31 = tail call i32 @RamWrite32A(i32 53248, i32 %30)
%32 = tail call i32 @RamWrite32A(i32 49152, i32 14708764)
%33 = shl nuw i32 1, %7
%34 = tail call i32 @RamWrite32A(i32 53248, i32 %33)
%35 = tail call i32 @RamWrite32A(i32 49152, i32 14708744)
%36 = or i32 %14, 65536
%37 = tail call i32 @RamWrite32A(i32 53248, i32 %36)
%38 = tail call i32 @RamWrite32A(i32 49152, i32 14708752)
%39 = tail call i32 @RamWrite32A(i32 53248, i32 1)
%40 = tail call i32 @RamWrite32A(i32 49152, i32 14708736)
%41 = shl nuw nsw i32 %7, 3
%42 = zext i32 %41 to i64
%43 = zext i16 %3 to i64
br label %44
44: ; preds = %17, %44
%45 = phi i64 [ 0, %17 ], [ %51, %44 ]
%46 = call i32 @RamRead32A(i32 53248, i64* %5)
%47 = load i64, i64* %5
%48 = lshr i64 %47, %42
%49 = trunc i64 %48 to i8
%50 = getelementptr inbounds i8, i8* %2, i64 %45
store i8 %49, i8* %50
%51 = add nuw nsw i64 %45, 1
%52 = icmp eq i64 %51, %43
br i1 %52, label %53, label %44
53: ; preds = %44
%54 = call i32 (...) @FlashReset()
br label %55
55: ; preds = %13, %9, %4, %53
ret void
}