unsigned char *FindNextH264StartCode(unsigned char *pBuf,
                                     unsigned char *pBuf_end) {
  unsigned char zero_cnt;
  zero_cnt = 0;
  while (1) {
    if (pBuf == pBuf_end) {
      break;
    }
    if (*pBuf == 0) {
      zero_cnt++;
    } else if ((zero_cnt >= 3) && (*pBuf == 1)) {
      zero_cnt = 0;
      pBuf++;
      break;
    } else {
      zero_cnt = 0;
    }
    pBuf++;
  }
  return pBuf;
}


x86 assembly (clang, -O0):
<FindNextH264StartCode>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1123 <FindNextH264StartCode+0x23>
jmp    118c <FindNextH264StartCode+0x8c>
mov    -0x8(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
jne    1140 <FindNextH264StartCode+0x40>
mov    -0x11(%rbp),%al
add    $0x1,%al
mov    %al,-0x11(%rbp)
jmp    117b <FindNextH264StartCode+0x7b>
movzbl -0x11(%rbp),%eax
cmp    $0x3,%eax
jl     1172 <FindNextH264StartCode+0x72>
mov    -0x8(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x1,%eax
jne    1172 <FindNextH264StartCode+0x72>
movb   $0x0,-0x11(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    118c <FindNextH264StartCode+0x8c>
movb   $0x0,-0x11(%rbp)
jmp    117b <FindNextH264StartCode+0x7b>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1110 <FindNextH264StartCode+0x10>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<FindNextH264StartCode>:
mov    %rdi,%rax
cmp    %rsi,%rdi
je     1136 <FindNextH264StartCode+0x36>
xor    %ecx,%ecx
jmp    111c <FindNextH264StartCode+0x1c>
nopl   0x0(%rax)
add    $0x1,%cl
add    $0x1,%rax
cmp    %rsi,%rax
je     1137 <FindNextH264StartCode+0x37>
movzbl (%rax),%edx
test   %dl,%dl
je     1110 <FindNextH264StartCode+0x10>
cmp    $0x3,%cl
mov    $0x0,%ecx
jb     1113 <FindNextH264StartCode+0x13>
cmp    $0x1,%dl
jne    1113 <FindNextH264StartCode+0x13>
add    $0x1,%rax
ret
mov    %rsi,%rax
ret

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

LLVM IR (-O2):
define i8* @FindNextH264StartCode(i8* %0, i8* readnone %1) {
%3 = icmp eq i8* %0, %1
br i1 %3, label %26, label %4
4: ; preds = %2
%5 = ptrtoint i8* %1 to i64
%6 = ptrtoint i8* %0 to i64
%7 = sub i64 %5, %6
%8 = getelementptr i8, i8* %0, i64 %7
br label %9
9: ; preds = %4, %22
%10 = phi i8 [ %23, %22 ], [ 0, %4 ]
%11 = phi i8* [ %24, %22 ], [ %0, %4 ]
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = add i8 %10, 1
br label %22
16: ; preds = %9
%17 = icmp ugt i8 %10, 2
%18 = icmp eq i8 %12, 1
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = getelementptr inbounds i8, i8* %11, i64 1
br label %26
22: ; preds = %16, %14
%23 = phi i8 [ %15, %14 ], [ 0, %16 ]
%24 = getelementptr inbounds i8, i8* %11, i64 1
%25 = icmp eq i8* %24, %1
br i1 %25, label %26, label %9
26: ; preds = %22, %2, %20
%27 = phi i8* [ %21, %20 ], [ %0, %2 ], [ %8, %22 ]
ret i8* %27
}