#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*/ Delay(int);
int /*<<< orphan*/ NF_ADDR(unsigned int);
int /*<<< orphan*/ NF_CMD(int);
int NF_IsBadBlock(unsigned int);
int /*<<< orphan*/ NF_MarkBadBlock(unsigned int);
int NF_RDDATA();
int /*<<< orphan*/ NF_WAITRB();
int /*<<< orphan*/ NF_nFCE_H();
int /*<<< orphan*/ NF_nFCE_L();
int NF_EraseBlock(unsigned int block) {
  unsigned int blockPage = (block << 5);
  int bad;
  bad = NF_IsBadBlock(block);
  if (block != 0 && bad != 0xFF) {
    return 2;
  }
  NF_nFCE_L();
  NF_CMD(0x60);
  NF_ADDR(blockPage & 0xff);
  NF_ADDR((blockPage >> 8) & 0x7f);
  NF_CMD(0xd0);
  Delay(1);
  NF_WAITRB();
  NF_CMD(0x70);
  if (NF_RDDATA() & 0x1) {
    NF_nFCE_H();
    NF_MarkBadBlock(block);
    return 1;
  } else {
    NF_nFCE_H();
    return 0;
  }
}


x86 assembly (clang, -O0):
<NF_EraseBlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x5,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   10b0 <NF_IsBadBlock@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11d2 <NF_EraseBlock+0x42>
cmpl   $0xff,-0x10(%rbp)
je     11d2 <NF_EraseBlock+0x42>
movl   $0x2,-0x4(%rbp)
jmp    1260 <NF_EraseBlock+0xd0>
mov    $0x0,%al
call   1060 <NF_nFCE_L@plt>
mov    $0x60,%edi
call   1090 <NF_CMD@plt>
mov    -0xc(%rbp),%edi
and    $0xff,%edi
call   10a0 <NF_ADDR@plt>
mov    -0xc(%rbp),%edi
shr    $0x8,%edi
and    $0x7f,%edi
call   10a0 <NF_ADDR@plt>
mov    $0xd0,%edi
call   1090 <NF_CMD@plt>
mov    $0x1,%edi
call   1050 <Delay@plt>
mov    $0x0,%al
call   1070 <NF_WAITRB@plt>
mov    $0x70,%edi
call   1090 <NF_CMD@plt>
mov    $0x0,%al
call   1080 <NF_RDDATA@plt>
and    $0x1,%eax
cmp    $0x0,%eax
je     1252 <NF_EraseBlock+0xc2>
mov    $0x0,%al
call   1030 <NF_nFCE_H@plt>
mov    -0x8(%rbp),%edi
call   1040 <NF_MarkBadBlock@plt>
movl   $0x1,-0x4(%rbp)
jmp    1260 <NF_EraseBlock+0xd0>
mov    $0x0,%al
call   1030 <NF_nFCE_H@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NF_EraseBlock>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
call   10b0 <NF_IsBadBlock@plt>
test   %ebp,%ebp
je     11ac <NF_EraseBlock+0x1c>
mov    $0x2,%r14d
cmp    $0xff,%eax
jne    1226 <NF_EraseBlock+0x96>
mov    %ebp,%ebx
shl    $0x5,%ebx
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <NF_nFCE_L@plt>
mov    $0x60,%edi
call   1090 <NF_CMD@plt>
mov    %ebx,%edi
and    $0xe0,%edi
call   10a0 <NF_ADDR@plt>
shr    $0x8,%ebx
and    $0x7f,%ebx
mov    %ebx,%edi
call   10a0 <NF_ADDR@plt>
mov    $0xd0,%edi
call   1090 <NF_CMD@plt>
mov    $0x1,%edi
call   1050 <Delay@plt>
xor    %eax,%eax
call   1070 <NF_WAITRB@plt>
mov    $0x70,%edi
call   1090 <NF_CMD@plt>
xor    %eax,%eax
call   1080 <NF_RDDATA@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <NF_nFCE_H@plt>
test   $0x1,%bl
je     1226 <NF_EraseBlock+0x96>
mov    %ebp,%edi
call   1040 <NF_MarkBadBlock@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @NF_EraseBlock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = shl i32 %6, 5
store i32 %7, i32* %4
%8 = load i32, i32* %3
%9 = call i32 @NF_IsBadBlock(i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %3
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 255
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 2, i32* %2
br label %39
16: ; preds = %12, %1
%17 = call i32 (...) @NF_nFCE_L()
%18 = call i32 @NF_CMD(i32 96)
%19 = load i32, i32* %4
%20 = and i32 %19, 255
%21 = call i32 @NF_ADDR(i32 %20)
%22 = load i32, i32* %4
%23 = lshr i32 %22, 8
%24 = and i32 %23, 127
%25 = call i32 @NF_ADDR(i32 %24)
%26 = call i32 @NF_CMD(i32 208)
%27 = call i32 @Delay(i32 1)
%28 = call i32 (...) @NF_WAITRB()
%29 = call i32 @NF_CMD(i32 112)
%30 = call i32 (...) @NF_RDDATA()
%31 = and i32 %30, 1
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %16
%34 = call i32 (...) @NF_nFCE_H()
%35 = load i32, i32* %3
%36 = call i32 @NF_MarkBadBlock(i32 %35)
store i32 1, i32* %2
br label %39
37: ; preds = %16
%38 = call i32 (...) @NF_nFCE_H()
store i32 0, i32* %2
br label %39
39: ; preds = %37, %33, %15
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
define i32 @NF_EraseBlock(i32 %0) {
%2 = shl i32 %0, 5
%3 = tail call i32 @NF_IsBadBlock(i32 %0)
%4 = icmp ne i32 %0, 0
%5 = icmp ne i32 %3, 255
%6 = select i1 %4, i1 %5, i1 false
br i1 %6, label %25, label %7
7: ; preds = %1
%8 = tail call i32 (...) @NF_nFCE_L()
%9 = tail call i32 @NF_CMD(i32 96)
%10 = and i32 %2, 224
%11 = tail call i32 @NF_ADDR(i32 %10)
%12 = lshr i32 %2, 8
%13 = and i32 %12, 127
%14 = tail call i32 @NF_ADDR(i32 %13)
%15 = tail call i32 @NF_CMD(i32 208)
%16 = tail call i32 @Delay(i32 1)
%17 = tail call i32 (...) @NF_WAITRB()
%18 = tail call i32 @NF_CMD(i32 112)
%19 = tail call i32 (...) @NF_RDDATA()
%20 = and i32 %19, 1
%21 = icmp eq i32 %20, 0
%22 = tail call i32 (...) @NF_nFCE_H()
br i1 %21, label %25, label %23
23: ; preds = %7
%24 = tail call i32 @NF_MarkBadBlock(i32 %0)
br label %25
25: ; preds = %7, %1, %23
%26 = phi i32 [ 1, %23 ], [ 2, %1 ], [ 0, %7 ]
ret i32 %26
}