#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_PageRead(int, unsigned char *);
void nand_to_ram(unsigned long start_addr, unsigned char *sdram_addr,
                 int size) {
  int i;
  unsigned int page_shift = 12;
  for (i = 0; i < 4; i++, sdram_addr += 2048) {
    NF_PageRead(i, sdram_addr);
  }
  size -= 1024 * 8;
  for (i = 4; size > 0;) {
    NF_PageRead(i, sdram_addr);
    size -= 4096;
    sdram_addr += 4096;
    i++;
  }
}


x86 assembly (clang, -O0):
<nand_to_ram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xc,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jge    1163 <nand_to_ram+0x53>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <NF_PageRead@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x800,%rax
mov    %rax,-0x10(%rbp)
jmp    1131 <nand_to_ram+0x21>
mov    -0x14(%rbp),%eax
sub    $0x2000,%eax
mov    %eax,-0x14(%rbp)
movl   $0x4,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11b2 <nand_to_ram+0xa2>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <NF_PageRead@plt>
mov    -0x14(%rbp),%eax
sub    $0x1000,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1175 <nand_to_ram+0x65>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nand_to_ram>:
push   %r15
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
xor    %edi,%edi
call   1030 <NF_PageRead@plt>
lea    0x800(%r15),%rsi
mov    $0x1,%edi
call   1030 <NF_PageRead@plt>
lea    0x1000(%r15),%rsi
mov    $0x2,%edi
call   1030 <NF_PageRead@plt>
lea    0x1800(%r15),%rsi
mov    $0x3,%edi
call   1030 <NF_PageRead@plt>
cmp    $0x2001,%r14d
jl     11a4 <nand_to_ram+0x94>
add    $0x2000,%r15
add    $0xffffdfff,%r14d
shr    $0xc,%r14d
not    %r14d
mov    $0x4,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
call   1030 <NF_PageRead@plt>
add    $0x1000,%r15
lea    (%r14,%rbx,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x4,%eax
jne    1180 <nand_to_ram+0x70>
pop    %rbx
pop    %r14
pop    %r15
ret

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

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