#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*/ nand_chip_disable();
int /*<<< orphan*/ nand_chip_enable();
int /*<<< orphan*/ nand_clear_Rnb();
int /*<<< orphan*/ nand_read_one_byte();
int /*<<< orphan*/ nand_wait_Rnb_ready();
int /*<<< orphan*/ nand_write_addr(int);
int /*<<< orphan*/ nand_write_cmmd(int);
int nand_read_one_page(int addr, int ram_addr) {
  int block_addr = 0;
  int page_addr = 0;
  int i = 0;
  unsigned char *dst_addr = (unsigned char *)ram_addr;
  block_addr = addr / (2048 * 64);
  page_addr = (addr % (2048 * 64)) / (2048);
  nand_chip_enable();
  nand_clear_Rnb();
  nand_write_cmmd(0x00);
  nand_write_addr(0x00);
  nand_write_addr(0x00);
  nand_write_addr(((block_addr & 0x3) << 6) | (page_addr & 0xff));
  nand_write_addr((block_addr >> 2) & 0xff);
  nand_write_addr((block_addr >> 10) & 0x03);
  nand_write_cmmd(0x30);
  nand_wait_Rnb_ready();
  for (i = 0; i < 2048; i++) {
    *dst_addr++ = nand_read_one_byte();
  }
  nand_chip_disable();
  return 0;
}


x86 assembly (clang, -O0):
<nand_read_one_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movslq -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x20000,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x20000,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0x800,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <nand_chip_enable@plt>
mov    $0x0,%al
call   1050 <nand_clear_Rnb@plt>
xor    %edi,%edi
call   1080 <nand_write_cmmd@plt>
xor    %edi,%edi
call   1060 <nand_write_addr@plt>
xor    %edi,%edi
call   1060 <nand_write_addr@plt>
mov    -0xc(%rbp),%edi
and    $0x3,%edi
shl    $0x6,%edi
mov    -0x10(%rbp),%eax
and    $0xff,%eax
or     %eax,%edi
call   1060 <nand_write_addr@plt>
mov    -0xc(%rbp),%edi
sar    $0x2,%edi
and    $0xff,%edi
call   1060 <nand_write_addr@plt>
mov    -0xc(%rbp),%edi
sar    $0xa,%edi
and    $0x3,%edi
call   1060 <nand_write_addr@plt>
mov    $0x30,%edi
call   1080 <nand_write_cmmd@plt>
mov    $0x0,%al
call   1030 <nand_wait_Rnb_ready@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x800,-0x14(%rbp)
jge    1268 <nand_read_one_page+0xf8>
mov    $0x0,%al
call   1070 <nand_read_one_byte@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1233 <nand_read_one_page+0xc3>
mov    $0x0,%al
call   1090 <nand_chip_disable@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nand_read_one_page>:
push   %rbp
push   %r14
push   %rbx
movslq %esi,%rbx
lea    0x1ffff(%rdi),%eax
test   %edi,%edi
cmovns %edi,%eax
mov    %eax,%ebp
sar    $0x11,%ebp
and    $0xfffe0000,%eax
mov    %edi,%ecx
sub    %eax,%ecx
neg    %eax
lea    (%rdi,%rax,1),%r14d
add    $0x7ff,%r14d
test   %ecx,%ecx
cmovns %ecx,%r14d
shr    $0xb,%r14d
xor    %eax,%eax
call   1040 <nand_chip_enable@plt>
xor    %eax,%eax
call   1050 <nand_clear_Rnb@plt>
xor    %edi,%edi
call   1080 <nand_write_cmmd@plt>
xor    %edi,%edi
call   1060 <nand_write_addr@plt>
xor    %edi,%edi
call   1060 <nand_write_addr@plt>
mov    %ebp,%eax
shl    $0x6,%eax
or     %r14d,%eax
movzbl %al,%edi
call   1060 <nand_write_addr@plt>
mov    %ebp,%eax
shr    $0x2,%eax
movzbl %al,%edi
call   1060 <nand_write_addr@plt>
shr    $0xa,%ebp
and    $0x3,%ebp
mov    %ebp,%edi
call   1060 <nand_write_addr@plt>
mov    $0x30,%edi
call   1080 <nand_write_cmmd@plt>
xor    %eax,%eax
call   1030 <nand_wait_Rnb_ready@plt>
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <nand_read_one_byte@plt>
mov    %al,(%rbx,%rbp,1)
add    $0x1,%rbp
cmp    $0x800,%ebp
jne    1210 <nand_read_one_page+0xa0>
xor    %eax,%eax
call   1090 <nand_chip_disable@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nand_read_one_page(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
%9 = load i32, i32* %4
%10 = sext i32 %9 to i64
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %8
%12 = load i32, i32* %3
%13 = sdiv i32 %12, 131072
store i32 %13, i32* %5
%14 = load i32, i32* %3
%15 = srem i32 %14, 131072
%16 = sdiv i32 %15, 2048
store i32 %16, i32* %6
%17 = call i32 (...) @nand_chip_enable()
%18 = call i32 (...) @nand_clear_Rnb()
%19 = call i32 @nand_write_cmmd(i32 0)
%20 = call i32 @nand_write_addr(i32 0)
%21 = call i32 @nand_write_addr(i32 0)
%22 = load i32, i32* %5
%23 = and i32 %22, 3
%24 = shl i32 %23, 6
%25 = load i32, i32* %6
%26 = and i32 %25, 255
%27 = or i32 %24, %26
%28 = call i32 @nand_write_addr(i32 %27)
%29 = load i32, i32* %5
%30 = ashr i32 %29, 2
%31 = and i32 %30, 255
%32 = call i32 @nand_write_addr(i32 %31)
%33 = load i32, i32* %5
%34 = ashr i32 %33, 10
%35 = and i32 %34, 3
%36 = call i32 @nand_write_addr(i32 %35)
%37 = call i32 @nand_write_cmmd(i32 48)
%38 = call i32 (...) @nand_wait_Rnb_ready()
store i32 0, i32* %7
br label %39
39: ; preds = %47, %2
%40 = load i32, i32* %7
%41 = icmp slt i32 %40, 2048
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = call i32 (...) @nand_read_one_byte()
%44 = trunc i32 %43 to i8
%45 = load i8*, i8** %8
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %8
store i8 %44, i8* %45
br label %47
47: ; preds = %42
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %39
50: ; preds = %39
%51 = call i32 (...) @nand_chip_disable()
ret i32 0
}

LLVM IR (-O2):
define i32 @nand_read_one_page(i32 %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = inttoptr i64 %3 to i8*
%5 = sdiv i32 %0, 131072
%6 = srem i32 %0, 131072
%7 = sdiv i32 %6, 2048
%8 = tail call i32 (...) @nand_chip_enable()
%9 = tail call i32 (...) @nand_clear_Rnb()
%10 = tail call i32 @nand_write_cmmd(i32 0)
%11 = tail call i32 @nand_write_addr(i32 0)
%12 = tail call i32 @nand_write_addr(i32 0)
%13 = shl nsw i32 %5, 6
%14 = and i32 %13, 192
%15 = and i32 %7, 255
%16 = or i32 %14, %15
%17 = tail call i32 @nand_write_addr(i32 %16)
%18 = lshr i32 %5, 2
%19 = and i32 %18, 255
%20 = tail call i32 @nand_write_addr(i32 %19)
%21 = lshr i32 %5, 10
%22 = and i32 %21, 3
%23 = tail call i32 @nand_write_addr(i32 %22)
%24 = tail call i32 @nand_write_cmmd(i32 48)
%25 = tail call i32 (...) @nand_wait_Rnb_ready()
br label %26
26: ; preds = %2, %26
%27 = phi i8* [ %4, %2 ], [ %31, %26 ]
%28 = phi i32 [ 0, %2 ], [ %32, %26 ]
%29 = tail call i32 (...) @nand_read_one_byte()
%30 = trunc i32 %29 to i8
%31 = getelementptr inbounds i8, i8* %27, i64 1
store i8 %30, i8* %27
%32 = add nuw nsw i32 %28, 1
%33 = icmp eq i32 %32, 2048
br i1 %33, label %34, label %26
34: ; preds = %26
%35 = tail call i32 (...) @nand_chip_disable()
ret i32 0
}