#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);
unsigned char NF_RDDATA();
int /*<<< orphan*/ NF_WAITRB();
int /*<<< orphan*/ NF_nFCE_H();
int /*<<< orphan*/ NF_nFCE_L();
int NF_IsBadBlock(unsigned int block) {
  unsigned int blockPage;
  unsigned char data1, data2;
  blockPage = (block << 5);
  NF_nFCE_L();
  NF_CMD(0x50);
  NF_ADDR(517 & 0xf);
  NF_ADDR(blockPage & 0xff);
  NF_ADDR((blockPage >> 8) & 0x7f);
  Delay(1);
  NF_WAITRB();
  data1 = NF_RDDATA();
  NF_nFCE_H();
  NF_nFCE_L();
  NF_CMD(0x50);
  NF_ADDR(517 & 0xf);
  NF_ADDR((blockPage & 0xff) + 1);
  NF_ADDR((blockPage >> 8) & 0x7f);
  Delay(1);
  NF_WAITRB();
  data2 = NF_RDDATA();
  NF_nFCE_H();
  return (data1 != 0xFF) ? data1 : data2;
}


x86 assembly (clang, -O0):
<NF_IsBadBlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x5,%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <NF_nFCE_L@plt>
mov    $0x50,%edi
call   1080 <NF_CMD@plt>
mov    $0x5,%edi
call   1090 <NF_ADDR@plt>
mov    -0x8(%rbp),%edi
and    $0xff,%edi
call   1090 <NF_ADDR@plt>
mov    -0x8(%rbp),%edi
shr    $0x8,%edi
and    $0x7f,%edi
call   1090 <NF_ADDR@plt>
mov    $0x1,%edi
call   1040 <Delay@plt>
mov    $0x0,%al
call   1060 <NF_WAITRB@plt>
mov    $0x0,%al
call   1070 <NF_RDDATA@plt>
mov    %al,-0x9(%rbp)
mov    $0x0,%al
call   1030 <NF_nFCE_H@plt>
mov    $0x0,%al
call   1050 <NF_nFCE_L@plt>
mov    $0x50,%edi
call   1080 <NF_CMD@plt>
mov    $0x5,%edi
call   1090 <NF_ADDR@plt>
mov    -0x8(%rbp),%edi
and    $0xff,%edi
add    $0x1,%edi
call   1090 <NF_ADDR@plt>
mov    -0x8(%rbp),%edi
shr    $0x8,%edi
and    $0x7f,%edi
call   1090 <NF_ADDR@plt>
mov    $0x1,%edi
call   1040 <Delay@plt>
mov    $0x0,%al
call   1060 <NF_WAITRB@plt>
mov    $0x0,%al
call   1070 <NF_RDDATA@plt>
mov    %al,-0xa(%rbp)
mov    $0x0,%al
call   1030 <NF_nFCE_H@plt>
movzbl -0x9(%rbp),%eax
cmp    $0xff,%eax
je     1254 <NF_IsBadBlock+0xe4>
movzbl -0x9(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    125b <NF_IsBadBlock+0xeb>
movzbl -0xa(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NF_IsBadBlock>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
mov    %edi,%ebp
shl    $0x5,%ebp
xor    %eax,%eax
call   1050 <NF_nFCE_L@plt>
mov    $0x50,%edi
call   1080 <NF_CMD@plt>
mov    $0x5,%edi
call   1090 <NF_ADDR@plt>
movzbl %bpl,%ebp
mov    %ebp,%edi
call   1090 <NF_ADDR@plt>
shr    $0x3,%ebx
and    $0x7f,%ebx
mov    %ebx,%edi
call   1090 <NF_ADDR@plt>
mov    $0x1,%edi
call   1040 <Delay@plt>
xor    %eax,%eax
call   1060 <NF_WAITRB@plt>
xor    %eax,%eax
call   1070 <NF_RDDATA@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1030 <NF_nFCE_H@plt>
xor    %eax,%eax
call   1050 <NF_nFCE_L@plt>
mov    $0x50,%edi
call   1080 <NF_CMD@plt>
mov    $0x5,%edi
call   1090 <NF_ADDR@plt>
or     $0x1,%ebp
mov    %ebp,%edi
call   1090 <NF_ADDR@plt>
mov    %ebx,%edi
call   1090 <NF_ADDR@plt>
mov    $0x1,%edi
call   1040 <Delay@plt>
xor    %eax,%eax
call   1060 <NF_WAITRB@plt>
xor    %eax,%eax
call   1070 <NF_RDDATA@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <NF_nFCE_H@plt>
cmp    $0xff,%r14b
movzbl %r14b,%eax
movzbl %bl,%ecx
cmovne %eax,%ecx
movzbl %cl,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @NF_IsBadBlock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = shl i32 %6, 5
store i32 %7, i32* %3
%8 = call i32 (...) @NF_nFCE_L()
%9 = call i32 @NF_CMD(i32 80)
%10 = call i32 @NF_ADDR(i32 5)
%11 = load i32, i32* %3
%12 = and i32 %11, 255
%13 = call i32 @NF_ADDR(i32 %12)
%14 = load i32, i32* %3
%15 = lshr i32 %14, 8
%16 = and i32 %15, 127
%17 = call i32 @NF_ADDR(i32 %16)
%18 = call i32 @Delay(i32 1)
%19 = call i32 (...) @NF_WAITRB()
%20 = call zeroext i8 (...) @NF_RDDATA()
store i8 %20, i8* %4
%21 = call i32 (...) @NF_nFCE_H()
%22 = call i32 (...) @NF_nFCE_L()
%23 = call i32 @NF_CMD(i32 80)
%24 = call i32 @NF_ADDR(i32 5)
%25 = load i32, i32* %3
%26 = and i32 %25, 255
%27 = add i32 %26, 1
%28 = call i32 @NF_ADDR(i32 %27)
%29 = load i32, i32* %3
%30 = lshr i32 %29, 8
%31 = and i32 %30, 127
%32 = call i32 @NF_ADDR(i32 %31)
%33 = call i32 @Delay(i32 1)
%34 = call i32 (...) @NF_WAITRB()
%35 = call zeroext i8 (...) @NF_RDDATA()
store i8 %35, i8* %5
%36 = call i32 (...) @NF_nFCE_H()
%37 = load i8, i8* %4
%38 = zext i8 %37 to i32
%39 = icmp ne i32 %38, 255
br i1 %39, label %40, label %43
40: ; preds = %1
%41 = load i8, i8* %4
%42 = zext i8 %41 to i32
br label %46
43: ; preds = %1
%44 = load i8, i8* %5
%45 = zext i8 %44 to i32
br label %46
46: ; preds = %43, %40
%47 = phi i32 [ %42, %40 ], [ %45, %43 ]
ret i32 %47
}

LLVM IR (-O2):
define i32 @NF_IsBadBlock(i32 %0) {
%2 = shl i32 %0, 5
%3 = tail call i32 (...) @NF_nFCE_L()
%4 = tail call i32 @NF_CMD(i32 80)
%5 = tail call i32 @NF_ADDR(i32 5)
%6 = and i32 %2, 224
%7 = tail call i32 @NF_ADDR(i32 %6)
%8 = lshr i32 %2, 8
%9 = and i32 %8, 127
%10 = tail call i32 @NF_ADDR(i32 %9)
%11 = tail call i32 @Delay(i32 1)
%12 = tail call i32 (...) @NF_WAITRB()
%13 = tail call zeroext i8 (...) @NF_RDDATA()
%14 = tail call i32 (...) @NF_nFCE_H()
%15 = tail call i32 (...) @NF_nFCE_L()
%16 = tail call i32 @NF_CMD(i32 80)
%17 = tail call i32 @NF_ADDR(i32 5)
%18 = or i32 %6, 1
%19 = tail call i32 @NF_ADDR(i32 %18)
%20 = tail call i32 @NF_ADDR(i32 %9)
%21 = tail call i32 @Delay(i32 1)
%22 = tail call i32 (...) @NF_WAITRB()
%23 = tail call zeroext i8 (...) @NF_RDDATA()
%24 = tail call i32 (...) @NF_nFCE_H()
%25 = icmp eq i8 %13, -1
%26 = select i1 %25, i8 %23, i8 %13
%27 = zext i8 %26 to i32
ret i32 %27
}