#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 /*<<< orphan*/ NF_MarkBadBlock(unsigned int);
int NF_RDDATA();
int /*<<< orphan*/ NF_WAITRB();
int /*<<< orphan*/ NF_WRDATA(unsigned char);
int /*<<< orphan*/ NF_nFCE_H();
int /*<<< orphan*/ NF_nFCE_L();
int NF_WritePage(unsigned int block, unsigned int page, unsigned char *buffer,
                 unsigned char *spareBuf) {
  int i;
  unsigned int blockPage = (block << 5) + page;
  unsigned char *bufPt = buffer;
  NF_nFCE_L();
  NF_CMD(0x0);
  NF_CMD(0x80);
  NF_ADDR(0);
  NF_ADDR(blockPage & 0xff);
  NF_ADDR((blockPage >> 8) & 0x7f);
  for (i = 0; i < 512; i++) {
    NF_WRDATA(*bufPt++);
  }
  if (spareBuf != NULL) {
    for (i = 0; i < 16; i++) {
      NF_WRDATA(spareBuf[i]);
    }
  }
  NF_CMD(0x10);
  Delay(1);
  NF_WAITRB();
  NF_CMD(0x70);
  Delay(1);
  if (NF_RDDATA() & 0x1) {
    NF_nFCE_H();
    NF_MarkBadBlock(block);
    return 3;
  } else {
    NF_nFCE_H();
    return 0;
  }
}


x86 assembly (clang, -O0):
<NF_WritePage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x5,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1060 <NF_nFCE_L@plt>
xor    %edi,%edi
call   10a0 <NF_CMD@plt>
mov    $0x80,%edi
call   10a0 <NF_CMD@plt>
xor    %edi,%edi
call   10b0 <NF_ADDR@plt>
mov    -0x28(%rbp),%edi
and    $0xff,%edi
call   10b0 <NF_ADDR@plt>
mov    -0x28(%rbp),%edi
shr    $0x8,%edi
and    $0x7f,%edi
call   10b0 <NF_ADDR@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x200,-0x24(%rbp)
jge    122e <NF_WritePage+0x9e>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movzbl (%rax),%edi
call   1090 <NF_WRDATA@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11fc <NF_WritePage+0x6c>
cmpq   $0x0,-0x20(%rbp)
je     126e <NF_WritePage+0xde>
movl   $0x0,-0x24(%rbp)
cmpl   $0x10,-0x24(%rbp)
jge    1269 <NF_WritePage+0xd9>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1090 <NF_WRDATA@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1240 <NF_WritePage+0xb0>
jmp    126e <NF_WritePage+0xde>
mov    $0x10,%edi
call   10a0 <NF_CMD@plt>
mov    $0x1,%edi
call   1050 <Delay@plt>
mov    $0x0,%al
call   1070 <NF_WAITRB@plt>
mov    $0x70,%edi
call   10a0 <NF_CMD@plt>
mov    $0x1,%edi
call   1050 <Delay@plt>
mov    $0x0,%al
call   1080 <NF_RDDATA@plt>
and    $0x1,%eax
cmp    $0x0,%eax
je     12cb <NF_WritePage+0x13b>
mov    $0x0,%al
call   1030 <NF_nFCE_H@plt>
mov    -0x8(%rbp),%edi
call   1040 <NF_MarkBadBlock@plt>
movl   $0x3,-0x4(%rbp)
jmp    12d9 <NF_WritePage+0x149>
mov    $0x0,%al
call   1030 <NF_nFCE_H@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NF_WritePage>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %edi,%r14d
mov    %edi,%r12d
shl    $0x5,%r12d
add    %esi,%r12d
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <NF_nFCE_L@plt>
xor    %edi,%edi
call   10a0 <NF_CMD@plt>
mov    $0x80,%edi
call   10a0 <NF_CMD@plt>
xor    %edi,%edi
call   10b0 <NF_ADDR@plt>
movzbl %r12b,%edi
call   10b0 <NF_ADDR@plt>
shr    $0x8,%r12d
and    $0x7f,%r12d
mov    %r12d,%edi
call   10b0 <NF_ADDR@plt>
cs nopw 0x0(%rax,%rax,1)
nop
movzbl 0x0(%rbp,%rbx,1),%edi
call   1090 <NF_WRDATA@plt>
add    $0x1,%rbx
cmp    $0x200,%ebx
jne    11f0 <NF_WritePage+0x60>
test   %r15,%r15
je     12ae <NF_WritePage+0x11e>
movzbl (%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0x1(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0x2(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0x3(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0x4(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0x5(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0x6(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0x7(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0x8(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0x9(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0xa(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0xb(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0xc(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0xd(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0xe(%r15),%edi
call   1090 <NF_WRDATA@plt>
movzbl 0xf(%r15),%edi
call   1090 <NF_WRDATA@plt>
mov    $0x10,%edi
call   10a0 <NF_CMD@plt>
mov    $0x1,%edi
call   1050 <Delay@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <NF_WAITRB@plt>
mov    $0x70,%edi
call   10a0 <NF_CMD@plt>
mov    $0x1,%edi
call   1050 <Delay@plt>
xor    %eax,%eax
call   1080 <NF_RDDATA@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <NF_nFCE_H@plt>
test   $0x1,%bpl
je     1302 <NF_WritePage+0x172>
mov    %r14d,%edi
call   1040 <NF_MarkBadBlock@plt>
mov    $0x3,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @NF_WritePage(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = load i32, i32* %6
%14 = shl i32 %13, 5
%15 = load i32, i32* %7
%16 = add i32 %14, %15
store i32 %16, i32* %11
%17 = load i8*, i8** %8
store i8* %17, i8** %12
%18 = call i32 (...) @NF_nFCE_L()
%19 = call i32 @NF_CMD(i32 0)
%20 = call i32 @NF_CMD(i32 128)
%21 = call i32 @NF_ADDR(i32 0)
%22 = load i32, i32* %11
%23 = and i32 %22, 255
%24 = call i32 @NF_ADDR(i32 %23)
%25 = load i32, i32* %11
%26 = lshr i32 %25, 8
%27 = and i32 %26, 127
%28 = call i32 @NF_ADDR(i32 %27)
store i32 0, i32* %10
br label %29
29: ; preds = %37, %4
%30 = load i32, i32* %10
%31 = icmp slt i32 %30, 512
br i1 %31, label %32, label %40
32: ; preds = %29
%33 = load i8*, i8** %12
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %12
%35 = load i8, i8* %33
%36 = call i32 @NF_WRDATA(i8 zeroext %35)
br label %37
37: ; preds = %32
%38 = load i32, i32* %10
%39 = add nsw i32 %38, 1
store i32 %39, i32* %10
br label %29
40: ; preds = %29
%41 = load i8*, i8** %9
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %58
43: ; preds = %40
store i32 0, i32* %10
br label %44
44: ; preds = %54, %43
%45 = load i32, i32* %10
%46 = icmp slt i32 %45, 16
br i1 %46, label %47, label %57
47: ; preds = %44
%48 = load i8*, i8** %9
%49 = load i32, i32* %10
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = call i32 @NF_WRDATA(i8 zeroext %52)
br label %54
54: ; preds = %47
%55 = load i32, i32* %10
%56 = add nsw i32 %55, 1
store i32 %56, i32* %10
br label %44
57: ; preds = %44
br label %58
58: ; preds = %57, %40
%59 = call i32 @NF_CMD(i32 16)
%60 = call i32 @Delay(i32 1)
%61 = call i32 (...) @NF_WAITRB()
%62 = call i32 @NF_CMD(i32 112)
%63 = call i32 @Delay(i32 1)
%64 = call i32 (...) @NF_RDDATA()
%65 = and i32 %64, 1
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %58
%68 = call i32 (...) @NF_nFCE_H()
%69 = load i32, i32* %6
%70 = call i32 @NF_MarkBadBlock(i32 %69)
store i32 3, i32* %5
br label %73
71: ; preds = %58
%72 = call i32 (...) @NF_nFCE_H()
store i32 0, i32* %5
br label %73
73: ; preds = %71, %67
%74 = load i32, i32* %5
ret i32 %74
}

LLVM IR (-O2):
define i32 @NF_WritePage(i32 %0, i32 %1, i8* nocapture %2, i8* %3) {
%5 = shl i32 %0, 5
%6 = add i32 %5, %1
%7 = tail call i32 (...) @NF_nFCE_L()
%8 = tail call i32 @NF_CMD(i32 0)
%9 = tail call i32 @NF_CMD(i32 128)
%10 = tail call i32 @NF_ADDR(i32 0)
%11 = and i32 %6, 255
%12 = tail call i32 @NF_ADDR(i32 %11)
%13 = lshr i32 %6, 8
%14 = and i32 %13, 127
%15 = tail call i32 @NF_ADDR(i32 %14)
br label %16
16: ; preds = %4, %16
%17 = phi i8* [ %2, %4 ], [ %19, %16 ]
%18 = phi i32 [ 0, %4 ], [ %22, %16 ]
%19 = getelementptr inbounds i8, i8* %17, i64 1
%20 = load i8, i8* %17
%21 = tail call i32 @NF_WRDATA(i8 zeroext %20)
%22 = add nuw nsw i32 %18, 1
%23 = icmp eq i32 %22, 512
br i1 %23, label %24, label %16
24: ; preds = %16
%25 = icmp eq i8* %3, null
br i1 %25, label %74, label %26
26: ; preds = %24
%27 = load i8, i8* %3
%28 = tail call i32 @NF_WRDATA(i8 zeroext %27)
%29 = getelementptr inbounds i8, i8* %3, i64 1
%30 = load i8, i8* %29
%31 = tail call i32 @NF_WRDATA(i8 zeroext %30)
%32 = getelementptr inbounds i8, i8* %3, i64 2
%33 = load i8, i8* %32
%34 = tail call i32 @NF_WRDATA(i8 zeroext %33)
%35 = getelementptr inbounds i8, i8* %3, i64 3
%36 = load i8, i8* %35
%37 = tail call i32 @NF_WRDATA(i8 zeroext %36)
%38 = getelementptr inbounds i8, i8* %3, i64 4
%39 = load i8, i8* %38
%40 = tail call i32 @NF_WRDATA(i8 zeroext %39)
%41 = getelementptr inbounds i8, i8* %3, i64 5
%42 = load i8, i8* %41
%43 = tail call i32 @NF_WRDATA(i8 zeroext %42)
%44 = getelementptr inbounds i8, i8* %3, i64 6
%45 = load i8, i8* %44
%46 = tail call i32 @NF_WRDATA(i8 zeroext %45)
%47 = getelementptr inbounds i8, i8* %3, i64 7
%48 = load i8, i8* %47
%49 = tail call i32 @NF_WRDATA(i8 zeroext %48)
%50 = getelementptr inbounds i8, i8* %3, i64 8
%51 = load i8, i8* %50
%52 = tail call i32 @NF_WRDATA(i8 zeroext %51)
%53 = getelementptr inbounds i8, i8* %3, i64 9
%54 = load i8, i8* %53
%55 = tail call i32 @NF_WRDATA(i8 zeroext %54)
%56 = getelementptr inbounds i8, i8* %3, i64 10
%57 = load i8, i8* %56
%58 = tail call i32 @NF_WRDATA(i8 zeroext %57)
%59 = getelementptr inbounds i8, i8* %3, i64 11
%60 = load i8, i8* %59
%61 = tail call i32 @NF_WRDATA(i8 zeroext %60)
%62 = getelementptr inbounds i8, i8* %3, i64 12
%63 = load i8, i8* %62
%64 = tail call i32 @NF_WRDATA(i8 zeroext %63)
%65 = getelementptr inbounds i8, i8* %3, i64 13
%66 = load i8, i8* %65
%67 = tail call i32 @NF_WRDATA(i8 zeroext %66)
%68 = getelementptr inbounds i8, i8* %3, i64 14
%69 = load i8, i8* %68
%70 = tail call i32 @NF_WRDATA(i8 zeroext %69)
%71 = getelementptr inbounds i8, i8* %3, i64 15
%72 = load i8, i8* %71
%73 = tail call i32 @NF_WRDATA(i8 zeroext %72)
br label %74
74: ; preds = %26, %24
%75 = tail call i32 @NF_CMD(i32 16)
%76 = tail call i32 @Delay(i32 1)
%77 = tail call i32 (...) @NF_WAITRB()
%78 = tail call i32 @NF_CMD(i32 112)
%79 = tail call i32 @Delay(i32 1)
%80 = tail call i32 (...) @NF_RDDATA()
%81 = and i32 %80, 1
%82 = icmp eq i32 %81, 0
%83 = tail call i32 (...) @NF_nFCE_H()
br i1 %82, label %86, label %84
84: ; preds = %74
%85 = tail call i32 @NF_MarkBadBlock(i32 %0)
br label %86
86: ; preds = %74, %84
%87 = phi i32 [ 3, %84 ], [ 0, %74 ]
ret i32 %87
}