#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 = nand_start % 2048;
  int 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 < 2048 && count < len; i++) {
      dest[count++] = nand_get_data();
      addr++;
    }
    i = 0;
  }
  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)
mov    -0x4(%rbp),%eax
and    $0x7ff,%eax
mov    %eax,-0x14(%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    1232 <nand_read+0xd2>
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>
xor    %eax,%eax
cmpl   $0x800,-0x14(%rbp)
mov    %al,-0x21(%rbp)
jge    11e1 <nand_read+0x81>
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
setb   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11f1 <nand_read+0x91>
jmp    1226 <nand_read+0xc6>
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    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c3 <nand_read+0x63>
movl   $0x0,-0x14(%rbp)
jmp    1197 <nand_read+0x37>
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,%r15d
mov    %esi,%r14d
mov    %edi,%r12d
xor    %eax,%eax
call   1070 <nand_select@plt>
test   %r15d,%r15d
je     1207 <nand_read+0xa7>
mov    %r14d,%r14d
mov    %r12d,%ebp
and    $0x7ff,%ebp
xor    %r13d,%r13d
jmp    11a7 <nand_read+0x47>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %ebp,%ebp
cmp    %r15d,%r13d
jae    1207 <nand_read+0xa7>
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    %r15d,%r13d
jae    11a0 <nand_read+0x40>
movslq %r13d,%rbx
add    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <nand_get_data@plt>
mov    %al,(%rbx)
add    $0x1,%r12d
add    $0x1,%r13d
cmp    $0x7fe,%ebp
ja     11a0 <nand_read+0x40>
add    $0x1,%ebp
add    $0x1,%rbx
cmp    %r15d,%r13d
jb     11e0 <nand_read+0x80>
jmp    11a0 <nand_read+0x40>
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
%12 = load i32, i32* %4
%13 = urem i32 %12, 2048
store i32 %13, i32* %8
store i32 0, i32* %9
%14 = load i32, i32* %5
%15 = zext i32 %14 to i64
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %10
%17 = call i32 (...) @nand_select()
br label %18
18: ; preds = %49, %3
%19 = load i32, i32* %9
%20 = load i32, i32* %6
%21 = icmp ult i32 %19, %20
br i1 %21, label %22, label %50
22: ; preds = %18
%23 = call i32 @nand_cmd(i32 0)
%24 = load i32, i32* %7
%25 = call i32 @nand_send_addr(i32 %24)
%26 = call i32 @nand_cmd(i32 48)
%27 = call i32 (...) @wait_ready()
br label %28
28: ; preds = %46, %22
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 2048
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i32, i32* %9
%33 = load i32, i32* %6
%34 = icmp ult i32 %32, %33
br label %35
35: ; preds = %31, %28
%36 = phi i1 [ false, %28 ], [ %34, %31 ]
br i1 %36, label %37, label %49
37: ; preds = %35
%38 = call zeroext i8 (...) @nand_get_data()
%39 = load i8*, i8** %10
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
store i8 %38, i8* %43
%44 = load i32, i32* %7
%45 = add i32 %44, 1
store i32 %45, i32* %7
br label %46
46: ; preds = %37
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
store i32 %48, i32* %8
br label %28
49: ; preds = %35
store i32 0, i32* %8
br label %18
50: ; preds = %18
%51 = 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 %40, label %8
8: ; preds = %3
%9 = and i32 %0, 2047
br label %16
10: ; preds = %27
%11 = trunc i64 %32 to i32
br label %12
12: ; preds = %10, %16
%13 = phi i32 [ %18, %16 ], [ %11, %10 ]
%14 = phi i32 [ %17, %16 ], [ %34, %10 ]
%15 = icmp ult i32 %13, %2
br i1 %15, label %16, label %40
16: ; preds = %8, %12
%17 = phi i32 [ %14, %12 ], [ %0, %8 ]
%18 = phi i32 [ %13, %12 ], [ 0, %8 ]
%19 = phi i32 [ 0, %12 ], [ %9, %8 ]
%20 = tail call i32 @nand_cmd(i32 0)
%21 = tail call i32 @nand_send_addr(i32 %17)
%22 = tail call i32 @nand_cmd(i32 48)
%23 = tail call i32 (...) @wait_ready()
%24 = icmp ult i32 %18, %2
br i1 %24, label %25, label %12
25: ; preds = %16
%26 = sext i32 %18 to i64
br label %27
27: ; preds = %25, %27
%28 = phi i64 [ %26, %25 ], [ %32, %27 ]
%29 = phi i32 [ %17, %25 ], [ %34, %27 ]
%30 = phi i32 [ %19, %25 ], [ %35, %27 ]
%31 = tail call zeroext i8 (...) @nand_get_data()
%32 = add nsw i64 %28, 1
%33 = getelementptr inbounds i8, i8* %5, i64 %28
store i8 %31, i8* %33
%34 = add i32 %29, 1
%35 = add nuw nsw i32 %30, 1
%36 = icmp ult i32 %30, 2047
%37 = trunc i64 %32 to i32
%38 = icmp ult i32 %37, %2
%39 = select i1 %36, i1 %38, i1 false
br i1 %39, label %27, label %10
40: ; preds = %12, %3
%41 = tail call i32 (...) @nand_deselect()
ret i32 0
}