#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_cmd(int);
int /*<<< orphan*/ nand_deselect();
unsigned char nand_get_data();
int /*<<< orphan*/ nand_select();
int /*<<< orphan*/ nand_send_addr(unsigned int);
int /*<<< orphan*/ wait_ready();
int nand_read(unsigned int nand_start, unsigned int ddr_start,
              unsigned int len) {
  unsigned int addr = nand_start;
  int i, count = 0;
  unsigned char *dest = (unsigned char *)ddr_start;
  nand_select();
  while (count < len) {
    nand_cmd(0x00);
    nand_send_addr(addr);
    nand_cmd(0x30);
    wait_ready();
    for (i = 0; i < 2048 && count < len; i++) {
      dest[count++] = nand_get_data();
    }
    addr += 2048;
  }
  nand_deselect();
  return 0;
}


x86 assembly (clang, -O0):
<nand_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <nand_select@plt>
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    1229 <nand_read+0xc9>
xor    %edi,%edi
call   1050 <nand_cmd@plt>
mov    -0x10(%rbp),%edi
call   1080 <nand_send_addr@plt>
mov    $0x30,%edi
call   1050 <nand_cmd@plt>
mov    $0x0,%al
call   1060 <wait_ready@plt>
movl   $0x0,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x800,-0x14(%rbp)
mov    %al,-0x21(%rbp)
jge    11dd <nand_read+0x7d>
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
setb   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11ed <nand_read+0x8d>
jmp    1219 <nand_read+0xb9>
mov    $0x0,%al
call   1030 <nand_get_data@plt>
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11bf <nand_read+0x5f>
mov    -0x10(%rbp),%eax
add    $0x800,%eax
mov    %eax,-0x10(%rbp)
jmp    118c <nand_read+0x2c>
mov    $0x0,%al
call   1040 <nand_deselect@plt>
xor    %eax,%eax
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    %edx,%r13d
mov    %esi,%r14d
mov    %edi,%r12d
xor    %eax,%eax
call   1070 <nand_select@plt>
test   %r13d,%r13d
je     11f5 <nand_read+0x95>
mov    %r14d,%eax
mov    %rax,(%rsp)
xor    %r15d,%r15d
jmp    119f <nand_read+0x3f>
nopl   0x0(%rax)
add    %ebx,%r15d
add    $0x800,%r12d
cmp    %r13d,%r15d
jae    11f5 <nand_read+0x95>
xor    %edi,%edi
call   1050 <nand_cmd@plt>
mov    %r12d,%edi
call   1080 <nand_send_addr@plt>
mov    $0x30,%edi
call   1050 <nand_cmd@plt>
xor    %eax,%eax
call   1060 <wait_ready@plt>
cmp    %r13d,%r15d
jae    1193 <nand_read+0x33>
movslq %r15d,%rbp
add    (%rsp),%rbp
mov    %r15d,%r14d
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <nand_get_data@plt>
mov    %al,0x0(%rbp,%rbx,1)
mov    %ebx,%eax
add    $0x1,%rbx
cmp    $0x7fe,%eax
ja     1190 <nand_read+0x30>
add    %r14d,%eax
add    $0x1,%eax
cmp    %r13d,%eax
jb     11d0 <nand_read+0x70>
jmp    1190 <nand_read+0x30>
xor    %eax,%eax
call   1040 <nand_deselect@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nand_read(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 i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i32, i32* %4
store i32 %11, i32* %7
store i32 0, i32* %9
%12 = load i32, i32* %5
%13 = zext i32 %12 to i64
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %10
%15 = call i32 (...) @nand_select()
br label %16
16: ; preds = %45, %3
%17 = load i32, i32* %9
%18 = load i32, i32* %6
%19 = icmp ult i32 %17, %18
br i1 %19, label %20, label %48
20: ; preds = %16
%21 = call i32 @nand_cmd(i32 0)
%22 = load i32, i32* %7
%23 = call i32 @nand_send_addr(i32 %22)
%24 = call i32 @nand_cmd(i32 48)
%25 = call i32 (...) @wait_ready()
store i32 0, i32* %8
br label %26
26: ; preds = %42, %20
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 2048
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* %9
%31 = load i32, i32* %6
%32 = icmp ult i32 %30, %31
br label %33
33: ; preds = %29, %26
%34 = phi i1 [ false, %26 ], [ %32, %29 ]
br i1 %34, label %35, label %45
35: ; preds = %33
%36 = call zeroext i8 (...) @nand_get_data()
%37 = load i8*, i8** %10
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
store i32 %39, i32* %9
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 %36, i8* %41
br label %42
42: ; preds = %35
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %26
45: ; preds = %33
%46 = load i32, i32* %7
%47 = add i32 %46, 2048
store i32 %47, i32* %7
br label %16
48: ; preds = %16
%49 = call i32 (...) @nand_deselect()
ret i32 0
}

LLVM IR (-O2):
define i32 @nand_read(i32 %0, i32 %1, i32 %2) {
%4 = zext i32 %1 to i64
%5 = inttoptr i64 %4 to i8*
%6 = tail call i32 (...) @nand_select()
%7 = icmp eq i32 %2, 0
br i1 %7, label %35, label %8
8: ; preds = %3, %31
%9 = phi i32 [ %33, %31 ], [ %0, %3 ]
%10 = phi i32 [ %32, %31 ], [ 0, %3 ]
%11 = tail call i32 @nand_cmd(i32 0)
%12 = tail call i32 @nand_send_addr(i32 %9)
%13 = tail call i32 @nand_cmd(i32 48)
%14 = tail call i32 (...) @wait_ready()
%15 = icmp ult i32 %10, %2
br i1 %15, label %16, label %31
16: ; preds = %8
%17 = sext i32 %10 to i64
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ %17, %16 ], [ %22, %18 ]
%20 = phi i32 [ 0, %16 ], [ %24, %18 ]
%21 = tail call zeroext i8 (...) @nand_get_data()
%22 = add nsw i64 %19, 1
%23 = getelementptr inbounds i8, i8* %5, i64 %19
store i8 %21, i8* %23
%24 = add nuw nsw i32 %20, 1
%25 = icmp ult i32 %20, 2047
%26 = trunc i64 %22 to i32
%27 = icmp ult i32 %26, %2
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %18, label %29
29: ; preds = %18
%30 = trunc i64 %22 to i32
br label %31
31: ; preds = %29, %8
%32 = phi i32 [ %10, %8 ], [ %30, %29 ]
%33 = add i32 %9, 2048
%34 = icmp ult i32 %32, %2
br i1 %34, label %8, label %35
35: ; preds = %31, %3
%36 = tail call i32 (...) @nand_deselect()
ret i32 0
}