#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*/ NF_Page_Read(int, unsigned char *, unsigned long);
void copy_2ddr(unsigned long nand_start, unsigned char *buff,
               unsigned long size) {
  unsigned long i;
  unsigned long rest = size;
  unsigned long len;
  for (i = 0; i != 4; i++) {
    len = rest > 2048 ? 2048 : rest;
    NF_Page_Read(2048 * i, buff, len);
    buff += 2048;
    rest -= len;
    if (rest == 0)
      return;
  }
  NF_Page_Read(2048 * 4, buff, rest);
}


x86 assembly (clang, -O0):
<copy_2ddr>:
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)
movq   $0x0,-0x20(%rbp)
cmpq   $0x4,-0x20(%rbp)
je     11c5 <copy_2ddr+0xb5>
cmpq   $0x800,-0x28(%rbp)
jbe    115b <copy_2ddr+0x4b>
mov    $0x800,%eax
mov    %rax,-0x38(%rbp)
jmp    1163 <copy_2ddr+0x53>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
shl    $0xb,%rax
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <NF_Page_Read@plt>
mov    -0x10(%rbp),%rax
add    $0x800,%rax
mov    %rax,-0x10(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11af <copy_2ddr+0x9f>
jmp    11d7 <copy_2ddr+0xc7>
jmp    11b4 <copy_2ddr+0xa4>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1134 <copy_2ddr+0x24>
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    $0x2000,%edi
call   1030 <NF_Page_Read@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_2ddr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
cmp    $0x800,%rdx
mov    $0x800,%r15d
mov    $0x800,%ebx
cmovb  %rdx,%rbx
xor    %edi,%edi
mov    %rbx,%rdx
call   1030 <NF_Page_Read@plt>
sub    %rbx,%r12
je     11e0 <copy_2ddr+0xd0>
lea    0x800(%r14),%rsi
cmp    $0x800,%r12
cmovb  %r12,%r15
mov    $0x800,%edi
mov    %r15,%rdx
call   1030 <NF_Page_Read@plt>
sub    %r15,%r12
je     11e0 <copy_2ddr+0xd0>
lea    0x1000(%r14),%rsi
cmp    $0x800,%r12
mov    $0x800,%r15d
mov    $0x800,%ebx
cmovb  %r12,%rbx
mov    $0x1000,%edi
mov    %rbx,%rdx
call   1030 <NF_Page_Read@plt>
sub    %rbx,%r12
je     11e0 <copy_2ddr+0xd0>
lea    0x1800(%r14),%rsi
cmp    $0x800,%r12
cmovb  %r12,%r15
mov    $0x1800,%edi
mov    %r15,%rdx
call   1030 <NF_Page_Read@plt>
sub    %r15,%r12
je     11e0 <copy_2ddr+0xd0>
add    $0x2000,%r14
mov    $0x2000,%edi
mov    %r14,%rsi
mov    %r12,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <NF_Page_Read@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @copy_2ddr(i64 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%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
store i64 0, i64* %7
br label %11
11: ; preds = %37, %3
%12 = load i64, i64* %7
%13 = icmp ne i64 %12, 4
br i1 %13, label %14, label %40
14: ; preds = %11
%15 = load i64, i64* %8
%16 = icmp ugt i64 %15, 2048
br i1 %16, label %17, label %18
17: ; preds = %14
br label %20
18: ; preds = %14
%19 = load i64, i64* %8
br label %20
20: ; preds = %18, %17
%21 = phi i64 [ 2048, %17 ], [ %19, %18 ]
store i64 %21, i64* %9
%22 = load i64, i64* %7
%23 = mul i64 2048, %22
%24 = trunc i64 %23 to i32
%25 = load i8*, i8** %5
%26 = load i64, i64* %9
%27 = call i32 @NF_Page_Read(i32 %24, i8* %25, i64 %26)
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i64 2048
store i8* %29, i8** %5
%30 = load i64, i64* %9
%31 = load i64, i64* %8
%32 = sub i64 %31, %30
store i64 %32, i64* %8
%33 = load i64, i64* %8
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %20
br label %44
36: ; preds = %20
br label %37
37: ; preds = %36
%38 = load i64, i64* %7
%39 = add i64 %38, 1
store i64 %39, i64* %7
br label %11
40: ; preds = %11
%41 = load i8*, i8** %5
%42 = load i64, i64* %8
%43 = call i32 @NF_Page_Read(i32 8192, i8* %41, i64 %42)
br label %44
44: ; preds = %40, %35
ret void
}

LLVM IR (-O2):
define void @copy_2ddr(i64 %0, i8* %1, i64 %2) {
%4 = icmp ult i64 %2, 2048
%5 = select i1 %4, i64 %2, i64 2048
%6 = tail call i32 @NF_Page_Read(i32 0, i8* %1, i64 %5)
%7 = sub i64 %2, %5
%8 = icmp eq i64 %7, 0
br i1 %8, label %33, label %9
9: ; preds = %3
%10 = getelementptr inbounds i8, i8* %1, i64 2048
%11 = icmp ult i64 %7, 2048
%12 = select i1 %11, i64 %7, i64 2048
%13 = tail call i32 @NF_Page_Read(i32 2048, i8* %10, i64 %12)
%14 = sub i64 %7, %12
%15 = icmp eq i64 %14, 0
br i1 %15, label %33, label %16
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %1, i64 4096
%18 = icmp ult i64 %14, 2048
%19 = select i1 %18, i64 %14, i64 2048
%20 = tail call i32 @NF_Page_Read(i32 4096, i8* %17, i64 %19)
%21 = sub i64 %14, %19
%22 = icmp eq i64 %21, 0
br i1 %22, label %33, label %23
23: ; preds = %16
%24 = getelementptr inbounds i8, i8* %1, i64 6144
%25 = icmp ult i64 %21, 2048
%26 = select i1 %25, i64 %21, i64 2048
%27 = tail call i32 @NF_Page_Read(i32 6144, i8* %24, i64 %26)
%28 = sub i64 %21, %26
%29 = icmp eq i64 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %23
%31 = getelementptr inbounds i8, i8* %1, i64 8192
%32 = tail call i32 @NF_Page_Read(i32 8192, i8* %31, i64 %28)
br label %33
33: ; preds = %3, %9, %16, %23, %30
ret void
}