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

/* Variables and functions */
int flashfsGetSize();
int m25p16_readBytes(int, int /*<<< orphan*/ *, int);
int flashfsIdentifyStartOfFreeSpace(void) {
  enum {
    FREE_BLOCK_SIZE = 2048,
    FREE_BLOCK_TEST_SIZE_INTS = 4,
    FREE_BLOCK_TEST_SIZE_BYTES = FREE_BLOCK_TEST_SIZE_INTS * sizeof(uint32_t),
  };
  union {
    uint8_t bytes[FREE_BLOCK_TEST_SIZE_BYTES];
    uint32_t ints[FREE_BLOCK_TEST_SIZE_INTS];
  } testBuffer;
  int left = 0;
  int right = flashfsGetSize() / FREE_BLOCK_SIZE;
  int mid;
  int result = right;
  int i;
  bool blockErased;
  while (left < right) {
    mid = (left + right) / 2;
    if (m25p16_readBytes(mid * FREE_BLOCK_SIZE, testBuffer.bytes,
                         FREE_BLOCK_TEST_SIZE_BYTES) <
        FREE_BLOCK_TEST_SIZE_BYTES) {
      break;
    }
    blockErased = true;
    for (i = 0; i < FREE_BLOCK_TEST_SIZE_INTS; i++) {
      if (testBuffer.ints[i] != 0xFFFFFFFF) {
        blockErased = false;
        break;
      }
    }
    if (blockErased) {
      result = mid;
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return result * FREE_BLOCK_SIZE;
}


x86 assembly (clang, -O0):
<flashfsIdentifyStartOfFreeSpace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x44(%rbp)
mov    $0x0,%al
call   1030 <flashfsGetSize@plt>
mov    $0x800,%ecx
cltd
idiv   %ecx
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x48(%rbp),%eax
jge    11f5 <flashfsIdentifyStartOfFreeSpace+0xd5>
mov    -0x44(%rbp),%eax
add    -0x48(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%edi
shl    $0xb,%edi
lea    -0x40(%rbp),%rsi
mov    $0x10,%edx
call   1040 <m25p16_readBytes@plt>
cmp    $0x10,%eax
jge    1186 <flashfsIdentifyStartOfFreeSpace+0x66>
jmp    11f5 <flashfsIdentifyStartOfFreeSpace+0xd5>
movl   $0x1,-0x58(%rbp)
movl   $0x0,-0x54(%rbp)
cmpl   $0x4,-0x54(%rbp)
jge    11cc <flashfsIdentifyStartOfFreeSpace+0xac>
movslq -0x54(%rbp),%rax
cmpl   $0xffffffff,-0x40(%rbp,%rax,4)
je     11b9 <flashfsIdentifyStartOfFreeSpace+0x99>
movl   $0x0,-0x58(%rbp)
jmp    11cc <flashfsIdentifyStartOfFreeSpace+0xac>
jmp    11be <flashfsIdentifyStartOfFreeSpace+0x9e>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1194 <flashfsIdentifyStartOfFreeSpace+0x74>
cmpl   $0x0,-0x58(%rbp)
je     11e7 <flashfsIdentifyStartOfFreeSpace+0xc7>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x48(%rbp)
jmp    11f0 <flashfsIdentifyStartOfFreeSpace+0xd0>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1147 <flashfsIdentifyStartOfFreeSpace+0x27>
mov    -0x50(%rbp),%eax
shl    $0xb,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flashfsIdentifyStartOfFreeSpace>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <flashfsGetSize@plt>
lea    0x7ff(%rax),%r12d
test   %eax,%eax
cmovns %eax,%r12d
sar    $0xb,%r12d
cmp    $0x800,%eax
jl     11ac <flashfsIdentifyStartOfFreeSpace+0x8c>
mov    %rsp,%r14
mov    %r12d,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    (%r15,%rbx,1),%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
mov    %ebp,%edi
shl    $0xb,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1040 <m25p16_readBytes@plt>
cmp    $0x10,%eax
jl     11ac <flashfsIdentifyStartOfFreeSpace+0x8c>
movdqa (%rsp),%xmm0
pcmpeqd 0xe6f(%rip),%xmm0
movmskps %xmm0,%eax
lea    0x1(%rbp),%ecx
cmp    $0xf,%eax
cmove  %ebp,%ebx
cmove  %r15d,%ecx
cmove  %ebp,%r12d
mov    %ecx,%r15d
cmp    %ebx,%ecx
jl     1160 <flashfsIdentifyStartOfFreeSpace+0x40>
shl    $0xb,%r12d
mov    %r12d,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.anon = type { [16 x i32] }
define i32 @flashfsIdentifyStartOfFreeSpace() {
%1 = alloca %union.anon
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %2
%8 = call i32 (...) @flashfsGetSize()
%9 = sdiv i32 %8, 2048
store i32 %9, i32* %3
%10 = load i32, i32* %3
store i32 %10, i32* %5
br label %11
11: ; preds = %52, %0
%12 = load i32, i32* %2
%13 = load i32, i32* %3
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %53
15: ; preds = %11
%16 = load i32, i32* %2
%17 = load i32, i32* %3
%18 = add nsw i32 %16, %17
%19 = sdiv i32 %18, 2
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = mul nsw i32 %20, 2048
%22 = bitcast %union.anon* %1 to [16 x i32]*
%23 = getelementptr inbounds [16 x i32], [16 x i32]* %22, i64 0, i64 0
%24 = call i32 @m25p16_readBytes(i32 %21, i32* %23, i32 16)
%25 = icmp slt i32 %24, 16
br i1 %25, label %26, label %27
26: ; preds = %15
br label %53
27: ; preds = %15
store i32 1, i32* %7
store i32 0, i32* %6
br label %28
28: ; preds = %40, %27
%29 = load i32, i32* %6
%30 = icmp slt i32 %29, 4
br i1 %30, label %31, label %43
31: ; preds = %28
%32 = bitcast %union.anon* %1 to [4 x i32]*
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [4 x i32], [4 x i32]* %32, i64 0, i64 %34
%36 = load i32, i32* %35
%37 = icmp ne i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %31
store i32 0, i32* %7
br label %43
39: ; preds = %31
br label %40
40: ; preds = %39
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
br label %28
43: ; preds = %38, %28
%44 = load i32, i32* %7
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* %4
store i32 %47, i32* %5
%48 = load i32, i32* %4
store i32 %48, i32* %3
br label %52
49: ; preds = %43
%50 = load i32, i32* %4
%51 = add nsw i32 %50, 1
store i32 %51, i32* %2
br label %52
52: ; preds = %49, %46
br label %11
53: ; preds = %26, %11
%54 = load i32, i32* %5
%55 = mul nsw i32 %54, 2048
ret i32 %55
}

LLVM IR (-O2):
%union.anon = type { [16 x i32] }
define i32 @flashfsIdentifyStartOfFreeSpace() {
%1 = alloca %union.anon
%2 = bitcast %union.anon* %1 to i8*
%3 = tail call i32 (...) @flashfsGetSize()
%4 = sdiv i32 %3, 2048
%5 = icmp sgt i32 %3, 2047
br i1 %5, label %6, label %29
6: ; preds = %0
%7 = getelementptr inbounds %union.anon, %union.anon* %1, i64 0, i32 0, i64 0
%8 = bitcast %union.anon* %1 to <4 x i32>*
br label %9
9: ; preds = %6, %18
%10 = phi i32 [ %4, %6 ], [ %27, %18 ]
%11 = phi i32 [ 0, %6 ], [ %26, %18 ]
%12 = phi i32 [ %4, %6 ], [ %25, %18 ]
%13 = add nsw i32 %11, %12
%14 = sdiv i32 %13, 2
%15 = shl nsw i32 %14, 11
%16 = call i32 @m25p16_readBytes(i32 %15, i32* %7, i32 16)
%17 = icmp slt i32 %16, 16
br i1 %17, label %29, label %18
18: ; preds = %9
%19 = load <4 x i32>, <4 x i32>* %8
%20 = freeze <4 x i32> %19
%21 = icmp ne <4 x i32> %20, <i32 -1, i32 -1, i32 -1, i32 -1>
%22 = bitcast <4 x i1> %21 to i4
%23 = icmp eq i4 %22, 0
%24 = add nsw i32 %14, 1
%25 = select i1 %23, i32 %14, i32 %12
%26 = select i1 %23, i32 %11, i32 %24
%27 = select i1 %23, i32 %14, i32 %10
%28 = icmp slt i32 %26, %25
br i1 %28, label %9, label %29
29: ; preds = %18, %9, %0
%30 = phi i32 [ %4, %0 ], [ %10, %9 ], [ %27, %18 ]
%31 = shl nsw i32 %30, 11
ret i32 %31
}