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

/* Variables and functions */
uint8_t *find_sync(const uint8_t *buf, int len) {
  int i;
  for (i = 1; i < len; i++)
    if (buf[i] == 0xAA && buf[i - 1] == 0xAA)
      return (uint8_t *)&buf[i - 1];
  return NULL;
}


x86 assembly (clang, -O0):
<find_sync>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    117d <find_sync+0x7d>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmpl   $0xaa,(%rax,%rcx,4)
jne    116a <find_sync+0x6a>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmpl   $0xaa,(%rax,%rcx,4)
jne    116a <find_sync+0x6a>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1185 <find_sync+0x85>
jmp    116f <find_sync+0x6f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1112 <find_sync+0x12>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_sync>:
cmp    $0x2,%esi
jl     112c <find_sync+0x2c>
mov    %rdi,%rax
mov    %esi,%ecx
add    $0xffffffffffffffff,%rcx
jmp    111a <find_sync+0x1a>
add    $0x4,%rax
add    $0xffffffffffffffff,%rcx
je     112c <find_sync+0x2c>
cmpl   $0xaa,0x4(%rax)
jne    1110 <find_sync+0x10>
cmpl   $0xaa,(%rax)
jne    1110 <find_sync+0x10>
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32* @find_sync(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 1, i32* %6
br label %7
7: ; preds = %33, %2
%8 = load i32, i32* %6
%9 = load i32, i32* %5
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %36
11: ; preds = %7
%12 = load i32*, i32** %4
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i32, i32* %12, i64 %14
%16 = load i32, i32* %15
%17 = icmp eq i32 %16, 170
br i1 %17, label %18, label %32
18: ; preds = %11
%19 = load i32*, i32** %4
%20 = load i32, i32* %6
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %19, i64 %22
%24 = load i32, i32* %23
%25 = icmp eq i32 %24, 170
br i1 %25, label %26, label %32
26: ; preds = %18
%27 = load i32*, i32** %4
%28 = load i32, i32* %6
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %27, i64 %30
store i32* %31, i32** %3
br label %37
32: ; preds = %18, %11
br label %33
33: ; preds = %32
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %7
36: ; preds = %7
store i32* null, i32** %3
br label %37
37: ; preds = %36, %26
%38 = load i32*, i32** %3
ret i32* %38
}

LLVM IR (-O2):
define i32* @find_sync(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %21
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %16
%7 = phi i64 [ 1, %4 ], [ %17, %16 ]
%8 = getelementptr inbounds i32, i32* %0, i64 %7
%9 = load i32, i32* %8
%10 = icmp eq i32 %9, 170
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = add nsw i64 %7, -1
%13 = getelementptr inbounds i32, i32* %0, i64 %12
%14 = load i32, i32* %13
%15 = icmp eq i32 %14, 170
br i1 %15, label %19, label %16
16: ; preds = %6, %11
%17 = add nuw nsw i64 %7, 1
%18 = icmp eq i64 %17, %5
br i1 %18, label %21, label %6
19: ; preds = %11
%20 = getelementptr inbounds i32, i32* %0, i64 %12
br label %21
21: ; preds = %16, %19, %2
%22 = phi i32* [ null, %2 ], [ %20, %19 ], [ null, %16 ]
ret i32* %22
}