#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 */

/* Variables and functions */
int comp32(unsigned long, int);
unsigned long int *sync_find(register unsigned long int *dptr) {
  int num_err;
  int sync_window = 18;
  int bit_cnt;
  unsigned long int sample1, sample2;
  sample1 = *dptr++;
  while (sync_window > 0) {
    sample2 = *dptr++;
    bit_cnt = 0;
    while (bit_cnt < 32) {
      num_err = comp32(sample1, 0x3e4ba81b);
      if (num_err <= 2)
        return (dptr);
      num_err = comp32(sample1, 0xaaaaaaaa);
      if ((num_err <= 2) || (num_err >= 30)) {
        sample1 = sample2 >> bit_cnt;
        sync_window = 19;
        break;
      } else {
        sample1 = (sample1 << 1) | (sample2 >> 31);
        sample2 = sample2 << 1;
      }
      bit_cnt++;
    }
    sync_window--;
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<sync_find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x12,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1210 <sync_find+0x100>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x20,-0x1c(%rbp)
jge    1202 <sync_find+0xf2>
mov    -0x28(%rbp),%rdi
mov    $0x3e4ba81b,%esi
call   1030 <comp32@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jg     1192 <sync_find+0x82>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1218 <sync_find+0x108>
mov    -0x28(%rbp),%rdi
mov    $0xaaaaaaaa,%esi
call   1030 <comp32@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jle    11b7 <sync_find+0xa7>
cmpl   $0x1e,-0x14(%rbp)
jl     11d1 <sync_find+0xc1>
mov    -0x30(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shr    %cl,%rax
mov    %rax,-0x28(%rbp)
movl   $0x13,-0x18(%rbp)
jmp    1202 <sync_find+0xf2>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    -0x30(%rbp),%rcx
shr    $0x1f,%rcx
or     %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1160 <sync_find+0x50>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1139 <sync_find+0x29>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sync_find>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
mov    (%rdi),%rbx
add    $0x8,%r14
mov    $0x12,%r12d
mov    (%r14),%r13
add    $0x8,%r14
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x3e4ba81b,%esi
call   1030 <comp32@plt>
cmp    $0x3,%eax
jl     11b4 <sync_find+0xa4>
mov    %rbx,%rdi
mov    $0xaaaaaaaa,%esi
call   1030 <comp32@plt>
add    $0xffffffe2,%eax
cmp    $0xffffffe4,%eax
jbe    1190 <sync_find+0x80>
mov    %rbx,%rax
add    %rbx,%rax
mov    %r13,%rbx
shr    $0x1f,%rbx
or     %rax,%rbx
add    %r13,%r13
add    $0x1,%r15
cmp    $0x20,%r15d
jne    1140 <sync_find+0x30>
jmp    119f <sync_find+0x8f>
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%ecx
shr    %cl,%r13
mov    $0x13,%r12d
mov    %r13,%rbx
lea    -0x1(%r12),%eax
cmp    $0x1,%r12d
mov    %eax,%r12d
jg     1129 <sync_find+0x19>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64* @sync_find(i64* %0) {
%2 = alloca i64*
%3 = alloca i64*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i64* %0, i64** %3
store i32 18, i32* %5
%9 = load i64*, i64** %3
%10 = getelementptr inbounds i64, i64* %9, i32 1
store i64* %10, i64** %3
%11 = load i64, i64* %9
store i64 %11, i64* %7
br label %12
12: ; preds = %53, %1
%13 = load i32, i32* %5
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %56
15: ; preds = %12
%16 = load i64*, i64** %3
%17 = getelementptr inbounds i64, i64* %16, i32 1
store i64* %17, i64** %3
%18 = load i64, i64* %16
store i64 %18, i64* %8
store i32 0, i32* %6
br label %19
19: ; preds = %50, %15
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 32
br i1 %21, label %22, label %53
22: ; preds = %19
%23 = load i64, i64* %7
%24 = call i32 @comp32(i64 %23, i32 1045145627)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = icmp sle i32 %25, 2
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = load i64*, i64** %3
store i64* %28, i64** %2
br label %57
29: ; preds = %22
%30 = load i64, i64* %7
%31 = call i32 @comp32(i64 %30, i32 -1431655766)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp sle i32 %32, 2
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = load i32, i32* %4
%36 = icmp sge i32 %35, 30
br i1 %36, label %37, label %42
37: ; preds = %34, %29
%38 = load i64, i64* %8
%39 = load i32, i32* %6
%40 = zext i32 %39 to i64
%41 = lshr i64 %38, %40
store i64 %41, i64* %7
store i32 19, i32* %5
br label %53
42: ; preds = %34
%43 = load i64, i64* %7
%44 = shl i64 %43, 1
%45 = load i64, i64* %8
%46 = lshr i64 %45, 31
%47 = or i64 %44, %46
store i64 %47, i64* %7
%48 = load i64, i64* %8
%49 = shl i64 %48, 1
store i64 %49, i64* %8
br label %50
50: ; preds = %42
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %19
53: ; preds = %37, %19
%54 = load i32, i32* %5
%55 = add nsw i32 %54, -1
store i32 %55, i32* %5
br label %12
56: ; preds = %12
store i64* null, i64** %2
br label %57
57: ; preds = %56, %27
%58 = load i64*, i64** %2
ret i64* %58
}

LLVM IR (-O2):
define i64* @sync_find(i64* %0) {
%2 = getelementptr inbounds i64, i64* %0, i64 1
%3 = load i64, i64* %0
br label %4
4: ; preds = %1, %30
%5 = phi i64 [ %3, %1 ], [ %32, %30 ]
%6 = phi i32 [ 18, %1 ], [ %33, %30 ]
%7 = phi i64* [ %2, %1 ], [ %8, %30 ]
%8 = getelementptr inbounds i64, i64* %7, i64 1
%9 = load i64, i64* %7
br label %10
10: ; preds = %4, %23
%11 = phi i64 [ %9, %4 ], [ %27, %23 ]
%12 = phi i64 [ %5, %4 ], [ %26, %23 ]
%13 = phi i32 [ 0, %4 ], [ %28, %23 ]
%14 = tail call i32 @comp32(i64 %12, i32 1045145627)
%15 = icmp slt i32 %14, 3
br i1 %15, label %35, label %16
16: ; preds = %10
%17 = tail call i32 @comp32(i64 %12, i32 -1431655766)
%18 = add i32 %17, -30
%19 = icmp ult i32 %18, -27
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = zext i32 %13 to i64
%22 = lshr i64 %11, %21
br label %30
23: ; preds = %16
%24 = shl i64 %12, 1
%25 = lshr i64 %11, 31
%26 = or i64 %25, %24
%27 = shl i64 %11, 1
%28 = add nuw nsw i32 %13, 1
%29 = icmp eq i32 %28, 32
br i1 %29, label %30, label %10
30: ; preds = %23, %20
%31 = phi i32 [ 19, %20 ], [ %6, %23 ]
%32 = phi i64 [ %22, %20 ], [ %26, %23 ]
%33 = add nsw i32 %31, -1
%34 = icmp sgt i32 %31, 1
br i1 %34, label %4, label %35
35: ; preds = %30, %10
%36 = phi i64* [ %8, %10 ], [ null, %30 ]
ret i64* %36
}