void nand_read(unsigned char *buf, unsigned long start_addr,
               unsigned long size) {
  int i, j;
  do {
    (*(volatile unsigned long *)(0x4E000000 + 0x04)) &= (~(1 << 1));
  } while (0);
  for (i = start_addr; i < start_addr + size;) {
    do {
      (*(volatile unsigned long *)(0x4E000000 + 0x20)) |= (1 << 2);
    } while (0);
    do {
      (*(volatile unsigned char *)(0x4E000000 + 0x08)) = 0x00;
    } while (0);
    do {
      (*(volatile unsigned char *)(0x4E000000 + 0x0C)) = i & 0xFF;
    } while (0);
    do {
      (*(volatile unsigned char *)(0x4E000000 + 0x0C)) = (i >> 8) & 0xFF;
    } while (0);
    do {
      (*(volatile unsigned char *)(0x4E000000 + 0x0C)) = (i >> 11) & 0xFF;
    } while (0);
    do {
      (*(volatile unsigned char *)(0x4E000000 + 0x0C)) = (i >> 19) & 0xFF;
    } while (0);
    do {
      (*(volatile unsigned char *)(0x4E000000 + 0x0C)) = (i >> 27) & 0xFF;
    } while (0);
    do {
      (*(volatile unsigned char *)(0x4E000000 + 0x08)) = 0x30;
    } while (0);
    while (!((*(volatile unsigned long *)(0x4E000000 + 0x20)) & (1 << 2)))
      ;
    for (j = 0; j < (1 << 11); ++j, ++i) {
      *buf++ = ((*(volatile unsigned char *)(0x4E000000 + 0x10))) & 0xFF;
    }
  }
  do {
    (*(volatile unsigned long *)(0x4E000000 + 0x04)) |= (1 << 1);
  } while (0);
}


x86 assembly (clang, -O0):
<nand_read>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x4e000004,%eax
mov    (%rax),%rcx
and    $0xfffffffffffffffd,%rcx
mov    $0x4e000004,%eax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jae    126b <nand_read+0x16b>
jmp    1145 <nand_read+0x45>
mov    $0x4e000020,%eax
mov    (%rax),%rcx
or     $0x4,%rcx
mov    $0x4e000020,%eax
mov    %rcx,(%rax)
jmp    115e <nand_read+0x5e>
mov    $0x4e000008,%eax
movb   $0x0,(%rax)
jmp    116b <nand_read+0x6b>
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    $0x4e00000c,%eax
mov    %cl,(%rax)
jmp    1181 <nand_read+0x81>
mov    -0x1c(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    $0x4e00000c,%eax
mov    %cl,(%rax)
jmp    119a <nand_read+0x9a>
mov    -0x1c(%rbp),%eax
sar    $0xb,%eax
and    $0xff,%eax
mov    %al,%cl
mov    $0x4e00000c,%eax
mov    %cl,(%rax)
jmp    11b3 <nand_read+0xb3>
mov    -0x1c(%rbp),%eax
sar    $0x13,%eax
and    $0xff,%eax
mov    %al,%cl
mov    $0x4e00000c,%eax
mov    %cl,(%rax)
jmp    11cc <nand_read+0xcc>
mov    -0x1c(%rbp),%eax
sar    $0x1b,%eax
and    $0xff,%eax
mov    %al,%cl
mov    $0x4e00000c,%eax
mov    %cl,(%rax)
jmp    11e5 <nand_read+0xe5>
mov    $0x4e000008,%eax
movb   $0x30,(%rax)
jmp    11f2 <nand_read+0xf2>
mov    $0x4e000020,%eax
mov    (%rax),%rax
and    $0x4,%rax
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1214 <nand_read+0x114>
jmp    1219 <nand_read+0x119>
jmp    11f2 <nand_read+0xf2>
movl   $0x0,-0x20(%rbp)
cmpl   $0x800,-0x20(%rbp)
jge    1266 <nand_read+0x166>
mov    $0x4e000010,%eax
mov    (%rax),%al
movzbl %al,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1220 <nand_read+0x120>
jmp    112b <nand_read+0x2b>
jmp    1270 <nand_read+0x170>
mov    $0x4e000004,%eax
mov    (%rax),%rcx
or     $0x2,%rcx
mov    $0x4e000004,%eax
mov    %rcx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<nand_read>:
push   %rbx
andq   $0xfffffffffffffffd,0x4e000004
4e fd
add    %rsi,%rdx
movslq %esi,%rax
cmp    %rax,%rdx
jbe    1200 <nand_read+0x100>
mov    %esi,%ebx
nopl   0x0(%rax,%rax,1)
orq    $0x4,0x4e000020
4e 04
movb   $0x0,0x4e000008
mov    %sil,0x4e00000c
4e
mov    %bh,0x4e00000c
mov    %ebx,%ecx
shr    $0xb,%ecx
mov    %cl,0x4e00000c
mov    %ebx,%ecx
shr    $0x13,%ecx
mov    %cl,0x4e00000c
mov    %ebx,%ecx
sar    $0x1b,%ecx
mov    %cl,0x4e00000c
movb   $0x30,0x4e000008
30
nopl   0x0(%rax)
testq  $0x4,0x4e000020
je     1170 <nand_read+0x70>
xor    %ecx,%ecx
movzbl 0x4e000010,%eax
4e
mov    %al,(%rdi,%rcx,1)
movzbl 0x4e000010,%eax
4e
mov    %al,0x1(%rdi,%rcx,1)
movzbl 0x4e000010,%eax
4e
mov    %al,0x2(%rdi,%rcx,1)
movzbl 0x4e000010,%eax
4e
mov    %al,0x3(%rdi,%rcx,1)
movzbl 0x4e000010,%eax
4e
mov    %al,0x4(%rdi,%rcx,1)
movzbl 0x4e000010,%eax
4e
mov    %al,0x5(%rdi,%rcx,1)
movzbl 0x4e000010,%eax
4e
mov    %al,0x6(%rdi,%rcx,1)
movzbl 0x4e000010,%eax
4e
mov    %al,0x7(%rdi,%rcx,1)
add    $0x8,%rcx
cmp    $0x800,%ecx
jne    1180 <nand_read+0x80>
add    $0x800,%ebx
movslq %ebx,%r8
add    %rcx,%rdi
cmp    %r8,%rdx
ja     1120 <nand_read+0x20>
orq    $0x2,0x4e000004
4e 02
pop    %rbx
ret

LLVM IR (-O0):
define void @nand_read(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
br label %9
9: ; preds = %3
%10 = load volatile i64, i64* inttoptr (i64 1308622852 to i64*)
%11 = and i64 %10, -3
store volatile i64 %11, i64* inttoptr (i64 1308622852 to i64*)
br label %12
12: ; preds = %9
%13 = load i64, i64* %5
%14 = trunc i64 %13 to i32
store i32 %14, i32* %7
br label %15
15: ; preds = %82, %12
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = load i64, i64* %5
%19 = load i64, i64* %6
%20 = add i64 %18, %19
%21 = icmp ult i64 %17, %20
br i1 %21, label %22, label %83
22: ; preds = %15
br label %23
23: ; preds = %22
%24 = load volatile i64, i64* inttoptr (i64 1308622880 to i64*)
%25 = or i64 %24, 4
store volatile i64 %25, i64* inttoptr (i64 1308622880 to i64*)
br label %26
26: ; preds = %23
br label %27
27: ; preds = %26
store volatile i8 0, i8* inttoptr (i64 1308622856 to i8*)
br label %28
28: ; preds = %27
br label %29
29: ; preds = %28
%30 = load i32, i32* %7
%31 = and i32 %30, 255
%32 = trunc i32 %31 to i8
store volatile i8 %32, i8* inttoptr (i64 1308622860 to i8*)
br label %33
33: ; preds = %29
br label %34
34: ; preds = %33
%35 = load i32, i32* %7
%36 = ashr i32 %35, 8
%37 = and i32 %36, 255
%38 = trunc i32 %37 to i8
store volatile i8 %38, i8* inttoptr (i64 1308622860 to i8*)
br label %39
39: ; preds = %34
br label %40
40: ; preds = %39
%41 = load i32, i32* %7
%42 = ashr i32 %41, 11
%43 = and i32 %42, 255
%44 = trunc i32 %43 to i8
store volatile i8 %44, i8* inttoptr (i64 1308622860 to i8*)
br label %45
45: ; preds = %40
br label %46
46: ; preds = %45
%47 = load i32, i32* %7
%48 = ashr i32 %47, 19
%49 = and i32 %48, 255
%50 = trunc i32 %49 to i8
store volatile i8 %50, i8* inttoptr (i64 1308622860 to i8*)
br label %51
51: ; preds = %46
br label %52
52: ; preds = %51
%53 = load i32, i32* %7
%54 = ashr i32 %53, 27
%55 = and i32 %54, 255
%56 = trunc i32 %55 to i8
store volatile i8 %56, i8* inttoptr (i64 1308622860 to i8*)
br label %57
57: ; preds = %52
br label %58
58: ; preds = %57
store volatile i8 48, i8* inttoptr (i64 1308622856 to i8*)
br label %59
59: ; preds = %58
br label %60
60: ; preds = %65, %59
%61 = load volatile i64, i64* inttoptr (i64 1308622880 to i64*)
%62 = and i64 %61, 4
%63 = icmp ne i64 %62, 0
%64 = xor i1 %63, true
br i1 %64, label %65, label %66
65: ; preds = %60
br label %60
66: ; preds = %60
store i32 0, i32* %8
br label %67
67: ; preds = %77, %66
%68 = load i32, i32* %8
%69 = icmp slt i32 %68, 2048
br i1 %69, label %70, label %82
70: ; preds = %67
%71 = load volatile i8, i8* inttoptr (i64 1308622864 to i8*)
%72 = zext i8 %71 to i32
%73 = and i32 %72, 255
%74 = trunc i32 %73 to i8
%75 = load i8*, i8** %4
%76 = getelementptr inbounds i8, i8* %75, i32 1
store i8* %76, i8** %4
store i8 %74, i8* %75
br label %77
77: ; preds = %70
%78 = load i32, i32* %8
%79 = add nsw i32 %78, 1
store i32 %79, i32* %8
%80 = load i32, i32* %7
%81 = add nsw i32 %80, 1
store i32 %81, i32* %7
br label %67
82: ; preds = %67
br label %15
83: ; preds = %15
br label %84
84: ; preds = %83
%85 = load volatile i64, i64* inttoptr (i64 1308622852 to i64*)
%86 = or i64 %85, 2
store volatile i64 %86, i64* inttoptr (i64 1308622852 to i64*)
br label %87
87: ; preds = %84
ret void
}

LLVM IR (-O2):
define void @nand_read(i8* nocapture writeonly %0, i64 %1, i64 %2) {
%4 = load volatile i64, i64* inttoptr (i64 1308622852 to i64*)
%5 = and i64 %4, -3
store volatile i64 %5, i64* inttoptr (i64 1308622852 to i64*)
%6 = add i64 %2, %1
%7 = shl i64 %1, 32
%8 = ashr exact i64 %7, 32
%9 = icmp ugt i64 %6, %8
br i1 %9, label %10, label %55
10: ; preds = %3
%11 = trunc i64 %1 to i32
%12 = trunc i64 %1 to i8
br label %17
13: ; preds = %34
%14 = add i32 %18, 2048
%15 = sext i32 %14 to i64
%16 = icmp ugt i64 %6, %15
br i1 %16, label %17, label %55
17: ; preds = %10, %13
%18 = phi i32 [ %14, %13 ], [ %11, %10 ]
%19 = phi i8* [ %52, %13 ], [ %0, %10 ]
%20 = load volatile i64, i64* inttoptr (i64 1308622880 to i64*)
%21 = or i64 %20, 4
store volatile i64 %21, i64* inttoptr (i64 1308622880 to i64*)
store volatile i8 0, i8* inttoptr (i64 1308622856 to i8*)
store volatile i8 %12, i8* inttoptr (i64 1308622860 to i8*)
%22 = lshr i32 %18, 8
%23 = trunc i32 %22 to i8
store volatile i8 %23, i8* inttoptr (i64 1308622860 to i8*)
%24 = lshr i32 %18, 11
%25 = trunc i32 %24 to i8
store volatile i8 %25, i8* inttoptr (i64 1308622860 to i8*)
%26 = lshr i32 %18, 19
%27 = trunc i32 %26 to i8
store volatile i8 %27, i8* inttoptr (i64 1308622860 to i8*)
%28 = ashr i32 %18, 27
%29 = trunc i32 %28 to i8
store volatile i8 %29, i8* inttoptr (i64 1308622860 to i8*)
store volatile i8 48, i8* inttoptr (i64 1308622856 to i8*)
br label %30
30: ; preds = %30, %17
%31 = load volatile i64, i64* inttoptr (i64 1308622880 to i64*)
%32 = and i64 %31, 4
%33 = icmp eq i64 %32, 0
br i1 %33, label %30, label %34
34: ; preds = %30, %34
%35 = phi i32 [ %53, %34 ], [ 0, %30 ]
%36 = phi i8* [ %52, %34 ], [ %19, %30 ]
%37 = load volatile i8, i8* inttoptr (i64 1308622864 to i8*)
%38 = getelementptr inbounds i8, i8* %36, i64 1
store i8 %37, i8* %36
%39 = load volatile i8, i8* inttoptr (i64 1308622864 to i8*)
%40 = getelementptr inbounds i8, i8* %36, i64 2
store i8 %39, i8* %38
%41 = load volatile i8, i8* inttoptr (i64 1308622864 to i8*)
%42 = getelementptr inbounds i8, i8* %36, i64 3
store i8 %41, i8* %40
%43 = load volatile i8, i8* inttoptr (i64 1308622864 to i8*)
%44 = getelementptr inbounds i8, i8* %36, i64 4
store i8 %43, i8* %42
%45 = load volatile i8, i8* inttoptr (i64 1308622864 to i8*)
%46 = getelementptr inbounds i8, i8* %36, i64 5
store i8 %45, i8* %44
%47 = load volatile i8, i8* inttoptr (i64 1308622864 to i8*)
%48 = getelementptr inbounds i8, i8* %36, i64 6
store i8 %47, i8* %46
%49 = load volatile i8, i8* inttoptr (i64 1308622864 to i8*)
%50 = getelementptr inbounds i8, i8* %36, i64 7
store i8 %49, i8* %48
%51 = load volatile i8, i8* inttoptr (i64 1308622864 to i8*)
%52 = getelementptr inbounds i8, i8* %36, i64 8
store i8 %51, i8* %50
%53 = add nuw nsw i32 %35, 8
%54 = icmp eq i32 %53, 2048
br i1 %54, label %13, label %34
55: ; preds = %13, %3
%56 = load volatile i64, i64* inttoptr (i64 1308622852 to i64*)
%57 = or i64 %56, 2
store volatile i64 %57, i64* inttoptr (i64 1308622852 to i64*)
ret void
}