#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*/ RamWrite32A(int, int);
int ReadWPB();
int /*<<< orphan*/ WPBCtrl(int);
short FlashNVR_WriteData_Byte(unsigned char Sel, unsigned char SetAddress,
                              unsigned char *WritePtr, unsigned short Num) {
  unsigned short UsNum;
  if (Sel >= 3)
    return (1);
  if (Num == 0 || Num > 256)
    return (1);
  if (SetAddress + Num > 256)
    return (1);
  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 + 0x30));
  RamWrite32A(0xD000, 0x73);
  RamWrite32A(0xC000, (0xE07000 + 0x34));
  RamWrite32A(0xD000, 0x43);
  WPBCtrl(0x01);
  if (ReadWPB() != 1)
    return (5);
  RamWrite32A(0xC000, (0xE07000 + 0x0C));
  RamWrite32A(0xD000, 0);
  RamWrite32A(0xC000, (0xE07000 + 0x1C));
  RamWrite32A(0xD000, (1 << Sel));
  RamWrite32A(0xC000, (0xE07000 + 0x08));
  RamWrite32A(0xD000, 0x00010000 + SetAddress);
  for (UsNum = 0; UsNum < Num; UsNum++) {
    RamWrite32A(0xC000, (0xE07000 + 0x04));
    RamWrite32A(0xD000, ((unsigned long)(WritePtr[UsNum]) << (Sel * 8)));
    RamWrite32A(0xC000, (0xE07000 + 0x10));
    RamWrite32A(0xD000, 2);
  }
  RamWrite32A(0xC000, (0xE07000 + 0x14));
  RamWrite32A(0xD000, 0);
  WPBCtrl(0x00);
  FlashReset();
  return (0);
}


x86 assembly (clang, -O0):
<FlashNVR_WriteData_Byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cx,%ax
mov    %sil,%cl
mov    %dil,%sil
mov    %sil,-0x3(%rbp)
mov    %cl,-0x4(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movzbl -0x3(%rbp),%eax
cmp    $0x3,%eax
jl     1188 <FlashNVR_WriteData_Byte+0x38>
movw   $0x1,-0x2(%rbp)
jmp    13d6 <FlashNVR_WriteData_Byte+0x286>
movzwl -0x12(%rbp),%eax
cmp    $0x0,%eax
je     11a4 <FlashNVR_WriteData_Byte+0x54>
movzwl -0x12(%rbp),%eax
cmp    $0x100,%eax
jle    11af <FlashNVR_WriteData_Byte+0x5f>
movw   $0x1,-0x2(%rbp)
jmp    13d6 <FlashNVR_WriteData_Byte+0x286>
movzbl -0x4(%rbp),%eax
movzwl -0x12(%rbp),%ecx
add    %ecx,%eax
cmp    $0x100,%eax
jle    11cf <FlashNVR_WriteData_Byte+0x7f>
movw   $0x1,-0x2(%rbp)
jmp    13d6 <FlashNVR_WriteData_Byte+0x286>
mov    $0x0,%al
call   1040 <FlashResetRelease@plt>
mov    $0xc000,%edi
mov    $0xe07014,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x1,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x10100,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0700c,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07030,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x73,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07034,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x43,%esi
call   1070 <RamWrite32A@plt>
mov    $0x1,%edi
call   1050 <WPBCtrl@plt>
mov    $0x0,%al
call   1060 <ReadWPB@plt>
cmp    $0x1,%eax
je     12cd <FlashNVR_WriteData_Byte+0x17d>
movw   $0x5,-0x2(%rbp)
jmp    13d6 <FlashNVR_WriteData_Byte+0x286>
mov    $0xc000,%edi
mov    $0xe0700c,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
xor    %esi,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1070 <RamWrite32A@plt>
movzbl -0x3(%rbp),%ecx
mov    $0x1,%esi
shl    %cl,%esi
mov    $0xd000,%edi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1070 <RamWrite32A@plt>
movzbl -0x4(%rbp),%esi
add    $0x10000,%esi
mov    $0xd000,%edi
call   1070 <RamWrite32A@plt>
movw   $0x0,-0x14(%rbp)
movzwl -0x14(%rbp),%eax
movzwl -0x12(%rbp),%ecx
cmp    %ecx,%eax
jge    13a7 <FlashNVR_WriteData_Byte+0x257>
mov    $0xc000,%edi
mov    $0xe07004,%esi
call   1070 <RamWrite32A@plt>
mov    -0x10(%rbp),%rax
movzwl -0x14(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
movzbl -0x3(%rbp),%ecx
shl    $0x3,%ecx
mov    %ecx,%ecx
shl    %cl,%rax
mov    %eax,%esi
mov    $0xd000,%edi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x2,%esi
call   1070 <RamWrite32A@plt>
mov    -0x14(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x14(%rbp)
jmp    1335 <FlashNVR_WriteData_Byte+0x1e5>
mov    $0xc000,%edi
mov    $0xe07014,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
xor    %esi,%esi
call   1070 <RamWrite32A@plt>
xor    %edi,%edi
call   1050 <WPBCtrl@plt>
mov    $0x0,%al
call   1030 <FlashReset@plt>
movw   $0x0,-0x2(%rbp)
movswl -0x2(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FlashNVR_WriteData_Byte>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0x1,%bp
cmp    $0x2,%bl
ja     1375 <FlashNVR_WriteData_Byte+0x225>
lea    -0x101(%rcx),%eax
movzwl %ax,%eax
cmp    $0xff00,%eax
jb     1375 <FlashNVR_WriteData_Byte+0x225>
movzwl %cx,%r13d
movzbl %sil,%r15d
lea    (%r15,%r13,1),%eax
cmp    $0x100,%eax
ja     1375 <FlashNVR_WriteData_Byte+0x225>
mov    %rdx,%r14
xor    %eax,%eax
call   1040 <FlashResetRelease@plt>
mov    $0xc000,%edi
mov    $0xe07014,%esi
call   1070 <RamWrite32A@plt>
mov    $0x1,%r12d
mov    $0xd000,%edi
mov    $0x1,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x10100,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0700c,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07030,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x73,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07034,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x43,%esi
call   1070 <RamWrite32A@plt>
mov    $0x1,%edi
call   1050 <WPBCtrl@plt>
xor    %eax,%eax
call   1060 <ReadWPB@plt>
mov    $0x5,%bp
cmp    $0x1,%eax
jne    1375 <FlashNVR_WriteData_Byte+0x225>
mov    $0xc000,%edi
mov    $0xe0700c,%esi
call   1070 <RamWrite32A@plt>
xor    %ebp,%ebp
mov    $0xd000,%edi
xor    %esi,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1070 <RamWrite32A@plt>
mov    %ebx,%ecx
shl    %cl,%r12d
mov    $0xd000,%edi
mov    %r12d,%esi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1070 <RamWrite32A@plt>
or     $0x10000,%r15d
mov    $0xd000,%edi
mov    %r15d,%esi
call   1070 <RamWrite32A@plt>
shl    $0x3,%ebx
nopl   0x0(%rax)
mov    $0xc000,%edi
mov    $0xe07004,%esi
call   1070 <RamWrite32A@plt>
movzbl (%r14,%rbp,1),%esi
mov    %ebx,%ecx
shl    %cl,%rsi
mov    $0xd000,%edi
call   1070 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1070 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x2,%esi
call   1070 <RamWrite32A@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1300 <FlashNVR_WriteData_Byte+0x1b0>
mov    $0xc000,%edi
mov    $0xe07014,%esi
call   1070 <RamWrite32A@plt>
xor    %ebp,%ebp
mov    $0xd000,%edi
xor    %esi,%esi
call   1070 <RamWrite32A@plt>
xor    %edi,%edi
call   1050 <WPBCtrl@plt>
xor    %eax,%eax
call   1030 <FlashReset@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define signext i16 @FlashNVR_WriteData_Byte(i8 zeroext %0, i8 zeroext %1, i8* %2, i16 zeroext %3) {
%5 = alloca i16
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i16
%10 = alloca i16
store i8 %0, i8* %6
store i8 %1, i8* %7
store i8* %2, i8** %8
store i16 %3, i16* %9
%11 = load i8, i8* %6
%12 = zext i8 %11 to i32
%13 = icmp sge i32 %12, 3
br i1 %13, label %14, label %15
14: ; preds = %4
store i16 1, i16* %5
br label %96
15: ; preds = %4
%16 = load i16, i16* %9
%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* %9
%21 = zext i16 %20 to i32
%22 = icmp sgt i32 %21, 256
br i1 %22, label %23, label %24
23: ; preds = %19, %15
store i16 1, i16* %5
br label %96
24: ; preds = %19
%25 = load i8, i8* %7
%26 = zext i8 %25 to i32
%27 = load i16, i16* %9
%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
store i16 1, i16* %5
br label %96
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 14708784)
%45 = call i32 @RamWrite32A(i32 53248, i32 115)
%46 = call i32 @RamWrite32A(i32 49152, i32 14708788)
%47 = call i32 @RamWrite32A(i32 53248, i32 67)
%48 = call i32 @WPBCtrl(i32 1)
%49 = call i32 (...) @ReadWPB()
%50 = icmp ne i32 %49, 1
br i1 %50, label %51, label %52
51: ; preds = %32
store i16 5, i16* %5
br label %96
52: ; preds = %32
%53 = call i32 @RamWrite32A(i32 49152, i32 14708748)
%54 = call i32 @RamWrite32A(i32 53248, i32 0)
%55 = call i32 @RamWrite32A(i32 49152, i32 14708764)
%56 = load i8, i8* %6
%57 = zext i8 %56 to i32
%58 = shl i32 1, %57
%59 = call i32 @RamWrite32A(i32 53248, i32 %58)
%60 = call i32 @RamWrite32A(i32 49152, i32 14708744)
%61 = load i8, i8* %7
%62 = zext i8 %61 to i32
%63 = add nsw i32 65536, %62
%64 = call i32 @RamWrite32A(i32 53248, i32 %63)
store i16 0, i16* %10
br label %65
65: ; preds = %88, %52
%66 = load i16, i16* %10
%67 = zext i16 %66 to i32
%68 = load i16, i16* %9
%69 = zext i16 %68 to i32
%70 = icmp slt i32 %67, %69
br i1 %70, label %71, label %91
71: ; preds = %65
%72 = call i32 @RamWrite32A(i32 49152, i32 14708740)
%73 = load i8*, i8** %8
%74 = load i16, i16* %10
%75 = zext i16 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
%77 = load i8, i8* %76
%78 = zext i8 %77 to i64
%79 = load i8, i8* %6
%80 = zext i8 %79 to i32
%81 = mul nsw i32 %80, 8
%82 = zext i32 %81 to i64
%83 = shl i64 %78, %82
%84 = trunc i64 %83 to i32
%85 = call i32 @RamWrite32A(i32 53248, i32 %84)
%86 = call i32 @RamWrite32A(i32 49152, i32 14708752)
%87 = call i32 @RamWrite32A(i32 53248, i32 2)
br label %88
88: ; preds = %71
%89 = load i16, i16* %10
%90 = add i16 %89, 1
store i16 %90, i16* %10
br label %65
91: ; preds = %65
%92 = call i32 @RamWrite32A(i32 49152, i32 14708756)
%93 = call i32 @RamWrite32A(i32 53248, i32 0)
%94 = call i32 @WPBCtrl(i32 0)
%95 = call i32 (...) @FlashReset()
store i16 0, i16* %5
br label %96
96: ; preds = %91, %51, %31, %23, %14
%97 = load i16, i16* %5
ret i16 %97
}

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