#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;
  int 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;
int IPV6_DSTOPTS;
int 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) == 22) &&
       !((cmsg->cmsg_type) == IPV6_DSTOPTS || (cmsg->cmsg_type) == 23)))
    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    119f <inet6_option_next+0x6f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%eax
je     11ab <inet6_option_next+0x7b>
mov    -0x10(%rbp),%rax
cmpl   $0x16,0x8(%rax)
je     11ab <inet6_option_next+0x7b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%eax
je     11ab <inet6_option_next+0x7b>
mov    -0x10(%rbp),%rax
cmpl   $0x17,0x8(%rax)
je     11ab <inet6_option_next+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <inet6_option_next+0x1c3>
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    11dd <inet6_option_next+0xad>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <inet6_option_next+0x1c3>
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    1229 <inet6_option_next+0xf9>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <inet6_option_next+0x1c3>
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    125e <inet6_option_next+0x12e>
mov    -0x20(%rbp),%rcx
add    $0x4,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    129f <inet6_option_next+0x16f>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <ip6optlen@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jne    1286 <inet6_option_next+0x156>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <inet6_option_next+0x1c3>
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     12c7 <inet6_option_next+0x197>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <inet6_option_next+0x1c3>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <ip6optlen@plt>
cmp    $0x0,%eax
jne    12ec <inet6_option_next+0x1bc>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <inet6_option_next+0x1c3>
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    1202 <inet6_option_next+0xd2>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x8(%rdi),%eax
cmp    $0x16,%eax
je     117d <inet6_option_next+0x4d>
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%eax
je     117d <inet6_option_next+0x4d>
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%eax
je     117d <inet6_option_next+0x4d>
cmp    $0x17,%eax
jne    1202 <inet6_option_next+0xd2>
mov    0x10(%rbx),%r15
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
cmp    %rax,%r15
jl     1202 <inet6_option_next+0xd2>
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     1202 <inet6_option_next+0xd2>
movslq %r12d,%rax
lea    (%r15,%rax,4),%rbx
mov    (%r14),%rdi
test   %rdi,%rdi
je     11dd <inet6_option_next+0xad>
mov    %rbx,%rsi
call   1050 <ip6optlen@plt>
test   %eax,%eax
je     1202 <inet6_option_next+0xd2>
movslq %eax,%r15
shl    $0x2,%r15
add    (%r14),%r15
jmp    11e1 <inet6_option_next+0xb1>
add    $0x4,%r15
mov    %r15,(%r14)
cmp    %rbx,%r15
jae    11fb <inet6_option_next+0xcb>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <ip6optlen@plt>
cmp    $0x1,%eax
sbb    %ebp,%ebp
jmp    1202 <inet6_option_next+0xd2>
movq   $0x0,(%r14)
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmsghdr = type { i64, i32, i64 }
%struct.ip6_ext = type { i32 }
@IPPROTO_IPV6 = global i64 0
@IPV6_HOPOPTS = global i32 0
@IPV6_DSTOPTS = global i32 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 %37, 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 i32, i32* %17
%19 = load i32, i32* @IPV6_HOPOPTS
%20 = icmp eq i32 %18, %19
br i1 %20, label %38, label %21
21: ; preds = %15
%22 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%23 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %22, i32 0, i32 1
%24 = load i32, i32* %23
%25 = icmp eq i32 %24, 22
br i1 %25, label %38, label %26
26: ; preds = %21
%27 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%28 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i32 0, i32 1
%29 = load i32, i32* %28
%30 = load i32, i32* @IPV6_DSTOPTS
%31 = icmp eq i32 %29, %30
br i1 %31, label %38, label %32
32: ; preds = %26
%33 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%34 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %33, i32 0, i32 1
%35 = load i32, i32* %34
%36 = icmp eq i32 %35, 23
br i1 %36, label %38, label %37
37: ; preds = %32, %2
store i32 -1, i32* %3
br label %104
38: ; preds = %32, %26, %21, %15
%39 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%40 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %39, i32 0, i32 2
%41 = load i64, i64* %40
%42 = call i64 @CMSG_SPACE(i32 4)
%43 = icmp slt i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %38
store i32 -1, i32* %3
br label %104
45: ; preds = %38
%46 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%47 = call i64 @CMSG_DATA(%struct.cmsghdr* %46)
%48 = inttoptr i64 %47 to %struct.ip6_ext*
store %struct.ip6_ext* %48, %struct.ip6_ext** %6
%49 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%50 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = add nsw i32 %51, 1
%53 = shl i32 %52, 3
store i32 %53, i32* %7
%54 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%55 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %54, i32 0, i32 2
%56 = load i64, i64* %55
%57 = load i32, i32* %7
%58 = call i64 @CMSG_SPACE(i32 %57)
%59 = icmp slt i64 %56, %58
br i1 %59, label %60, label %61
60: ; preds = %45
store i32 -1, i32* %3
br label %104
61: ; preds = %45
%62 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%63 = bitcast %struct.ip6_ext* %62 to i32*
%64 = load i32, i32* %7
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %63, i64 %65
store i32* %66, i32** %9
%67 = load i32**, i32*** %5
%68 = load i32*, i32** %67
%69 = icmp eq i32* %68, null
br i1 %69, label %70, label %75
70: ; preds = %61
%71 = load %struct.ip6_ext*, %struct.ip6_ext** %6
%72 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %71, i64 1
%73 = bitcast %struct.ip6_ext* %72 to i32*
%74 = load i32**, i32*** %5
store i32* %73, i32** %74
br label %89
75: ; preds = %61
%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, %70
%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, %60, %44, %37
%105 = load i32, i32* %3
ret i32 %105
}

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