#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 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 *CMSG_DATA(struct cmsghdr const *);
scalar_t__ CMSG_LEN(int);
scalar_t__ IPPROTO_IPV6;
scalar_t__ IPV6_DSTOPTS;
scalar_t__ IPV6_HOPOPTS;
scalar_t__ get_opt_end(int const **, int const *, int const *);
int inet6_option_find(const struct cmsghdr *cmsg, uint8_t **tptrp, int type) {
  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 *next;
  if (*tptrp == NULL)
    next = (const uint8_t *)(ip6e + 1);
  else {
    if (*tptrp < (const uint8_t *)(ip6e + 1))
      return -1;
    if (get_opt_end(&next, *tptrp, endp) != 0)
      return -1;
  }
  const uint8_t *result;
  do {
    result = next;
    if (get_opt_end(&next, result, endp) != 0)
      return -1;
  } while (*result != type);
  *tptrp = (uint8_t *)result;
  return 0;
}


x86 assembly (clang, -O0):
<inet6_option_find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <inet6_option_find+0x5a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%rax
je     1196 <inet6_option_find+0x66>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%rax
je     1196 <inet6_option_find+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <inet6_option_find+0x1c1>
mov    -0x10(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x48(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
jl     11f6 <inet6_option_find+0xc6>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
shl    $0x3,%edi
call   1050 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
cmp    %rcx,%rax
jge    1202 <inet6_option_find+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <inet6_option_find+0x1c1>
mov    -0x10(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    -0x28(%rbp),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
shl    $0x3,%ecx
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1244 <inet6_option_find+0x114>
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
jmp    1297 <inet6_option_find+0x167>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
add    $0x4,%rcx
cmp    %rcx,%rax
jae    1268 <inet6_option_find+0x138>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <inet6_option_find+0x1c1>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rdi
call   1040 <get_opt_end@plt>
cmp    $0x0,%rax
je     1292 <inet6_option_find+0x162>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <inet6_option_find+0x1c1>
jmp    1297 <inet6_option_find+0x167>
jmp    129c <inet6_option_find+0x16c>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rdi
call   1040 <get_opt_end@plt>
cmp    $0x0,%rax
je     12cb <inet6_option_find+0x19b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <inet6_option_find+0x1c1>
jmp    12d0 <inet6_option_find+0x1a0>
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
cmp    -0x1c(%rbp),%eax
jne    129c <inet6_option_find+0x16c>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_option_find>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    (%rdi),%rax
mov    0x2e8b(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
jne    1228 <inet6_option_find+0xf8>
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbp
mov    0x8(%rdi),%rax
mov    0x2e81(%rip),%rcx
cmp    (%rcx),%rax
je     117c <inet6_option_find+0x4c>
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <inet6_option_find+0xf8>
mov    %rbp,%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,%r12
mov    0x10(%rbp),%r13
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
cmp    %rax,%r13
jl     1228 <inet6_option_find+0xf8>
mov    0x10(%rbp),%r13
mov    (%r12),%eax
lea    0x8(,%rax,8),%edi
call   1050 <CMSG_LEN@plt>
cmp    %rax,%r13
jl     1228 <inet6_option_find+0xf8>
mov    %rbp,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%r12),%ecx
lea    0x8(,%rcx,8),%ecx
movslq %ecx,%rcx
lea    (%rax,%rcx,4),%rbp
mov    (%r14),%rsi
add    $0x4,%r12
test   %rsi,%rsi
je     11f9 <inet6_option_find+0xc9>
mov    $0xffffffff,%ebx
cmp    %r12,%rsi
jb     1228 <inet6_option_find+0xf8>
mov    %rsp,%rdi
mov    %rbp,%rdx
call   1040 <get_opt_end@plt>
test   %rax,%rax
jne    1228 <inet6_option_find+0xf8>
jmp    11fd <inet6_option_find+0xcd>
mov    %r12,(%rsp)
mov    %rsp,%r12
mov    (%rsp),%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <get_opt_end@plt>
test   %rax,%rax
jne    1223 <inet6_option_find+0xf3>
cmp    %r15d,(%rbx)
jne    1200 <inet6_option_find+0xd0>
mov    %rbx,(%r14)
xor    %ebx,%ebx
jmp    1228 <inet6_option_find+0xf8>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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_find(%struct.cmsghdr* %0, i32** %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.cmsghdr*
%6 = alloca i32**
%7 = alloca i32
%8 = alloca %struct.ip6_ext*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
store %struct.cmsghdr* %0, %struct.cmsghdr** %5
store i32** %1, i32*** %6
store i32 %2, i32* %7
%12 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%13 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %12, i32 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* @IPPROTO_IPV6
%16 = icmp ne i64 %14, %15
br i1 %16, label %29, label %17
17: ; preds = %3
%18 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%19 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %18, i32 0, i32 1
%20 = load i64, i64* %19
%21 = load i64, i64* @IPV6_HOPOPTS
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %30
23: ; preds = %17
%24 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%25 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %24, i32 0, i32 1
%26 = load i64, i64* %25
%27 = load i64, i64* @IPV6_DSTOPTS
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %23, %3
store i32 -1, i32* %4
br label %101
30: ; preds = %23, %17
%31 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%32 = call i32* @CMSG_DATA(%struct.cmsghdr* %31)
%33 = bitcast i32* %32 to %struct.ip6_ext*
store %struct.ip6_ext* %33, %struct.ip6_ext** %8
%34 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%35 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %34, i32 0, i32 2
%36 = load i64, i64* %35
%37 = call i64 @CMSG_LEN(i32 4)
%38 = icmp slt i64 %36, %37
br i1 %38, label %50, label %39
39: ; preds = %30
%40 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%41 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %40, i32 0, i32 2
%42 = load i64, i64* %41
%43 = load %struct.ip6_ext*, %struct.ip6_ext** %8
%44 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = add nsw i32 %45, 1
%47 = mul nsw i32 %46, 8
%48 = call i64 @CMSG_LEN(i32 %47)
%49 = icmp slt i64 %42, %48
br i1 %49, label %50, label %51
50: ; preds = %39, %30
store i32 -1, i32* %4
br label %101
51: ; preds = %39
%52 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%53 = call i32* @CMSG_DATA(%struct.cmsghdr* %52)
%54 = load %struct.ip6_ext*, %struct.ip6_ext** %8
%55 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = add nsw i32 %56, 1
%58 = mul nsw i32 %57, 8
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %53, i64 %59
store i32* %60, i32** %9
%61 = load i32**, i32*** %6
%62 = load i32*, i32** %61
%63 = icmp eq i32* %62, null
br i1 %63, label %64, label %68
64: ; preds = %51
%65 = load %struct.ip6_ext*, %struct.ip6_ext** %8
%66 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %65, i64 1
%67 = bitcast %struct.ip6_ext* %66 to i32*
store i32* %67, i32** %10
br label %84
68: ; preds = %51
%69 = load i32**, i32*** %6
%70 = load i32*, i32** %69
%71 = load %struct.ip6_ext*, %struct.ip6_ext** %8
%72 = getelementptr inbounds %struct.ip6_ext, %struct.ip6_ext* %71, i64 1
%73 = bitcast %struct.ip6_ext* %72 to i32*
%74 = icmp ult i32* %70, %73
br i1 %74, label %75, label %76
75: ; preds = %68
store i32 -1, i32* %4
br label %101
76: ; preds = %68
%77 = load i32**, i32*** %6
%78 = load i32*, i32** %77
%79 = load i32*, i32** %9
%80 = call i64 @get_opt_end(i32** %10, i32* %78, i32* %79)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %83
82: ; preds = %76
store i32 -1, i32* %4
br label %101
83: ; preds = %76
br label %84
84: ; preds = %83, %64
br label %85
85: ; preds = %93, %84
%86 = load i32*, i32** %10
store i32* %86, i32** %11
%87 = load i32*, i32** %11
%88 = load i32*, i32** %9
%89 = call i64 @get_opt_end(i32** %10, i32* %87, i32* %88)
%90 = icmp ne i64 %89, 0
br i1 %90, label %91, label %92
91: ; preds = %85
store i32 -1, i32* %4
br label %101
92: ; preds = %85
br label %93
93: ; preds = %92
%94 = load i32*, i32** %11
%95 = load i32, i32* %94
%96 = load i32, i32* %7
%97 = icmp ne i32 %95, %96
br i1 %97, label %85, label %98
98: ; preds = %93
%99 = load i32*, i32** %11
%100 = load i32**, i32*** %6
store i32* %99, i32** %100
store i32 0, i32* %4
br label %101
101: ; preds = %98, %91, %82, %75, %50, %29
%102 = load i32, i32* %4
ret i32 %102
}

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