#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();
int /*<<< orphan*/ nand_select();
int /*<<< orphan*/ nand_send_addr(unsigned long);
int /*<<< orphan*/ nand_send_data(unsigned char);
int /*<<< orphan*/ wait_ready();
int nand_write(unsigned int nand_start, unsigned char *buf, unsigned int len) {
  unsigned long count = 0;
  unsigned long addr = nand_start;
  int i = nand_start % 2048;
  nand_select();
  while (count < len) {
    nand_cmd(0x80);
    nand_send_addr(addr);
    for (; i < 2048 && count < len; i++) {
      nand_send_data(buf[count++]);
      addr++;
    }
    nand_cmd(0x10);
    wait_ready();
    i = 0;
  }
  nand_deselect();
}


x86 assembly (clang, -O0):
<nand_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
and    $0x7ff,%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1070 <nand_select@plt>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
cmp    %rcx,%rax
jae    123b <nand_write+0xdb>
mov    $0x80,%edi
call   1040 <nand_cmd@plt>
mov    -0x28(%rbp),%rdi
call   1080 <nand_send_addr@plt>
xor    %eax,%eax
cmpl   $0x800,-0x2c(%rbp)
mov    %al,-0x2d(%rbp)
jge    11d8 <nand_write+0x78>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    11e8 <nand_write+0x88>
jmp    121e <nand_write+0xbe>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
movzbl (%rax,%rcx,1),%edi
call   1060 <nand_send_data@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b6 <nand_write+0x56>
mov    $0x10,%edi
call   1040 <nand_cmd@plt>
mov    $0x0,%al
call   1050 <wait_ready@plt>
movl   $0x0,-0x2c(%rbp)
jmp    1193 <nand_write+0x33>
mov    $0x0,%al
call   1030 <nand_deselect@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nand_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <nand_select@plt>
test   %ebx,%ebx
je     11e4 <nand_write+0x84>
mov    %ebx,%r15d
mov    %ebp,%r12d
and    $0x7ff,%ebp
xor    %ebx,%ebx
jmp    11a8 <nand_write+0x48>
nopw   0x0(%rax,%rax,1)
mov    $0x10,%edi
call   1040 <nand_cmd@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <wait_ready@plt>
cmp    %r15,%rbx
jae    11e4 <nand_write+0x84>
mov    $0x80,%edi
call   1040 <nand_cmd@plt>
mov    %r12,%rdi
call   1080 <nand_send_addr@plt>
cmp    %r15,%rbx
jae    1190 <nand_write+0x30>
nop
movzbl (%r14,%rbx,1),%edi
add    $0x1,%rbx
call   1060 <nand_send_data@plt>
add    $0x1,%r12
cmp    $0x7fe,%ebp
ja     1190 <nand_write+0x30>
add    $0x1,%ebp
cmp    %r15,%rbx
jb     11c0 <nand_write+0x60>
jmp    1190 <nand_write+0x30>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <nand_deselect@plt>

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

LLVM IR (-O2):
define i32 @nand_write(i32 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 (...) @nand_select()
%5 = zext i32 %2 to i64
%6 = icmp eq i32 %2, 0
br i1 %6, label %36, label %7
7: ; preds = %3
%8 = and i32 %0, 2047
%9 = zext i32 %0 to i64
br label %10
10: ; preds = %7, %30
%11 = phi i32 [ 0, %30 ], [ %8, %7 ]
%12 = phi i64 [ %32, %30 ], [ %9, %7 ]
%13 = phi i64 [ %31, %30 ], [ 0, %7 ]
%14 = tail call i32 @nand_cmd(i32 128)
%15 = tail call i32 @nand_send_addr(i64 %12)
%16 = icmp ult i64 %13, %5
br i1 %16, label %17, label %30
17: ; preds = %10, %17
%18 = phi i32 [ %26, %17 ], [ %11, %10 ]
%19 = phi i64 [ %25, %17 ], [ %12, %10 ]
%20 = phi i64 [ %21, %17 ], [ %13, %10 ]
%21 = add nuw nsw i64 %20, 1
%22 = getelementptr inbounds i8, i8* %1, i64 %20
%23 = load i8, i8* %22
%24 = tail call i32 @nand_send_data(i8 zeroext %23)
%25 = add i64 %19, 1
%26 = add nuw nsw i32 %18, 1
%27 = icmp ult i32 %18, 2047
%28 = icmp ult i64 %21, %5
%29 = select i1 %27, i1 %28, i1 false
br i1 %29, label %17, label %30
30: ; preds = %17, %10
%31 = phi i64 [ %13, %10 ], [ %21, %17 ]
%32 = phi i64 [ %12, %10 ], [ %25, %17 ]
%33 = tail call i32 @nand_cmd(i32 16)
%34 = tail call i32 (...) @wait_ready()
%35 = icmp ult i64 %31, %5
br i1 %35, label %10, label %36
36: ; preds = %30, %3
%37 = tail call i32 (...) @nand_deselect()
ret i32 undef
}