#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_hex(unsigned int);
int /*<<< orphan*/ uart_printf(char *);
int /*<<< orphan*/ uart_puts(char *);
void write_to_nand(unsigned int src, unsigned int dest, unsigned int length) {
  unsigned int count = 0;
  unsigned int page = dest / 4096;
  unsigned int column = dest - page * 4096;
  (*(volatile unsigned int *)(0x70200004)) &= ~2;
  while (1) {
    uart_printf("\npage = ");
    uart_print_hex(page);
    uart_puts("");
    (*(volatile unsigned int *)(0x70200008)) = 0x80;
    (*(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;
    for (int i = column; i < 4096 && count < length; i += 4) {
      (*(volatile unsigned int *)(0x70200010)) = *(unsigned int *)src;
      src += 4;
      count += 4;
    }
    (*(volatile unsigned int *)(0x70200008)) = 0x10;
    while (1) {
      if ((*(volatile unsigned int *)(0x70200028)) & 1)
        break;
    }
    if (count >= length)
      break;
    ++page;
  }
  (*(volatile unsigned int *)(0x70200004)) |= 2;
  return;
}


x86 assembly (clang, -O0):
<write_to_nand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%eax
shr    $0xc,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    $0xc,%ecx
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    $0x70200004,%eax
mov    (%rax),%ecx
and    $0xfffffffd,%ecx
mov    $0x70200004,%eax
mov    %ecx,(%rax)
lea    0xe89(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <uart_print_hex@plt>
lea    0xe7d(%rip),%rdi
call   1050 <uart_puts@plt>
mov    $0x70200008,%eax
movl   $0x80,(%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    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x1000,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jge    1215 <write_to_nand+0xe5>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
setb   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1225 <write_to_nand+0xf5>
jmp    1251 <write_to_nand+0x121>
mov    -0x4(%rbp),%eax
mov    (%rax),%ecx
mov    $0x70200010,%eax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f7 <write_to_nand+0xc7>
mov    $0x70200008,%eax
movl   $0x10,(%rax)
mov    $0x70200028,%eax
mov    (%rax),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1274 <write_to_nand+0x144>
jmp    1279 <write_to_nand+0x149>
jmp    125c <write_to_nand+0x12c>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jb     128a <write_to_nand+0x15a>
jmp    1298 <write_to_nand+0x168>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1170 <write_to_nand+0x40>
mov    $0x70200004,%eax
mov    (%rax),%ecx
or     $0x2,%ecx
mov    $0x70200004,%eax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_nand>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %edi,%ebp
mov    %esi,%r12d
movzbl %sil,%r13d
mov    %esi,%edx
shr    $0xc,%edx
and    $0xfff,%r12d
andl   $0xfffffffd,0x70200004
fd
mov    %r12d,0x4(%rsp)
shr    $0x8,%r12d
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
lea    0xe89(%rip),%rdi
mov    %edx,%r14d
call   1030 <uart_printf@plt>
mov    %r14d,%edi
call   1040 <uart_print_hex@plt>
lea    0xe7a(%rip),%rdi
call   1050 <uart_puts@plt>
mov    %r14d,%edx
movl   $0x80,0x70200008
mov    %r13d,0x7020000c
70
mov    %r12d,0x7020000c
70
movzbl %dl,%eax
mov    %eax,0x7020000c
movzbl %dh,%eax
mov    %eax,0x7020000c
mov    %r14d,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x7020000c
cmp    %ebx,%r15d
jae    1210 <write_to_nand+0xe0>
mov    0x4(%rsp),%ecx
xchg   %ax,%ax
mov    %ebp,%eax
mov    (%rax),%eax
mov    %eax,0x70200010
add    $0x4,%ebp
add    $0x4,%r15d
cmp    %ebx,%r15d
setb   %al
cmp    $0xffb,%ecx
ja     1212 <write_to_nand+0xe2>
add    $0x4,%ecx
cmp    %ebx,%r15d
jb     11e0 <write_to_nand+0xb0>
jmp    1212 <write_to_nand+0xe2>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
movl   $0x10,0x70200008
nopl   (%rax)
testl  $0x1,0x70200028
je     1220 <write_to_nand+0xf0>
add    $0x1,%edx
test   %al,%al
jne    1170 <write_to_nand+0x40>
orl    $0x2,0x70200004
02
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"\0Apage = \00"
@.str.1 = [1 x i8] zeroinitializer
define void @write_to_nand(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
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%11 = load i32, i32* %5
%12 = udiv i32 %11, 4096
store i32 %12, i32* %8
%13 = load i32, i32* %5
%14 = load i32, i32* %8
%15 = mul i32 %14, 4096
%16 = sub i32 %13, %15
store i32 %16, i32* %9
%17 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%18 = and i32 %17, -3
store volatile i32 %18, i32* inttoptr (i64 1881145348 to i32*)
br label %19
19: ; preds = %3, %71
%20 = call i32 @uart_printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* %8
%22 = call i32 @uart_print_hex(i32 %21)
%23 = call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store volatile i32 128, i32* inttoptr (i64 1881145352 to i32*)
%24 = load i32, i32* %9
%25 = and i32 %24, 255
store volatile i32 %25, i32* inttoptr (i64 1881145356 to i32*)
%26 = load i32, i32* %9
%27 = lshr i32 %26, 8
%28 = and i32 %27, 31
store volatile i32 %28, i32* inttoptr (i64 1881145356 to i32*)
%29 = load i32, i32* %8
%30 = and i32 %29, 255
store volatile i32 %30, i32* inttoptr (i64 1881145356 to i32*)
%31 = load i32, i32* %8
%32 = lshr i32 %31, 8
%33 = and i32 %32, 255
store volatile i32 %33, i32* inttoptr (i64 1881145356 to i32*)
%34 = load i32, i32* %8
%35 = lshr i32 %34, 16
%36 = and i32 %35, 255
store volatile i32 %36, i32* inttoptr (i64 1881145356 to i32*)
%37 = load i32, i32* %9
store i32 %37, i32* %10
br label %38
38: ; preds = %56, %19
%39 = load i32, i32* %10
%40 = icmp slt i32 %39, 4096
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = load i32, i32* %7
%43 = load i32, i32* %6
%44 = icmp ult i32 %42, %43
br label %45
45: ; preds = %41, %38
%46 = phi i1 [ false, %38 ], [ %44, %41 ]
br i1 %46, label %47, label %59
47: ; preds = %45
%48 = load i32, i32* %4
%49 = zext i32 %48 to i64
%50 = inttoptr i64 %49 to i32*
%51 = load i32, i32* %50
store volatile i32 %51, i32* inttoptr (i64 1881145360 to i32*)
%52 = load i32, i32* %4
%53 = add i32 %52, 4
store i32 %53, i32* %4
%54 = load i32, i32* %7
%55 = add i32 %54, 4
store i32 %55, i32* %7
br label %56
56: ; preds = %47
%57 = load i32, i32* %10
%58 = add nsw i32 %57, 4
store i32 %58, i32* %10
br label %38
59: ; preds = %45
store volatile i32 16, i32* inttoptr (i64 1881145352 to i32*)
br label %60
60: ; preds = %59, %65
%61 = load volatile i32, i32* inttoptr (i64 1881145384 to i32*)
%62 = and i32 %61, 1
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %60
br label %66
65: ; preds = %60
br label %60
66: ; preds = %64
%67 = load i32, i32* %7
%68 = load i32, i32* %6
%69 = icmp uge i32 %67, %68
br i1 %69, label %70, label %71
70: ; preds = %66
br label %74
71: ; preds = %66
%72 = load i32, i32* %8
%73 = add i32 %72, 1
store i32 %73, i32* %8
br label %19
74: ; preds = %70
%75 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%76 = or i32 %75, 2
store volatile i32 %76, i32* inttoptr (i64 1881145348 to i32*)
ret void
}

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