#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*/ 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*/ CCMSG_DATA(struct cmsghdr const *);
scalar_t__ CMSG_SPACE(int);
scalar_t__ IPPROTO_IPV6;
scalar_t__ IPV6_DSTOPTS;
scalar_t__ IPV6_HOPOPTS;
int /*<<< orphan*/ _DIAGASSERT(int /*<<< orphan*/);
struct ip6_ext *__UNCONST(int /*<<< orphan*/);
int ip6optlen(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int inet6_option_next(const struct cmsghdr *cmsg, uint8_t **tptrp) {
  struct ip6_ext *ip6e;
  int hdrlen, optlen;
  uint8_t *lim;
  _DIAGASSERT(cmsg != NULL);
  _DIAGASSERT(tptrp != NULL);
  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 = __UNCONST(CCMSG_DATA(cmsg));
  hdrlen = (ip6e->ip6e_len + 1) << 3;
  if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
    return (-1);
  lim = (uint8_t *)(void *)ip6e + hdrlen;
  if (*tptrp == NULL)
    *tptrp = (uint8_t *)(void *)(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)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <_DIAGASSERT@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <_DIAGASSERT@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jne    11cb <inet6_option_next+0x7b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
je     11d7 <inet6_option_next+0x87>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%rax
je     11d7 <inet6_option_next+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <inet6_option_next+0x1d6>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    $0x4,%edi
call   1030 <CMSG_SPACE@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jge    1209 <inet6_option_next+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <inet6_option_next+0x1d6>
mov    -0x10(%rbp),%rdi
call   1040 <CCMSG_DATA@plt>
mov    %eax,%edi
call   1070 <__UNCONST@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   1030 <CMSG_SPACE@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jge    125c <inet6_option_next+0x10c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <inet6_option_next+0x1d6>
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    1291 <inet6_option_next+0x141>
mov    -0x20(%rbp),%rcx
add    $0x4,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    12d2 <inet6_option_next+0x182>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <ip6optlen@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jne    12b9 <inet6_option_next+0x169>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <inet6_option_next+0x1d6>
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     12fa <inet6_option_next+0x1aa>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <inet6_option_next+0x1d6>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <ip6optlen@plt>
cmp    $0x0,%eax
jne    131f <inet6_option_next+0x1cf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <inet6_option_next+0x1d6>
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    %rsi,%rbx
mov    %rdi,%rbp
xor    %edi,%edi
test   %rbp,%rbp
setne  %dil
call   1050 <_DIAGASSERT@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <_DIAGASSERT@plt>
mov    0x0(%rbp),%rax
mov    0x2e4b(%rip),%rcx
mov    $0xffffffff,%r14d
cmp    (%rcx),%rax
jne    1241 <inet6_option_next+0xf1>
mov    0x8(%rbp),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
je     11b4 <inet6_option_next+0x64>
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
jne    1241 <inet6_option_next+0xf1>
mov    0x10(%rbp),%r15
mov    $0x4,%edi
call   1030 <CMSG_SPACE@plt>
cmp    %rax,%r15
jl     1241 <inet6_option_next+0xf1>
mov    %rbp,%rdi
call   1040 <CCMSG_DATA@plt>
mov    %eax,%edi
call   1070 <__UNCONST@plt>
mov    %rax,%r15
mov    (%rax),%eax
lea    0x8(,%rax,8),%r12d
mov    0x10(%rbp),%rbp
mov    %r12d,%edi
call   1030 <CMSG_SPACE@plt>
cmp    %rax,%rbp
jl     1241 <inet6_option_next+0xf1>
movslq %r12d,%rax
lea    (%r15,%rax,4),%rbp
mov    (%rbx),%rdi
test   %rdi,%rdi
je     121b <inet6_option_next+0xcb>
mov    %rbp,%rsi
call   1060 <ip6optlen@plt>
test   %eax,%eax
je     1241 <inet6_option_next+0xf1>
movslq %eax,%r15
shl    $0x2,%r15
add    (%rbx),%r15
jmp    121f <inet6_option_next+0xcf>
add    $0x4,%r15
mov    %r15,(%rbx)
cmp    %rbp,%r15
jae    123a <inet6_option_next+0xea>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1060 <ip6optlen@plt>
cmp    $0x1,%eax
sbb    %r14d,%r14d
jmp    1241 <inet6_option_next+0xf1>
movq   $0x0,(%rbx)
mov    %r14d,%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 = icmp ne %struct.cmsghdr* %10, null
%12 = zext i1 %11 to i32
%13 = call i32 @_DIAGASSERT(i32 %12)
%14 = load i32**, i32*** %5
%15 = icmp ne i32** %14, null
%16 = zext i1 %15 to i32
%17 = call i32 @_DIAGASSERT(i32 %16)
%18 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%19 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @IPPROTO_IPV6
%22 = icmp ne i64 %20, %21
br i1 %22, label %35, label %23
23: ; preds = %2
%24 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%25 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %24, i32 0, i32 1
%26 = load i64, i64* %25
%27 = load i64, i64* @IPV6_HOPOPTS
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %36
29: ; preds = %23
%30 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i32 0, i32 1
%32 = load i64, i64* %31
%33 = load i64, i64* @IPV6_DSTOPTS
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %29, %2
store i32 -1, i32* %3
br label %104
36: ; preds = %29, %23
%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 = call i64 @CMSG_SPACE(i32 4)
%41 = icmp slt i64 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 -1, i32* %3
br label %104
43: ; preds = %36
%44 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%45 = call i32 @CCMSG_DATA(%struct.cmsghdr* %44)
%46 = call %struct.ip6_ext* @__UNCONST(i32 %45)
store %struct.ip6_ext* %46, %struct.ip6_ext** %6
%47 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%48 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = add nsw i32 %49, 1
%51 = shl i32 %50, 3
store i32 %51, i32* %7
%52 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%53 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %52, i32 0, i32 2
%54 = load i64, i64* %53
%55 = load i32, i32* %7
%56 = call i64 @CMSG_SPACE(i32 %55)
%57 = icmp slt i64 %54, %56
br i1 %57, label %58, label %59
58: ; preds = %43
store i32 -1, i32* %3
br label %104
59: ; preds = %43
%60 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%61 = bitcast %struct.ip6_ext* %60 to i8*
%62 = bitcast i8* %61 to i32*
%63 = load i32, i32* %7
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i32, i32* %62, i64 %64
store i32* %65, i32** %9
%66 = load i32**, i32*** %5
%67 = load i32*, i32** %66
%68 = icmp eq i32* %67, null
br i1 %68, label %69, label %75
69: ; preds = %59
%70 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%71 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %70, i64 1
%72 = bitcast %struct.ip6_ext* %71 to i8*
%73 = bitcast i8* %72 to i32*
%74 = load i32**, i32*** %5
store i32* %73, i32** %74
br label %89
75: ; preds = %59
%76 = load i32**, i32*** %5
%77 = load i32*, i32** %76
%78 = load i32*, i32** %9
%79 = call i32 @ip6optlen(i32* %77, i32* %78)
store i32 %79, i32* %8
%80 = icmp eq i32 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %75
store i32 -1, i32* %3
br label %104
82: ; preds = %75
%83 = load i32**, i32*** %5
%84 = load i32*, i32** %83
%85 = load i32, i32* %8
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i32, i32* %84, i64 %86
%88 = load i32**, i32*** %5
store i32* %87, i32** %88
br label %89
89: ; preds = %82, %69
%90 = load i32**, i32*** %5
%91 = load i32*, i32** %90
%92 = load i32*, i32** %9
%93 = icmp uge i32* %91, %92
br i1 %93, label %94, label %96
94: ; preds = %89
%95 = load i32**, i32*** %5
store i32* null, i32** %95
store i32 -1, i32* %3
br label %104
96: ; preds = %89
%97 = load i32**, i32*** %5
%98 = load i32*, i32** %97
%99 = load i32*, i32** %9
%100 = call i32 @ip6optlen(i32* %98, i32* %99)
%101 = icmp eq i32 %100, 0
br i1 %101, label %102, label %103
102: ; preds = %96
store i32 -1, i32* %3
br label %104
103: ; preds = %96
store i32 0, i32* %3
br label %104
104: ; preds = %103, %102, %94, %81, %58, %42, %35
%105 = load i32, i32* %3
ret i32 %105
}

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