#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 uint8_t;
typedef scalar_t__ uint16_t;

/* Variables and functions */
uint16_t stackmonitor_free(void) {
  extern const uint8_t _end;
  extern const uint8_t __stack;
  const uint8_t *cur = &_end;
  uint16_t free = 0;
  while ((*cur == 0xfc) && (cur <= &__stack)) {
    cur++;
    free++;
  }
  return (free);
}


x86 assembly (clang, -O0):
<stackmonitor_free>:
push   %rbp
mov    %rsp,%rbp
mov    0x2ed5(%rip),%rax
mov    %rax,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
cmpl   $0xfc,(%rcx)
mov    %al,-0x11(%rbp)
jne    113d <stackmonitor_free+0x3d>
mov    0x2eb5(%rip),%rax
cmp    %rax,-0x8(%rbp)
setbe  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    114d <stackmonitor_free+0x4d>
jmp    116a <stackmonitor_free+0x6a>
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1117 <stackmonitor_free+0x17>
mov    -0x10(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<stackmonitor_free>:
mov    0x2ed9(%rip),%rcx
xor    %eax,%eax
cmpl   $0xfc,(%rcx)
jne    1152 <stackmonitor_free+0x52>
cmp    0x2ed0(%rip),%rcx
ja     1152 <stackmonitor_free+0x52>
xor    %edx,%edx
mov    0x2ec5(%rip),%r8
xor    %edi,%edi
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1(%rdi),%rax
lea    (%rcx,%rdx,1),%rsi
add    $0x4,%rsi
cmp    %r8,%rsi
ja     1152 <stackmonitor_free+0x52>
add    $0x4,%rdx
cmpl   $0xfc,0x4(%rcx,%rdi,4)
mov    %rax,%rdi
je     1130 <stackmonitor_free+0x30>
ret

LLVM IR (-O0):
@_end = external constant i32
@__stack = external constant i32
define i64 @stackmonitor_free() {
%1 = alloca i32*
%2 = alloca i64
store i32* @_end, i32** %1
store i64 0, i64* %2
br label %3
3: ; preds = %12, %0
%4 = load i32*, i32** %1
%5 = load i32, i32* %4
%6 = icmp eq i32 %5, 252
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32*, i32** %1
%9 = icmp ule i32* %8, @__stack
br label %10
10: ; preds = %7, %3
%11 = phi i1 [ false, %3 ], [ %9, %7 ]
br i1 %11, label %12, label %17
12: ; preds = %10
%13 = load i32*, i32** %1
%14 = getelementptr inbounds i32, i32* %13, i32 1
store i32* %14, i32** %1
%15 = load i64, i64* %2
%16 = add nsw i64 %15, 1
store i64 %16, i64* %2
br label %3
17: ; preds = %10
%18 = load i64, i64* %2
ret i64 %18
}

LLVM IR (-O2):
@_end = external constant i32
@__stack = external constant i32
define i64 @stackmonitor_free() {
%1 = load i32, i32* @_end
%2 = icmp eq i32 %1, 252
%3 = and i1 %2, icmp ule (i32* @_end, i32* @__stack)
br i1 %3, label %4, label %13
4: ; preds = %0, %4
%5 = phi i64 [ %8, %4 ], [ 0, %0 ]
%6 = phi i32* [ %7, %4 ], [ @_end, %0 ]
%7 = getelementptr inbounds i32, i32* %6, i64 1
%8 = add nuw nsw i64 %5, 1
%9 = load i32, i32* %7
%10 = icmp eq i32 %9, 252
%11 = icmp ule i32* %7, @__stack
%12 = and i1 %11, %10
br i1 %12, label %4, label %13
13: ; preds = %4, %0
%14 = phi i64 [ 0, %0 ], [ %8, %4 ]
ret i64 %14
}