#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 size_t off_t;

/* Variables and functions */
scalar_t__ stp_find_first_sync(char *, size_t, size_t, size_t *, size_t *);
int stp_find_first_block(char *buf, size_t u8size, off_t start, off_t *out_off,
                         size_t *out_size) {
  off_t sync_off, head = start;
  size_t sync_len;
  while (head < u8size) {
    if (stp_find_first_sync(buf, u8size, head, &sync_off, &sync_len) != 0) {
      *out_off = head;
      *out_size = u8size - head;
      return 0;
    } else if (sync_off > head) {
      *out_off = head;
      *out_size = sync_off - head;
      return 0;
    }
    head = sync_off + sync_len;
  }
  return -1;
}


x86 assembly (clang, -O0):
<stp_find_first_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11d5 <stp_find_first_block+0xc5>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x40(%rbp),%rdx
lea    -0x38(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   1030 <stp_find_first_sync@plt>
cmp    $0x0,%rax
je     118b <stp_find_first_block+0x7b>
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
sub    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11dc <stp_find_first_block+0xcc>
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jbe    11bf <stp_find_first_block+0xaf>
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
sub    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11dc <stp_find_first_block+0xcc>
jmp    11c4 <stp_find_first_block+0xb4>
mov    -0x38(%rbp),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    1134 <stp_find_first_block+0x24>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stp_find_first_block>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0xffffffff,%r12d
cmp    %rsi,%rdx
jae    119b <stp_find_first_block+0x8b>
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r13
mov    %rcx,0x8(%rsp)
mov    %r8,0x10(%rsp)
lea    0x20(%rsp),%r14
lea    0x18(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
mov    %r15,%r8
call   1030 <stp_find_first_sync@plt>
test   %rax,%rax
jne    1185 <stp_find_first_block+0x75>
mov    0x20(%rsp),%rax
cmp    %rbp,%rax
ja     1182 <stp_find_first_block+0x72>
add    0x18(%rsp),%rax
mov    %rax,%rbp
cmp    %rbx,%rax
jb     1150 <stp_find_first_block+0x40>
jmp    119b <stp_find_first_block+0x8b>
mov    %rax,%rbx
mov    0x8(%rsp),%rax
mov    %rbp,(%rax)
sub    %rbp,%rbx
mov    0x10(%rsp),%rax
mov    %rbx,(%rax)
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @stp_find_first_block(i8* %0, i64 %1, i64 %2, i64* %3, i64* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64*
%11 = alloca i64*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
store i8* %0, i8** %7
store i64 %1, i64* %8
store i64 %2, i64* %9
store i64* %3, i64** %10
store i64* %4, i64** %11
%15 = load i64, i64* %9
store i64 %15, i64* %13
br label %16
16: ; preds = %45, %5
%17 = load i64, i64* %13
%18 = load i64, i64* %8
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %49
20: ; preds = %16
%21 = load i8*, i8** %7
%22 = load i64, i64* %8
%23 = load i64, i64* %13
%24 = call i64 @stp_find_first_sync(i8* %21, i64 %22, i64 %23, i64* %12, i64* %14)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = load i64, i64* %13
%28 = load i64*, i64** %10
store i64 %27, i64* %28
%29 = load i64, i64* %8
%30 = load i64, i64* %13
%31 = sub i64 %29, %30
%32 = load i64*, i64** %11
store i64 %31, i64* %32
store i32 0, i32* %6
br label %50
33: ; preds = %20
%34 = load i64, i64* %12
%35 = load i64, i64* %13
%36 = icmp ugt i64 %34, %35
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = load i64, i64* %13
%39 = load i64*, i64** %10
store i64 %38, i64* %39
%40 = load i64, i64* %12
%41 = load i64, i64* %13
%42 = sub i64 %40, %41
%43 = load i64*, i64** %11
store i64 %42, i64* %43
store i32 0, i32* %6
br label %50
44: ; preds = %33
br label %45
45: ; preds = %44
%46 = load i64, i64* %12
%47 = load i64, i64* %14
%48 = add i64 %46, %47
store i64 %48, i64* %13
br label %16
49: ; preds = %16
store i32 -1, i32* %6
br label %50
50: ; preds = %49, %37, %26
%51 = load i32, i32* %6
ret i32 %51
}

LLVM IR (-O2):
define i32 @stp_find_first_block(i8* %0, i64 %1, i64 %2, i64* nocapture writeonly %3, i64* nocapture writeonly %4) {
%6 = alloca i64
%7 = alloca i64
%8 = bitcast i64* %6 to i8*
%9 = bitcast i64* %7 to i8*
%10 = icmp ult i64 %2, %1
br i1 %10, label %11, label %25
11: ; preds = %5, %18
%12 = phi i64 [ %20, %18 ], [ %2, %5 ]
%13 = call i64 @stp_find_first_sync(i8* %0, i64 %1, i64 %12, i64* %6, i64* %7)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = load i64, i64* %6
%17 = icmp ugt i64 %16, %12
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = load i64, i64* %7
%20 = add i64 %19, %16
%21 = icmp ult i64 %20, %1
br i1 %21, label %11, label %25
22: ; preds = %15, %11
%23 = phi i64 [ %1, %11 ], [ %16, %15 ]
store i64 %12, i64* %3
%24 = sub i64 %23, %12
store i64 %24, i64* %4
br label %25
25: ; preds = %18, %22, %5
%26 = phi i32 [ -1, %5 ], [ 0, %22 ], [ -1, %18 ]
ret i32 %26
}