#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*/ uint8_t;

/* Variables and functions */
int check_sector(int, int, int /*<<< orphan*/ *, int, int);
int flash_sector_erase(int, int);
int /*<<< orphan*/ printf(char *, ...);
int write_sector(int, int, int /*<<< orphan*/ *, int, int);
int write_firmware(int fd, int start, uint8_t *buf, int count, int sector_size,
                   int page_size) {
  int left = 0;
  int offset = 0;
  int len = 0;
  int next_sector_offset = 0;
  int ret = 0;
  int i = 0;
  left = count;
  offset = start;
  next_sector_offset = (offset / sector_size + 1) * sector_size;
  while (left > 0) {
    len = left > sector_size ? next_sector_offset - offset : left;
    printf("   [%d] handle sector from 0x%x, len = 0x%x\n", i++, offset, len);
    ret = flash_sector_erase(fd, offset);
    if (ret) {
      printf("erase sector failed.\n");
      return -1;
    }
    ret = write_sector(fd, offset, buf, len, page_size);
    if (ret) {
      printf("write secotr failed.\n");
      return -1;
    }
    ret = check_sector(fd, offset, buf, len, (4 * 1024));
    if (ret) {
      printf("check sector failed.\n");
      return -1;
    }
    buf += len;
    offset += len;
    left -= len;
    next_sector_offset += sector_size;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_firmware>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cltd
idivl  -0x20(%rbp)
add    $0x1,%eax
imul   -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    12d8 <write_firmware+0x198>
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    11c8 <write_firmware+0x88>
mov    -0x34(%rbp),%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x40(%rbp)
jmp    11ce <write_firmware+0x8e>
mov    -0x28(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x3c(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1040 <flash_sector_erase@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     1225 <write_firmware+0xe5>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <write_firmware+0x19f>
mov    -0x8(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%ecx
mov    -0x24(%rbp),%r8d
call   1050 <write_sector@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     1262 <write_firmware+0x122>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <write_firmware+0x19f>
mov    -0x8(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%ecx
mov    $0x1000,%r8d
call   1060 <check_sector@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     12a1 <write_firmware+0x161>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <write_firmware+0x19f>
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    11a4 <write_firmware+0x64>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_firmware>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,0x10(%rsp)
test   %ecx,%ecx
jle    121c <write_firmware+0xcc>
mov    %ecx,%ebx
mov    %rdx,%r13
mov    %esi,%ebp
mov    %edi,0xc(%rsp)
mov    %esi,%eax
cltd
idiv   %r8d
add    $0x1,%eax
imul   %r8d,%eax
xor    %r15d,%r15d
mov    %r8d,0x8(%rsp)
mov    0xc(%rsp),%r12d
mov    %eax,0x14(%rsp)
mov    %eax,%r14d
sub    %ebp,%r14d
cmp    %r8d,%ebx
cmovle %ebx,%r14d
lea    0xe58(%rip),%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12d,%edi
mov    %ebp,%esi
call   1050 <flash_sector_erase@plt>
test   %eax,%eax
jne    1220 <write_firmware+0xd0>
mov    %r12d,%edi
mov    %ebp,%esi
mov    %r13,%rdx
mov    %r14d,%ecx
mov    0x10(%rsp),%r8d
call   1060 <write_sector@plt>
test   %eax,%eax
jne    1229 <write_firmware+0xd9>
mov    %r12d,%edi
mov    %ebp,%esi
mov    %r13,%rdx
mov    %r14d,%ecx
mov    $0x1000,%r8d
call   1070 <check_sector@plt>
test   %eax,%eax
jne    1232 <write_firmware+0xe2>
movslq %r14d,%rax
lea    0x0(%r13,%rax,4),%r13
add    %r14d,%ebp
mov    0x8(%rsp),%r8d
mov    0x14(%rsp),%eax
add    %r8d,%eax
add    $0x1,%r15d
sub    %r14d,%ebx
jg     1190 <write_firmware+0x40>
xor    %eax,%eax
jmp    1243 <write_firmware+0xf3>
lea    0xe30(%rip),%rdi
jmp    1239 <write_firmware+0xe9>
lea    0xe12(%rip),%rdi
jmp    1239 <write_firmware+0xe9>
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [45 x i8] c" [%d] handle sector from 0x%x, len = 0x%x\0A\00"
@.str.1 = [22 x i8] c"erase sector failed.\0A\00"
@.str.2 = [22 x i8] c"write secotr failed.\0A\00"
@.str.3 = [22 x i8] c"check sector failed.\0A\00"
define i32 @write_firmware(i32 %0, i32 %1, i32* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 %0, i32* %8
store i32 %1, i32* %9
store i32* %2, i32** %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
store i32 0, i32* %16
store i32 0, i32* %17
store i32 0, i32* %18
store i32 0, i32* %19
%20 = load i32, i32* %11
store i32 %20, i32* %14
%21 = load i32, i32* %9
store i32 %21, i32* %15
%22 = load i32, i32* %15
%23 = load i32, i32* %12
%24 = sdiv i32 %22, %23
%25 = add nsw i32 %24, 1
%26 = load i32, i32* %12
%27 = mul nsw i32 %25, %26
store i32 %27, i32* %17
br label %28
28: ; preds = %76, %6
%29 = load i32, i32* %14
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %90
31: ; preds = %28
%32 = load i32, i32* %14
%33 = load i32, i32* %12
%34 = icmp sgt i32 %32, %33
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = load i32, i32* %17
%37 = load i32, i32* %15
%38 = sub nsw i32 %36, %37
br label %41
39: ; preds = %31
%40 = load i32, i32* %14
br label %41
41: ; preds = %39, %35
%42 = phi i32 [ %38, %35 ], [ %40, %39 ]
store i32 %42, i32* %16
%43 = load i32, i32* %19
%44 = add nsw i32 %43, 1
store i32 %44, i32* %19
%45 = load i32, i32* %15
%46 = load i32, i32* %16
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %43, i32 %45, i32 %46)
%48 = load i32, i32* %8
%49 = load i32, i32* %15
%50 = call i32 @flash_sector_erase(i32 %48, i32 %49)
store i32 %50, i32* %18
%51 = load i32, i32* %18
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %41
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %7
br label %91
55: ; preds = %41
%56 = load i32, i32* %8
%57 = load i32, i32* %15
%58 = load i32*, i32** %10
%59 = load i32, i32* %16
%60 = load i32, i32* %13
%61 = call i32 @write_sector(i32 %56, i32 %57, i32* %58, i32 %59, i32 %60)
store i32 %61, i32* %18
%62 = load i32, i32* %18
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %55
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %7
br label %91
66: ; preds = %55
%67 = load i32, i32* %8
%68 = load i32, i32* %15
%69 = load i32*, i32** %10
%70 = load i32, i32* %16
%71 = call i32 @check_sector(i32 %67, i32 %68, i32* %69, i32 %70, i32 4096)
store i32 %71, i32* %18
%72 = load i32, i32* %18
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %66
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %7
br label %91
76: ; preds = %66
%77 = load i32, i32* %16
%78 = load i32*, i32** %10
%79 = sext i32 %77 to i64
%80 = getelementptr inbounds i32, i32* %78, i64 %79
store i32* %80, i32** %10
%81 = load i32, i32* %16
%82 = load i32, i32* %15
%83 = add nsw i32 %82, %81
store i32 %83, i32* %15
%84 = load i32, i32* %16
%85 = load i32, i32* %14
%86 = sub nsw i32 %85, %84
store i32 %86, i32* %14
%87 = load i32, i32* %12
%88 = load i32, i32* %17
%89 = add nsw i32 %88, %87
store i32 %89, i32* %17
br label %28
90: ; preds = %28
store i32 0, i32* %7
br label %91
91: ; preds = %90, %74, %64, %53
%92 = load i32, i32* %7
ret i32 %92
}

LLVM IR (-O2):
@.str = [45 x i8] c" [%d] handle sector from 0x%x, len = 0x%x\0A\00"
@str = [21 x i8] c"check sector failed.\00"
@str.4 = [21 x i8] c"write secotr failed.\00"
@str.5 = [21 x i8] c"erase sector failed.\00"
define i32 @write_firmware(i32 %0, i32 %1, i32* %2, i32 %3, i32 %4, i32 %5) {
%7 = icmp sgt i32 %3, 0
br i1 %7, label %8, label %41
8: ; preds = %6
%9 = sdiv i32 %1, %4
%10 = add nsw i32 %9, 1
%11 = mul nsw i32 %10, %4
br label %12
12: ; preds = %8, %31
%13 = phi i32 [ %21, %31 ], [ 0, %8 ]
%14 = phi i32 [ %36, %31 ], [ %11, %8 ]
%15 = phi i32 [ %34, %31 ], [ %1, %8 ]
%16 = phi i32 [ %35, %31 ], [ %3, %8 ]
%17 = phi i32* [ %33, %31 ], [ %2, %8 ]
%18 = icmp sgt i32 %16, %4
%19 = sub nsw i32 %14, %15
%20 = select i1 %18, i32 %19, i32 %16
%21 = add nuw nsw i32 %13, 1
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %15, i32 %20)
%23 = tail call i32 @flash_sector_erase(i32 %0, i32 %15)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %12
%26 = tail call i32 @write_sector(i32 %0, i32 %15, i32* %17, i32 %20, i32 %5)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %25
%29 = tail call i32 @check_sector(i32 %0, i32 %15, i32* %17, i32 %20, i32 4096)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = sext i32 %20 to i64
%33 = getelementptr inbounds i32, i32* %17, i64 %32
%34 = add nsw i32 %20, %15
%35 = sub nsw i32 %16, %20
%36 = add nsw i32 %14, %4
%37 = icmp sgt i32 %35, 0
br i1 %37, label %12, label %41
38: ; preds = %28, %25, %12
%39 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.5, i64 0, i64 0), %12 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.4, i64 0, i64 0), %25 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0), %28 ]
%40 = tail call i32 @puts(i8* dereferenceable(1) %39)
br label %41
41: ; preds = %31, %38, %6
%42 = phi i32 [ 0, %6 ], [ -1, %38 ], [ 0, %31 ]
ret i32 %42
}