#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_deselect_chip();
int /*<<< orphan*/ nand_select_chip();
unsigned char read_data();
int /*<<< orphan*/ wait_idle();
int /*<<< orphan*/ write_addr(int);
int /*<<< orphan*/ write_cmd(int);
void nand_read(unsigned char *buf, unsigned long start_addr, int size) {
  int i, j;
  nand_select_chip();
  for (i = start_addr; i < (start_addr + size);) {
    write_cmd(0);
    write_addr(i);
    write_cmd(0x30);
    wait_idle();
    for (j = 0; j < 2048; j++, i++) {
      *buf = read_data();
      buf++;
    }
  }
  nand_deselect_chip();
  return;
}


x86 assembly (clang, -O0):
<nand_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1070 <nand_select_chip@plt>
mov    -0x10(%rbp),%rax
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x14(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1204 <nand_read+0xa4>
xor    %edi,%edi
call   1080 <write_cmd@plt>
mov    -0x18(%rbp),%edi
call   1060 <write_addr@plt>
mov    $0x30,%edi
call   1080 <write_cmd@plt>
mov    $0x0,%al
call   1050 <wait_idle@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x800,-0x1c(%rbp)
jge    11ff <nand_read+0x9f>
mov    $0x0,%al
call   1030 <read_data@plt>
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c0 <nand_read+0x60>
jmp    1181 <nand_read+0x21>
mov    $0x0,%al
call   1040 <nand_deselect_chip@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nand_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <nand_select_chip@plt>
movslq %ebp,%r15
add    %r14,%r15
movslq %r14d,%rax
cmp    %rax,%r15
jbe    11e8 <nand_read+0x88>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
call   1080 <write_cmd@plt>
mov    %r14d,%edi
call   1060 <write_addr@plt>
mov    $0x30,%edi
call   1080 <write_cmd@plt>
xor    %eax,%eax
call   1050 <wait_idle@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <read_data@plt>
mov    %al,(%rbx,%rbp,1)
add    $0x1,%rbp
cmp    $0x800,%ebp
jne    11c0 <nand_read+0x60>
add    $0x800,%r14d
movslq %r14d,%rax
add    %rbp,%rbx
cmp    %rax,%r15
ja     1190 <nand_read+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <nand_deselect_chip@plt>

LLVM IR (-O0):
define void @nand_read(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%9 = call i32 (...) @nand_select_chip()
%10 = load i64, i64* %5
%11 = trunc i64 %10 to i32
store i32 %11, i32* %7
br label %12
12: ; preds = %39, %3
%13 = load i32, i32* %7
%14 = sext i32 %13 to i64
%15 = load i64, i64* %5
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = add i64 %15, %17
%19 = icmp ult i64 %14, %18
br i1 %19, label %20, label %40
20: ; preds = %12
%21 = call i32 @write_cmd(i32 0)
%22 = load i32, i32* %7
%23 = call i32 @write_addr(i32 %22)
%24 = call i32 @write_cmd(i32 48)
%25 = call i32 (...) @wait_idle()
store i32 0, i32* %8
br label %26
26: ; preds = %34, %20
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 2048
br i1 %28, label %29, label %39
29: ; preds = %26
%30 = call zeroext i8 (...) @read_data()
%31 = load i8*, i8** %4
store i8 %30, i8* %31
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %4
br label %34
34: ; preds = %29
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %26
39: ; preds = %26
br label %12
40: ; preds = %12
%41 = call i32 (...) @nand_deselect_chip()
ret void
}

LLVM IR (-O2):
define void @nand_read(i8* nocapture writeonly %0, i64 %1, i32 %2) {
%4 = tail call i32 (...) @nand_select_chip()
%5 = sext i32 %2 to i64
%6 = add i64 %5, %1
%7 = shl i64 %1, 32
%8 = ashr exact i64 %7, 32
%9 = icmp ugt i64 %6, %8
br i1 %9, label %10, label %30
10: ; preds = %3
%11 = trunc i64 %1 to i32
br label %16
12: ; preds = %23
%13 = add i32 %17, 2048
%14 = sext i32 %13 to i64
%15 = icmp ugt i64 %6, %14
br i1 %15, label %16, label %30
16: ; preds = %10, %12
%17 = phi i32 [ %13, %12 ], [ %11, %10 ]
%18 = phi i8* [ %27, %12 ], [ %0, %10 ]
%19 = tail call i32 @write_cmd(i32 0)
%20 = tail call i32 @write_addr(i32 %17)
%21 = tail call i32 @write_cmd(i32 48)
%22 = tail call i32 (...) @wait_idle()
br label %23
23: ; preds = %16, %23
%24 = phi i32 [ 0, %16 ], [ %28, %23 ]
%25 = phi i8* [ %18, %16 ], [ %27, %23 ]
%26 = tail call zeroext i8 (...) @read_data()
store i8 %26, i8* %25
%27 = getelementptr inbounds i8, i8* %25, i64 1
%28 = add nuw nsw i32 %24, 1
%29 = icmp eq i32 %28, 2048
br i1 %29, label %12, label %23
30: ; preds = %12, %3
%31 = tail call i32 (...) @nand_deselect_chip()
ret void
}