#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 */
typedef scalar_t__ i;

/* Variables and functions */
int /*<<< orphan*/ nand_init();
scalar_t__ nand_isbadblock_p2048(unsigned int);
int /*<<< orphan*/ nand_read_page2048(unsigned int, unsigned char *);
void load_program_from_nand(void) {
  unsigned int i, pages, goodpages;
  unsigned char *program_addr =
      (unsigned char *)(0x30000000 + 0x04000000 - 0x00200000);
  unsigned long *ptr;
  pages = 0x00200000 >> 11;
  nand_init();
  for (i = 0, goodpages = 0; goodpages < pages;) {
    if ((((i) & ((1 << 6) - 1)) == 0)) {
      if (nand_isbadblock_p2048(i >> 6)) {
        i += (1 << 6);
        continue;
      }
    }
    nand_read_page2048(i, program_addr + (goodpages << 11));
    ++i;
    ++goodpages;
  }
  ptr = (unsigned long *)0x30000000;
  *ptr++ = goodpages;
  *ptr++ = i;
  *ptr = pages;
}


x86 assembly (clang, -O0):
<load_program_from_nand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x33e00000,%eax
mov    %rax,-0x18(%rbp)
movl   $0x400,-0x8(%rbp)
mov    $0x0,%al
call   1050 <nand_init@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    11ce <load_program_from_nand+0x9e>
mov    -0x4(%rbp),%eax
and    $0x3f,%eax
cmp    $0x0,%eax
jne    11a0 <load_program_from_nand+0x70>
mov    -0x4(%rbp),%edi
shr    $0x6,%edi
call   1030 <nand_isbadblock_p2048@plt>
cmp    $0x0,%rax
je     119b <load_program_from_nand+0x6b>
mov    -0x4(%rbp),%eax
add    $0x40,%eax
mov    %eax,-0x4(%rbp)
jmp    115d <load_program_from_nand+0x2d>
jmp    11a0 <load_program_from_nand+0x70>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%eax
shl    $0xb,%eax
mov    %eax,%eax
add    %rax,%rsi
call   1040 <nand_read_page2048@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    115d <load_program_from_nand+0x2d>
mov    $0x30000000,%eax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x8,%rdx
mov    %rdx,-0x20(%rbp)
mov    %rcx,(%rax)
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x8,%rdx
mov    %rdx,-0x20(%rbp)
mov    %rcx,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_program_from_nand>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <nand_init@plt>
mov    $0x1,%r15b
xor    %ebp,%ebp
jmp    1182 <load_program_from_nand+0x52>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rsi
shl    $0xb,%rsi
add    $0x33e00000,%rsi
mov    %ebx,%edi
call   1040 <nand_read_page2048@plt>
lea    0x1(%rbx),%ebp
cmp    $0x3ff,%r14
lea    0x1(%r14),%rax
setb   %r15b
mov    %rax,%r14
cmp    $0x400,%rax
je     11da <load_program_from_nand+0xaa>
mov    %ebp,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   $0x3f,%bpl
jne    1150 <load_program_from_nand+0x20>
mov    %ebx,%edi
shr    $0x6,%edi
call   1030 <nand_isbadblock_p2048@plt>
test   %rax,%rax
je     1150 <load_program_from_nand+0x20>
add    $0x40,%ebx
test   $0x1,%r15b
jne    1190 <load_program_from_nand+0x60>
mov    %r14d,%eax
mov    %rax,0x30000000
30
mov    %ebx,%eax
mov    %rax,0x30000008
30
movq   $0x400,0x30000010
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0x1,%ebx
mov    $0x400,%eax
jmp    11b1 <load_program_from_nand+0x81>

LLVM IR (-O0):
define void @load_program_from_nand() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
store i8* inttoptr (i64 870318080 to i8*), i8** %4
store i32 1024, i32* %2
%6 = call i32 (...) @nand_init()
store i32 0, i32* %1
store i32 0, i32* %3
br label %7
7: ; preds = %24, %20, %0
%8 = load i32, i32* %3
%9 = load i32, i32* %2
%10 = icmp ult i32 %8, %9
br i1 %10, label %11, label %36
11: ; preds = %7
%12 = load i32, i32* %1
%13 = and i32 %12, 63
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %11
%16 = load i32, i32* %1
%17 = lshr i32 %16, 6
%18 = call i64 @nand_isbadblock_p2048(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* %1
%22 = add i32 %21, 64
store i32 %22, i32* %1
br label %7
23: ; preds = %15
br label %24
24: ; preds = %23, %11
%25 = load i32, i32* %1
%26 = load i8*, i8** %4
%27 = load i32, i32* %3
%28 = shl i32 %27, 11
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
%31 = call i32 @nand_read_page2048(i32 %25, i8* %30)
%32 = load i32, i32* %1
%33 = add i32 %32, 1
store i32 %33, i32* %1
%34 = load i32, i32* %3
%35 = add i32 %34, 1
store i32 %35, i32* %3
br label %7
36: ; preds = %7
store i64* inttoptr (i64 805306368 to i64*), i64** %5
%37 = load i32, i32* %3
%38 = zext i32 %37 to i64
%39 = load i64*, i64** %5
%40 = getelementptr inbounds i64, i64* %39, i32 1
store i64* %40, i64** %5
store i64 %38, i64* %39
%41 = load i32, i32* %1
%42 = zext i32 %41 to i64
%43 = load i64*, i64** %5
%44 = getelementptr inbounds i64, i64* %43, i32 1
store i64* %44, i64** %5
store i64 %42, i64* %43
%45 = load i32, i32* %2
%46 = zext i32 %45 to i64
%47 = load i64*, i64** %5
store i64 %46, i64* %47
ret void
}

LLVM IR (-O2):
define void @load_program_from_nand() {
%1 = tail call i32 (...) @nand_init()
br label %2
2: ; preds = %0, %16
%3 = phi i64 [ 0, %0 ], [ %21, %16 ]
%4 = phi i1 [ true, %0 ], [ %22, %16 ]
%5 = phi i32 [ 0, %0 ], [ %20, %16 ]
%6 = and i32 %5, 63
%7 = icmp eq i32 %6, 0
br label %8
8: ; preds = %2, %14
%9 = phi i32 [ %5, %2 ], [ %15, %14 ]
br i1 %7, label %10, label %16
10: ; preds = %8
%11 = lshr i32 %9, 6
%12 = tail call i64 @nand_isbadblock_p2048(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = add i32 %9, 64
br i1 %4, label %8, label %24
16: ; preds = %10, %8
%17 = shl nuw nsw i64 %3, 11
%18 = getelementptr inbounds i8, i8* inttoptr (i64 870318080 to i8*), i64 %17
%19 = tail call i32 @nand_read_page2048(i32 %9, i8* %18)
%20 = add i32 %9, 1
%21 = add nuw nsw i64 %3, 1
%22 = icmp ult i64 %3, 1023
%23 = icmp eq i64 %21, 1024
br i1 %23, label %26, label %2
24: ; preds = %14
%25 = and i64 %3, 4294967295
br label %26
26: ; preds = %16, %24
%27 = phi i64 [ %25, %24 ], [ 1024, %16 ]
%28 = phi i32 [ %15, %24 ], [ %20, %16 ]
store i64 %27, i64* inttoptr (i64 805306368 to i64*)
%29 = zext i32 %28 to i64
store i64 %29, i64* inttoptr (i64 805306376 to i64*)
store i64 1024, i64* inttoptr (i64 805306384 to i64*)
ret void
}