#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*/ nand_wait_idle();
int /*<<< orphan*/ nand_write_address(unsigned long);
void nand_read_page(unsigned char *buffer, unsigned long start_addr, int size) {
  unsigned long addr = 0;
  unsigned char data = 0;
  int index = 0;
  if ((start_addr & (2048 - 1)) || (size & (2048 - 1))) {
    return;
  }
  ((*(volatile unsigned long *)(0x70200000 + 0x04)) &= ~(1 << 1));
  for (addr = start_addr; addr < (start_addr + size); addr += 2048) {
    (*(volatile unsigned long *)(0x70200000 + 0x08)) = 0x0;
    nand_write_address(addr);
    (*(volatile unsigned long *)(0x70200000 + 0x08)) = 0x30;
    nand_wait_idle();
    for (index = 0; index < 2048; index++)
      *buffer++ = (*(volatile unsigned char *)(0x70200000 + 0x10));
  }
  ((*(volatile unsigned long *)(0x70200000 + 0x04)) |= (1 << 1));
}


x86 assembly (clang, -O0):
<nand_read_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
and    $0x7ff,%rax
cmp    $0x0,%rax
jne    116b <nand_read_page+0x4b>
mov    -0x14(%rbp),%eax
and    $0x7ff,%eax
cmp    $0x0,%eax
je     1170 <nand_read_page+0x50>
jmp    1232 <nand_read_page+0x112>
mov    $0x70200004,%eax
mov    (%rax),%rcx
and    $0xfffffffffffffffd,%rcx
mov    $0x70200004,%eax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x14(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    121e <nand_read_page+0xfe>
mov    $0x70200008,%eax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <nand_write_address@plt>
mov    $0x70200008,%eax
movq   $0x30,(%rax)
mov    $0x0,%al
call   1030 <nand_wait_idle@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0x800,-0x28(%rbp)
jge    1206 <nand_read_page+0xe6>
mov    $0x70200010,%eax
mov    (%rax),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11d3 <nand_read_page+0xb3>
jmp    120b <nand_read_page+0xeb>
mov    -0x20(%rbp),%rax
add    $0x800,%rax
mov    %rax,-0x20(%rbp)
jmp    118c <nand_read_page+0x6c>
mov    $0x70200004,%eax
mov    (%rax),%rcx
or     $0x2,%rcx
mov    $0x70200004,%eax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nand_read_page>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
test   $0x7ff,%r14d
jne    1216 <nand_read_page+0xf6>
mov    %edx,%eax
and    $0x7ff,%eax
jne    1216 <nand_read_page+0xf6>
andq   $0xfffffffffffffffd,0x70200004
70 fd
movslq %edx,%r15
add    %r14,%r15
cmp    %r14,%r15
jbe    120d <nand_read_page+0xed>
mov    %rdi,%rbx
nopl   (%rax)
movq   $0x0,0x70200008
mov    %r14,%rdi
call   1040 <nand_write_address@plt>
movq   $0x30,0x70200008
xor    %eax,%eax
call   1030 <nand_wait_idle@plt>
xor    %eax,%eax
nopl   0x0(%rax)
movzbl 0x70200010,%ecx
70
mov    %cl,(%rbx,%rax,1)
movzbl 0x70200010,%ecx
70
mov    %cl,0x1(%rbx,%rax,1)
movzbl 0x70200010,%ecx
70
mov    %cl,0x2(%rbx,%rax,1)
movzbl 0x70200010,%ecx
70
mov    %cl,0x3(%rbx,%rax,1)
movzbl 0x70200010,%ecx
70
mov    %cl,0x4(%rbx,%rax,1)
movzbl 0x70200010,%ecx
70
mov    %cl,0x5(%rbx,%rax,1)
movzbl 0x70200010,%ecx
70
mov    %cl,0x6(%rbx,%rax,1)
movzbl 0x70200010,%ecx
70
mov    %cl,0x7(%rbx,%rax,1)
add    $0x8,%rax
cmp    $0x800,%eax
jne    1190 <nand_read_page+0x70>
add    $0x800,%r14
add    %rax,%rbx
cmp    %r15,%r14
jb     1160 <nand_read_page+0x40>
orq    $0x2,0x70200004
70 02
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @nand_read_page(i8* nocapture writeonly %0, i64 %1, i32 %2) {
%4 = and i64 %1, 2047
%5 = icmp eq i64 %4, 0
%6 = and i32 %2, 2047
%7 = icmp eq i32 %6, 0
%8 = and i1 %5, %7
br i1 %8, label %9, label %47
9: ; preds = %3
%10 = load volatile i64, i64* inttoptr (i64 1881145348 to i64*)
%11 = and i64 %10, -3
store volatile i64 %11, i64* inttoptr (i64 1881145348 to i64*)
%12 = sext i32 %2 to i64
%13 = add i64 %12, %1
%14 = icmp ugt i64 %13, %1
br i1 %14, label %15, label %44
15: ; preds = %9, %41
%16 = phi i8* [ %38, %41 ], [ %0, %9 ]
%17 = phi i64 [ %42, %41 ], [ %1, %9 ]
store volatile i64 0, i64* inttoptr (i64 1881145352 to i64*)
%18 = tail call i32 @nand_write_address(i64 %17)
store volatile i64 48, i64* inttoptr (i64 1881145352 to i64*)
%19 = tail call i32 (...) @nand_wait_idle()
br label %20
20: ; preds = %20, %15
%21 = phi i32 [ 0, %15 ], [ %39, %20 ]
%22 = phi i8* [ %16, %15 ], [ %38, %20 ]
%23 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%24 = getelementptr inbounds i8, i8* %22, i64 1
store i8 %23, i8* %22
%25 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%26 = getelementptr inbounds i8, i8* %22, i64 2
store i8 %25, i8* %24
%27 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%28 = getelementptr inbounds i8, i8* %22, i64 3
store i8 %27, i8* %26
%29 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%30 = getelementptr inbounds i8, i8* %22, i64 4
store i8 %29, i8* %28
%31 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%32 = getelementptr inbounds i8, i8* %22, i64 5
store i8 %31, i8* %30
%33 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%34 = getelementptr inbounds i8, i8* %22, i64 6
store i8 %33, i8* %32
%35 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%36 = getelementptr inbounds i8, i8* %22, i64 7
store i8 %35, i8* %34
%37 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%38 = getelementptr inbounds i8, i8* %22, i64 8
store i8 %37, i8* %36
%39 = add nuw nsw i32 %21, 8
%40 = icmp eq i32 %39, 2048
br i1 %40, label %41, label %20
41: ; preds = %20
%42 = add i64 %17, 2048
%43 = icmp ult i64 %42, %13
br i1 %43, label %15, label %44
44: ; preds = %41, %9
%45 = load volatile i64, i64* inttoptr (i64 1881145348 to i64*)
%46 = or i64 %45, 2
store volatile i64 %46, i64* inttoptr (i64 1881145348 to i64*)
br label %47
47: ; preds = %3, %44
ret void
}