#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);
int ReadWPB();
int /*<<< orphan*/ WPBCtrl(int);
int /*<<< orphan*/ WitTim(int);
short FlashNVRSectorErase_Byte(unsigned char Sel) {
  unsigned char UcCnt;
  unsigned long UlReadVal;
  if (Sel >= 3)
    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 + 0x48));
  RamWrite32A(0xD000, 0x5A);
  RamWrite32A(0xC000, (0xE07000 + 0x08));
  RamWrite32A(0xD000, 0x00010000);
  RamWrite32A(0xC000, (0xE07000 + 0x1C));
  RamWrite32A(0xD000, (1 << Sel));
  WPBCtrl(0x01);
  if (ReadWPB() != 1)
    return (5);
  RamWrite32A(0xC000, (0xE07000 + 0x10));
  RamWrite32A(0xD000, 4);
  RamWrite32A(0xC000, (0xE07000 + 0x18));
  UcCnt = 0;
  do {
    RamRead32A(0xD000, &UlReadVal);
    WitTim(5);
    if (++UcCnt >= 2) {
      WPBCtrl(0x00);
      FlashReset();
      return (2);
    }
  } while (UlReadVal == 0x80);
  WPBCtrl(0x00);
  FlashReset();
  return (0);
}


x86 assembly (clang, -O0):
<FlashNVRSectorErase_Byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x3(%rbp)
movzbl -0x3(%rbp),%eax
cmp    $0x3,%eax
jl     1196 <FlashNVRSectorErase_Byte+0x26>
movw   $0x1,-0x2(%rbp)
jmp    1355 <FlashNVRSectorErase_Byte+0x1e5>
mov    $0x0,%al
call   1040 <FlashResetRelease@plt>
mov    $0xc000,%edi
mov    $0xe07014,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x1,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x10100,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0700c,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07048,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x5a,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x10000,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1080 <RamWrite32A@plt>
movzbl -0x3(%rbp),%ecx
mov    $0x1,%esi
shl    %cl,%esi
mov    $0xd000,%edi
call   1080 <RamWrite32A@plt>
mov    $0x1,%edi
call   1050 <WPBCtrl@plt>
mov    $0x0,%al
call   1060 <ReadWPB@plt>
cmp    $0x1,%eax
je     12b8 <FlashNVRSectorErase_Byte+0x148>
movw   $0x5,-0x2(%rbp)
jmp    1355 <FlashNVRSectorErase_Byte+0x1e5>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x4,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07018,%esi
call   1080 <RamWrite32A@plt>
movb   $0x0,-0x4(%rbp)
mov    $0xd000,%edi
lea    -0x10(%rbp),%rsi
call   1070 <RamRead32A@plt>
mov    $0x5,%edi
call   1090 <WitTim@plt>
mov    -0x4(%rbp),%al
add    $0x1,%al
mov    %al,-0x4(%rbp)
movzbl %al,%eax
cmp    $0x2,%eax
jl     132e <FlashNVRSectorErase_Byte+0x1be>
xor    %edi,%edi
call   1050 <WPBCtrl@plt>
mov    $0x0,%al
call   1030 <FlashReset@plt>
movw   $0x2,-0x2(%rbp)
jmp    1355 <FlashNVRSectorErase_Byte+0x1e5>
jmp    1333 <FlashNVRSectorErase_Byte+0x1c3>
cmpq   $0x80,-0x10(%rbp)
je     12e9 <FlashNVRSectorErase_Byte+0x179>
xor    %edi,%edi
call   1050 <WPBCtrl@plt>
mov    $0x0,%al
call   1030 <FlashReset@plt>
movw   $0x0,-0x2(%rbp)
movswl -0x2(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FlashNVRSectorErase_Byte>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0x1,%bp
cmp    $0x2,%bl
ja     1314 <FlashNVRSectorErase_Byte+0x1a4>
xor    %eax,%eax
call   1040 <FlashResetRelease@plt>
mov    $0xc000,%edi
mov    $0xe07014,%esi
call   1080 <RamWrite32A@plt>
mov    $0x1,%ebp
mov    $0xd000,%edi
mov    $0x1,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x10100,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0700c,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x7,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07048,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x5a,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07008,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x10000,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe0701c,%esi
call   1080 <RamWrite32A@plt>
mov    %ebx,%ecx
shl    %cl,%ebp
mov    $0xd000,%edi
mov    %ebp,%esi
call   1080 <RamWrite32A@plt>
mov    $0x1,%edi
call   1050 <WPBCtrl@plt>
xor    %eax,%eax
call   1060 <ReadWPB@plt>
mov    $0x5,%bp
cmp    $0x1,%eax
jne    1314 <FlashNVRSectorErase_Byte+0x1a4>
mov    $0xc000,%edi
mov    $0xe07010,%esi
call   1080 <RamWrite32A@plt>
mov    $0xd000,%edi
mov    $0x4,%esi
call   1080 <RamWrite32A@plt>
mov    $0xc000,%edi
mov    $0xe07018,%esi
call   1080 <RamWrite32A@plt>
mov    %rsp,%rsi
mov    $0xd000,%edi
call   1070 <RamRead32A@plt>
mov    $0x5,%edi
call   1090 <WitTim@plt>
cmpq   $0x80,(%rsp)
jne    1304 <FlashNVRSectorErase_Byte+0x194>
mov    %rsp,%rsi
mov    $0xd000,%edi
call   1070 <RamRead32A@plt>
mov    $0x5,%edi
call   1090 <WitTim@plt>
mov    $0x2,%bp
jmp    1306 <FlashNVRSectorErase_Byte+0x196>
xor    %ebp,%ebp
xor    %edi,%edi
call   1050 <WPBCtrl@plt>
xor    %eax,%eax
call   1030 <FlashReset@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define signext i16 @FlashNVRSectorErase_Byte(i8 zeroext %0) {
%2 = alloca i16
%3 = alloca i8
%4 = alloca i8
%5 = alloca i64
store i8 %0, i8* %3
%6 = load i8, i8* %3
%7 = zext i8 %6 to i32
%8 = icmp sge i32 %7, 3
br i1 %8, label %9, label %10
9: ; preds = %1
store i16 1, i16* %2
br label %56
10: ; preds = %1
%11 = call i32 (...) @FlashResetRelease()
%12 = call i32 @RamWrite32A(i32 49152, i32 14708756)
%13 = call i32 @RamWrite32A(i32 53248, i32 1)
%14 = call i32 @RamWrite32A(i32 49152, i32 14708764)
%15 = call i32 @RamWrite32A(i32 53248, i32 7)
%16 = call i32 @RamWrite32A(i32 49152, i32 14708744)
%17 = call i32 @RamWrite32A(i32 53248, i32 65792)
%18 = call i32 @RamWrite32A(i32 49152, i32 14708748)
%19 = call i32 @RamWrite32A(i32 53248, i32 7)
%20 = call i32 @RamWrite32A(i32 49152, i32 14708752)
%21 = call i32 @RamWrite32A(i32 53248, i32 7)
%22 = call i32 @RamWrite32A(i32 49152, i32 14708808)
%23 = call i32 @RamWrite32A(i32 53248, i32 90)
%24 = call i32 @RamWrite32A(i32 49152, i32 14708744)
%25 = call i32 @RamWrite32A(i32 53248, i32 65536)
%26 = call i32 @RamWrite32A(i32 49152, i32 14708764)
%27 = load i8, i8* %3
%28 = zext i8 %27 to i32
%29 = shl i32 1, %28
%30 = call i32 @RamWrite32A(i32 53248, i32 %29)
%31 = call i32 @WPBCtrl(i32 1)
%32 = call i32 (...) @ReadWPB()
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %10
store i16 5, i16* %2
br label %56
35: ; preds = %10
%36 = call i32 @RamWrite32A(i32 49152, i32 14708752)
%37 = call i32 @RamWrite32A(i32 53248, i32 4)
%38 = call i32 @RamWrite32A(i32 49152, i32 14708760)
store i8 0, i8* %4
br label %39
39: ; preds = %50, %35
%40 = call i32 @RamRead32A(i32 53248, i64* %5)
%41 = call i32 @WitTim(i32 5)
%42 = load i8, i8* %4
%43 = add i8 %42, 1
store i8 %43, i8* %4
%44 = zext i8 %43 to i32
%45 = icmp sge i32 %44, 2
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = call i32 @WPBCtrl(i32 0)
%48 = call i32 (...) @FlashReset()
store i16 2, i16* %2
br label %56
49: ; preds = %39
br label %50
50: ; preds = %49
%51 = load i64, i64* %5
%52 = icmp eq i64 %51, 128
br i1 %52, label %39, label %53
53: ; preds = %50
%54 = call i32 @WPBCtrl(i32 0)
%55 = call i32 (...) @FlashReset()
store i16 0, i16* %2
br label %56
56: ; preds = %53, %46, %34, %9
%57 = load i16, i16* %2
ret i16 %57
}

LLVM IR (-O2):
define signext i16 @FlashNVRSectorErase_Byte(i8 zeroext %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = icmp ugt i8 %0, 2
br i1 %4, label %43, label %5
5: ; preds = %1
%6 = zext i8 %0 to i32
%7 = tail call i32 (...) @FlashResetRelease()
%8 = tail call i32 @RamWrite32A(i32 49152, i32 14708756)
%9 = tail call i32 @RamWrite32A(i32 53248, i32 1)
%10 = tail call i32 @RamWrite32A(i32 49152, i32 14708764)
%11 = tail call i32 @RamWrite32A(i32 53248, i32 7)
%12 = tail call i32 @RamWrite32A(i32 49152, i32 14708744)
%13 = tail call i32 @RamWrite32A(i32 53248, i32 65792)
%14 = tail call i32 @RamWrite32A(i32 49152, i32 14708748)
%15 = tail call i32 @RamWrite32A(i32 53248, i32 7)
%16 = tail call i32 @RamWrite32A(i32 49152, i32 14708752)
%17 = tail call i32 @RamWrite32A(i32 53248, i32 7)
%18 = tail call i32 @RamWrite32A(i32 49152, i32 14708808)
%19 = tail call i32 @RamWrite32A(i32 53248, i32 90)
%20 = tail call i32 @RamWrite32A(i32 49152, i32 14708744)
%21 = tail call i32 @RamWrite32A(i32 53248, i32 65536)
%22 = tail call i32 @RamWrite32A(i32 49152, i32 14708764)
%23 = shl nuw nsw i32 1, %6
%24 = tail call i32 @RamWrite32A(i32 53248, i32 %23)
%25 = tail call i32 @WPBCtrl(i32 1)
%26 = tail call i32 (...) @ReadWPB()
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %43
28: ; preds = %5
%29 = tail call i32 @RamWrite32A(i32 49152, i32 14708752)
%30 = tail call i32 @RamWrite32A(i32 53248, i32 4)
%31 = tail call i32 @RamWrite32A(i32 49152, i32 14708760)
%32 = call i32 @RamRead32A(i32 53248, i64* %2)
%33 = call i32 @WitTim(i32 5)
%34 = load i64, i64* %2
%35 = icmp eq i64 %34, 128
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = call i32 @RamRead32A(i32 53248, i64* %2)
%38 = call i32 @WitTim(i32 5)
br label %39
39: ; preds = %28, %36
%40 = phi i16 [ 2, %36 ], [ 0, %28 ]
%41 = call i32 @WPBCtrl(i32 0)
%42 = call i32 (...) @FlashReset()
br label %43
43: ; preds = %39, %5, %1
%44 = phi i16 [ 1, %1 ], [ 5, %5 ], [ %40, %39 ]
ret i16 %44
}