#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*/ nf_cmd(int);
unsigned char nf_data();
int /*<<< orphan*/ nf_ncs0(int);
int /*<<< orphan*/ nf_reset();
int /*<<< orphan*/ nf_setaddr(int);
int /*<<< orphan*/ nf_wait_rb();
int nf_read(const int addr, unsigned char *buf) {
  int i;
  int page = addr >> 11;
  ;
  nf_reset();
  nf_ncs0(0);
  nf_cmd(0x00);
  nf_setaddr(0);
  nf_setaddr(0);
  nf_setaddr(page & 0xff);
  nf_setaddr((page >> 8) & 0xff);
  nf_setaddr((page >> 16) & 0xff);
  nf_cmd(0x30);
  nf_wait_rb();
  for (i = 0; i < 2048; i++) {
    buf[i] = nf_data();
  }
  nf_ncs0(1);
  ;
  return 0;
}


x86 assembly (clang, -O0):
<nf_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
sar    $0xb,%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <nf_reset@plt>
xor    %edi,%edi
call   1040 <nf_ncs0@plt>
xor    %edi,%edi
call   1030 <nf_cmd@plt>
xor    %edi,%edi
call   1070 <nf_setaddr@plt>
xor    %edi,%edi
call   1070 <nf_setaddr@plt>
mov    -0x18(%rbp),%edi
and    $0xff,%edi
call   1070 <nf_setaddr@plt>
mov    -0x18(%rbp),%edi
sar    $0x8,%edi
and    $0xff,%edi
call   1070 <nf_setaddr@plt>
mov    -0x18(%rbp),%edi
sar    $0x10,%edi
and    $0xff,%edi
call   1070 <nf_setaddr@plt>
mov    $0x30,%edi
call   1030 <nf_cmd@plt>
mov    $0x0,%al
call   1060 <nf_wait_rb@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x800,-0x14(%rbp)
jge    1212 <nf_read+0xb2>
mov    $0x0,%al
call   1050 <nf_data@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11e3 <nf_read+0x83>
mov    $0x1,%edi
call   1040 <nf_ncs0@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nf_read>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %edi,%r14d
shr    $0xb,%r14d
xor    %eax,%eax
call   1080 <nf_reset@plt>
xor    %edi,%edi
call   1040 <nf_ncs0@plt>
xor    %edi,%edi
call   1030 <nf_cmd@plt>
xor    %edi,%edi
call   1070 <nf_setaddr@plt>
xor    %edi,%edi
call   1070 <nf_setaddr@plt>
movzbl %r14b,%edi
call   1070 <nf_setaddr@plt>
mov    %ebp,%eax
shr    $0x13,%eax
movzbl %al,%edi
call   1070 <nf_setaddr@plt>
sar    $0x1b,%ebp
movzbl %bpl,%edi
call   1070 <nf_setaddr@plt>
mov    $0x30,%edi
call   1030 <nf_cmd@plt>
xor    %eax,%eax
call   1060 <nf_wait_rb@plt>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <nf_data@plt>
mov    %al,(%rbx,%rbp,1)
add    $0x1,%rbp
cmp    $0x800,%rbp
jne    11d0 <nf_read+0x70>
mov    $0x1,%edi
call   1040 <nf_ncs0@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nf_read(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i32, i32* %3
%8 = ashr i32 %7, 11
store i32 %8, i32* %6
%9 = call i32 (...) @nf_reset()
%10 = call i32 @nf_ncs0(i32 0)
%11 = call i32 @nf_cmd(i32 0)
%12 = call i32 @nf_setaddr(i32 0)
%13 = call i32 @nf_setaddr(i32 0)
%14 = load i32, i32* %6
%15 = and i32 %14, 255
%16 = call i32 @nf_setaddr(i32 %15)
%17 = load i32, i32* %6
%18 = ashr i32 %17, 8
%19 = and i32 %18, 255
%20 = call i32 @nf_setaddr(i32 %19)
%21 = load i32, i32* %6
%22 = ashr i32 %21, 16
%23 = and i32 %22, 255
%24 = call i32 @nf_setaddr(i32 %23)
%25 = call i32 @nf_cmd(i32 48)
%26 = call i32 (...) @nf_wait_rb()
store i32 0, i32* %5
br label %27
27: ; preds = %36, %2
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 2048
br i1 %29, label %30, label %39
30: ; preds = %27
%31 = call zeroext i8 (...) @nf_data()
%32 = load i8*, i8** %4
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 %31, i8* %35
br label %36
36: ; preds = %30
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %27
39: ; preds = %27
%40 = call i32 @nf_ncs0(i32 1)
ret i32 0
}

LLVM IR (-O2):
define i32 @nf_read(i32 %0, i8* nocapture writeonly %1) {
%3 = lshr i32 %0, 11
%4 = tail call i32 (...) @nf_reset()
%5 = tail call i32 @nf_ncs0(i32 0)
%6 = tail call i32 @nf_cmd(i32 0)
%7 = tail call i32 @nf_setaddr(i32 0)
%8 = tail call i32 @nf_setaddr(i32 0)
%9 = and i32 %3, 255
%10 = tail call i32 @nf_setaddr(i32 %9)
%11 = lshr i32 %0, 19
%12 = and i32 %11, 255
%13 = tail call i32 @nf_setaddr(i32 %12)
%14 = ashr i32 %0, 27
%15 = and i32 %14, 255
%16 = tail call i32 @nf_setaddr(i32 %15)
%17 = tail call i32 @nf_cmd(i32 48)
%18 = tail call i32 (...) @nf_wait_rb()
br label %19
19: ; preds = %2, %19
%20 = phi i64 [ 0, %2 ], [ %23, %19 ]
%21 = tail call zeroext i8 (...) @nf_data()
%22 = getelementptr inbounds i8, i8* %1, i64 %20
store i8 %21, i8* %22
%23 = add nuw nsw i64 %20, 1
%24 = icmp eq i64 %23, 2048
br i1 %24, label %25, label %19
25: ; preds = %19
%26 = tail call i32 @nf_ncs0(i32 1)
ret i32 0
}