#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_erase_block(int /*<<< orphan*/);
int /*<<< orphan*/ sdram_read(unsigned int, unsigned int);
scalar_t__ uart_get_file(unsigned char *);
int uart_get_file_size();
char uart_getc();
int /*<<< orphan*/ uart_print_int(int);
int /*<<< orphan*/ uart_printf(char *);
int /*<<< orphan*/ uart_puts(char *);
int /*<<< orphan*/ write_to_boot(unsigned int, int);
int update_program(void) {
  unsigned int addr = 0x5FC00000;
  unsigned char *buffer = (unsigned char *)addr;
  unsigned int wait_time = 0;
  int start = 0;
  int length = 0;
  int size = 0;
  uart_puts("Please send binary file!");
  size = uart_get_file_size();
  uart_printf("file size = ");
  uart_print_int(size);
  uart_puts(" bytes\nReceiving binary file ...");
  while (1) {
    if (uart_get_file(&buffer[length])) {
      start = 1;
      wait_time = 0;
      ++length;
    } else {
      if (start)
        ++wait_time;
    }
    if (length == size)
      break;
    if (wait_time == 0x00FFFFFF)
      break;
  }
  if (length == 0) {
    uart_puts("No file receive.");
    return -1;
  }
  uart_printf("\nReceived ");
  uart_print_int(length);
  uart_printf(" bytes data.\n");
  uart_printf("\nThe first 16 byte are:");
  sdram_read(addr, addr + 0xF);
  uart_printf("\nThe last 16 bytes are:");
  sdram_read(addr + length - 1 - 0xF, addr + length - 1);
  uart_puts("\nDo you sure update this program? (Y/n)");
  uart_printf(" >> ");
  char ch = uart_getc();
  uart_puts("");
  if (ch != 'Y' && ch != 'y') {
    uart_puts("You have cancel update.");
    return -1;
  }
  uart_puts("Updating ...");
  nand_erase_block(0);
  write_to_boot(addr, length);
  uart_puts("Update done!");
  return 0;
}


x86 assembly (clang, -O0):
<update_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x5fc00000,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xe37(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    $0x0,%al
call   1070 <uart_get_file_size@plt>
mov    %eax,-0x20(%rbp)
lea    0xe3a(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x20(%rbp),%edi
call   1040 <uart_print_int@plt>
lea    0xe33(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
call   1080 <uart_get_file@plt>
cmp    $0x0,%rax
je     122e <update_program+0x9e>
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1246 <update_program+0xb6>
cmpl   $0x0,-0x18(%rbp)
je     1241 <update_program+0xb1>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1246 <update_program+0xb6>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    1257 <update_program+0xc7>
jmp    126e <update_program+0xde>
cmpl   $0xffffff,-0x14(%rbp)
jne    1269 <update_program+0xd9>
jmp    126e <update_program+0xde>
jmp    11f8 <update_program+0x68>
cmpl   $0x0,-0x1c(%rbp)
jne    1290 <update_program+0x100>
lea    0xdc8(%rip),%rdi
call   10b0 <uart_puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <update_program+0x1f1>
lea    0xdc1(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <uart_print_int@plt>
lea    0xdb8(%rip),%rdi
call   1030 <uart_printf@plt>
lea    0xdba(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
add    $0xf,%esi
call   1090 <sdram_read@plt>
lea    0xdb8(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x8(%rbp),%edi
add    -0x1c(%rbp),%edi
sub    $0x1,%edi
sub    $0xf,%edi
mov    -0x8(%rbp),%esi
add    -0x1c(%rbp),%esi
sub    $0x1,%esi
call   1090 <sdram_read@plt>
lea    0xdaa(%rip),%rdi
call   10b0 <uart_puts@plt>
lea    0xdc6(%rip),%rdi
call   1030 <uart_printf@plt>
mov    $0x0,%al
call   10a0 <uart_getc@plt>
mov    %al,-0x21(%rbp)
lea    0xd57(%rip),%rdi
call   10b0 <uart_puts@plt>
movsbl -0x21(%rbp),%eax
cmp    $0x59,%eax
je     1350 <update_program+0x1c0>
movsbl -0x21(%rbp),%eax
cmp    $0x79,%eax
je     1350 <update_program+0x1c0>
lea    0xd8f(%rip),%rdi
call   10b0 <uart_puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <update_program+0x1f1>
lea    0xd8f(%rip),%rdi
call   10b0 <uart_puts@plt>
xor    %edi,%edi
call   1050 <nand_erase_block@plt>
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <write_to_boot@plt>
lea    0xd7e(%rip),%rdi
call   10b0 <uart_puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_program>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe5e(%rip),%rdi
call   10b0 <uart_puts@plt>
xor    %eax,%eax
call   1070 <uart_get_file_size@plt>
mov    %eax,%r12d
lea    0xe61(%rip),%rdi
call   1030 <uart_printf@plt>
mov    %r12d,%edi
call   1040 <uart_print_int@plt>
lea    0xe5a(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    $0x1,%r15d
xor    %ebp,%ebp
xor    %ebx,%ebx
xor    %r13d,%r13d
xchg   %ax,%ax
mov    %r13d,%r14d
mov    %r13d,%edi
add    $0x5fc00000,%rdi
call   1080 <uart_get_file@plt>
xor    %ecx,%ecx
test   %ebx,%ebx
setne  %cl
add    %ecx,%ebp
xor    %r13d,%r13d
test   %rax,%rax
setne  %r13b
mov    $0x0,%eax
cmovne %eax,%ebp
mov    %ecx,%ebx
cmovne %r15d,%ebx
add    %r14d,%r13d
cmp    %r12d,%r13d
je     1223 <update_program+0x93>
cmp    $0xffffff,%ebp
jne    11e0 <update_program+0x50>
mov    $0xffffffff,%ebp
test   %r13d,%r13d
je     12ee <update_program+0x15e>
lea    0xe20(%rip),%rdi
call   1030 <uart_printf@plt>
mov    %r13d,%edi
call   1040 <uart_print_int@plt>
lea    0xe17(%rip),%rdi
call   1030 <uart_printf@plt>
lea    0xe19(%rip),%rdi
call   1030 <uart_printf@plt>
mov    $0x5fc00000,%edi
mov    $0x5fc0000f,%esi
call   1090 <sdram_read@plt>
lea    0xe16(%rip),%rdi
call   1030 <uart_printf@plt>
lea    0x5fbfffff(%r13),%esi
lea    0x5fbffff0(%r13),%edi
call   1090 <sdram_read@plt>
lea    0xe0f(%rip),%rdi
call   10b0 <uart_puts@plt>
lea    0xe2b(%rip),%rdi
call   1030 <uart_printf@plt>
xor    %eax,%eax
call   10a0 <uart_getc@plt>
mov    %eax,%r14d
lea    0xdbc(%rip),%rdi
call   10b0 <uart_puts@plt>
and    $0xdf,%r14b
cmp    $0x59,%r14b
jne    12f7 <update_program+0x167>
lea    0xe1c(%rip),%rdi
call   10b0 <uart_puts@plt>
xor    %edi,%edi
call   1050 <nand_erase_block@plt>
mov    $0x5fc00000,%edi
mov    %r13d,%esi
call   1060 <write_to_boot@plt>
lea    0xe09(%rip),%rdi
xor    %ebp,%ebp
jmp    12fe <update_program+0x16e>
lea    0xd52(%rip),%rdi
jmp    12fe <update_program+0x16e>
lea    0xdd0(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Please send binary file!\00"
@.str.1 = [13 x i8] c"file size = \00"
@.str.2 = [33 x i8] c" bytes\0AReceiving binary file ...\00"
@.str.3 = [17 x i8] c"No file receive.\00"
@.str.4 = [11 x i8] c"\0AReceived \00"
@.str.5 = [14 x i8] c" bytes data.\0A\00"
@.str.6 = [24 x i8] c"\0AThe first 16 byte are:\00"
@.str.7 = [24 x i8] c"\0AThe last 16 bytes are:\00"
@.str.8 = [40 x i8] c"\0ADo you sure update this program? (Y/n)\00"
@.str.9 = [5 x i8] c" >> \00"
@.str.10 = [1 x i8] zeroinitializer
@.str.11 = [24 x i8] c"You have cancel update.\00"
@.str.12 = [13 x i8] c"Updating ...\00"
@.str.13 = [13 x i8] c"Update done!\00"
define i32 @update_program() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i32 1606418432, i32* %2
%9 = load i32, i32* %2
%10 = zext i32 %9 to i64
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
%12 = call i32 @uart_puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%13 = call i32 (...) @uart_get_file_size()
store i32 %13, i32* %7
%14 = call i32 @uart_printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* %7
%16 = call i32 @uart_print_int(i32 %15)
%17 = call i32 @uart_puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %0, %44
%19 = load i8*, i8** %3
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = call i64 @uart_get_file(i8* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %18
store i32 1, i32* %5
store i32 0, i32* %4
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
store i32 %27, i32* %6
br label %35
28: ; preds = %18
%29 = load i32, i32* %5
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* %4
%33 = add i32 %32, 1
store i32 %33, i32* %4
br label %34
34: ; preds = %31, %28
br label %35
35: ; preds = %34, %25
%36 = load i32, i32* %6
%37 = load i32, i32* %7
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
br label %45
40: ; preds = %35
%41 = load i32, i32* %4
%42 = icmp eq i32 %41, 16777215
br i1 %42, label %43, label %44
43: ; preds = %40
br label %45
44: ; preds = %40
br label %18
45: ; preds = %43, %39
%46 = load i32, i32* %6
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @uart_puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %91
50: ; preds = %45
%51 = call i32 @uart_printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%52 = load i32, i32* %6
%53 = call i32 @uart_print_int(i32 %52)
%54 = call i32 @uart_printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%55 = call i32 @uart_printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%56 = load i32, i32* %2
%57 = load i32, i32* %2
%58 = add i32 %57, 15
%59 = call i32 @sdram_read(i32 %56, i32 %58)
%60 = call i32 @uart_printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%61 = load i32, i32* %2
%62 = load i32, i32* %6
%63 = add i32 %61, %62
%64 = sub i32 %63, 1
%65 = sub i32 %64, 15
%66 = load i32, i32* %2
%67 = load i32, i32* %6
%68 = add i32 %66, %67
%69 = sub i32 %68, 1
%70 = call i32 @sdram_read(i32 %65, i32 %69)
%71 = call i32 @uart_puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.8, i64 0, i64 0))
%72 = call i32 @uart_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%73 = call signext i8 (...) @uart_getc()
store i8 %73, i8* %8
%74 = call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.10, i64 0, i64 0))
%75 = load i8, i8* %8
%76 = sext i8 %75 to i32
%77 = icmp ne i32 %76, 89
br i1 %77, label %78, label %84
78: ; preds = %50
%79 = load i8, i8* %8
%80 = sext i8 %79 to i32
%81 = icmp ne i32 %80, 121
br i1 %81, label %82, label %84
82: ; preds = %78
%83 = call i32 @uart_puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0))
store i32 -1, i32* %1
br label %91
84: ; preds = %78, %50
%85 = call i32 @uart_puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.12, i64 0, i64 0))
%86 = call i32 @nand_erase_block(i32 0)
%87 = load i32, i32* %2
%88 = load i32, i32* %6
%89 = call i32 @write_to_boot(i32 %87, i32 %88)
%90 = call i32 @uart_puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.13, i64 0, i64 0))
store i32 0, i32* %1
br label %91
91: ; preds = %84, %82, %48
%92 = load i32, i32* %1
ret i32 %92
}

LLVM IR (-O2):
@.str = [25 x i8] c"Please send binary file!\00"
@.str.1 = [13 x i8] c"file size = \00"
@.str.2 = [33 x i8] c" bytes\0AReceiving binary file ...\00"
@.str.3 = [17 x i8] c"No file receive.\00"
@.str.4 = [11 x i8] c"\0AReceived \00"
@.str.5 = [14 x i8] c" bytes data.\0A\00"
@.str.6 = [24 x i8] c"\0AThe first 16 byte are:\00"
@.str.7 = [24 x i8] c"\0AThe last 16 bytes are:\00"
@.str.8 = [40 x i8] c"\0ADo you sure update this program? (Y/n)\00"
@.str.9 = [5 x i8] c" >> \00"
@.str.10 = [1 x i8] zeroinitializer
@.str.11 = [24 x i8] c"You have cancel update.\00"
@.str.12 = [13 x i8] c"Updating ...\00"
@.str.13 = [13 x i8] c"Update done!\00"
define i32 @update_program() {
%1 = tail call i32 @uart_puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 (...) @uart_get_file_size()
%3 = tail call i32 @uart_printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 @uart_print_int(i32 %2)
%5 = tail call i32 @uart_puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %6
6: ; preds = %6, %0
%7 = phi i32 [ 0, %0 ], [ %17, %6 ]
%8 = phi i32 [ 0, %0 ], [ %18, %6 ]
%9 = phi i32 [ 0, %0 ], [ %21, %6 ]
%10 = zext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* inttoptr (i64 1606418432 to i8*), i64 %10
%12 = tail call i64 @uart_get_file(i8* %11)
%13 = icmp eq i64 %12, 0
%14 = icmp ne i32 %8, 0
%15 = zext i1 %14 to i32
%16 = add i32 %7, %15
%17 = select i1 %13, i32 %16, i32 0
%18 = select i1 %13, i32 %15, i32 1
%19 = xor i1 %13, true
%20 = zext i1 %19 to i32
%21 = add nuw nsw i32 %9, %20
%22 = icmp eq i32 %21, %2
%23 = icmp eq i32 %17, 16777215
%24 = select i1 %22, i1 true, i1 %23
br i1 %24, label %25, label %6
25: ; preds = %6
%26 = icmp eq i32 %21, 0
br i1 %26, label %47, label %27
27: ; preds = %25
%28 = tail call i32 @uart_printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%29 = tail call i32 @uart_print_int(i32 %21)
%30 = tail call i32 @uart_printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%31 = tail call i32 @uart_printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%32 = tail call i32 @sdram_read(i32 1606418432, i32 1606418447)
%33 = tail call i32 @uart_printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%34 = add nuw i32 %21, 1606418431
%35 = add nuw i32 %21, 1606418416
%36 = tail call i32 @sdram_read(i32 %35, i32 %34)
%37 = tail call i32 @uart_puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.8, i64 0, i64 0))
%38 = tail call i32 @uart_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%39 = tail call signext i8 (...) @uart_getc()
%40 = tail call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.10, i64 0, i64 0))
%41 = and i8 %39, -33
%42 = icmp eq i8 %41, 89
br i1 %42, label %43, label %47
43: ; preds = %27
%44 = tail call i32 @uart_puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.12, i64 0, i64 0))
%45 = tail call i32 @nand_erase_block(i32 0)
%46 = tail call i32 @write_to_boot(i32 1606418432, i32 %21)
br label %47
47: ; preds = %27, %25, %43
%48 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.13, i64 0, i64 0), %43 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), %25 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0), %27 ]
%49 = phi i32 [ 0, %43 ], [ -1, %25 ], [ -1, %27 ]
%50 = tail call i32 @uart_puts(i8* %48)
ret i32 %49
}