#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*/ UART0_SendString(char *);
int /*<<< orphan*/ nand_wait_idle();
int /*<<< orphan*/ nand_write_address(unsigned long);
void nand_write_page(unsigned char *buffer, int size,
                     unsigned long start_addr) {
  unsigned long addr = 0;
  unsigned char data = 0;
  int index = 0;
  if ((start_addr & (2048 - 1)) || (size & (2048 - 1))) {
    return;
  }
  ((*(volatile unsigned long *)(0x70200000 + 0x04)) &= ~(1 << 1));
  for (addr = start_addr; addr < (start_addr + size); addr += 2048) {
    (*(volatile unsigned long *)(0x70200000 + 0x08)) = 0x80;
    nand_write_address(addr);
    for (index = 0; index < 2048; index++)
      (*(volatile unsigned char *)(0x70200000 + 0x10)) = *buffer++;
    (*(volatile unsigned long *)(0x70200000 + 0x08)) = 0x10;
    nand_wait_idle();
    (*(volatile unsigned long *)(0x70200000 + 0x08)) = 0x70;
    data = (*(volatile unsigned char *)(0x70200000 + 0x10));
    if (data & 0x1)
      UART0_SendString("\n\rError in Program\n\r");
  }
  ((*(volatile unsigned long *)(0x70200000 + 0x04)) |= (1 << 1));
}


x86 assembly (clang, -O0):
<nand_write_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
and    $0x7ff,%rax
cmp    $0x0,%rax
jne    117b <nand_write_page+0x4b>
mov    -0xc(%rbp),%eax
and    $0x7ff,%eax
cmp    $0x0,%eax
je     1180 <nand_write_page+0x50>
jmp    1274 <nand_write_page+0x144>
mov    $0x70200004,%eax
mov    (%rax),%rcx
and    $0xfffffffffffffffd,%rcx
mov    $0x70200004,%eax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0xc(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1260 <nand_write_page+0x130>
mov    $0x70200008,%eax
movq   $0x80,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <nand_write_address@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0x800,-0x28(%rbp)
jge    1203 <nand_write_page+0xd3>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    $0x70200010,%eax
mov    %cl,(%rax)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11d0 <nand_write_page+0xa0>
mov    $0x70200008,%eax
movq   $0x10,(%rax)
mov    $0x0,%al
call   1030 <nand_wait_idle@plt>
mov    $0x70200008,%eax
movq   $0x70,(%rax)
mov    $0x70200010,%eax
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movzbl -0x21(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1248 <nand_write_page+0x118>
lea    0xdbd(%rip),%rdi
call   1050 <UART0_SendString@plt>
jmp    124d <nand_write_page+0x11d>
mov    -0x20(%rbp),%rax
add    $0x800,%rax
mov    %rax,-0x20(%rbp)
jmp    119c <nand_write_page+0x6c>
mov    $0x70200004,%eax
mov    (%rax),%rcx
or     $0x2,%rcx
mov    $0x70200004,%eax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nand_write_page>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   $0x7ff,%esi
jne    1267 <nand_write_page+0x137>
mov    %rdx,%r14
mov    %r14d,%eax
and    $0x7ff,%eax
test   %rax,%rax
jne    1267 <nand_write_page+0x137>
andq   $0xfffffffffffffffd,0x70200004
70 fd
movslq %esi,%r12
add    %r14,%r12
cmp    %r14,%r12
jbe    125e <nand_write_page+0x12e>
mov    %rdi,%r13
lea    0xe85(%rip),%r15
jmp    1193 <nand_write_page+0x63>
nopl   (%rax)
add    %rbx,%r13
add    $0x800,%r14
cmp    %r12,%r14
jae    125e <nand_write_page+0x12e>
movq   $0x80,0x70200008
mov    %r14,%rdi
call   1040 <nand_write_address@plt>
xor    %ebx,%ebx
nopl   0x0(%rax)
movzbl 0x0(%r13,%rbx,1),%eax
mov    %al,0x70200010
movzbl 0x1(%r13,%rbx,1),%eax
mov    %al,0x70200010
movzbl 0x2(%r13,%rbx,1),%eax
mov    %al,0x70200010
movzbl 0x3(%r13,%rbx,1),%eax
mov    %al,0x70200010
movzbl 0x4(%r13,%rbx,1),%eax
mov    %al,0x70200010
movzbl 0x5(%r13,%rbx,1),%eax
mov    %al,0x70200010
movzbl 0x6(%r13,%rbx,1),%eax
mov    %al,0x70200010
movzbl 0x7(%r13,%rbx,1),%eax
mov    %al,0x70200010
add    $0x8,%rbx
cmp    $0x800,%ebx
jne    11b0 <nand_write_page+0x80>
movq   $0x10,0x70200008
xor    %eax,%eax
call   1030 <nand_wait_idle@plt>
movq   $0x70,0x70200008
testb  $0x1,0x70200010
01
je     1180 <nand_write_page+0x50>
mov    %r15,%rdi
call   1050 <UART0_SendString@plt>
jmp    1180 <nand_write_page+0x50>
orq    $0x2,0x70200004
70 02
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"\0A\0DError in Program\0A\0D\00"
define void @nand_write_page(i8* %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
store i64 0, i64* %7
store i8 0, i8* %8
store i32 0, i32* %9
%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* %5
%15 = and i32 %14, 2047
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13, %3
br label %58
18: ; preds = %13
%19 = load volatile i64, i64* inttoptr (i64 1881145348 to i64*)
%20 = and i64 %19, -3
store volatile i64 %20, i64* inttoptr (i64 1881145348 to i64*)
%21 = load i64, i64* %6
store i64 %21, i64* %7
br label %22
22: ; preds = %52, %18
%23 = load i64, i64* %7
%24 = load i64, i64* %6
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = add i64 %24, %26
%28 = icmp ult i64 %23, %27
br i1 %28, label %29, label %55
29: ; preds = %22
store volatile i64 128, i64* inttoptr (i64 1881145352 to i64*)
%30 = load i64, i64* %7
%31 = call i32 @nand_write_address(i64 %30)
store i32 0, i32* %9
br label %32
32: ; preds = %39, %29
%33 = load i32, i32* %9
%34 = icmp slt i32 %33, 2048
br i1 %34, label %35, label %42
35: ; preds = %32
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %4
%38 = load i8, i8* %36
store volatile i8 %38, i8* inttoptr (i64 1881145360 to i8*)
br label %39
39: ; preds = %35
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
br label %32
42: ; preds = %32
store volatile i64 16, i64* inttoptr (i64 1881145352 to i64*)
%43 = call i32 (...) @nand_wait_idle()
store volatile i64 112, i64* inttoptr (i64 1881145352 to i64*)
%44 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
store i8 %44, i8* %8
%45 = load i8, i8* %8
%46 = zext i8 %45 to i32
%47 = and i32 %46, 1
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = call i32 @UART0_SendString(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %51
51: ; preds = %49, %42
br label %52
52: ; preds = %51
%53 = load i64, i64* %7
%54 = add i64 %53, 2048
store i64 %54, i64* %7
br label %22
55: ; preds = %22
%56 = load volatile i64, i64* inttoptr (i64 1881145348 to i64*)
%57 = or i64 %56, 2
store volatile i64 %57, i64* inttoptr (i64 1881145348 to i64*)
br label %58
58: ; preds = %55, %17
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"\0A\0DError in Program\0A\0D\00"
define void @nand_write_page(i8* nocapture %0, i32 %1, i64 %2) {
%4 = and i64 %2, 2047
%5 = icmp eq i64 %4, 0
%6 = and i32 %1, 2047
%7 = icmp eq i32 %6, 0
%8 = and i1 %7, %5
br i1 %8, label %9, label %53
9: ; preds = %3
%10 = load volatile i64, i64* inttoptr (i64 1881145348 to i64*)
%11 = and i64 %10, -3
store volatile i64 %11, i64* inttoptr (i64 1881145348 to i64*)
%12 = sext i32 %1 to i64
%13 = add i64 %12, %2
%14 = icmp ugt i64 %13, %2
br i1 %14, label %15, label %50
15: ; preds = %9, %47
%16 = phi i64 [ %48, %47 ], [ %2, %9 ]
%17 = phi i8* [ %36, %47 ], [ %0, %9 ]
store volatile i64 128, i64* inttoptr (i64 1881145352 to i64*)
%18 = tail call i32 @nand_write_address(i64 %16)
br label %19
19: ; preds = %19, %15
%20 = phi i32 [ 0, %15 ], [ %38, %19 ]
%21 = phi i8* [ %17, %15 ], [ %36, %19 ]
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = load i8, i8* %21
store volatile i8 %23, i8* inttoptr (i64 1881145360 to i8*)
%24 = getelementptr inbounds i8, i8* %21, i64 2
%25 = load i8, i8* %22
store volatile i8 %25, i8* inttoptr (i64 1881145360 to i8*)
%26 = getelementptr inbounds i8, i8* %21, i64 3
%27 = load i8, i8* %24
store volatile i8 %27, i8* inttoptr (i64 1881145360 to i8*)
%28 = getelementptr inbounds i8, i8* %21, i64 4
%29 = load i8, i8* %26
store volatile i8 %29, i8* inttoptr (i64 1881145360 to i8*)
%30 = getelementptr inbounds i8, i8* %21, i64 5
%31 = load i8, i8* %28
store volatile i8 %31, i8* inttoptr (i64 1881145360 to i8*)
%32 = getelementptr inbounds i8, i8* %21, i64 6
%33 = load i8, i8* %30
store volatile i8 %33, i8* inttoptr (i64 1881145360 to i8*)
%34 = getelementptr inbounds i8, i8* %21, i64 7
%35 = load i8, i8* %32
store volatile i8 %35, i8* inttoptr (i64 1881145360 to i8*)
%36 = getelementptr inbounds i8, i8* %21, i64 8
%37 = load i8, i8* %34
store volatile i8 %37, i8* inttoptr (i64 1881145360 to i8*)
%38 = add nuw nsw i32 %20, 8
%39 = icmp eq i32 %38, 2048
br i1 %39, label %40, label %19
40: ; preds = %19
store volatile i64 16, i64* inttoptr (i64 1881145352 to i64*)
%41 = tail call i32 (...) @nand_wait_idle()
store volatile i64 112, i64* inttoptr (i64 1881145352 to i64*)
%42 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%43 = and i8 %42, 1
%44 = icmp eq i8 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %40
%46 = tail call i32 @UART0_SendString(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %47
47: ; preds = %40, %45
%48 = add i64 %16, 2048
%49 = icmp ult i64 %48, %13
br i1 %49, label %15, label %50
50: ; preds = %47, %9
%51 = load volatile i64, i64* inttoptr (i64 1881145348 to i64*)
%52 = or i64 %51, 2
store volatile i64 %52, i64* inttoptr (i64 1881145348 to i64*)
br label %53
53: ; preds = %3, %50
ret void
}