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

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr const *);
scalar_t__ CMSG_SPACE(int);
scalar_t__ IPPROTO_IPV6;
scalar_t__ IPV6_DSTOPTS;
scalar_t__ IPV6_HOPOPTS;
int ip6optlen(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int inet6_option_next(const struct cmsghdr *cmsg, u_int8_t **tptrp) {
  struct ip6_ext *ip6e;
  int hdrlen, optlen;
  u_int8_t *lim;
  if (cmsg->cmsg_level != IPPROTO_IPV6 ||
      (cmsg->cmsg_type != IPV6_HOPOPTS && cmsg->cmsg_type != IPV6_DSTOPTS))
    return (-1);
  if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
    return (-1);
  ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
  hdrlen = (ip6e->ip6e_len + 1) << 3;
  if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
    return (-1);
  lim = (u_int8_t *)ip6e + hdrlen;
  if (*tptrp == NULL)
    *tptrp = (u_int8_t *)(ip6e + 1);
  else {
    if ((optlen = ip6optlen(*tptrp, lim)) == 0)
      return (-1);
    *tptrp = *tptrp + optlen;
  }
  if (*tptrp >= lim) {
    *tptrp = NULL;
    return (-1);
  }
  if (ip6optlen(*tptrp, lim) == 0)
    return (-1);
  else
    return (0);
}


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    12db <inet6_option_next+0x1ab>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jge    11c5 <inet6_option_next+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <inet6_option_next+0x1ab>
mov    -0x10(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
shl    $0x3,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <CMSG_SPACE@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jge    1211 <inet6_option_next+0xe1>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <inet6_option_next+0x1ab>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1246 <inet6_option_next+0x116>
mov    -0x20(%rbp),%rcx
add    $0x4,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1287 <inet6_option_next+0x157>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <ip6optlen@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jne    126e <inet6_option_next+0x13e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <inet6_option_next+0x1ab>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
movslq -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x30(%rbp),%rax
jb     12af <inet6_option_next+0x17f>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <inet6_option_next+0x1ab>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <ip6optlen@plt>
cmp    $0x0,%eax
jne    12d4 <inet6_option_next+0x1a4>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <inet6_option_next+0x1ab>
movl   $0x0,-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
mov    (%rdi),%rax
mov    0x2e8e(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    11fb <inet6_option_next+0xcb>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x8(%rdi),%rax
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%rax
je     1176 <inet6_option_next+0x46>
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jne    11fb <inet6_option_next+0xcb>
mov    0x10(%rbx),%r15
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
cmp    %rax,%r15
jl     11fb <inet6_option_next+0xcb>
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,%r15
mov    (%rax),%eax
lea    0x8(,%rax,8),%r12d
mov    0x10(%rbx),%rbx
mov    %r12d,%edi
call   1040 <CMSG_SPACE@plt>
cmp    %rax,%rbx
jl     11fb <inet6_option_next+0xcb>
movslq %r12d,%rax
lea    (%r15,%rax,4),%r12
mov    (%r14),%rdi
test   %rdi,%rdi
je     11d6 <inet6_option_next+0xa6>
mov    %r12,%rsi
call   1050 <ip6optlen@plt>
test   %eax,%eax
je     11fb <inet6_option_next+0xcb>
movslq %eax,%r15
shl    $0x2,%r15
add    (%r14),%r15
jmp    11da <inet6_option_next+0xaa>
add    $0x4,%r15
mov    %r15,(%r14)
cmp    %r12,%r15
jae    11f4 <inet6_option_next+0xc4>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <ip6optlen@plt>
cmp    $0x1,%eax
sbb    %ebp,%ebp
jmp    11fb <inet6_option_next+0xcb>
movq   $0x0,(%r14)
mov    %ebp,%eax
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
%9 = alloca i32*
store %struct.cmsghdr* %0, %struct.cmsghdr** %4
store i32** %1, i32*** %5
%10 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%11 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @IPPROTO_IPV6
%14 = icmp ne i64 %12, %13
br i1 %14, label %27, label %15
15: ; preds = %2
%16 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%17 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %16, i32 0, i32 1
%18 = load i64, i64* %17
%19 = load i64, i64* @IPV6_HOPOPTS
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %28
21: ; preds = %15
%22 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%23 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %22, i32 0, i32 1
%24 = load i64, i64* %23
%25 = load i64, i64* @IPV6_DSTOPTS
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %21, %2
store i32 -1, i32* %3
br label %94
28: ; preds = %21, %15
%29 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%30 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %29, i32 0, i32 2
%31 = load i64, i64* %30
%32 = call i64 @CMSG_SPACE(i32 4)
%33 = icmp slt i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %28
store i32 -1, i32* %3
br label %94
35: ; preds = %28
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%37 = call i64 @CMSG_DATA(%struct.cmsghdr* %36)
%38 = inttoptr i64 %37 to %struct.ip6_ext*
store %struct.ip6_ext* %38, %struct.ip6_ext** %6
%39 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%40 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = add nsw i32 %41, 1
%43 = shl i32 %42, 3
store i32 %43, i32* %7
%44 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%45 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %44, i32 0, i32 2
%46 = load i64, i64* %45
%47 = load i32, i32* %7
%48 = call i64 @CMSG_SPACE(i32 %47)
%49 = icmp slt i64 %46, %48
br i1 %49, label %50, label %51
50: ; preds = %35
store i32 -1, i32* %3
br label %94
51: ; preds = %35
%52 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%53 = bitcast %struct.ip6_ext* %52 to i32*
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %53, i64 %55
store i32* %56, i32** %9
%57 = load i32**, i32*** %5
%58 = load i32*, i32** %57
%59 = icmp eq i32* %58, null
br i1 %59, label %60, label %65
60: ; preds = %51
%61 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%62 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %61, i64 1
%63 = bitcast %struct.ip6_ext* %62 to i32*
%64 = load i32**, i32*** %5
store i32* %63, i32** %64
br label %79
65: ; preds = %51
%66 = load i32**, i32*** %5
%67 = load i32*, i32** %66
%68 = load i32*, i32** %9
%69 = call i32 @ip6optlen(i32* %67, i32* %68)
store i32 %69, i32* %8
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %65
store i32 -1, i32* %3
br label %94
72: ; preds = %65
%73 = load i32**, i32*** %5
%74 = load i32*, i32** %73
%75 = load i32, i32* %8
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i32, i32* %74, i64 %76
%78 = load i32**, i32*** %5
store i32* %77, i32** %78
br label %79
79: ; preds = %72, %60
%80 = load i32**, i32*** %5
%81 = load i32*, i32** %80
%82 = load i32*, i32** %9
%83 = icmp uge i32* %81, %82
br i1 %83, label %84, label %86
84: ; preds = %79
%85 = load i32**, i32*** %5
store i32* null, i32** %85
store i32 -1, i32* %3
br label %94
86: ; preds = %79
%87 = load i32**, i32*** %5
%88 = load i32*, i32** %87
%89 = load i32*, i32** %9
%90 = call i32 @ip6optlen(i32* %88, i32* %89)
%91 = icmp eq i32 %90, 0
br i1 %91, label %92, label %93
92: ; preds = %86
store i32 -1, i32* %3
br label %94
93: ; preds = %86
store i32 0, i32* %3
br label %94
94: ; preds = %93, %92, %84, %71, %50, %34, %27
%95 = load i32, i32* %3
ret i32 %95
}

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