#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_fast_read(int, int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int read_sector(int fd, int start, uint8_t *buf, int count, int spidev_size) {
  int left = 0;
  int offset = 0;
  int len = 0;
  int next_offset = 0;
  int ret = 0;
  int i = 0;
  left = count;
  offset = start;
  next_offset = (offset / spidev_size + 1) * spidev_size;
  while (left > 0) {
    len = left > spidev_size ? next_offset - offset : left;
    printf("       [%d] read from 0x%x, size = 0x%x to %p   \r", i++, offset,
           len, buf);
    fflush(stdout);
    ret = flash_fast_read(fd, offset, buf, len);
    if (ret < 0) {
      printf("flash_fast_read failed.\n");
      return -1;
    }
    buf += len;
    offset += len;
    left -= len;
    next_offset += spidev_size;
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<read_sector>:
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)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%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    1261 <read_sector+0x131>
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    11b4 <read_sector+0x84>
mov    -0x30(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    11ba <read_sector+0x8a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%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
mov    -0x18(%rbp),%r8
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2df6(%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   1040 <flash_fast_read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    122a <read_sector+0xfa>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <read_sector+0x146>
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    1190 <read_sector+0x60>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sector>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,0x4(%rsp)
test   %ecx,%ecx
jle    11f3 <read_sector+0xa3>
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
mov    %r15,%r8
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x4(%rsp),%edi
mov    %ebx,%esi
mov    %r15,%rdx
mov    %r13d,%ecx
call   1060 <flash_fast_read@plt>
test   %eax,%eax
js     1201 <read_sector+0xb1>
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 <read_sector+0x40>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
jmp    1212 <read_sector+0xc2>
lea    0xe2a(%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 = [50 x i8] c" [%d] read from 0x%x, size = 0x%x to %p \0D\00"
@stdout = global i32 0
@.str.1 = [25 x i8] c"flash_fast_read failed.\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @read_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
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
%18 = load i32, i32* %10
store i32 %18, i32* %12
%19 = load i32, i32* %8
store i32 %19, i32* %13
%20 = load i32, i32* %13
%21 = load i32, i32* %11
%22 = sdiv i32 %20, %21
%23 = add nsw i32 %22, 1
%24 = load i32, i32* %11
%25 = mul nsw i32 %23, %24
store i32 %25, i32* %15
br label %26
26: ; preds = %58, %5
%27 = load i32, i32* %12
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %72
29: ; preds = %26
%30 = load i32, i32* %12
%31 = load i32, i32* %11
%32 = icmp sgt i32 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* %15
%35 = load i32, i32* %13
%36 = sub nsw i32 %34, %35
br label %39
37: ; preds = %29
%38 = load i32, i32* %12
br label %39
39: ; preds = %37, %33
%40 = phi i32 [ %36, %33 ], [ %38, %37 ]
store i32 %40, i32* %14
%41 = load i32, i32* %17
%42 = add nsw i32 %41, 1
store i32 %42, i32* %17
%43 = load i32, i32* %13
%44 = load i32, i32* %14
%45 = load i32*, i32** %9
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %41, i32 %43, 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_fast_read(i32 %49, i32 %50, i32* %51, i32 %52)
store i32 %53, i32* %16
%54 = load i32, i32* %16
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %39
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %74
58: ; preds = %39
%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 %26
72: ; preds = %26
%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 = [50 x i8] c" [%d] read from 0x%x, size = 0x%x to %p \0D\00"
@stdout = global i32 0
@str = [24 x i8] c"flash_fast_read failed.\00"
define i32 @read_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 ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %14, i32 %19, i32* %16)
%21 = load i32, i32* @stdout
%22 = tail call i32 @fflush(i32 %21)
%23 = tail call i32 @flash_fast_read(i32 %0, i32 %14, i32* %16, i32 %19)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %11
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 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
}