#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 /*<<< orphan*/ fflush(int /*<<< orphan*/);
int flash_page_program(int, int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int write_sector(int fd, int start, uint8_t *buf, int count, int page_size) {
  int left = 0;
  int offset = 0;
  int len = 0;
  int next_page_offset = 0;
  int ret = 0;
  int i = 0;
  uint8_t *tbuf = NULL;
  left = count;
  offset = start;
  next_page_offset = (offset / page_size + 1) * page_size;
  while (left > 0) {
    len = left > page_size ? next_page_offset - offset : left;
    printf("       [%d] page program page from 0x%x, size 0x%x    \r", i++,
           offset, len);
    fflush(stdout);
    ret = flash_page_program(fd, offset, buf, len);
    if (ret) {
      printf("page program failed\n");
      return -1;
    }
    buf += len;
    offset += len;
    left -= len;
    next_page_offset += page_size;
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<write_sector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cltd
idivl  -0x20(%rbp)
add    $0x1,%eax
imul   -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1265 <write_sector+0x135>
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    11bc <write_sector+0x8c>
mov    -0x30(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    11c2 <write_sector+0x92>
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%edx
mov    -0x2c(%rbp),%ecx
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1030 <flash_page_program@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     122e <write_sector+0xfe>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <write_sector+0x14a>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    1198 <write_sector+0x68>
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_sector>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,0x4(%rsp)
test   %ecx,%ecx
jle    11f0 <write_sector+0xa0>
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %esi,%ebx
mov    %esi,%eax
cltd
idiv   %r8d
mov    %eax,%r14d
add    $0x1,%r14d
imul   %r8d,%r14d
xor    %r12d,%r12d
mov    %r8d,(%rsp)
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%r13d
sub    %ebx,%r13d
cmp    %r8d,%ebp
cmovle %ebp,%r13d
lea    0xe5c(%rip),%rdi
mov    %r12d,%esi
mov    %ebx,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x4(%rsp),%edi
mov    %ebx,%esi
mov    %r15,%rdx
mov    %r13d,%ecx
call   1050 <flash_page_program@plt>
test   %eax,%eax
jne    11fe <write_sector+0xae>
add    $0x1,%r12d
movslq %r13d,%rax
lea    (%r15,%rax,4),%r15
add    %r13d,%ebx
mov    (%rsp),%r8d
add    %r8d,%r14d
sub    %r13d,%ebp
jg     1190 <write_sector+0x40>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
jmp    120f <write_sector+0xbf>
lea    0xe33(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [56 x i8] c" [%d] page program page from 0x%x, size 0x%x \0D\00"
@stdout = global i32 0
@.str.1 = [21 x i8] c"page program failed\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @write_sector(i32 %0, i32 %1, i32* %2, i32 %3, i32 %4) {
%6 = alloca i32
%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*
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32* %2, i32** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
store i32 0, i32* %16
store i32 0, i32* %17
store i32* null, i32** %18
%19 = load i32, i32* %10
store i32 %19, i32* %12
%20 = load i32, i32* %8
store i32 %20, i32* %13
%21 = load i32, i32* %13
%22 = load i32, i32* %11
%23 = sdiv i32 %21, %22
%24 = add nsw i32 %23, 1
%25 = load i32, i32* %11
%26 = mul nsw i32 %24, %25
store i32 %26, i32* %15
br label %27
27: ; preds = %58, %5
%28 = load i32, i32* %12
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %72
30: ; preds = %27
%31 = load i32, i32* %12
%32 = load i32, i32* %11
%33 = icmp sgt i32 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* %15
%36 = load i32, i32* %13
%37 = sub nsw i32 %35, %36
br label %40
38: ; preds = %30
%39 = load i32, i32* %12
br label %40
40: ; preds = %38, %34
%41 = phi i32 [ %37, %34 ], [ %39, %38 ]
store i32 %41, i32* %14
%42 = load i32, i32* %17
%43 = add nsw i32 %42, 1
store i32 %43, i32* %17
%44 = load i32, i32* %13
%45 = load i32, i32* %14
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %42, i32 %44, i32 %45)
%47 = load i32, i32* @stdout
%48 = call i32 @fflush(i32 %47)
%49 = load i32, i32* %7
%50 = load i32, i32* %13
%51 = load i32*, i32** %9
%52 = load i32, i32* %14
%53 = call i32 @flash_page_program(i32 %49, i32 %50, i32* %51, i32 %52)
store i32 %53, i32* %16
%54 = load i32, i32* %16
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %40
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %74
58: ; preds = %40
%59 = load i32, i32* %14
%60 = load i32*, i32** %9
%61 = sext i32 %59 to i64
%62 = getelementptr inbounds i32, i32* %60, i64 %61
store i32* %62, i32** %9
%63 = load i32, i32* %14
%64 = load i32, i32* %13
%65 = add nsw i32 %64, %63
store i32 %65, i32* %13
%66 = load i32, i32* %14
%67 = load i32, i32* %12
%68 = sub nsw i32 %67, %66
store i32 %68, i32* %12
%69 = load i32, i32* %11
%70 = load i32, i32* %15
%71 = add nsw i32 %70, %69
store i32 %71, i32* %15
br label %27
72: ; preds = %27
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
br label %74
74: ; preds = %72, %56
%75 = load i32, i32* %6
ret i32 %75
}

LLVM IR (-O2):
@.str = [56 x i8] c" [%d] page program page from 0x%x, size 0x%x \0D\00"
@stdout = global i32 0
@str = [20 x i8] c"page program failed\00"
define i32 @write_sector(i32 %0, i32 %1, i32* %2, i32 %3, i32 %4) {
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %35
7: ; preds = %5
%8 = sdiv i32 %1, %4
%9 = add nsw i32 %8, 1
%10 = mul nsw i32 %9, %4
br label %11
11: ; preds = %7, %27
%12 = phi i32 [ %28, %27 ], [ 0, %7 ]
%13 = phi i32 [ %33, %27 ], [ %10, %7 ]
%14 = phi i32 [ %31, %27 ], [ %1, %7 ]
%15 = phi i32 [ %32, %27 ], [ %3, %7 ]
%16 = phi i32* [ %30, %27 ], [ %2, %7 ]
%17 = icmp sgt i32 %15, %4
%18 = sub nsw i32 %13, %14
%19 = select i1 %17, i32 %18, i32 %15
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %14, i32 %19)
%21 = load i32, i32* @stdout
%22 = tail call i32 @fflush(i32 %21)
%23 = tail call i32 @flash_page_program(i32 %0, i32 %14, i32* %16, i32 %19)
%24 = icmp eq i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %11
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %37
27: ; preds = %11
%28 = add nuw nsw i32 %12, 1
%29 = sext i32 %19 to i64
%30 = getelementptr inbounds i32, i32* %16, i64 %29
%31 = add nsw i32 %19, %14
%32 = sub nsw i32 %15, %19
%33 = add nsw i32 %13, %4
%34 = icmp sgt i32 %32, 0
br i1 %34, label %11, label %35
35: ; preds = %27, %5
%36 = tail call i32 @putchar(i32 10)
br label %37
37: ; preds = %35, %25
%38 = phi i32 [ -1, %25 ], [ 0, %35 ]
ret i32 %38
}