#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*/ clear_RnB();
int /*<<< orphan*/ delete_chip();
int /*<<< orphan*/ select_chip();
int /*<<< orphan*/ send_addr(unsigned long);
int /*<<< orphan*/ send_cmd(int);
int /*<<< orphan*/ wait_RnB();
void NF_Page_Write(unsigned long addr, unsigned char *buff, int size) {
  int i;
  int ret, len;
  select_chip();
  clear_RnB();
  send_cmd(0x80);
  send_addr(0x00);
  send_addr(0x00);
  send_addr(addr & 0xFF);
  send_addr((addr >> 8) & (0xFF));
  send_addr((addr >> 16) & (0xFF));
  if (size > 2048)
    len = size > 2048 * 4 ? 2048 * 4 : size;
  else if (size < 2048)
    len = size;
  else
    len = 2048;
  for (i = 0; i < len; i++) {
    (*(volatile unsigned char *)0x70200010) = *buff++;
  }
  send_cmd(0x10);
  wait_RnB();
  send_cmd(0x70);
  ret = (*(volatile unsigned char *)0x70200010);
  delete_chip();
}


x86 assembly (clang, -O0):
<NF_Page_Write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1040 <select_chip@plt>
mov    $0x0,%al
call   1070 <clear_RnB@plt>
mov    $0x80,%edi
call   1080 <send_cmd@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1030 <send_addr@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1030 <send_addr@plt>
mov    -0x8(%rbp),%rdi
and    $0xff,%rdi
call   1030 <send_addr@plt>
mov    -0x8(%rbp),%rdi
shr    $0x8,%rdi
and    $0xff,%rdi
call   1030 <send_addr@plt>
mov    -0x8(%rbp),%rdi
shr    $0x10,%rdi
and    $0xff,%rdi
call   1030 <send_addr@plt>
cmpl   $0x800,-0x14(%rbp)
jle    120d <NF_Page_Write+0xad>
cmpl   $0x2000,-0x14(%rbp)
jle    11fc <NF_Page_Write+0x9c>
mov    $0x2000,%eax
mov    %eax,-0x24(%rbp)
jmp    1202 <NF_Page_Write+0xa2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1231 <NF_Page_Write+0xd1>
cmpl   $0x800,-0x14(%rbp)
jge    1225 <NF_Page_Write+0xc5>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    122c <NF_Page_Write+0xcc>
movl   $0x800,-0x20(%rbp)
jmp    1231 <NF_Page_Write+0xd1>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    126a <NF_Page_Write+0x10a>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    $0x70200010,%eax
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1238 <NF_Page_Write+0xd8>
mov    $0x10,%edi
call   1080 <send_cmd@plt>
mov    $0x0,%al
call   1050 <wait_RnB@plt>
mov    $0x70,%edi
call   1080 <send_cmd@plt>
mov    $0x70200010,%eax
mov    (%rax),%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <delete_chip@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NF_Page_Write>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
xor    %eax,%eax
call   1040 <select_chip@plt>
xor    %eax,%eax
call   1070 <clear_RnB@plt>
mov    $0x80,%edi
call   1080 <send_cmd@plt>
xor    %edi,%edi
call   1030 <send_addr@plt>
xor    %edi,%edi
call   1030 <send_addr@plt>
movzbl %bpl,%edi
call   1030 <send_addr@plt>
mov    %rbp,%rax
movzbl %ah,%edi
call   1030 <send_addr@plt>
shr    $0x10,%ebp
movzbl %bpl,%edi
call   1030 <send_addr@plt>
cmp    $0x2000,%r14d
mov    $0x2000,%eax
cmovl  %r14d,%eax
cmp    $0x801,%r14d
cmovl  %r14d,%eax
test   %eax,%eax
jle    1273 <NF_Page_Write+0x113>
lea    -0x1(%rax),%ecx
cmp    $0x7,%ecx
jb     1250 <NF_Page_Write+0xf0>
mov    %eax,%ecx
and    $0xfffffff8,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%rbx),%edx
mov    %dl,0x70200010
movzbl 0x1(%rbx),%edx
mov    %dl,0x70200010
movzbl 0x2(%rbx),%edx
mov    %dl,0x70200010
movzbl 0x3(%rbx),%edx
mov    %dl,0x70200010
movzbl 0x4(%rbx),%edx
mov    %dl,0x70200010
movzbl 0x5(%rbx),%edx
mov    %dl,0x70200010
movzbl 0x6(%rbx),%edx
mov    %dl,0x70200010
movzbl 0x7(%rbx),%edx
add    $0x8,%rbx
mov    %dl,0x70200010
add    $0xfffffff8,%ecx
jne    11f0 <NF_Page_Write+0x90>
test   $0x7,%al
je     1273 <NF_Page_Write+0x113>
and    $0x7,%eax
xor    %ecx,%ecx
nopl   0x0(%rax)
movzbl (%rbx,%rcx,1),%edx
mov    %dl,0x70200010
add    $0x1,%rcx
cmp    %ecx,%eax
jne    1260 <NF_Page_Write+0x100>
mov    $0x10,%edi
call   1080 <send_cmd@plt>
xor    %eax,%eax
call   1050 <wait_RnB@plt>
mov    $0x70,%edi
call   1080 <send_cmd@plt>
mov    0x70200010,%al
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <delete_chip@plt>

LLVM IR (-O0):
define void @NF_Page_Write(i64 %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i64 %0, i64* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = call i32 (...) @select_chip()
%11 = call i32 (...) @clear_RnB()
%12 = call i32 @send_cmd(i32 128)
%13 = call i32 @send_addr(i64 0)
%14 = call i32 @send_addr(i64 0)
%15 = load i64, i64* %4
%16 = and i64 %15, 255
%17 = call i32 @send_addr(i64 %16)
%18 = load i64, i64* %4
%19 = lshr i64 %18, 8
%20 = and i64 %19, 255
%21 = call i32 @send_addr(i64 %20)
%22 = load i64, i64* %4
%23 = lshr i64 %22, 16
%24 = and i64 %23, 255
%25 = call i32 @send_addr(i64 %24)
%26 = load i32, i32* %6
%27 = icmp sgt i32 %26, 2048
br i1 %27, label %28, label %36
28: ; preds = %3
%29 = load i32, i32* %6
%30 = icmp sgt i32 %29, 8192
br i1 %30, label %31, label %32
31: ; preds = %28
br label %34
32: ; preds = %28
%33 = load i32, i32* %6
br label %34
34: ; preds = %32, %31
%35 = phi i32 [ 8192, %31 ], [ %33, %32 ]
store i32 %35, i32* %9
br label %43
36: ; preds = %3
%37 = load i32, i32* %6
%38 = icmp slt i32 %37, 2048
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = load i32, i32* %6
store i32 %40, i32* %9
br label %42
41: ; preds = %36
store i32 2048, i32* %9
br label %42
42: ; preds = %41, %39
br label %43
43: ; preds = %42, %34
store i32 0, i32* %7
br label %44
44: ; preds = %52, %43
%45 = load i32, i32* %7
%46 = load i32, i32* %9
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %55
48: ; preds = %44
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %5
%51 = load i8, i8* %49
store volatile i8 %51, i8* inttoptr (i64 1881145360 to i8*)
br label %52
52: ; preds = %48
%53 = load i32, i32* %7
%54 = add nsw i32 %53, 1
store i32 %54, i32* %7
br label %44
55: ; preds = %44
%56 = call i32 @send_cmd(i32 16)
%57 = call i32 (...) @wait_RnB()
%58 = call i32 @send_cmd(i32 112)
%59 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%60 = zext i8 %59 to i32
store i32 %60, i32* %8
%61 = call i32 (...) @delete_chip()
ret void
}

LLVM IR (-O2):
define void @NF_Page_Write(i64 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 (...) @select_chip()
%5 = tail call i32 (...) @clear_RnB()
%6 = tail call i32 @send_cmd(i32 128)
%7 = tail call i32 @send_addr(i64 0)
%8 = tail call i32 @send_addr(i64 0)
%9 = and i64 %0, 255
%10 = tail call i32 @send_addr(i64 %9)
%11 = lshr i64 %0, 8
%12 = and i64 %11, 255
%13 = tail call i32 @send_addr(i64 %12)
%14 = lshr i64 %0, 16
%15 = and i64 %14, 255
%16 = tail call i32 @send_addr(i64 %15)
%17 = icmp sgt i32 %2, 2048
%18 = icmp slt i32 %2, 8192
%19 = select i1 %18, i32 %2, i32 8192
%20 = select i1 %17, i32 %19, i32 %2
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %59
22: ; preds = %3
%23 = add i32 %20, -1
%24 = and i32 %20, 7
%25 = icmp ult i32 %23, 7
br i1 %25, label %49, label %26
26: ; preds = %22
%27 = and i32 %20, -8
br label %28
28: ; preds = %28, %26
%29 = phi i8* [ %1, %26 ], [ %45, %28 ]
%30 = phi i32 [ 0, %26 ], [ %47, %28 ]
%31 = getelementptr inbounds i8, i8* %29, i64 1
%32 = load i8, i8* %29
store volatile i8 %32, i8* inttoptr (i64 1881145360 to i8*)
%33 = getelementptr inbounds i8, i8* %29, i64 2
%34 = load i8, i8* %31
store volatile i8 %34, i8* inttoptr (i64 1881145360 to i8*)
%35 = getelementptr inbounds i8, i8* %29, i64 3
%36 = load i8, i8* %33
store volatile i8 %36, i8* inttoptr (i64 1881145360 to i8*)
%37 = getelementptr inbounds i8, i8* %29, i64 4
%38 = load i8, i8* %35
store volatile i8 %38, i8* inttoptr (i64 1881145360 to i8*)
%39 = getelementptr inbounds i8, i8* %29, i64 5
%40 = load i8, i8* %37
store volatile i8 %40, i8* inttoptr (i64 1881145360 to i8*)
%41 = getelementptr inbounds i8, i8* %29, i64 6
%42 = load i8, i8* %39
store volatile i8 %42, i8* inttoptr (i64 1881145360 to i8*)
%43 = getelementptr inbounds i8, i8* %29, i64 7
%44 = load i8, i8* %41
store volatile i8 %44, i8* inttoptr (i64 1881145360 to i8*)
%45 = getelementptr inbounds i8, i8* %29, i64 8
%46 = load i8, i8* %43
store volatile i8 %46, i8* inttoptr (i64 1881145360 to i8*)
%47 = add i32 %30, 8
%48 = icmp eq i32 %47, %27
br i1 %48, label %49, label %28
49: ; preds = %28, %22
%50 = phi i8* [ %1, %22 ], [ %45, %28 ]
%51 = icmp eq i32 %24, 0
br i1 %51, label %59, label %52
52: ; preds = %49, %52
%53 = phi i8* [ %55, %52 ], [ %50, %49 ]
%54 = phi i32 [ %57, %52 ], [ 0, %49 ]
%55 = getelementptr inbounds i8, i8* %53, i64 1
%56 = load i8, i8* %53
store volatile i8 %56, i8* inttoptr (i64 1881145360 to i8*)
%57 = add i32 %54, 1
%58 = icmp eq i32 %57, %24
br i1 %58, label %59, label %52
59: ; preds = %49, %52, %3
%60 = tail call i32 @send_cmd(i32 16)
%61 = tail call i32 (...) @wait_RnB()
%62 = tail call i32 @send_cmd(i32 112)
%63 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%64 = tail call i32 (...) @delete_chip()
ret void
}