#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*/ clear_RnB();
int /*<<< orphan*/ delete_chip();
int /*<<< orphan*/ nand_addr(unsigned long);
int /*<<< orphan*/ select_chip();
int /*<<< orphan*/ send_cmd(int);
int /*<<< orphan*/ wait_RnB();
void NF_Page_Write(unsigned long addr, unsigned char *buff,
                   unsigned long size) {
  unsigned long count = 0;
  int ret;
  unsigned long rest = size;
  unsigned long page;
  unsigned int col = addr % 2048;
  select_chip();
  while (count < rest) {
    clear_RnB();
    send_cmd(0x80);
    nand_addr(addr);
    for (; (col < 2048) && (count < rest); col++) {
      (*(volatile unsigned char *)0x70200010) = buff[count++];
      addr++;
    }
    col = 0;
    send_cmd(0x10);
    wait_RnB();
    send_cmd(0x70);
    ret = (*(volatile unsigned char *)0x70200010);
  }
  delete_chip();
}


x86 assembly (clang, -O0):
<NF_Page_Write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
and    $0x7ff,%rax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1040 <select_chip@plt>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    125b <NF_Page_Write+0xfb>
mov    $0x0,%al
call   1070 <clear_RnB@plt>
mov    $0x80,%edi
call   1080 <send_cmd@plt>
mov    -0x8(%rbp),%rdi
call   1030 <nand_addr@plt>
xor    %eax,%eax
cmpl   $0x800,-0x3c(%rbp)
mov    %al,-0x3d(%rbp)
jae    11e0 <NF_Page_Write+0x80>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
setb   %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    11f0 <NF_Page_Write+0x90>
jmp    1227 <NF_Page_Write+0xc7>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    (%rax,%rcx,1),%cl
mov    $0x70200010,%eax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11c0 <NF_Page_Write+0x60>
movl   $0x0,-0x3c(%rbp)
mov    $0x10,%edi
call   1080 <send_cmd@plt>
mov    $0x0,%al
call   1050 <wait_RnB@plt>
mov    $0x70,%edi
call   1080 <send_cmd@plt>
mov    $0x70200010,%eax
mov    (%rax),%al
movzbl %al,%eax
mov    %eax,-0x24(%rbp)
jmp    1198 <NF_Page_Write+0x38>
mov    $0x0,%al
call   1060 <delete_chip@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NF_Page_Write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
xor    %eax,%eax
call   1040 <select_chip@plt>
test   %r15,%r15
je     1206 <NF_Page_Write+0xa6>
mov    %r12d,%ebp
and    $0x7ff,%ebp
xor    %ebx,%ebx
jmp    11b9 <NF_Page_Write+0x59>
xchg   %ax,%ax
mov    $0x10,%edi
call   1080 <send_cmd@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <wait_RnB@plt>
mov    $0x70,%edi
call   1080 <send_cmd@plt>
mov    0x70200010,%al
cmp    %r15,%rbx
jae    1206 <NF_Page_Write+0xa6>
xor    %eax,%eax
call   1070 <clear_RnB@plt>
mov    $0x80,%edi
call   1080 <send_cmd@plt>
mov    %r12,%rdi
call   1030 <nand_addr@plt>
cmp    %r15,%rbx
jae    1190 <NF_Page_Write+0x30>
nopw   0x0(%rax,%rax,1)
movzbl (%r14,%rbx,1),%eax
add    $0x1,%rbx
mov    %al,0x70200010
add    $0x1,%r12
cmp    $0x7fe,%ebp
ja     1190 <NF_Page_Write+0x30>
add    $0x1,%ebp
cmp    %r15,%rbx
jb     11e0 <NF_Page_Write+0x80>
jmp    1190 <NF_Page_Write+0x30>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <delete_chip@plt>

LLVM IR (-O0):
define void @NF_Page_Write(i64 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
store i64 %0, i64* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
%12 = load i64, i64* %6
store i64 %12, i64* %9
%13 = load i64, i64* %4
%14 = urem i64 %13, 2048
%15 = trunc i64 %14 to i32
store i32 %15, i32* %11
%16 = call i32 (...) @select_chip()
br label %17
17: ; preds = %46, %3
%18 = load i64, i64* %7
%19 = load i64, i64* %9
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %52
21: ; preds = %17
%22 = call i32 (...) @clear_RnB()
%23 = call i32 @send_cmd(i32 128)
%24 = load i64, i64* %4
%25 = call i32 @nand_addr(i64 %24)
br label %26
26: ; preds = %43, %21
%27 = load i32, i32* %11
%28 = icmp ult i32 %27, 2048
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i64, i64* %7
%31 = load i64, i64* %9
%32 = icmp ult i64 %30, %31
br label %33
33: ; preds = %29, %26
%34 = phi i1 [ false, %26 ], [ %32, %29 ]
br i1 %34, label %35, label %46
35: ; preds = %33
%36 = load i8*, i8** %5
%37 = load i64, i64* %7
%38 = add i64 %37, 1
store i64 %38, i64* %7
%39 = getelementptr inbounds i8, i8* %36, i64 %37
%40 = load i8, i8* %39
store volatile i8 %40, i8* inttoptr (i64 1881145360 to i8*)
%41 = load i64, i64* %4
%42 = add i64 %41, 1
store i64 %42, i64* %4
br label %43
43: ; preds = %35
%44 = load i32, i32* %11
%45 = add i32 %44, 1
store i32 %45, i32* %11
br label %26
46: ; preds = %33
store i32 0, i32* %11
%47 = call i32 @send_cmd(i32 16)
%48 = call i32 (...) @wait_RnB()
%49 = call i32 @send_cmd(i32 112)
%50 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%51 = zext i8 %50 to i32
store i32 %51, i32* %8
br label %17
52: ; preds = %17
%53 = call i32 (...) @delete_chip()
ret void
}

LLVM IR (-O2):
define void @NF_Page_Write(i64 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 (...) @select_chip()
%5 = icmp eq i64 %2, 0
br i1 %5, label %37, label %6
6: ; preds = %3
%7 = trunc i64 %0 to i32
%8 = and i32 %7, 2047
br label %9
9: ; preds = %6, %29
%10 = phi i32 [ 0, %29 ], [ %8, %6 ]
%11 = phi i64 [ %31, %29 ], [ %0, %6 ]
%12 = phi i64 [ %30, %29 ], [ 0, %6 ]
%13 = tail call i32 (...) @clear_RnB()
%14 = tail call i32 @send_cmd(i32 128)
%15 = tail call i32 @nand_addr(i64 %11)
%16 = icmp ult i64 %12, %2
br i1 %16, label %17, label %29
17: ; preds = %9, %17
%18 = phi i32 [ %25, %17 ], [ %10, %9 ]
%19 = phi i64 [ %24, %17 ], [ %11, %9 ]
%20 = phi i64 [ %21, %17 ], [ %12, %9 ]
%21 = add nuw i64 %20, 1
%22 = getelementptr inbounds i8, i8* %1, i64 %20
%23 = load i8, i8* %22
store volatile i8 %23, i8* inttoptr (i64 1881145360 to i8*)
%24 = add i64 %19, 1
%25 = add nuw nsw i32 %18, 1
%26 = icmp ult i32 %18, 2047
%27 = icmp ult i64 %21, %2
%28 = select i1 %26, i1 %27, i1 false
br i1 %28, label %17, label %29
29: ; preds = %17, %9
%30 = phi i64 [ %12, %9 ], [ %21, %17 ]
%31 = phi i64 [ %11, %9 ], [ %24, %17 ]
%32 = tail call i32 @send_cmd(i32 16)
%33 = tail call i32 (...) @wait_RnB()
%34 = tail call i32 @send_cmd(i32 112)
%35 = load volatile i8, i8* inttoptr (i64 1881145360 to i8*)
%36 = icmp ult i64 %30, %2
br i1 %36, label %9, label %37
37: ; preds = %29, %3
%38 = tail call i32 (...) @delete_chip()
ret void
}