#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_read_id();
int /*<<< orphan*/ nand_read_one_page(int volatile, unsigned int volatile);
int /*<<< orphan*/ nand_reset();
int copy_u_boot_to_dram(int uboot_start_addr, int ram_addr, int u_boot_size) {
  volatile int ofs = 0;
  volatile unsigned int dram_addr = ram_addr;
  if (uboot_start_addr % 2048 || u_boot_size % 2048) {
    return 1;
  }
  nand_reset();
  nand_read_id();
  for (ofs = uboot_start_addr; ofs < uboot_start_addr + u_boot_size;
       ofs += 2048) {
    nand_read_one_page(ofs, dram_addr);
    dram_addr += 2048;
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_u_boot_to_dram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x800,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1176 <copy_u_boot_to_dram+0x46>
mov    -0x10(%rbp),%eax
mov    $0x800,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1182 <copy_u_boot_to_dram+0x52>
movl   $0x1,-0x4(%rbp)
jmp    11d4 <copy_u_boot_to_dram+0xa4>
mov    $0x0,%al
call   1040 <nand_reset@plt>
mov    $0x0,%al
call   1050 <nand_read_id@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x8(%rbp),%ecx
add    -0x10(%rbp),%ecx
cmp    %ecx,%eax
jge    11cd <copy_u_boot_to_dram+0x9d>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <nand_read_one_page@plt>
mov    -0x18(%rbp),%eax
add    $0x800,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x800,%eax
mov    %eax,-0x14(%rbp)
jmp    1196 <copy_u_boot_to_dram+0x66>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_u_boot_to_dram>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0x8(%rsp)
mov    %esi,0xc(%rsp)
mov    %edx,%eax
or     %edi,%eax
mov    $0x1,%r14d
test   $0x7ff,%eax
je     1161 <copy_u_boot_to_dram+0x31>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %edx,%ebx
mov    %edi,%ebp
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <nand_reset@plt>
xor    %eax,%eax
call   1050 <nand_read_id@plt>
mov    %ebp,0x8(%rsp)
add    %ebp,%ebx
nopl   0x0(%rax)
mov    0x8(%rsp),%eax
cmp    %ebx,%eax
jge    1155 <copy_u_boot_to_dram+0x25>
mov    0x8(%rsp),%edi
mov    0xc(%rsp),%esi
call   1030 <nand_read_one_page@plt>
addl   $0x800,0xc(%rsp)
addl   $0x800,0x8(%rsp)
jmp    1180 <copy_u_boot_to_dram+0x50>

LLVM IR (-O0):
define i32 @copy_u_boot_to_dram(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store volatile i32 0, i32* %8
%10 = load i32, i32* %6
store volatile i32 %10, i32* %9
%11 = load i32, i32* %5
%12 = srem i32 %11, 2048
%13 = icmp ne i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %3
%15 = load i32, i32* %7
%16 = srem i32 %15, 2048
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14, %3
store i32 1, i32* %4
br label %39
19: ; preds = %14
%20 = call i32 (...) @nand_reset()
%21 = call i32 (...) @nand_read_id()
%22 = load i32, i32* %5
store volatile i32 %22, i32* %8
br label %23
23: ; preds = %35, %19
%24 = load volatile i32, i32* %8
%25 = load i32, i32* %5
%26 = load i32, i32* %7
%27 = add nsw i32 %25, %26
%28 = icmp slt i32 %24, %27
br i1 %28, label %29, label %38
29: ; preds = %23
%30 = load volatile i32, i32* %8
%31 = load volatile i32, i32* %9
%32 = call i32 @nand_read_one_page(i32 %30, i32 %31)
%33 = load volatile i32, i32* %9
%34 = add i32 %33, 2048
store volatile i32 %34, i32* %9
br label %35
35: ; preds = %29
%36 = load volatile i32, i32* %8
%37 = add nsw i32 %36, 2048
store volatile i32 %37, i32* %8
br label %23
38: ; preds = %23
store i32 0, i32* %4
br label %39
39: ; preds = %38, %18
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
define i32 @copy_u_boot_to_dram(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
store volatile i32 0, i32* %4
%7 = bitcast i32* %5 to i8*
store volatile i32 %1, i32* %5
%8 = or i32 %2, %0
%9 = and i32 %8, 2047
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %3
%12 = tail call i32 (...) @nand_reset()
%13 = tail call i32 (...) @nand_read_id()
store volatile i32 %0, i32* %4
%14 = add nsw i32 %2, %0
%15 = load volatile i32, i32* %4
%16 = icmp slt i32 %15, %14
br i1 %16, label %17, label %27
17: ; preds = %11, %17
%18 = load volatile i32, i32* %4
%19 = load volatile i32, i32* %5
%20 = tail call i32 @nand_read_one_page(i32 %18, i32 %19)
%21 = load volatile i32, i32* %5
%22 = add i32 %21, 2048
store volatile i32 %22, i32* %5
%23 = load volatile i32, i32* %4
%24 = add nsw i32 %23, 2048
store volatile i32 %24, i32* %4
%25 = load volatile i32, i32* %4
%26 = icmp slt i32 %25, %14
br i1 %26, label %17, label %27
27: ; preds = %17, %11, %3
%28 = phi i32 [ 1, %3 ], [ 0, %11 ], [ 0, %17 ]
ret i32 %28
}