#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*/ cons_hook();
int /*<<< orphan*/ print_message(char *);
void XferToRam(void) {
  unsigned int temp;
  void (*entry_point)(void);
  cons_hook();
  print_message("Copying image from Flash to Ram.\n");
  for (temp = 0; temp < (0x100000 - 0x20000); temp = temp + 4) {
    *(volatile unsigned int *)(temp + 0xa0400000) =
        *(volatile unsigned int *)(temp + 0xbf020000);
    if (*(volatile unsigned int *)(temp + 0xa0400000) !=
        *(volatile unsigned int *)(temp + 0xbf020000)) {
      print_message("Error!: copy verification failed.\n");
      break;
    }
  }
  print_message("Now jumping to the code just xferred to ram.\n");
  entry_point = (void *)0x80400000;
  entry_point();
}


x86 assembly (clang, -O0):
<XferToRam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <cons_hook@plt>
lea    0xeca(%rip),%rdi
call   1040 <print_message@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0xe0000,-0x4(%rbp)
jae    11ac <XferToRam+0x8c>
mov    -0x4(%rbp),%eax
add    $0xbf020000,%eax
mov    %eax,%eax
mov    (%rax),%ecx
mov    -0x4(%rbp),%eax
add    $0xa0400000,%eax
mov    %eax,%eax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
add    $0xa0400000,%eax
mov    %eax,%eax
mov    (%rax),%eax
mov    -0x4(%rbp),%ecx
add    $0xbf020000,%ecx
mov    %ecx,%ecx
mov    (%rcx),%ecx
cmp    %ecx,%eax
je     1199 <XferToRam+0x79>
lea    0xe93(%rip),%rdi
call   1040 <print_message@plt>
jmp    11ac <XferToRam+0x8c>
jmp    119e <XferToRam+0x7e>
mov    -0x4(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x4(%rbp)
jmp    1142 <XferToRam+0x22>
lea    0xe92(%rip),%rdi
call   1040 <print_message@plt>
mov    $0x80400000,%eax
mov    %rax,-0x10(%rbp)
call   *-0x10(%rbp)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<XferToRam>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <cons_hook@plt>
lea    0xecf(%rip),%rdi
call   1040 <print_message@plt>
mov    $0x80400000,%ecx
nopl   0x0(%rax,%rax,1)
mov    0x3ec20000(%rcx,%rbx,1),%eax
mov    %eax,0x20000000(%rcx,%rbx,1)
mov    0x20000000(%rcx,%rbx,1),%eax
cmp    0x3ec20000(%rcx,%rbx,1),%eax
jne    1192 <XferToRam+0x72>
mov    0x3ec20004(%rcx,%rbx,1),%eax
mov    %eax,0x20000004(%rcx,%rbx,1)
mov    0x20000004(%rcx,%rbx,1),%eax
cmp    0x3ec20004(%rcx,%rbx,1),%eax
jne    1192 <XferToRam+0x72>
lea    0x8(%rbx),%rax
add    $0x4,%rbx
cmp    $0xdfffb,%rbx
mov    %rax,%rbx
jbe    1140 <XferToRam+0x20>
jmp    119e <XferToRam+0x7e>
lea    0xe89(%rip),%rdi
call   1040 <print_message@plt>
lea    0xea0(%rip),%rdi
call   1040 <print_message@plt>
mov    $0x80400000,%eax
pop    %rbx
jmp    *%rax

LLVM IR (-O0):
@.str = [34 x i8] c"Copying image from Flash to Ram.\0A\00"
@.str.1 = [35 x i8] c"Error!: copy verification failed.\0A\00"
@.str.2 = [46 x i8] c"Now jumping to the code just xferred to ram.\0A\00"
define void @XferToRam() {
%1 = alloca i32
%2 = alloca void ()*
%3 = call i32 (...) @cons_hook()
%4 = call i32 @print_message(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %5
5: ; preds = %32, %0
%6 = load i32, i32* %1
%7 = icmp ult i32 %6, 917504
br i1 %7, label %8, label %35
8: ; preds = %5
%9 = load i32, i32* %1
%10 = add i32 %9, -1090387968
%11 = zext i32 %10 to i64
%12 = inttoptr i64 %11 to i32*
%13 = load volatile i32, i32* %12
%14 = load i32, i32* %1
%15 = add i32 %14, -1606418432
%16 = zext i32 %15 to i64
%17 = inttoptr i64 %16 to i32*
store volatile i32 %13, i32* %17
%18 = load i32, i32* %1
%19 = add i32 %18, -1606418432
%20 = zext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = load volatile i32, i32* %21
%23 = load i32, i32* %1
%24 = add i32 %23, -1090387968
%25 = zext i32 %24 to i64
%26 = inttoptr i64 %25 to i32*
%27 = load volatile i32, i32* %26
%28 = icmp ne i32 %22, %27
br i1 %28, label %29, label %31
29: ; preds = %8
%30 = call i32 @print_message(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %35
31: ; preds = %8
br label %32
32: ; preds = %31
%33 = load i32, i32* %1
%34 = add i32 %33, 4
store i32 %34, i32* %1
br label %5
35: ; preds = %29, %5
%36 = call i32 @print_message(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
store void ()* inttoptr (i64 2151677952 to void ()*), void ()** %2
%37 = load void ()*, void ()** %2
ret void
}

LLVM IR (-O2):
@.str = [34 x i8] c"Copying image from Flash to Ram.\0A\00"
@.str.1 = [35 x i8] c"Error!: copy verification failed.\0A\00"
@.str.2 = [46 x i8] c"Now jumping to the code just xferred to ram.\0A\00"
define void @XferToRam() {
%1 = tail call i32 (...) @cons_hook()
%2 = tail call i32 @print_message(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %16
3: ; preds = %16
%4 = add nuw nsw i64 %17, 3204579332
%5 = inttoptr i64 %4 to i32*
%6 = load volatile i32, i32* %5
%7 = add nuw nsw i64 %17, 2688548868
%8 = inttoptr i64 %7 to i32*
store volatile i32 %6, i32* %8
%9 = load volatile i32, i32* %8
%10 = load volatile i32, i32* %5
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %26
12: ; preds = %3
%13 = or i64 %17, 4
%14 = add nuw nsw i64 %17, 8
%15 = icmp ult i64 %13, 917500
br i1 %15, label %16, label %28
16: ; preds = %12, %0
%17 = phi i64 [ 0, %0 ], [ %14, %12 ]
%18 = add nuw nsw i64 %17, 3204579328
%19 = inttoptr i64 %18 to i32*
%20 = load volatile i32, i32* %19
%21 = add nuw nsw i64 %17, 2688548864
%22 = inttoptr i64 %21 to i32*
store volatile i32 %20, i32* %22
%23 = load volatile i32, i32* %22
%24 = load volatile i32, i32* %19
%25 = icmp eq i32 %23, %24
br i1 %25, label %3, label %26
26: ; preds = %3, %16
%27 = tail call i32 @print_message(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %12, %26
%29 = tail call i32 @print_message(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
tail call void inttoptr (i64 2151677952 to void ()*)()
ret void
}