void copy_nand_to_sdram(unsigned int start_addr, unsigned int *dest_addr,
                        unsigned int length) {
  unsigned int count = 0;
  unsigned int page = 0;
  unsigned int column = 0;
  unsigned int size = 0;
  page = start_addr / 4096;
  column = start_addr - page * 4096;
  (*(volatile unsigned int *)(0x70200004)) &= ~2;
  while (1) {
    size = 4096;
    if (page >= 0 && page < 4)
      size = 2048;
    if (column >= size)
      ++page;
    (*(volatile unsigned int *)(0x70200008)) = 0;
    (*(volatile unsigned int *)(0x7020000C)) = column & 0xFF;
    (*(volatile unsigned int *)(0x7020000C)) = (column >> 8) & 0x1F;
    (*(volatile unsigned int *)(0x7020000C)) = page & 0xFF;
    (*(volatile unsigned int *)(0x7020000C)) = (page >> 8) & 0xFF;
    (*(volatile unsigned int *)(0x7020000C)) = (page >> 16) & 0xFF;
    (*(volatile unsigned int *)(0x70200008)) = 0x30;
    while (1) {
      if ((*(volatile unsigned int *)(0x70200028)) & 1)
        break;
    }
    for (int i = column; i < size && count < length; i += 4) {
      *dest_addr++ = (*(volatile unsigned int *)(0x70200010));
      count += 4;
    }
    if (count >= length)
      break;
    ++page;
    column = 0;
  }
  (*(volatile unsigned int *)(0x70200004)) |= 2;
  return;
}


x86 assembly (clang, -O0):
<copy_nand_to_sdram>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x4(%rbp),%eax
shr    $0xc,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shl    $0xc,%ecx
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    $0x70200004,%eax
mov    (%rax),%ecx
and    $0xfffffffd,%ecx
mov    $0x70200004,%eax
mov    %ecx,(%rax)
movl   $0x1000,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jb     1174 <copy_nand_to_sdram+0x74>
cmpl   $0x4,-0x1c(%rbp)
jae    1174 <copy_nand_to_sdram+0x74>
movl   $0x800,-0x24(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jb     1189 <copy_nand_to_sdram+0x89>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x70200008,%eax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%ecx
and    $0xff,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%ecx
shr    $0x8,%ecx
and    $0x1f,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%ecx
and    $0xff,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%ecx
shr    $0x8,%ecx
and    $0xff,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%ecx
shr    $0x10,%ecx
and    $0xff,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    $0x70200008,%eax
movl   $0x30,(%rax)
mov    $0x70200028,%eax
mov    (%rax),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     120d <copy_nand_to_sdram+0x10d>
jmp    1212 <copy_nand_to_sdram+0x112>
jmp    11f5 <copy_nand_to_sdram+0xf5>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
xor    %eax,%eax
cmp    -0x24(%rbp),%ecx
mov    %al,-0x29(%rbp)
jae    1235 <copy_nand_to_sdram+0x135>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
setb   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1245 <copy_nand_to_sdram+0x145>
jmp    1274 <copy_nand_to_sdram+0x174>
mov    $0x70200010,%eax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x18(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x28(%rbp)
jmp    1218 <copy_nand_to_sdram+0x118>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     1285 <copy_nand_to_sdram+0x185>
jmp    129a <copy_nand_to_sdram+0x19a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
jmp    1152 <copy_nand_to_sdram+0x52>
mov    $0x70200004,%eax
mov    (%rax),%ecx
or     $0x2,%ecx
mov    $0x70200004,%eax
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_nand_to_sdram>:
push   %rbx
mov    %edi,%r8d
shr    $0xc,%r8d
and    $0xfff,%edi
andl   $0xfffffffd,0x70200004
fd
xor    %ecx,%ecx
jmp    112e <copy_nand_to_sdram+0x2e>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r8d
xor    %edi,%edi
test   %bl,%bl
je     11f9 <copy_nand_to_sdram+0xf9>
xor    %eax,%eax
cmp    $0x4,%r8d
setae  %al
shl    $0xb,%eax
add    $0x800,%eax
cmp    %eax,%edi
sbb    $0xffffffff,%r8d
movl   $0x0,0x70200008
movzbl %dil,%ebx
mov    %ebx,0x7020000c
mov    %edi,%ebx
shr    $0x8,%ebx
mov    %ebx,0x7020000c
movzbl %r8b,%ebx
mov    %ebx,0x7020000c
mov    %r8d,%ebx
movzbl %bh,%ebx
mov    %ebx,0x7020000c
mov    %r8d,%ebx
shr    $0x10,%ebx
movzbl %bl,%ebx
mov    %ebx,0x7020000c
movl   $0x30,0x70200008
nopw   0x0(%rax,%rax,1)
testl  $0x1,0x70200028
je     11a0 <copy_nand_to_sdram+0xa0>
cmp    %edx,%ecx
setb   %bl
cmp    %eax,%edi
jae    1120 <copy_nand_to_sdram+0x20>
cmp    %edx,%ecx
jae    1120 <copy_nand_to_sdram+0x20>
add    $0x4,%edi
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x70200010,%ebx
mov    %ebx,(%rsi)
add    $0x4,%rsi
add    $0x4,%ecx
cmp    %edx,%ecx
setb   %bl
cmp    %eax,%edi
jae    1120 <copy_nand_to_sdram+0x20>
add    $0x4,%edi
cmp    %edx,%ecx
jb     11d0 <copy_nand_to_sdram+0xd0>
jmp    1120 <copy_nand_to_sdram+0x20>
orl    $0x2,0x70200004
pop    %rbx
ret

LLVM IR (-O0):
define void @copy_nand_to_sdram(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%12 = load i32, i32* %4
%13 = udiv i32 %12, 4096
store i32 %13, i32* %8
%14 = load i32, i32* %4
%15 = load i32, i32* %8
%16 = mul i32 %15, 4096
%17 = sub i32 %14, %16
store i32 %17, i32* %9
%18 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%19 = and i32 %18, -3
store volatile i32 %19, i32* inttoptr (i64 1881145348 to i32*)
br label %20
20: ; preds = %3, %80
store i32 4096, i32* %10
%21 = load i32, i32* %8
%22 = icmp uge i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* %8
%25 = icmp ult i32 %24, 4
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 2048, i32* %10
br label %27
27: ; preds = %26, %23, %20
%28 = load i32, i32* %9
%29 = load i32, i32* %10
%30 = icmp uge i32 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* %8
%33 = add i32 %32, 1
store i32 %33, i32* %8
br label %34
34: ; preds = %31, %27
store volatile i32 0, i32* inttoptr (i64 1881145352 to i32*)
%35 = load i32, i32* %9
%36 = and i32 %35, 255
store volatile i32 %36, i32* inttoptr (i64 1881145356 to i32*)
%37 = load i32, i32* %9
%38 = lshr i32 %37, 8
%39 = and i32 %38, 31
store volatile i32 %39, i32* inttoptr (i64 1881145356 to i32*)
%40 = load i32, i32* %8
%41 = and i32 %40, 255
store volatile i32 %41, i32* inttoptr (i64 1881145356 to i32*)
%42 = load i32, i32* %8
%43 = lshr i32 %42, 8
%44 = and i32 %43, 255
store volatile i32 %44, i32* inttoptr (i64 1881145356 to i32*)
%45 = load i32, i32* %8
%46 = lshr i32 %45, 16
%47 = and i32 %46, 255
store volatile i32 %47, i32* inttoptr (i64 1881145356 to i32*)
store volatile i32 48, i32* inttoptr (i64 1881145352 to i32*)
br label %48
48: ; preds = %34, %53
%49 = load volatile i32, i32* inttoptr (i64 1881145384 to i32*)
%50 = and i32 %49, 1
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %48
br label %54
53: ; preds = %48
br label %48
54: ; preds = %52
%55 = load i32, i32* %9
store i32 %55, i32* %11
br label %56
56: ; preds = %72, %54
%57 = load i32, i32* %11
%58 = load i32, i32* %10
%59 = icmp ult i32 %57, %58
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = load i32, i32* %7
%62 = load i32, i32* %6
%63 = icmp ult i32 %61, %62
br label %64
64: ; preds = %60, %56
%65 = phi i1 [ false, %56 ], [ %63, %60 ]
br i1 %65, label %66, label %75
66: ; preds = %64
%67 = load volatile i32, i32* inttoptr (i64 1881145360 to i32*)
%68 = load i32*, i32** %5
%69 = getelementptr inbounds i32, i32* %68, i32 1
store i32* %69, i32** %5
store i32 %67, i32* %68
%70 = load i32, i32* %7
%71 = add i32 %70, 4
store i32 %71, i32* %7
br label %72
72: ; preds = %66
%73 = load i32, i32* %11
%74 = add nsw i32 %73, 4
store i32 %74, i32* %11
br label %56
75: ; preds = %64
%76 = load i32, i32* %7
%77 = load i32, i32* %6
%78 = icmp uge i32 %76, %77
br i1 %78, label %79, label %80
79: ; preds = %75
br label %83
80: ; preds = %75
%81 = load i32, i32* %8
%82 = add i32 %81, 1
store i32 %82, i32* %8
store i32 0, i32* %9
br label %20
83: ; preds = %79
%84 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%85 = or i32 %84, 2
store volatile i32 %85, i32* inttoptr (i64 1881145348 to i32*)
ret void
}

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