#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 sync_scan(unsigned char *, int, int);
int sync_test(unsigned char *, int, int, int);
int find_sync(unsigned char *buf, int n) {
  int i0, isync, nmatch, pad;
  int padbytes, option;
  i0 = 24;
  padbytes = 1;
  option = (buf[1] & 0x06) >> 1;
  if (option == 3) {
    padbytes = 4;
    i0 = 24;
  }
  pad = (buf[2] & 0x02) >> 1;
  n -= 3;
  while (i0 < 2000) {
    isync = sync_scan(buf, n, i0);
    i0 = isync + 1;
    isync -= pad;
    if (isync <= 0)
      return 0;
    nmatch = sync_test(buf, n, isync, padbytes);
    if (nmatch > 0)
      return isync;
  }
  return 0;
}


x86 assembly (clang, -O0):
<find_sync>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x18,-0x18(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
and    $0x6,%eax
sar    $0x1,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x3,-0x2c(%rbp)
jne    1166 <find_sync+0x46>
movl   $0x4,-0x28(%rbp)
movl   $0x18,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
and    $0x2,%eax
sar    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x7d0,-0x18(%rbp)
jge    11f8 <find_sync+0xd8>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <sync_scan@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11c9 <find_sync+0xa9>
movl   $0x0,-0x4(%rbp)
jmp    11ff <find_sync+0xdf>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%ecx
call   1040 <sync_test@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11f3 <find_sync+0xd3>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <find_sync+0xdf>
jmp    1180 <find_sync+0x60>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_sync>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r12
mov    0x1(%rdi),%al
not    %al
xor    %ecx,%ecx
test   $0x6,%al
sete   %cl
lea    (%rcx,%rcx,2),%eax
add    $0x1,%eax
mov    %eax,0x4(%rsp)
movzbl 0x2(%rdi),%r14d
shr    %r14d
and    $0x1,%r14d
add    $0xfffffffd,%r15d
mov    $0x18,%ebx
nopl   0x0(%rax)
xor    %r13d,%r13d
cmp    $0x7cf,%ebx
jg     119c <find_sync+0x7c>
mov    %r12,%rdi
mov    %r15d,%esi
mov    %ebx,%edx
call   1030 <sync_scan@plt>
mov    %eax,%ebp
sub    %r14d,%ebp
jle    119c <find_sync+0x7c>
mov    %eax,%ebx
add    $0x1,%ebx
mov    %r12,%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    0x4(%rsp),%ecx
call   1040 <sync_test@plt>
mov    %ebp,%r13d
test   %eax,%eax
jle    1160 <find_sync+0x40>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @find_sync(i8* %0, i32 %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 1
%4 = load i8, i8* %3
%5 = and i8 %4, 6
%6 = icmp eq i8 %5, 6
%7 = select i1 %6, i32 4, i32 1
%8 = getelementptr inbounds i8, i8* %0, i64 2
%9 = load i8, i8* %8
%10 = lshr i8 %9, 1
%11 = and i8 %10, 1
%12 = zext i8 %11 to i32
%13 = add nsw i32 %1, -3
br label %14
14: ; preds = %21, %2
%15 = phi i32 [ 24, %2 ], [ %22, %21 ]
%16 = icmp slt i32 %15, 2000
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = tail call i32 @sync_scan(i8* %0, i32 %13, i32 %15)
%19 = sub nsw i32 %18, %12
%20 = icmp slt i32 %19, 1
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = add nsw i32 %18, 1
%23 = tail call i32 @sync_test(i8* %0, i32 %13, i32 %19, i32 %7)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %14
25: ; preds = %14, %21, %17
%26 = phi i32 [ 0, %17 ], [ %19, %21 ], [ 0, %14 ]
ret i32 %26
}