#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_init();
int /*<<< orphan*/ nandll_read_page(unsigned char *, int, int);
int copy2ddr(unsigned int nand_start, unsigned int ddr_start,
             unsigned int len) {
  unsigned char *dest = (unsigned char *)ddr_start;
  int i;
  nand_init();
  for (i = 0; i < 4; i++, dest += 2048) {
    nandll_read_page(dest, i, 2);
  }
  for (i = 4; i < ((len + 4096) >> 13); i++, dest += 8192) {
    nandll_read_page(dest, i, 2);
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy2ddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <nand_init@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jge    117d <copy2ddr+0x5d>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    $0x2,%edx
call   1040 <nandll_read_page@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x800,%rax
mov    %rax,-0x18(%rbp)
jmp    1146 <copy2ddr+0x26>
movl   $0x4,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    $0x1000,%ecx
shr    $0xd,%ecx
cmp    %ecx,%eax
jae    11c8 <copy2ddr+0xa8>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    $0x2,%edx
call   1040 <nandll_read_page@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2000,%rax
mov    %rax,-0x18(%rbp)
jmp    1184 <copy2ddr+0x64>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy2ddr>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebx
xor    %eax,%eax
call   1030 <nand_init@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1040 <nandll_read_page@plt>
lea    0x800(%rbx),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
call   1040 <nandll_read_page@plt>
lea    0x1000(%rbx),%rdi
mov    $0x2,%esi
mov    $0x2,%edx
call   1040 <nandll_read_page@plt>
lea    0x1800(%rbx),%rdi
mov    $0x3,%esi
mov    $0x2,%edx
call   1040 <nandll_read_page@plt>
add    $0x1000,%r14d
cmp    $0xa000,%r14d
jb     11ce <copy2ddr+0xae>
shr    $0xd,%r14d
add    $0x2000,%rbx
mov    $0x4,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x2,%edx
call   1040 <nandll_read_page@plt>
add    $0x1,%ebp
add    $0x2000,%rbx
cmp    %ebp,%r14d
jne    11b0 <copy2ddr+0x90>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @copy2ddr(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %5
%10 = zext i32 %9 to i64
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %7
%12 = call i32 (...) @nand_init()
store i32 0, i32* %8
br label %13
13: ; preds = %20, %3
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 4
br i1 %15, label %16, label %25
16: ; preds = %13
%17 = load i8*, i8** %7
%18 = load i32, i32* %8
%19 = call i32 @nandll_read_page(i8* %17, i32 %18, i32 2)
br label %20
20: ; preds = %16
%21 = load i32, i32* %8
%22 = add nsw i32 %21, 1
store i32 %22, i32* %8
%23 = load i8*, i8** %7
%24 = getelementptr inbounds i8, i8* %23, i64 2048
store i8* %24, i8** %7
br label %13
25: ; preds = %13
store i32 4, i32* %8
br label %26
26: ; preds = %36, %25
%27 = load i32, i32* %8
%28 = load i32, i32* %6
%29 = add i32 %28, 4096
%30 = lshr i32 %29, 13
%31 = icmp ult i32 %27, %30
br i1 %31, label %32, label %41
32: ; preds = %26
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = call i32 @nandll_read_page(i8* %33, i32 %34, i32 2)
br label %36
36: ; preds = %32
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
store i32 %38, i32* %8
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i64 8192
store i8* %40, i8** %7
br label %26
41: ; preds = %26
ret i32 0
}

LLVM IR (-O2):
define i32 @copy2ddr(i32 %0, i32 %1, i32 %2) {
%4 = zext i32 %1 to i64
%5 = inttoptr i64 %4 to i8*
%6 = tail call i32 (...) @nand_init()
%7 = tail call i32 @nandll_read_page(i8* %5, i32 0, i32 2)
%8 = getelementptr inbounds i8, i8* %5, i64 2048
%9 = tail call i32 @nandll_read_page(i8* %8, i32 1, i32 2)
%10 = getelementptr inbounds i8, i8* %5, i64 4096
%11 = tail call i32 @nandll_read_page(i8* %10, i32 2, i32 2)
%12 = getelementptr inbounds i8, i8* %5, i64 6144
%13 = tail call i32 @nandll_read_page(i8* %12, i32 3, i32 2)
%14 = add i32 %2, 4096
%15 = icmp ugt i32 %14, 40959
br i1 %15, label %16, label %25
16: ; preds = %3
%17 = lshr i32 %14, 13
br label %18
18: ; preds = %16, %18
%19 = phi i32 [ %23, %18 ], [ 4, %16 ]
%20 = phi i8* [ %21, %18 ], [ %5, %16 ]
%21 = getelementptr inbounds i8, i8* %20, i64 8192
%22 = tail call i32 @nandll_read_page(i8* %21, i32 %19, i32 2)
%23 = add nuw nsw i32 %19, 1
%24 = icmp eq i32 %23, %17
br i1 %24, label %25, label %18
25: ; preds = %18, %3
ret i32 0
}