#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 uint32_t;

/* Variables and functions */
scalar_t__ SPIEraseBlock(int);
scalar_t__ SPIEraseSector(int);
int SPIEraseAreaEx(const uint32_t start, const uint32_t size) {
  if (start & (0x1000 - 1) != 0) {
    return 1;
  }
  const uint32_t sectors_per_block = 0x10000 / 0x1000;
  uint32_t current_sector = start / 0x1000;
  uint32_t sector_count = (size + 0x1000 - 1) / 0x1000;
  const uint32_t end = current_sector + sector_count;
  for (; current_sector < end && (current_sector & (sectors_per_block - 1));
       ++current_sector, --sector_count) {
    if (SPIEraseSector(current_sector)) {
      return 2;
    }
  }
  for (; current_sector + sectors_per_block <= end;
       current_sector += sectors_per_block, sector_count -= sectors_per_block) {
    if (SPIEraseBlock(current_sector / sectors_per_block)) {
      return 3;
    }
  }
  for (; current_sector < end; ++current_sector, --sector_count) {
    if (SPIEraseSector(current_sector)) {
      return 4;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<SPIEraseAreaEx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1149 <SPIEraseAreaEx+0x29>
movl   $0x1,-0x4(%rbp)
jmp    1291 <SPIEraseAreaEx+0x171>
movl   $0x10,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1000,%eax
sub    $0x1,%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%ecx
xor    %eax,%eax
cmp    -0x1c(%rbp),%ecx
mov    %al,-0x1d(%rbp)
jge    119d <SPIEraseAreaEx+0x7d>
mov    -0x14(%rbp),%eax
and    $0xf,%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11ad <SPIEraseAreaEx+0x8d>
jmp    11e7 <SPIEraseAreaEx+0xc7>
mov    -0x14(%rbp),%edi
call   1040 <SPIEraseSector@plt>
cmp    $0x0,%rax
je     11cb <SPIEraseAreaEx+0xab>
movl   $0x2,-0x4(%rbp)
jmp    1291 <SPIEraseAreaEx+0x171>
jmp    11d0 <SPIEraseAreaEx+0xb0>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    117d <SPIEraseAreaEx+0x5d>
jmp    11ec <SPIEraseAreaEx+0xcc>
mov    -0x14(%rbp),%eax
add    $0x10,%eax
cmp    -0x1c(%rbp),%eax
jg     123f <SPIEraseAreaEx+0x11f>
mov    -0x14(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1030 <SPIEraseBlock@plt>
cmp    $0x0,%rax
je     1223 <SPIEraseAreaEx+0x103>
movl   $0x3,-0x4(%rbp)
jmp    1291 <SPIEraseAreaEx+0x171>
jmp    1228 <SPIEraseAreaEx+0x108>
mov    -0x14(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x10,%eax
mov    %eax,-0x18(%rbp)
jmp    11ec <SPIEraseAreaEx+0xcc>
jmp    1244 <SPIEraseAreaEx+0x124>
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    128a <SPIEraseAreaEx+0x16a>
mov    -0x14(%rbp),%edi
call   1040 <SPIEraseSector@plt>
cmp    $0x0,%rax
je     126e <SPIEraseAreaEx+0x14e>
movl   $0x4,-0x4(%rbp)
jmp    1291 <SPIEraseAreaEx+0x171>
jmp    1273 <SPIEraseAreaEx+0x153>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1244 <SPIEraseAreaEx+0x124>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SPIEraseAreaEx>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x1,%r14d
test   $0x1,%dil
jne    11ec <SPIEraseAreaEx+0xcc>
lea    0xfff(%rdi),%ebx
test   %edi,%edi
cmovns %edi,%ebx
sar    $0xc,%ebx
lea    0xfff(%rsi),%eax
lea    0x1ffe(%rsi),%r15d
test   %eax,%eax
cmovns %eax,%r15d
sar    $0xc,%r15d
add    %ebx,%r15d
test   %esi,%esi
jle    11a7 <SPIEraseAreaEx+0x87>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%eax
and    $0xf,%eax
je     11a7 <SPIEraseAreaEx+0x87>
mov    %ebx,%edi
call   1040 <SPIEraseSector@plt>
test   %rax,%rax
jne    11de <SPIEraseAreaEx+0xbe>
add    $0x1,%ebx
cmp    %r15d,%ebx
jl     1170 <SPIEraseAreaEx+0x50>
jmp    11a7 <SPIEraseAreaEx+0x87>
nopl   (%rax)
lea    0xf(%rbx),%edi
test   %ebx,%ebx
cmovns %ebx,%edi
sar    $0x4,%edi
call   1030 <SPIEraseBlock@plt>
mov    %ebp,%ebx
test   %rax,%rax
jne    11d6 <SPIEraseAreaEx+0xb6>
lea    0x10(%rbx),%ebp
cmp    %r15d,%ebp
jle    1190 <SPIEraseAreaEx+0x70>
xor    %r14d,%r14d
cmp    %r15d,%ebx
jge    11ec <SPIEraseAreaEx+0xcc>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1040 <SPIEraseSector@plt>
test   %rax,%rax
jne    11e6 <SPIEraseAreaEx+0xc6>
add    $0x1,%ebx
cmp    %ebx,%r15d
jne    11c0 <SPIEraseAreaEx+0xa0>
jmp    11ec <SPIEraseAreaEx+0xcc>
mov    $0x3,%r14d
jmp    11ec <SPIEraseAreaEx+0xcc>
mov    $0x2,%r14d
jmp    11ec <SPIEraseAreaEx+0xcc>
mov    $0x4,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @SPIEraseAreaEx(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %4
%11 = and i32 %10, 1
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 1, i32* %3
br label %80
14: ; preds = %2
store i32 16, i32* %6
%15 = load i32, i32* %4
%16 = sdiv i32 %15, 4096
store i32 %16, i32* %7
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 4096
%19 = sub nsw i32 %18, 1
%20 = sdiv i32 %19, 4096
store i32 %20, i32* %8
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = add nsw i32 %21, %22
store i32 %23, i32* %9
br label %24
24: ; preds = %40, %14
%25 = load i32, i32* %7
%26 = load i32, i32* %9
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* %7
%30 = and i32 %29, 15
%31 = icmp ne i32 %30, 0
br label %32
32: ; preds = %28, %24
%33 = phi i1 [ false, %24 ], [ %31, %28 ]
br i1 %33, label %34, label %45
34: ; preds = %32
%35 = load i32, i32* %7
%36 = call i64 @SPIEraseSector(i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34
store i32 2, i32* %3
br label %80
39: ; preds = %34
br label %40
40: ; preds = %39
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
%43 = load i32, i32* %8
%44 = add nsw i32 %43, -1
store i32 %44, i32* %8
br label %24
45: ; preds = %32
br label %46
46: ; preds = %58, %45
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 16
%49 = load i32, i32* %9
%50 = icmp sle i32 %48, %49
br i1 %50, label %51, label %63
51: ; preds = %46
%52 = load i32, i32* %7
%53 = sdiv i32 %52, 16
%54 = call i64 @SPIEraseBlock(i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %51
store i32 3, i32* %3
br label %80
57: ; preds = %51
br label %58
58: ; preds = %57
%59 = load i32, i32* %7
%60 = add nsw i32 %59, 16
store i32 %60, i32* %7
%61 = load i32, i32* %8
%62 = sub nsw i32 %61, 16
store i32 %62, i32* %8
br label %46
63: ; preds = %46
br label %64
64: ; preds = %74, %63
%65 = load i32, i32* %7
%66 = load i32, i32* %9
%67 = icmp slt i32 %65, %66
br i1 %67, label %68, label %79
68: ; preds = %64
%69 = load i32, i32* %7
%70 = call i64 @SPIEraseSector(i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %68
store i32 4, i32* %3
br label %80
73: ; preds = %68
br label %74
74: ; preds = %73
%75 = load i32, i32* %7
%76 = add nsw i32 %75, 1
store i32 %76, i32* %7
%77 = load i32, i32* %8
%78 = add nsw i32 %77, -1
store i32 %78, i32* %8
br label %64
79: ; preds = %64
store i32 0, i32* %3
br label %80
80: ; preds = %79, %72, %56, %38, %13
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
define i32 @SPIEraseAreaEx(i32 %0, i32 %1) {
%3 = and i32 %0, 1
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %43
5: ; preds = %2
%6 = sdiv i32 %0, 4096
%7 = add nsw i32 %1, 4095
%8 = sdiv i32 %7, 4096
%9 = add nsw i32 %8, %6
%10 = icmp sgt i32 %1, 0
%11 = and i32 %6, 15
%12 = icmp ne i32 %11, 0
%13 = and i1 %10, %12
br i1 %13, label %14, label %24
14: ; preds = %5, %18
%15 = phi i32 [ %19, %18 ], [ %6, %5 ]
%16 = tail call i64 @SPIEraseSector(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %43
18: ; preds = %14
%19 = add nsw i32 %15, 1
%20 = icmp slt i32 %19, %9
%21 = and i32 %19, 15
%22 = icmp ne i32 %21, 0
%23 = and i1 %20, %22
br i1 %23, label %14, label %24
24: ; preds = %18, %5
%25 = phi i32 [ %6, %5 ], [ %19, %18 ]
br label %26
26: ; preds = %24, %32
%27 = phi i32 [ %28, %32 ], [ %25, %24 ]
%28 = add nsw i32 %27, 16
%29 = icmp sgt i32 %28, %9
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = icmp slt i32 %27, %9
br i1 %31, label %38, label %43
32: ; preds = %26
%33 = sdiv i32 %27, 16
%34 = tail call i64 @SPIEraseBlock(i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %26, label %43
36: ; preds = %38
%37 = icmp eq i32 %42, %9
br i1 %37, label %43, label %38
38: ; preds = %30, %36
%39 = phi i32 [ %42, %36 ], [ %27, %30 ]
%40 = tail call i64 @SPIEraseSector(i32 %39)
%41 = icmp eq i64 %40, 0
%42 = add nsw i32 %39, 1
br i1 %41, label %36, label %43
43: ; preds = %14, %32, %36, %38, %30, %2
%44 = phi i32 [ 1, %2 ], [ 0, %30 ], [ 0, %36 ], [ 4, %38 ], [ 3, %32 ], [ 2, %14 ]
ret i32 %44
}