#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 /*<<< orphan*/ const uint8_t;
struct ip6_ext {
  int ip6e_len;
};
struct cmsghdr {
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
  scalar_t__ cmsg_len;
};

/* Variables and functions */
int /*<<< orphan*/ const *CMSG_DATA(struct cmsghdr const *);
scalar_t__ CMSG_LEN(int);
scalar_t__ IPPROTO_IPV6;
scalar_t__ IPV6_DSTOPTS;
scalar_t__ IPV6_HOPOPTS;
int get_opt_end(int /*<<< orphan*/ const **, int /*<<< orphan*/ const *,
                int /*<<< orphan*/ const *);
int inet6_option_next(const struct cmsghdr *cmsg, uint8_t **tptrp) {
  if (cmsg->cmsg_level != IPPROTO_IPV6 ||
      (cmsg->cmsg_type != IPV6_HOPOPTS && cmsg->cmsg_type != IPV6_DSTOPTS))
    return -1;
  const struct ip6_ext *ip6e = (const struct ip6_ext *)CMSG_DATA(cmsg);
  if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ip6_ext)) ||
      cmsg->cmsg_len < CMSG_LEN((ip6e->ip6e_len + 1) * 8))
    return -1;
  const uint8_t *endp = CMSG_DATA(cmsg) + (ip6e->ip6e_len + 1) * 8;
  const uint8_t *result;
  if (*tptrp == NULL)
    result = (const uint8_t *)(ip6e + 1);
  else {
    if (*tptrp < (const uint8_t *)(ip6e + 1))
      return -1;
    if (get_opt_end(&result, *tptrp, endp) != 0)
      return -1;
  }
  *tptrp = (uint8_t *)result;
  return get_opt_end(&result, result, endp);
}


x86 assembly (clang, -O0):
<inet6_option_next>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%rax
jne    1187 <inet6_option_next+0x57>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%rax
je     1193 <inet6_option_next+0x63>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
je     1193 <inet6_option_next+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <inet6_option_next+0x182>
mov    -0x10(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jl     11f3 <inet6_option_next+0xc3>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
shl    $0x3,%edi
call   1050 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jge    11ff <inet6_option_next+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <inet6_option_next+0x182>
mov    -0x10(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
shl    $0x3,%ecx
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1241 <inet6_option_next+0x111>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
jmp    1293 <inet6_option_next+0x163>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
add    $0x4,%rcx
cmp    %rcx,%rax
jae    1265 <inet6_option_next+0x135>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <inet6_option_next+0x182>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rdi
call   1040 <get_opt_end@plt>
cmp    $0x0,%eax
je     128e <inet6_option_next+0x15e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <inet6_option_next+0x182>
jmp    1293 <inet6_option_next+0x163>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rdi
call   1040 <get_opt_end@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_option_next>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    (%rdi),%rax
mov    0x2e8a(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    1211 <inet6_option_next+0xe1>
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x8(%rdi),%rax
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%rax
je     117a <inet6_option_next+0x4a>
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
jne    1211 <inet6_option_next+0xe1>
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,%r14
mov    0x10(%rbx),%r12
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
cmp    %rax,%r12
jl     1211 <inet6_option_next+0xe1>
mov    0x10(%rbx),%r12
mov    (%r14),%eax
lea    0x8(,%rax,8),%edi
call   1050 <CMSG_LEN@plt>
cmp    %rax,%r12
jl     1211 <inet6_option_next+0xe1>
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%r14),%ecx
lea    0x8(,%rcx,8),%ecx
movslq %ecx,%rcx
lea    (%rax,%rcx,4),%rbx
mov    (%r15),%rsi
add    $0x4,%r14
test   %rsi,%rsi
je     11f7 <inet6_option_next+0xc7>
mov    $0xffffffff,%ebp
cmp    %r14,%rsi
jb     1211 <inet6_option_next+0xe1>
lea    0x8(%rsp),%rdi
mov    %rbx,%rdx
call   1040 <get_opt_end@plt>
test   %eax,%eax
jne    1211 <inet6_option_next+0xe1>
mov    0x8(%rsp),%r14
jmp    11fc <inet6_option_next+0xcc>
mov    %r14,0x8(%rsp)
mov    %r14,(%r15)
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <get_opt_end@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmsghdr = type { i64, i64, i64 }
%struct.ip6_ext = type { i32 }
@IPPROTO_IPV6 = global i64 0
@IPV6_HOPOPTS = global i64 0
@IPV6_DSTOPTS = global i64 0
define i32 @inet6_option_next(%struct.cmsghdr* %0, i32** %1) {
%3 = alloca i32
%4 = alloca %struct.cmsghdr*
%5 = alloca i32**
%6 = alloca %struct.ip6_ext*
%7 = alloca i32*
%8 = alloca i32*
store %struct.cmsghdr* %0, %struct.cmsghdr** %4
store i32** %1, i32*** %5
%9 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%10 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %9, i32 0, i32 0
%11 = load i64, i64* %10
%12 = load i64, i64* @IPPROTO_IPV6
%13 = icmp ne i64 %11, %12
br i1 %13, label %26, label %14
14: ; preds = %2
%15 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%16 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %15, i32 0, i32 1
%17 = load i64, i64* %16
%18 = load i64, i64* @IPV6_HOPOPTS
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %27
20: ; preds = %14
%21 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%22 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i32 0, i32 1
%23 = load i64, i64* %22
%24 = load i64, i64* @IPV6_DSTOPTS
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %20, %2
store i32 -1, i32* %3
br label %87
27: ; preds = %20, %14
%28 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%29 = call i32* @CMSG_DATA(%struct.cmsghdr* %28)
%30 = bitcast i32* %29 to %struct.ip6_ext*
store %struct.ip6_ext* %30, %struct.ip6_ext** %6
%31 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%32 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %31, i32 0, i32 2
%33 = load i64, i64* %32
%34 = call i64 @CMSG_LEN(i32 4)
%35 = icmp slt i64 %33, %34
br i1 %35, label %47, label %36
36: ; preds = %27
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%38 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %37, i32 0, i32 2
%39 = load i64, i64* %38
%40 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%41 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = add nsw i32 %42, 1
%44 = mul nsw i32 %43, 8
%45 = call i64 @CMSG_LEN(i32 %44)
%46 = icmp slt i64 %39, %45
br i1 %46, label %47, label %48
47: ; preds = %36, %27
store i32 -1, i32* %3
br label %87
48: ; preds = %36
%49 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%50 = call i32* @CMSG_DATA(%struct.cmsghdr* %49)
%51 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%52 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = add nsw i32 %53, 1
%55 = mul nsw i32 %54, 8
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %50, i64 %56
store i32* %57, i32** %7
%58 = load i32**, i32*** %5
%59 = load i32*, i32** %58
%60 = icmp eq i32* %59, null
br i1 %60, label %61, label %65
61: ; preds = %48
%62 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%63 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %62, i64 1
%64 = bitcast %struct.ip6_ext* %63 to i32*
store i32* %64, i32** %8
br label %81
65: ; preds = %48
%66 = load i32**, i32*** %5
%67 = load i32*, i32** %66
%68 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%69 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %68, i64 1
%70 = bitcast %struct.ip6_ext* %69 to i32*
%71 = icmp ult i32* %67, %70
br i1 %71, label %72, label %73
72: ; preds = %65
store i32 -1, i32* %3
br label %87
73: ; preds = %65
%74 = load i32**, i32*** %5
%75 = load i32*, i32** %74
%76 = load i32*, i32** %7
%77 = call i32 @get_opt_end(i32** %8, i32* %75, i32* %76)
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %73
store i32 -1, i32* %3
br label %87
80: ; preds = %73
br label %81
81: ; preds = %80, %61
%82 = load i32*, i32** %8
%83 = load i32**, i32*** %5
store i32* %82, i32** %83
%84 = load i32*, i32** %8
%85 = load i32*, i32** %7
%86 = call i32 @get_opt_end(i32** %8, i32* %84, i32* %85)
store i32 %86, i32* %3
br label %87
87: ; preds = %81, %79, %72, %47, %26
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.cmsghdr = type { i64, i64, i64 }
@IPPROTO_IPV6 = global i64 0
@IPV6_HOPOPTS = global i64 0
@IPV6_DSTOPTS = global i64 0
define i32 @inet6_option_next(%struct.cmsghdr* %0, i32** nocapture %1) {
%3 = alloca i32*
%4 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %0, i64 0, i32 0
%5 = load i64, i64* %4
%6 = load i64, i64* @IPPROTO_IPV6
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %53
8: ; preds = %2
%9 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %0, i64 0, i32 1
%10 = load i64, i64* %9
%11 = load i64, i64* @IPV6_HOPOPTS
%12 = icmp eq i64 %10, %11
%13 = load i64, i64* @IPV6_DSTOPTS
%14 = icmp eq i64 %10, %13
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %53
16: ; preds = %8
%17 = tail call i32* @CMSG_DATA(%struct.cmsghdr* %0)
%18 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %0, i64 0, i32 2
%19 = load i64, i64* %18
%20 = tail call i64 @CMSG_LEN(i32 4)
%21 = icmp slt i64 %19, %20
br i1 %21, label %53, label %22
22: ; preds = %16
%23 = load i64, i64* %18
%24 = load i32, i32* %17
%25 = shl i32 %24, 3
%26 = add i32 %25, 8
%27 = tail call i64 @CMSG_LEN(i32 %26)
%28 = icmp slt i64 %23, %27
br i1 %28, label %53, label %29
29: ; preds = %22
%30 = tail call i32* @CMSG_DATA(%struct.cmsghdr* %0)
%31 = load i32, i32* %17
%32 = shl i32 %31, 3
%33 = add i32 %32, 8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %30, i64 %34
%36 = bitcast i32** %3 to i8*
%37 = load i32*, i32** %1
%38 = icmp eq i32* %37, null
%39 = getelementptr inbounds i32, i32* %17, i64 1
br i1 %38, label %40, label %41
40: ; preds = %29
store i32* %39, i32** %3
br label %48
41: ; preds = %29
%42 = icmp ult i32* %37, %39
br i1 %42, label %51, label %43
43: ; preds = %41
%44 = call i32 @get_opt_end(i32** %3, i32* %37, i32* %35)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %43
%47 = load i32*, i32** %3
br label %48
48: ; preds = %46, %40
%49 = phi i32* [ %47, %46 ], [ %39, %40 ]
store i32* %49, i32** %1
%50 = call i32 @get_opt_end(i32** %3, i32* %49, i32* %35)
br label %51
51: ; preds = %43, %41, %48
%52 = phi i32 [ %50, %48 ], [ -1, %41 ], [ -1, %43 ]
br label %53
53: ; preds = %51, %22, %16, %2, %8
%54 = phi i32 [ -1, %8 ], [ -1, %2 ], [ %52, %51 ], [ -1, %22 ], [ -1, %16 ]
ret i32 %54
}