#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);
unsigned char nand_data();
int /*<<< orphan*/ nand_deselect();
int /*<<< orphan*/ nand_select();
int /*<<< orphan*/ nand_wait_ready();
void nand_read(unsigned int addr, unsigned char *buf, unsigned int len) {
  int col = addr % 2048;
  int i = 0;
  nand_select();
  while (i < len) {
    nand_cmd(0x00);
    nand_addr(addr);
    nand_cmd(0x30);
    nand_wait_ready();
    for (; (col < 2048) && (i < len); col++) {
      buf[i] = nand_data();
      i++;
      addr++;
    }
    col = 0;
  }
  nand_deselect();
}


x86 assembly (clang, -O0):
<nand_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%eax
and    $0x7ff,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <nand_select@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1225 <nand_read+0xc5>
xor    %edi,%edi
call   1070 <nand_cmd@plt>
mov    -0x4(%rbp),%edi
call   1060 <nand_addr@plt>
mov    $0x30,%edi
call   1070 <nand_cmd@plt>
mov    $0x0,%al
call   1050 <nand_wait_ready@plt>
xor    %eax,%eax
cmpl   $0x800,-0x18(%rbp)
mov    %al,-0x1d(%rbp)
jge    11d5 <nand_read+0x75>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
setb   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11e5 <nand_read+0x85>
jmp    1219 <nand_read+0xb9>
mov    $0x0,%al
call   1030 <nand_data@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11b7 <nand_read+0x57>
movl   $0x0,-0x18(%rbp)
jmp    118b <nand_read+0x2b>
mov    $0x0,%al
call   1040 <nand_deselect@plt>
add    $0x20,%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    %rsi,%r14
mov    %edi,%r12d
xor    %eax,%eax
call   1080 <nand_select@plt>
test   %r15d,%r15d
je     11f7 <nand_read+0x97>
mov    %r12d,%ebp
and    $0x7ff,%ebp
xor    %r13d,%r13d
jmp    1197 <nand_read+0x37>
xchg   %ax,%ax
xor    %ebp,%ebp
cmp    %r15d,%r13d
jae    11f7 <nand_read+0x97>
xor    %edi,%edi
call   1070 <nand_cmd@plt>
mov    %r12d,%edi
call   1060 <nand_addr@plt>
mov    $0x30,%edi
call   1070 <nand_cmd@plt>
xor    %eax,%eax
call   1050 <nand_wait_ready@plt>
cmp    %r15d,%r13d
jae    1190 <nand_read+0x30>
movslq %r13d,%rbx
add    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <nand_data@plt>
mov    %al,(%rbx)
add    $0x1,%r12d
add    $0x1,%r13d
cmp    $0x7fe,%ebp
ja     1190 <nand_read+0x30>
add    $0x1,%ebp
add    $0x1,%rbx
cmp    %r15d,%r13d
jb     11d0 <nand_read+0x70>
jmp    1190 <nand_read+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <nand_deselect@plt>

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

LLVM IR (-O2):
define void @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 %38, label %6
6: ; preds = %3
%7 = and i32 %0, 2047
br label %14
8: ; preds = %25
%9 = trunc i64 %31 to i32
br label %10
10: ; preds = %8, %14
%11 = phi i32 [ %17, %14 ], [ %32, %8 ]
%12 = phi i32 [ %15, %14 ], [ %9, %8 ]
%13 = icmp ult i32 %12, %2
br i1 %13, label %14, label %38
14: ; preds = %6, %10
%15 = phi i32 [ %12, %10 ], [ 0, %6 ]
%16 = phi i32 [ 0, %10 ], [ %7, %6 ]
%17 = phi i32 [ %11, %10 ], [ %0, %6 ]
%18 = tail call i32 @nand_cmd(i32 0)
%19 = tail call i32 @nand_addr(i32 %17)
%20 = tail call i32 @nand_cmd(i32 48)
%21 = tail call i32 (...) @nand_wait_ready()
%22 = icmp ult i32 %15, %2
br i1 %22, label %23, label %10
23: ; preds = %14
%24 = sext i32 %15 to i64
br label %25
25: ; preds = %23, %25
%26 = phi i64 [ %24, %23 ], [ %31, %25 ]
%27 = phi i32 [ %16, %23 ], [ %33, %25 ]
%28 = phi i32 [ %17, %23 ], [ %32, %25 ]
%29 = tail call zeroext i8 (...) @nand_data()
%30 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 %29, i8* %30
%31 = add nsw i64 %26, 1
%32 = add i32 %28, 1
%33 = add nuw nsw i32 %27, 1
%34 = icmp ult i32 %27, 2047
%35 = trunc i64 %31 to i32
%36 = icmp ult i32 %35, %2
%37 = select i1 %34, i1 %36, i1 false
br i1 %37, label %25, label %8
38: ; preds = %10, %3
%39 = tail call i32 (...) @nand_deselect()
ret void
}