#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();
  return 0;
}


x86 assembly (clang, -O0):
<nand_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %rax,-0x28(%rbp)
mov    -0x4(%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>
xor    %eax,%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
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_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i64 0, i64* %7
%10 = load i32, i32* %4
%11 = zext i32 %10 to i64
store i64 %11, i64* %8
%12 = load i32, i32* %4
%13 = urem i32 %12, 2048
store i32 %13, i32* %9
%14 = call i32 (...) @nand_select()
br label %15
15: ; preds = %46, %3
%16 = load i64, i64* %7
%17 = load i32, i32* %6
%18 = zext i32 %17 to i64
%19 = icmp ult i64 %16, %18
br i1 %19, label %20, label %49
20: ; preds = %15
%21 = call i32 @nand_cmd(i32 128)
%22 = load i64, i64* %8
%23 = call i32 @nand_send_addr(i64 %22)
br label %24
24: ; preds = %43, %20
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 2048
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i64, i64* %7
%29 = load i32, i32* %6
%30 = zext i32 %29 to i64
%31 = icmp ult i64 %28, %30
br label %32
32: ; preds = %27, %24
%33 = phi i1 [ false, %24 ], [ %31, %27 ]
br i1 %33, label %34, label %46
34: ; preds = %32
%35 = load i8*, i8** %5
%36 = load i64, i64* %7
%37 = add i64 %36, 1
store i64 %37, i64* %7
%38 = getelementptr inbounds i8, i8* %35, i64 %36
%39 = load i8, i8* %38
%40 = call i32 @nand_send_data(i8 zeroext %39)
%41 = load i64, i64* %8
%42 = add i64 %41, 1
store i64 %42, i64* %8
br label %43
43: ; preds = %34
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
store i32 %45, i32* %9
br label %24
46: ; preds = %32
%47 = call i32 @nand_cmd(i32 16)
%48 = call i32 (...) @wait_ready()
store i32 0, i32* %9
br label %15
49: ; preds = %15
%50 = call i32 (...) @nand_deselect()
ret i32 0
}

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 0
}