#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*/ nand_addr(unsigned long);
int /*<<< orphan*/ select_chip();
int /*<<< orphan*/ send_cmd(int);
int /*<<< orphan*/ wait_RnB();
void NF_Page_Read(unsigned long addr, unsigned char *buff, unsigned long size) {
  int i;
  unsigned long rest = size;
  unsigned long page;
  select_chip();
  while (rest > 0) {
    clear_RnB();
    send_cmd(0x00);
    nand_addr(addr);
    send_cmd(0x30);
    wait_RnB();
    page = rest > 2048 ? 2048 : rest;
    for (i = 0; i < page; i++) {
      *buff++ = (*(volatile unsigned char *)0x70200010);
    }
    rest -= page;
    addr += page;
  }
  delete_chip();
}


x86 assembly (clang, -O0):
<NF_Page_Read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <select_chip@plt>
cmpq   $0x0,-0x28(%rbp)
jbe    123d <NF_Page_Read+0xdd>
mov    $0x0,%al
call   1070 <clear_RnB@plt>
xor    %edi,%edi
call   1080 <send_cmd@plt>
mov    -0x8(%rbp),%rdi
call   1030 <nand_addr@plt>
mov    $0x30,%edi
call   1080 <send_cmd@plt>
mov    $0x0,%al
call   1050 <wait_RnB@plt>
cmpq   $0x800,-0x28(%rbp)
jbe    11d2 <NF_Page_Read+0x72>
mov    $0x800,%eax
mov    %rax,-0x38(%rbp)
jmp    11da <NF_Page_Read+0x7a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    121d <NF_Page_Read+0xbd>
mov    $0x70200010,%eax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e9 <NF_Page_Read+0x89>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1183 <NF_Page_Read+0x23>
mov    $0x0,%al
call   1060 <delete_chip@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NF_Page_Read>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %eax,%eax
call   1040 <select_chip@plt>
test   %r14,%r14
jne    1198 <NF_Page_Read+0x38>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <delete_chip@plt>
cs nopw 0x0(%rax,%rax,1)
add    %rsi,%r15
sub    %rsi,%r14
je     117a <NF_Page_Read+0x1a>
xor    %eax,%eax
call   1070 <clear_RnB@plt>
xor    %edi,%edi
call   1080 <send_cmd@plt>
mov    %r15,%rdi
call   1030 <nand_addr@plt>
mov    $0x30,%edi
call   1080 <send_cmd@plt>
xor    %eax,%eax
call   1050 <wait_RnB@plt>
cmp    $0x800,%r14
mov    $0x800,%esi
cmovb  %r14,%rsi
lea    -0x1(%rsi),%rdx
mov    %esi,%ecx
and    $0x7,%ecx
cmp    $0x7,%rdx
jb     1251 <NF_Page_Read+0xf1>
mov    %esi,%edx
and    $0xff8,%edx
cs nopw 0x0(%rax,%rax,1)
movzbl 0x70200010,%eax
70
mov    %al,(%rbx)
movzbl 0x70200010,%eax
70
mov    %al,0x1(%rbx)
movzbl 0x70200010,%eax
70
mov    %al,0x2(%rbx)
movzbl 0x70200010,%eax
70
mov    %al,0x3(%rbx)
movzbl 0x70200010,%eax
70
mov    %al,0x4(%rbx)
movzbl 0x70200010,%eax
70
mov    %al,0x5(%rbx)
movzbl 0x70200010,%eax
70
mov    %al,0x6(%rbx)
movzbl 0x70200010,%eax
70
mov    %al,0x7(%rbx)
add    $0x8,%rbx
add    $0xfffffffffffffff8,%rdx
jne    11f0 <NF_Page_Read+0x90>
test   %rcx,%rcx
je     1190 <NF_Page_Read+0x30>
nopw   0x0(%rax,%rax,1)
movzbl 0x70200010,%eax
70
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0xffffffffffffffff,%rcx
jne    1260 <NF_Page_Read+0x100>
jmp    1190 <NF_Page_Read+0x30>

LLVM IR (-O0):
define void @NF_Page_Read(i64 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
store i64 %0, i64* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i64, i64* %6
store i64 %10, i64* %8
%11 = call i32 (...) @select_chip()
br label %12
12: ; preds = %41, %3
%13 = load i64, i64* %8
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %48
15: ; preds = %12
%16 = call i32 (...) @clear_RnB()
%17 = call i32 @send_cmd(i32 0)
%18 = load i64, i64* %4
%19 = call i32 @nand_addr(i64 %18)
%20 = call i32 @send_cmd(i32 48)
%21 = call i32 (...) @wait_RnB()
%22 = load i64, i64* %8
%23 = icmp ugt i64 %22, 2048
br i1 %23, label %24, label %25
24: ; preds = %15
br label %27
25: ; preds = %15
%26 = load i64, i64* %8
br label %27
27: ; preds = %25, %24
%28 = phi i64 [ 2048, %24 ], [ %26, %25 ]
store i64 %28, i64* %9
store i32 0, i32* %7
br label %29
29: ; preds = %38, %27
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = load i64, i64* %9
%33 = icmp ult i64 %31, %32
br i1 %33, label %34, label %41
34: ; preds = %29
%35 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %5
store i8 %35, i8* %36
br label %38
38: ; preds = %34
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %29
41: ; preds = %29
%42 = load i64, i64* %9
%43 = load i64, i64* %8
%44 = sub i64 %43, %42
store i64 %44, i64* %8
%45 = load i64, i64* %9
%46 = load i64, i64* %4
%47 = add i64 %46, %45
store i64 %47, i64* %4
br label %12
48: ; preds = %12
%49 = call i32 (...) @delete_chip()
ret void
}

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