#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*/ nand_read(int /*<<< orphan*/, int);
int /*<<< orphan*/ sdram_read(unsigned int, unsigned int);
scalar_t__ uart_get_file(unsigned char *);
char uart_getc();
int /*<<< orphan*/ uart_printf(char *);
int /*<<< orphan*/ uart_puts(char *);
int /*<<< orphan*/ uart_send_int(int);
int /*<<< orphan*/ write_to_boot(unsigned int, int);
void update_program(void) {
  unsigned int addr = 0x55000000;
  unsigned char *buffer = (unsigned char *)addr;
  unsigned int wait_time = 0;
  int start = 0;
  int length = 0;
  uart_puts("please send file! (wait for a while)");
  while (1) {
    if (uart_get_file(&buffer[length])) {
      start = 1;
      wait_time = 0;
      ++length;
    } else {
      if (start)
        ++wait_time;
    }
    if (wait_time == 0x00FFFFFF)
      break;
  }
  if (length == 0) {
    uart_puts("No file receive.");
    return;
  }
  uart_printf("\nReceive ");
  uart_send_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;
  }
  uart_puts("Updating ...");
  nand_erase_block(0);
  write_to_boot(addr, length);
  uart_puts("Update done!");
  uart_puts("\nDo you want to show the content? (Y/n)");
  uart_printf(" >> ");
  ch = uart_getc();
  if (ch == 'Y' || ch == 'y')
    nand_read(0, 0x5000);
  return;
}


x86 assembly (clang, -O0):
<update_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x55000000,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe3e(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
call   1070 <uart_get_file@plt>
cmp    $0x0,%rax
je     11fd <update_program+0x6d>
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1215 <update_program+0x85>
cmpl   $0x0,-0x18(%rbp)
je     1210 <update_program+0x80>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1215 <update_program+0x85>
cmpl   $0xffffff,-0x14(%rbp)
jne    1227 <update_program+0x97>
jmp    122c <update_program+0x9c>
jmp    11c7 <update_program+0x37>
cmpl   $0x0,-0x1c(%rbp)
jne    1247 <update_program+0xb7>
lea    0xde8(%rip),%rdi
call   10b0 <uart_puts@plt>
jmp    1377 <update_program+0x1e7>
lea    0xde8(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <uart_send_int@plt>
lea    0xdde(%rip),%rdi
call   1030 <uart_printf@plt>
lea    0xde0(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
add    $0xf,%esi
call   1080 <sdram_read@plt>
lea    0xdde(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x4(%rbp),%edi
add    -0x1c(%rbp),%edi
sub    $0x1,%edi
sub    $0xf,%edi
mov    -0x4(%rbp),%esi
add    -0x1c(%rbp),%esi
sub    $0x1,%esi
call   1080 <sdram_read@plt>
lea    0xdd0(%rip),%rdi
call   10b0 <uart_puts@plt>
lea    0xdec(%rip),%rdi
call   1030 <uart_printf@plt>
mov    $0x0,%al
call   1090 <uart_getc@plt>
mov    %al,-0x1d(%rbp)
lea    0xd7d(%rip),%rdi
call   10b0 <uart_puts@plt>
movsbl -0x1d(%rbp),%eax
cmp    $0x59,%eax
je     1300 <update_program+0x170>
movsbl -0x1d(%rbp),%eax
cmp    $0x79,%eax
je     1300 <update_program+0x170>
lea    0xdb5(%rip),%rdi
call   10b0 <uart_puts@plt>
jmp    1377 <update_program+0x1e7>
lea    0xdbc(%rip),%rdi
call   10b0 <uart_puts@plt>
xor    %edi,%edi
call   1050 <nand_erase_block@plt>
mov    -0x4(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <write_to_boot@plt>
lea    0xdab(%rip),%rdi
call   10b0 <uart_puts@plt>
lea    0xdac(%rip),%rdi
call   10b0 <uart_puts@plt>
lea    0xd69(%rip),%rdi
call   1030 <uart_printf@plt>
mov    $0x0,%al
call   1090 <uart_getc@plt>
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x59,%eax
je     1366 <update_program+0x1d6>
movsbl -0x1d(%rbp),%eax
cmp    $0x79,%eax
jne    1372 <update_program+0x1e2>
xor    %edi,%edi
mov    $0x5000,%esi
call   10a0 <nand_read@plt>
jmp    1377 <update_program+0x1e7>
add    $0x20,%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    %r14d,%r14d
mov    $0x1,%r15d
xor    %ebp,%ebp
xor    %r12d,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%r13d
mov    %ebx,%edi
add    $0x55000000,%rdi
call   1070 <uart_get_file@plt>
xor    %ecx,%ecx
test   %r12d,%r12d
setne  %cl
add    %ecx,%ebp
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
cmovne %r14d,%ebp
cmovne %r15d,%ecx
add    %r13d,%ebx
mov    %ecx,%r12d
cmp    $0xffffff,%ebp
jne    11c0 <update_program+0x30>
test   %ebx,%ebx
je     12f9 <update_program+0x169>
lea    0xe2e(%rip),%rdi
call   1030 <uart_printf@plt>
mov    %ebx,%edi
call   1040 <uart_send_int@plt>
lea    0xe25(%rip),%rdi
call   1030 <uart_printf@plt>
lea    0xe27(%rip),%rdi
call   1030 <uart_printf@plt>
mov    $0x55000000,%edi
mov    $0x5500000f,%esi
call   1080 <sdram_read@plt>
lea    0xe24(%rip),%rdi
call   1030 <uart_printf@plt>
lea    0x54ffffff(%rbx),%esi
lea    0x54fffff0(%rbx),%edi
call   1080 <sdram_read@plt>
lea    0xe1f(%rip),%rdi
call   10b0 <uart_puts@plt>
lea    0xe3b(%rip),%rdi
call   1030 <uart_printf@plt>
xor    %eax,%eax
call   1090 <uart_getc@plt>
mov    %eax,%ebp
lea    0xdcd(%rip),%rdi
call   10b0 <uart_puts@plt>
and    $0xdf,%bpl
cmp    $0x59,%bpl
jne    1302 <update_program+0x172>
lea    0xe2d(%rip),%rdi
call   10b0 <uart_puts@plt>
xor    %edi,%edi
call   1050 <nand_erase_block@plt>
mov    $0x55000000,%edi
mov    %ebx,%esi
call   1060 <write_to_boot@plt>
lea    0xe1b(%rip),%rdi
call   10b0 <uart_puts@plt>
lea    0xe1c(%rip),%rdi
call   10b0 <uart_puts@plt>
lea    0xdd9(%rip),%rdi
call   1030 <uart_printf@plt>
xor    %eax,%eax
call   1090 <uart_getc@plt>
and    $0xdf,%al
cmp    $0x59,%al
jne    131c <update_program+0x18c>
xor    %edi,%edi
mov    $0x5000,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <nand_read@plt>
lea    0xd25(%rip),%rdi
jmp    1309 <update_program+0x179>
lea    0xda2(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <uart_puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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