#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 ts_packet_get_payload_offset(uint8_t *ts_packet) {
  if (ts_packet[0] != 0x47)
    return 0;
  uint8_t adapt_field = (ts_packet[3] & ~0xDF) >> 5;
  uint8_t payload_field = (ts_packet[3] & ~0xEF) >> 4;
  if (!adapt_field && !payload_field)
    return 0;
  if (adapt_field) {
    uint8_t adapt_len = ts_packet[4];
    if (payload_field && adapt_len > 182)
      return 0;
    if (!payload_field && adapt_len > 183)
      return 0;
    if (adapt_len + 4 > 188)
      return 0;
    return 4 + 1 + adapt_len;
  } else {
    return 4;
  }
}


x86 assembly (clang, -O0):
<ts_packet_get_payload_offset>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x47,(%rax)
je     1121 <ts_packet_get_payload_offset+0x21>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <ts_packet_get_payload_offset+0xf1>
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%eax
and    $0xffffff20,%eax
sar    $0x5,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%eax
and    $0xffffff10,%eax
sar    $0x4,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1165 <ts_packet_get_payload_offset+0x65>
cmpl   $0x0,-0x18(%rbp)
jne    1165 <ts_packet_get_payload_offset+0x65>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <ts_packet_get_payload_offset+0xf1>
cmpl   $0x0,-0x14(%rbp)
je     11ea <ts_packet_get_payload_offset+0xea>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     119c <ts_packet_get_payload_offset+0x9c>
cmpl   $0xb6,-0x1c(%rbp)
jle    119c <ts_packet_get_payload_offset+0x9c>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <ts_packet_get_payload_offset+0xf1>
cmpl   $0x0,-0x18(%rbp)
jne    11bf <ts_packet_get_payload_offset+0xbf>
cmpl   $0xb7,-0x1c(%rbp)
jle    11bf <ts_packet_get_payload_offset+0xbf>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <ts_packet_get_payload_offset+0xf1>
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
cmp    $0xbc,%eax
jle    11dc <ts_packet_get_payload_offset+0xdc>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <ts_packet_get_payload_offset+0xf1>
mov    -0x1c(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0x4(%rbp)
jmp    11f1 <ts_packet_get_payload_offset+0xf1>
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<ts_packet_get_payload_offset>:
xor    %eax,%eax
cmpl   $0x47,(%rdi)
jne    113a <ts_packet_get_payload_offset+0x3a>
mov    0xc(%rdi),%ecx
mov    %ecx,%edx
sar    $0x4,%edx
mov    %edx,%esi
and    $0xfffffff1,%esi
and    $0xffffff20,%ecx
xor    %r9d,%r9d
or     %ecx,%esi
setne  %r8b
test   %ecx,%ecx
je     113b <ts_packet_get_payload_offset+0x3b>
mov    0x10(%rdi),%ecx
test   $0xfffffff1,%edx
je     1146 <ts_packet_get_payload_offset+0x46>
cmp    $0xb6,%ecx
jle    1146 <ts_packet_get_payload_offset+0x46>
ret
mov    %r8b,%r9b
shl    $0x2,%r9d
mov    %r9d,%eax
ret
lea    0x5(%rcx),%esi
xor    %eax,%eax
cmp    $0xb9,%ecx
cmovge %eax,%esi
cmp    $0xb8,%ecx
cmovl  %esi,%eax
test   $0xfffffff1,%edx
cmovne %esi,%eax
ret

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

LLVM IR (-O2):
define i32 @ts_packet_get_payload_offset(i32* nocapture %0) {
%2 = load i32, i32* %0
%3 = icmp eq i32 %2, 71
br i1 %3, label %4, label %27
4: ; preds = %1
%5 = getelementptr inbounds i32, i32* %0, i64 3
%6 = load i32, i32* %5
%7 = ashr i32 %6, 4
%8 = and i32 %7, -15
%9 = and i32 %6, -224
%10 = icmp ne i32 %9, 0
%11 = icmp ne i32 %8, 0
%12 = select i1 %10, i1 true, i1 %11
%13 = select i1 %12, i32 4, i32 0
br i1 %10, label %14, label %27
14: ; preds = %4
%15 = getelementptr inbounds i32, i32* %0, i64 4
%16 = load i32, i32* %15
%17 = icmp sgt i32 %16, 182
%18 = select i1 %11, i1 %17, i1 false
br i1 %18, label %27, label %19
19: ; preds = %14
%20 = icmp eq i32 %8, 0
%21 = icmp sgt i32 %16, 183
%22 = select i1 %20, i1 %21, i1 false
%23 = icmp sgt i32 %16, 184
%24 = select i1 %22, i1 true, i1 %23
%25 = add nsw i32 %16, 5
%26 = select i1 %24, i32 0, i32 %25
br label %27
27: ; preds = %19, %4, %14, %1
%28 = phi i32 [ 0, %1 ], [ %13, %4 ], [ 0, %14 ], [ %26, %19 ]
ret i32 %28
}