int nand_read_ll(unsigned char *buf, unsigned long start_addr, int size) {
  int i, j;
  if ((start_addr & (2048 - 1)) || (size & (2048 - 1))) {
    return -1;
  }
  ((*(volatile unsigned int *)(0x4E000000 + 0x04)) &= ~(1 << 1));
  for (i = start_addr; i < (start_addr + size);) {
    ((*(volatile unsigned char *)(0x4E000000 + 0x20)) |= (1 << 2));
    (*(volatile unsigned char *)(0x4E000000 + 0x08)) = 0;
    (*(volatile unsigned char *)(0x4E000000 + 0x0C)) = 0;
    (*(volatile unsigned char *)(0x4E000000 + 0x0C)) = 0;
    (*(volatile unsigned char *)(0x4E000000 + 0x0C)) = (i >> 11) & 0xff;
    (*(volatile unsigned char *)(0x4E000000 + 0x0C)) = (i >> 19) & 0xff;
    (*(volatile unsigned char *)(0x4E000000 + 0x0C)) = (i >> 27) & 0xff;
    (*(volatile unsigned char *)(0x4E000000 + 0x08)) = 0x30;
    {
      while (!((*(volatile unsigned char *)(0x4E000000 + 0x20)) & (1 << 2)))
        ;
    };
    for (j = 0; j < 2048; j++, i++) {
      *buf = ((*(volatile unsigned char *)(0x4E000000 + 0x10)) & 0xff);
      buf++;
    }
  }
  ((*(volatile unsigned int *)(0x4E000000 + 0x04)) |= (1 << 1));
  return 0;
}


x86 assembly (clang, -O0):
<nand_read_ll>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
and    $0x7ff,%rax
cmp    $0x0,%rax
jne    1134 <nand_read_ll+0x34>
mov    -0x1c(%rbp),%eax
and    $0x7ff,%eax
cmp    $0x0,%eax
je     1140 <nand_read_ll+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1274 <nand_read_ll+0x174>
mov    $0x4e000004,%eax
mov    (%rax),%ecx
and    $0xfffffffd,%ecx
mov    $0x4e000004,%eax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    125c <nand_read_ll+0x15c>
mov    $0x4e000020,%eax
mov    (%rax),%al
movzbl %al,%eax
or     $0x4,%eax
mov    %al,%cl
mov    $0x4e000020,%eax
mov    %cl,(%rax)
mov    $0x4e000008,%eax
movb   $0x0,(%rax)
mov    $0x4e00000c,%eax
movb   $0x0,(%rax)
mov    $0x4e00000c,%eax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%eax
sar    $0xb,%eax
and    $0xff,%eax
mov    %al,%cl
mov    $0x4e00000c,%eax
mov    %cl,(%rax)
mov    -0x20(%rbp),%eax
sar    $0x13,%eax
and    $0xff,%eax
mov    %al,%cl
mov    $0x4e00000c,%eax
mov    %cl,(%rax)
mov    -0x20(%rbp),%eax
sar    $0x1b,%eax
and    $0xff,%eax
mov    %al,%cl
mov    $0x4e00000c,%eax
mov    %cl,(%rax)
mov    $0x4e000008,%eax
movb   $0x30,(%rax)
mov    $0x4e000020,%eax
mov    (%rax),%al
movzbl %al,%eax
and    $0x4,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1204 <nand_read_ll+0x104>
jmp    1209 <nand_read_ll+0x109>
jmp    11e2 <nand_read_ll+0xe2>
movl   $0x0,-0x24(%rbp)
cmpl   $0x800,-0x24(%rbp)
jge    1257 <nand_read_ll+0x157>
mov    $0x4e000010,%eax
mov    (%rax),%al
movzbl %al,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1210 <nand_read_ll+0x110>
jmp    1158 <nand_read_ll+0x58>
mov    $0x4e000004,%eax
mov    (%rax),%ecx
or     $0x2,%ecx
mov    $0x4e000004,%eax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<nand_read_ll>:
mov    $0xffffffff,%eax
test   $0x7ff,%esi
jne    122a <nand_read_ll+0x12a>
mov    %edx,%ecx
and    $0x7ff,%ecx
jne    122a <nand_read_ll+0x12a>
andl   $0xfffffffd,0x4e000004
fd
movslq %edx,%rax
add    %rsi,%rax
movslq %esi,%rcx
cmp    %rcx,%rax
jbe    1220 <nand_read_ll+0x120>
nopl   0x0(%rax)
orb    $0x4,0x4e000020
04
movb   $0x0,0x4e000008
movb   $0x0,0x4e00000c
movb   $0x0,0x4e00000c
mov    %esi,%ecx
shr    $0xb,%ecx
mov    %cl,0x4e00000c
mov    %esi,%ecx
shr    $0x13,%ecx
mov    %cl,0x4e00000c
mov    %esi,%ecx
sar    $0x1b,%ecx
mov    %cl,0x4e00000c
movb   $0x30,0x4e000008
30
nopl   0x0(%rax)
testb  $0x4,0x4e000020
04
je     1190 <nand_read_ll+0x90>
xor    %ecx,%ecx
nopl   0x0(%rax)
movzbl 0x4e000010,%edx
4e
mov    %dl,(%rdi,%rcx,1)
movzbl 0x4e000010,%edx
4e
mov    %dl,0x1(%rdi,%rcx,1)
movzbl 0x4e000010,%edx
4e
mov    %dl,0x2(%rdi,%rcx,1)
movzbl 0x4e000010,%edx
4e
mov    %dl,0x3(%rdi,%rcx,1)
movzbl 0x4e000010,%edx
4e
mov    %dl,0x4(%rdi,%rcx,1)
movzbl 0x4e000010,%edx
4e
mov    %dl,0x5(%rdi,%rcx,1)
movzbl 0x4e000010,%edx
4e
mov    %dl,0x6(%rdi,%rcx,1)
movzbl 0x4e000010,%edx
4e
mov    %dl,0x7(%rdi,%rcx,1)
add    $0x8,%rcx
cmp    $0x800,%ecx
jne    11a0 <nand_read_ll+0xa0>
add    $0x800,%esi
movslq %esi,%rdx
add    %rcx,%rdi
cmp    %rdx,%rax
ja     1140 <nand_read_ll+0x40>
orl    $0x2,0x4e000004
02
xor    %eax,%eax
ret

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

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