#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_read();
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_read();
    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    1220 <nand_read+0xc0>
xor    %edi,%edi
call   1060 <nand_cmd@plt>
mov    -0x4(%rbp),%edi
call   1050 <nand_addr@plt>
mov    $0x30,%edi
call   1060 <nand_cmd@plt>
mov    $0x0,%al
call   1070 <nand_wait_read@plt>
cmpl   $0x800,-0x18(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    -0x1c(%rbp),%ecx
cmp    -0x14(%rbp),%ecx
setb   %cl
and    $0x1,%cl
movzbl %cl,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1214 <nand_read+0xb4>
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   1060 <nand_cmd@plt>
mov    %r12d,%edi
call   1050 <nand_addr@plt>
mov    $0x30,%edi
call   1060 <nand_cmd@plt>
xor    %eax,%eax
call   1070 <nand_wait_read@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 = %45, %3
%13 = load i32, i32* %8
%14 = load i32, i32* %6
%15 = icmp ult i32 %13, %14
br i1 %15, label %16, label %46
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_read()
br label %22
22: ; preds = %42, %16
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 2048
%25 = zext i1 %24 to i32
%26 = load i32, i32* %8
%27 = load i32, i32* %6
%28 = icmp ult i32 %26, %27
%29 = zext i1 %28 to i32
%30 = and i32 %25, %29
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %45
32: ; preds = %22
%33 = call zeroext i8 (...) @nand_data()
%34 = load i8*, i8** %5
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 %33, i8* %37
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
%40 = load i32, i32* %4
%41 = add i32 %40, 1
store i32 %41, i32* %4
br label %42
42: ; preds = %32
%43 = load i32, i32* %7
%44 = add nsw i32 %43, 1
store i32 %44, i32* %7
br label %22
45: ; preds = %22
store i32 0, i32* %7
br label %12
46: ; preds = %12
%47 = 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_read()
%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 = and i1 %34, %36
br i1 %37, label %25, label %8
38: ; preds = %10, %3
%39 = tail call i32 (...) @nand_deselect()
ret void
}