#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 int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ bzero(unsigned char *, int);
int /*<<< orphan*/ cdi_flash_write(unsigned char *, int);
int /*<<< orphan*/ cdi_set_address(unsigned long);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int);
int hexfile_program(unsigned char *page_buffer, unsigned char *page_table) {
  int retry = 0;
  int error = 0;
  unsigned char buffer[256];
  int length;
  int i;
  int pages;
  unsigned long ext_addr = 0;
  unsigned short int addr = 0;
  printf("Starting programming.\n");
  error = 0;
  for (i = 0; i < 64; i++) {
    if (page_table[i] != 0) {
      ext_addr = 2048 * i;
      bzero(buffer, sizeof(buffer));
      usleep(3000);
      cdi_set_address(ext_addr);
      printf("\r                                                       \r");
      fflush(stdout);
      cdi_flash_write(&page_buffer[ext_addr], 2048);
      usleep(20000);
    }
  }
  return (1);
}


x86 assembly (clang, -O0):
<hexfile_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x138(%rbp)
movw   $0x0,-0x13a(%rbp)
lea    0xe64(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x128(%rbp)
cmpl   $0x40,-0x128(%rbp)
jge    1262 <hexfile_program+0x102>
mov    -0x10(%rbp),%rax
movslq -0x128(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1249 <hexfile_program+0xe9>
mov    -0x128(%rbp),%eax
shl    $0xb,%eax
cltq
mov    %rax,-0x138(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1070 <bzero@plt>
mov    $0xbb8,%edi
call   1080 <usleep@plt>
mov    -0x138(%rbp),%rdi
call   1030 <cdi_set_address@plt>
lea    0xe00(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    -0x8(%rbp),%rdi
add    -0x138(%rbp),%rdi
mov    $0x800,%esi
call   1040 <cdi_flash_write@plt>
mov    $0x4e20,%edi
call   1080 <usleep@plt>
jmp    124e <hexfile_program+0xee>
mov    -0x128(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x128(%rbp)
jmp    11b2 <hexfile_program+0x52>
mov    $0x1,%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexfile_program>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xec4(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
lea    0xe7c(%rip),%r12
mov    0x2e55(%rip),%r13
jmp    11a4 <hexfile_program+0x44>
nopl   (%rax)
add    $0x1,%r15
add    $0x800,%rbx
cmp    $0x20000,%rbx
je     11e7 <hexfile_program+0x87>
cmpb   $0x0,(%r15)
je     1190 <hexfile_program+0x30>
mov    $0xbb8,%edi
call   1080 <usleep@plt>
mov    %rbx,%rdi
call   1030 <cdi_set_address@plt>
mov    %r12,%rdi
call   1060 <printf@plt>
mov    0x0(%r13),%edi
call   1070 <fflush@plt>
lea    (%r14,%rbx,1),%rdi
mov    $0x800,%esi
call   1050 <cdi_flash_write@plt>
mov    $0x4e20,%edi
call   1080 <usleep@plt>
jmp    1190 <hexfile_program+0x30>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Starting programming.\0A\00"
@.str.1 = [58 x i8] c"\0D \0D\00"
@stdout = global i32 0
define i32 @hexfile_program(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i16
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i32 0, i32* %6
store i64 0, i64* %11
store i16 0, i16* %12
%13 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
store i32 0, i32* %9
br label %14
14: ; preds = %43, %2
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 64
br i1 %16, label %17, label %46
17: ; preds = %14
%18 = load i8*, i8** %4
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %42
25: ; preds = %17
%26 = load i32, i32* %9
%27 = mul nsw i32 2048, %26
%28 = sext i32 %27 to i64
store i64 %28, i64* %11
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%30 = call i32 @bzero(i8* %29, i32 256)
%31 = call i32 @usleep(i32 3000)
%32 = load i64, i64* %11
%33 = call i32 @cdi_set_address(i64 %32)
%34 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @stdout
%36 = call i32 @fflush(i32 %35)
%37 = load i8*, i8** %3
%38 = load i64, i64* %11
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = call i32 @cdi_flash_write(i8* %39, i32 2048)
%41 = call i32 @usleep(i32 20000)
br label %42
42: ; preds = %25, %17
br label %43
43: ; preds = %42
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
store i32 %45, i32* %9
br label %14
46: ; preds = %14
ret i32 1
}

LLVM IR (-O2):
@.str.1 = [58 x i8] c"\0D \0D\00"
@stdout = global i32 0
@str = [22 x i8] c"Starting programming.\00"
define i32 @hexfile_program(i8* %0, i8* nocapture %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %4
4: ; preds = %2, %19
%5 = phi i64 [ 0, %2 ], [ %20, %19 ]
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 0
br i1 %8, label %19, label %9
9: ; preds = %4
%10 = shl nuw nsw i64 %5, 11
%11 = tail call i32 @usleep(i32 3000)
%12 = tail call i32 @cdi_set_address(i64 %10)
%13 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @stdout
%15 = tail call i32 @fflush(i32 %14)
%16 = getelementptr inbounds i8, i8* %0, i64 %10
%17 = tail call i32 @cdi_flash_write(i8* %16, i32 2048)
%18 = tail call i32 @usleep(i32 20000)
br label %19
19: ; preds = %4, %9
%20 = add nuw nsw i64 %5, 1
%21 = icmp eq i64 %20, 64
br i1 %21, label %22, label %4
22: ; preds = %19
ret i32 1
}