#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*/ uart_print_addr(unsigned int);
int /*<<< orphan*/ uart_print_hex(unsigned int);
int /*<<< orphan*/ uart_printf(char *);
int /*<<< orphan*/ uart_puts(char *);
void nand_read(unsigned int start_addr, unsigned int end_addr) {
  unsigned int length = end_addr - start_addr;
  unsigned int count = 0;
  unsigned int page = 0;
  unsigned int column = 0;
  unsigned int data = 0;
  (*(volatile unsigned int *)(0x70200004)) &= ~2;
  while (1) {
    page = start_addr / 4096;
    column = start_addr - page * 4096;
    uart_puts("");
    uart_printf("start_addr = ");
    uart_print_hex(start_addr);
    uart_printf("  page = ");
    uart_print_hex(page);
    uart_printf("  column = ");
    uart_print_hex(column);
    uart_puts("");
    (*(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, j = 0; i < 4096 && count < length; i += 4, ++j) {
      data = (*(volatile unsigned int *)(0x70200010));
      if (j % 4 == 0) {
        uart_puts("");
        uart_print_hex(start_addr);
        uart_printf(" : ");
      }
      uart_print_addr(data);
      uart_printf("  ");
      start_addr += 4;
      count += 4;
    }
    uart_puts("");
    if (count >= length)
      break;
  }
  (*(volatile unsigned int *)(0x70200004)) |= 2;
  return;
}


x86 assembly (clang, -O0):
<nand_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x70200004,%eax
mov    (%rax),%ecx
and    $0xfffffffd,%ecx
mov    $0x70200004,%eax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
shr    $0xc,%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    $0xc,%ecx
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
lea    0xe88(%rip),%rdi
call   1060 <uart_puts@plt>
lea    0xe52(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x4(%rbp),%edi
call   1050 <uart_print_hex@plt>
lea    0xe4c(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <uart_print_hex@plt>
lea    0xe42(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <uart_print_hex@plt>
lea    0xe40(%rip),%rdi
call   1060 <uart_puts@plt>
mov    $0x70200008,%eax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%ecx
and    $0xff,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%ecx
shr    $0x8,%ecx
and    $0x1f,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
and    $0xff,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
shr    $0x8,%ecx
and    $0xff,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x14(%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     1273 <nand_read+0x133>
jmp    1278 <nand_read+0x138>
jmp    125b <nand_read+0x11b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x1000,-0x20(%rbp)
mov    %al,-0x25(%rbp)
jge    12a3 <nand_read+0x163>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
setb   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    12b3 <nand_read+0x173>
jmp    132e <nand_read+0x1ee>
mov    $0x70200010,%eax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12f1 <nand_read+0x1b1>
lea    0xd52(%rip),%rdi
call   1060 <uart_puts@plt>
mov    -0x4(%rbp),%edi
call   1050 <uart_print_hex@plt>
lea    0xd38(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <uart_print_addr@plt>
lea    0xd28(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x4(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1285 <nand_read+0x145>
lea    0xcf5(%rip),%rdi
call   1060 <uart_puts@plt>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jb     134b <nand_read+0x20b>
jmp    1350 <nand_read+0x210>
jmp    1184 <nand_read+0x44>
mov    $0x70200004,%eax
mov    (%rax),%ecx
or     $0x2,%ecx
mov    $0x70200004,%eax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nand_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r13d
andl   $0xfffffffd,0x70200004
fd
sub    %edi,%r14d
xor    %r15d,%r15d
lea    0xec4(%rip),%r12
jmp    1188 <nand_read+0x48>
nopl   0x0(%rax,%rax,1)
lea    0xeb3(%rip),%r12
mov    %r12,%rdi
call   1060 <uart_puts@plt>
cmp    %r14d,%r15d
jae    12d1 <nand_read+0x191>
mov    %r13d,%ebx
shr    $0xc,%ebx
mov    %r13d,%ebp
and    $0xfff,%ebp
mov    %r12,%rdi
call   1060 <uart_puts@plt>
lea    0xe5a(%rip),%rdi
call   1030 <uart_printf@plt>
mov    %r13d,%edi
call   1050 <uart_print_hex@plt>
lea    0xe54(%rip),%rdi
call   1030 <uart_printf@plt>
mov    %ebx,%edi
call   1050 <uart_print_hex@plt>
lea    0xe4b(%rip),%rdi
call   1030 <uart_printf@plt>
mov    %ebp,%edi
call   1050 <uart_print_hex@plt>
mov    %r12,%rdi
call   1060 <uart_puts@plt>
movl   $0x0,0x70200008
movzbl %r13b,%eax
mov    %eax,0x7020000c
mov    %ebp,%eax
shr    $0x8,%eax
mov    %eax,0x7020000c
movzbl %bl,%eax
mov    %eax,0x7020000c
mov    %r13d,%eax
shr    $0x14,%eax
movzbl %al,%eax
mov    %eax,0x7020000c
mov    %r13d,%eax
shr    $0x1c,%eax
mov    %eax,0x7020000c
movl   $0x30,0x70200008
cs nopw 0x0(%rax,%rax,1)
nop
testl  $0x1,0x70200028
je     1240 <nand_read+0x100>
cmp    %r14d,%r15d
jae    12c5 <nand_read+0x185>
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x70200010,%ebx
test   $0x3,%r12b
jne    128d <nand_read+0x14d>
lea    0xdb6(%rip),%rdi
call   1060 <uart_puts@plt>
mov    %r13d,%edi
call   1050 <uart_print_hex@plt>
lea    0xd9c(%rip),%rdi
call   1030 <uart_printf@plt>
mov    %ebx,%edi
call   1040 <uart_print_addr@plt>
lea    0xd8d(%rip),%rdi
call   1030 <uart_printf@plt>
add    $0x4,%r13d
add    $0x4,%r15d
cmp    $0xffb,%ebp
ja     1170 <nand_read+0x30>
add    $0x4,%ebp
add    $0x1,%r12d
cmp    %r14d,%r15d
jb     1260 <nand_read+0x120>
jmp    1170 <nand_read+0x30>
lea    0xd5e(%rip),%rdi
call   1060 <uart_puts@plt>
orl    $0x2,0x70200004
02
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [14 x i8] c"start_addr = \00"
@.str.2 = [10 x i8] c" page = \00"
@.str.3 = [12 x i8] c" column = \00"
@.str.4 = [4 x i8] c" : \00"
@.str.5 = [3 x i8] c" \00"
define void @nand_read(i32 %0, i32 %1) {
%3 = alloca i32
%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* %3
store i32 %1, i32* %4
%12 = load i32, i32* %4
%13 = load i32, i32* %3
%14 = sub i32 %12, %13
store i32 %14, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%15 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%16 = and i32 %15, -3
store volatile i32 %16, i32* inttoptr (i64 1881145348 to i32*)
br label %17
17: ; preds = %2, %94
%18 = load i32, i32* %3
%19 = udiv i32 %18, 4096
store i32 %19, i32* %7
%20 = load i32, i32* %3
%21 = load i32, i32* %7
%22 = mul i32 %21, 4096
%23 = sub i32 %20, %22
store i32 %23, i32* %8
%24 = call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @uart_printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* %3
%27 = call i32 @uart_print_hex(i32 %26)
%28 = call i32 @uart_printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* %7
%30 = call i32 @uart_print_hex(i32 %29)
%31 = call i32 @uart_printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32, i32* %8
%33 = call i32 @uart_print_hex(i32 %32)
%34 = call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store volatile i32 0, i32* inttoptr (i64 1881145352 to i32*)
%35 = load i32, i32* %8
%36 = and i32 %35, 255
store volatile i32 %36, i32* inttoptr (i64 1881145356 to i32*)
%37 = load i32, i32* %8
%38 = lshr i32 %37, 8
%39 = and i32 %38, 31
store volatile i32 %39, i32* inttoptr (i64 1881145356 to i32*)
%40 = load i32, i32* %7
%41 = and i32 %40, 255
store volatile i32 %41, i32* inttoptr (i64 1881145356 to i32*)
%42 = load i32, i32* %7
%43 = lshr i32 %42, 8
%44 = and i32 %43, 255
store volatile i32 %44, i32* inttoptr (i64 1881145356 to i32*)
%45 = load i32, i32* %7
%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 = %17, %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* %8
store i32 %55, i32* %10
store i32 0, i32* %11
br label %56
56: ; preds = %83, %54
%57 = load i32, i32* %10
%58 = icmp slt i32 %57, 4096
br i1 %58, label %59, label %63
59: ; preds = %56
%60 = load i32, i32* %6
%61 = load i32, i32* %5
%62 = icmp ult i32 %60, %61
br label %63
63: ; preds = %59, %56
%64 = phi i1 [ false, %56 ], [ %62, %59 ]
br i1 %64, label %65, label %88
65: ; preds = %63
%66 = load volatile i32, i32* inttoptr (i64 1881145360 to i32*)
store i32 %66, i32* %9
%67 = load i32, i32* %11
%68 = srem i32 %67, 4
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %75
70: ; preds = %65
%71 = call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%72 = load i32, i32* %3
%73 = call i32 @uart_print_hex(i32 %72)
%74 = call i32 @uart_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
br label %75
75: ; preds = %70, %65
%76 = load i32, i32* %9
%77 = call i32 @uart_print_addr(i32 %76)
%78 = call i32 @uart_printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%79 = load i32, i32* %3
%80 = add i32 %79, 4
store i32 %80, i32* %3
%81 = load i32, i32* %6
%82 = add i32 %81, 4
store i32 %82, i32* %6
br label %83
83: ; preds = %75
%84 = load i32, i32* %10
%85 = add nsw i32 %84, 4
store i32 %85, i32* %10
%86 = load i32, i32* %11
%87 = add nsw i32 %86, 1
store i32 %87, i32* %11
br label %56
88: ; preds = %63
%89 = call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%90 = load i32, i32* %6
%91 = load i32, i32* %5
%92 = icmp uge i32 %90, %91
br i1 %92, label %93, label %94
93: ; preds = %88
br label %95
94: ; preds = %88
br label %17
95: ; preds = %93
%96 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%97 = or i32 %96, 2
store volatile i32 %97, i32* inttoptr (i64 1881145348 to i32*)
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [14 x i8] c"start_addr = \00"
@.str.2 = [10 x i8] c" page = \00"
@.str.3 = [12 x i8] c" column = \00"
@.str.4 = [4 x i8] c" : \00"
@.str.5 = [3 x i8] c" \00"
define void @nand_read(i32 %0, i32 %1) {
%3 = sub i32 %1, %0
%4 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%5 = and i32 %4, -3
store volatile i32 %5, i32* inttoptr (i64 1881145348 to i32*)
br label %6
6: ; preds = %33, %2
%7 = phi i32 [ %0, %2 ], [ %50, %33 ]
%8 = phi i32 [ 0, %2 ], [ %51, %33 ]
%9 = lshr i32 %7, 12
%10 = and i32 %7, 4095
%11 = tail call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @uart_printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @uart_print_hex(i32 %7)
%14 = tail call i32 @uart_printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @uart_print_hex(i32 %9)
%16 = tail call i32 @uart_printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @uart_print_hex(i32 %10)
%18 = tail call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store volatile i32 0, i32* inttoptr (i64 1881145352 to i32*)
%19 = and i32 %7, 255
store volatile i32 %19, i32* inttoptr (i64 1881145356 to i32*)
%20 = lshr i32 %10, 8
store volatile i32 %20, i32* inttoptr (i64 1881145356 to i32*)
%21 = and i32 %9, 255
store volatile i32 %21, i32* inttoptr (i64 1881145356 to i32*)
%22 = lshr i32 %7, 20
%23 = and i32 %22, 255
store volatile i32 %23, i32* inttoptr (i64 1881145356 to i32*)
%24 = lshr i32 %7, 28
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, %6
%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 %8, %3
br i1 %30, label %35, label %31
31: ; preds = %29
%32 = tail call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %57
33: ; preds = %47
%34 = tail call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br i1 %55, label %6, label %57
35: ; preds = %29, %47
%36 = phi i32 [ %53, %47 ], [ 0, %29 ]
%37 = phi i32 [ %52, %47 ], [ %10, %29 ]
%38 = phi i32 [ %51, %47 ], [ %8, %29 ]
%39 = phi i32 [ %50, %47 ], [ %7, %29 ]
%40 = load volatile i32, i32* inttoptr (i64 1881145360 to i32*)
%41 = and i32 %36, 3
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = tail call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%45 = tail call i32 @uart_print_hex(i32 %39)
%46 = tail call i32 @uart_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %43, %35
%48 = tail call i32 @uart_print_addr(i32 %40)
%49 = tail call i32 @uart_printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%50 = add i32 %39, 4
%51 = add i32 %38, 4
%52 = add nuw nsw i32 %37, 4
%53 = add nuw nsw i32 %36, 1
%54 = icmp ult i32 %37, 4092
%55 = icmp ult i32 %51, %3
%56 = select i1 %54, i1 %55, i1 false
br i1 %56, label %35, label %33
57: ; preds = %33, %31
%58 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%59 = or i32 %58, 2
store volatile i32 %59, i32* inttoptr (i64 1881145348 to i32*)
ret void
}