#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_addr(unsigned int);
int /*<<< orphan*/ nand_cmd(int);
int /*<<< orphan*/ nand_deselect();
int /*<<< orphan*/ nand_ready();
int /*<<< orphan*/ nand_select();
int nand_read(unsigned int nand_start, unsigned char *buf, unsigned int len) {
  unsigned int addr = nand_start;
  int count = 0, i = 0;
  unsigned char *dest = buf;
  unsigned char data = 0;
  nand_select();
  while (count < len) {
    nand_cmd(0x00);
    nand_addr(addr);
    nand_cmd(0x30);
    nand_ready();
    for (i = 0; i < 2048 && count < len; i++) {
      data = *((volatile unsigned char *)0x70200010);
      dest[count++] = 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    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movb   $0x0,-0x29(%rbp)
mov    $0x0,%al
call   1070 <nand_select@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    122a <nand_read+0xda>
xor    %edi,%edi
call   1060 <nand_cmd@plt>
mov    -0x18(%rbp),%edi
call   1050 <nand_addr@plt>
mov    $0x30,%edi
call   1060 <nand_cmd@plt>
mov    $0x0,%al
call   1040 <nand_ready@plt>
movl   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x800,-0x20(%rbp)
mov    %al,-0x2a(%rbp)
jge    11da <nand_read+0x8a>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
setb   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
test   $0x1,%al
jne    11ea <nand_read+0x9a>
jmp    121a <nand_read+0xca>
mov    $0x70200010,%eax
mov    (%rax),%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%dl
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11bc <nand_read+0x6c>
mov    -0x18(%rbp),%eax
add    $0x800,%eax
mov    %eax,-0x18(%rbp)
jmp    1189 <nand_read+0x39>
mov    $0x0,%al
call   1030 <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   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r12d
xor    %eax,%eax
call   1070 <nand_select@plt>
test   %ebp,%ebp
je     11d4 <nand_read+0x84>
xor    %r15d,%r15d
jmp    117f <nand_read+0x2f>
add    %ecx,%r15d
add    $0x800,%r12d
cmp    %ebp,%r15d
jae    11d4 <nand_read+0x84>
xor    %edi,%edi
call   1060 <nand_cmd@plt>
mov    %r12d,%edi
call   1050 <nand_addr@plt>
mov    $0x30,%edi
call   1060 <nand_cmd@plt>
xor    %eax,%eax
call   1040 <nand_ready@plt>
cmp    %ebp,%r15d
jae    1173 <nand_read+0x23>
movslq %r15d,%rax
add    %r14,%rax
mov    %r15d,%edx
xor    %ecx,%ecx
nop
movzbl 0x70200010,%ebx
70
mov    %bl,(%rax,%rcx,1)
mov    %ecx,%esi
add    $0x1,%rcx
cmp    $0x7fe,%esi
ja     1170 <nand_read+0x20>
add    %edx,%esi
add    $0x1,%esi
cmp    %ebp,%esi
jb     11b0 <nand_read+0x60>
jmp    1170 <nand_read+0x20>
xor    %eax,%eax
call   1030 <nand_deselect@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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