#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;
  unsigned int free_page = 0;
  int i;
  if (len > 8192) {
    free_page = ((len - 8192) + 4096) >> 12;
  }
  nand_init();
  for (i = 0; i < 4; i++, dest += 2048) {
    nandll_read_page(dest, i, 2);
  }
  for (i = 4; i < (free_page + 4); i++, dest += 4096) {
    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)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2000,-0xc(%rbp)
jbe    115f <copy2ddr+0x3f>
mov    -0xc(%rbp),%eax
sub    $0x2000,%eax
add    $0x1000,%eax
shr    $0xc,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <nand_init@plt>
movl   $0x0,-0x20(%rbp)
cmpl   $0x4,-0x20(%rbp)
jge    11a4 <copy2ddr+0x84>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    $0x2,%edx
call   1040 <nandll_read_page@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x800,%rax
mov    %rax,-0x18(%rbp)
jmp    116d <copy2ddr+0x4d>
movl   $0x4,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
add    $0x4,%ecx
cmp    %ecx,%eax
jae    11e9 <copy2ddr+0xc9>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    $0x2,%edx
call   1040 <nandll_read_page@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x18(%rbp)
jmp    11ab <copy2ddr+0x8b>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy2ddr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebx
lea    -0x1000(%r14),%r15d
shr    $0xc,%r15d
add    $0x4,%r15d
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>
cmp    $0x2001,%r14d
mov    $0x4,%ebp
cmovb  %ebp,%r15d
cmp    $0x5,%r15d
jb     11ce <copy2ddr+0xae>
add    $0x2000,%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x2,%edx
call   1040 <nandll_read_page@plt>
add    $0x1,%ebp
add    $0x1000,%rbx
cmp    %ebp,%r15d
jne    11b0 <copy2ddr+0x90>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
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
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %5
%11 = zext i32 %10 to i64
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %7
store i32 0, i32* %8
%13 = load i32, i32* %6
%14 = icmp ugt i32 %13, 8192
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i32, i32* %6
%17 = sub i32 %16, 8192
%18 = add i32 %17, 4096
%19 = lshr i32 %18, 12
store i32 %19, i32* %8
br label %20
20: ; preds = %15, %3
%21 = call i32 (...) @nand_init()
store i32 0, i32* %9
br label %22
22: ; preds = %29, %20
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 4
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = load i8*, i8** %7
%27 = load i32, i32* %9
%28 = call i32 @nandll_read_page(i8* %26, i32 %27, i32 2)
br label %29
29: ; preds = %25
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i64 2048
store i8* %33, i8** %7
br label %22
34: ; preds = %22
store i32 4, i32* %9
br label %35
35: ; preds = %44, %34
%36 = load i32, i32* %9
%37 = load i32, i32* %8
%38 = add i32 %37, 4
%39 = icmp ult i32 %36, %38
br i1 %39, label %40, label %49
40: ; preds = %35
%41 = load i8*, i8** %7
%42 = load i32, i32* %9
%43 = call i32 @nandll_read_page(i8* %41, i32 %42, i32 2)
br label %44
44: ; preds = %40
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
store i32 %46, i32* %9
%47 = load i8*, i8** %7
%48 = getelementptr inbounds i8, i8* %47, i64 4096
store i8* %48, i8** %7
br label %35
49: ; preds = %35
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 = add i32 %2, -4096
%7 = lshr i32 %6, 12
%8 = add nuw nsw i32 %7, 4
%9 = tail call i32 (...) @nand_init()
%10 = tail call i32 @nandll_read_page(i8* %5, i32 0, i32 2)
%11 = getelementptr inbounds i8, i8* %5, i64 2048
%12 = tail call i32 @nandll_read_page(i8* %11, i32 1, i32 2)
%13 = getelementptr inbounds i8, i8* %5, i64 4096
%14 = tail call i32 @nandll_read_page(i8* %13, i32 2, i32 2)
%15 = getelementptr inbounds i8, i8* %5, i64 6144
%16 = tail call i32 @nandll_read_page(i8* %15, i32 3, i32 2)
%17 = icmp ugt i32 %2, 8192
%18 = select i1 %17, i32 %8, i32 4
%19 = icmp ugt i32 %18, 4
br i1 %19, label %20, label %29
20: ; preds = %3
%21 = getelementptr inbounds i8, i8* %5, i64 8192
br label %22
22: ; preds = %20, %22
%23 = phi i32 [ %26, %22 ], [ 4, %20 ]
%24 = phi i8* [ %27, %22 ], [ %21, %20 ]
%25 = tail call i32 @nandll_read_page(i8* %24, i32 %23, i32 2)
%26 = add nuw nsw i32 %23, 1
%27 = getelementptr inbounds i8, i8* %24, i64 4096
%28 = icmp eq i32 %26, %18
br i1 %28, label %29, label %22
29: ; preds = %22, %3
ret i32 0
}