#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 scalar_t__ uint8_t;
typedef int uint16_t;
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct nd_neighbor_advert {
  scalar_t__ nd_na_type;
  scalar_t__ nd_na_code;
  int /*<<< orphan*/ nd_na_target;
};

/* Variables and functions */
scalar_t__ ND_NEIGHBOR_ADVERT;
scalar_t__ const ND_OPT_TARGET_LINKADDR;
scalar_t__ memcmp(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ print_mac_address(scalar_t__ const *, int);
int /*<<< orphan*/ printf(char *);
int parse_advert(const uint8_t *buf, size_t len,
                 const struct sockaddr_in6 *dstip, bool verbose) {
  const struct nd_neighbor_advert *na = (const struct nd_neighbor_advert *)buf;
  const uint8_t *ptr;
  if ((len < sizeof(struct nd_neighbor_advert)) ||
      (na->nd_na_type != ND_NEIGHBOR_ADVERT) || (na->nd_na_code != 0) ||
      memcmp(&na->nd_na_target, &dstip->sin6_addr, 16))
    return -1;
  len -= sizeof(struct nd_neighbor_advert);
  ptr = buf + sizeof(struct nd_neighbor_advert);
  while (len >= 8) {
    uint16_t optlen;
    optlen = ((uint16_t)(ptr[1])) << 3;
    if (optlen == 0)
      break;
    if (len < optlen)
      break;
    len -= optlen;
    if (ptr[0] != ND_OPT_TARGET_LINKADDR) {
      ptr += optlen;
      continue;
    }
    ptr += 2;
    optlen -= 2;
    if (verbose)
      printf("Target link-layer address: ");
    print_mac_address(ptr, optlen);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<parse_advert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x18,-0x18(%rbp)
jb     11a0 <parse_advert+0x70>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
jne    11a0 <parse_advert+0x70>
mov    -0x30(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11a0 <parse_advert+0x70>
mov    -0x30(%rbp),%rdi
add    $0x10,%rdi
mov    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
je     11ac <parse_advert+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <parse_advert+0x15e>
mov    -0x18(%rbp),%rax
sub    $0x18,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0xc0,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x8,-0x18(%rbp)
jb     1287 <parse_advert+0x157>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
shl    $0x3,%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    11ee <parse_advert+0xbe>
jmp    1287 <parse_advert+0x157>
mov    -0x18(%rbp),%rax
movslq -0x3c(%rbp),%rcx
cmp    %rcx,%rax
jae    1204 <parse_advert+0xd4>
jmp    1287 <parse_advert+0x157>
movslq -0x3c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
je     1244 <parse_advert+0x114>
mov    -0x3c(%rbp),%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    11c6 <parse_advert+0x96>
mov    -0x38(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     126f <parse_advert+0x13f>
lea    0xd9e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1050 <print_mac_address@plt>
movl   $0x0,-0x4(%rbp)
jmp    128e <parse_advert+0x15e>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_advert>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%r15d
cmp    $0x18,%rsi
jb     1161 <parse_advert+0x31>
mov    %ecx,%r14d
mov    %rdi,%r12
mov    (%rdi),%rax
mov    0x2e94(%rip),%rcx
cmp    (%rcx),%rax
jne    1161 <parse_advert+0x31>
cmpq   $0x0,0x8(%r12)
je     116d <parse_advert+0x3d>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsi,%rbp
lea    0x10(%r12),%rdi
mov    %rdx,%rsi
mov    $0x10,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
jne    1161 <parse_advert+0x31>
add    $0xffffffffffffffe8,%rbp
cmp    $0x8,%rbp
jb     1161 <parse_advert+0x31>
add    $0xc0,%r12
nopl   0x0(%rax,%rax,1)
mov    0x8(%r12),%ebx
shl    $0x3,%ebx
test   %ebx,%ebx
je     1161 <parse_advert+0x31>
movslq %ebx,%rax
sub    %rax,%rbp
jb     1161 <parse_advert+0x31>
cmpq   $0x0,(%r12)
je     11c7 <parse_advert+0x97>
lea    (%r12,%rax,8),%r12
cmp    $0x7,%rbp
ja     11a0 <parse_advert+0x70>
jmp    1161 <parse_advert+0x31>
add    $0x10,%r12
add    $0xfffffffe,%ebx
test   %r14d,%r14d
je     11df <parse_advert+0xaf>
lea    0xe2e(%rip),%rdi
call   1030 <printf@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   1050 <print_mac_address@plt>
xor    %r15d,%r15d
jmp    1161 <parse_advert+0x31>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
%struct.nd_neighbor_advert = type { i64, i64, i32 }
@ND_NEIGHBOR_ADVERT = global i64 0
@ND_OPT_TARGET_LINKADDR = constant i64 0
@.str = [28 x i8] c"Target link-layer address: \00"
define i32 @parse_advert(i64* %0, i64 %1, %struct.sockaddr_in6* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64
%8 = alloca %struct.sockaddr_in6*
%9 = alloca i32
%10 = alloca %struct.nd_neighbor_advert*
%11 = alloca i64*
%12 = alloca i32
store i64* %0, i64** %6
store i64 %1, i64* %7
store %struct.sockaddr_in6* %2, %struct.sockaddr_in6** %8
store i32 %3, i32* %9
%13 = load i64*, i64** %6
%14 = bitcast i64* %13 to %struct.nd_neighbor_advert*
store %struct.nd_neighbor_advert* %14, %struct.nd_neighbor_advert** %10
%15 = load i64, i64* %7
%16 = icmp ult i64 %15, 24
br i1 %16, label %35, label %17
17: ; preds = %4
%18 = load %struct.nd_neighbor_advert*, %struct.nd_neighbor_advert** %10
%19 = getelementptr inbounds %struct.nd_neighbor_advert, %struct.nd_neighbor_advert* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @ND_NEIGHBOR_ADVERT
%22 = icmp ne i64 %20, %21
br i1 %22, label %35, label %23
23: ; preds = %17
%24 = load %struct.nd_neighbor_advert*, %struct.nd_neighbor_advert** %10
%25 = getelementptr inbounds %struct.nd_neighbor_advert, %struct.nd_neighbor_advert* %24, i32 0, i32 1
%26 = load i64, i64* %25
%27 = icmp ne i64 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %23
%29 = load %struct.nd_neighbor_advert*, %struct.nd_neighbor_advert** %10
%30 = getelementptr inbounds %struct.nd_neighbor_advert, %struct.nd_neighbor_advert* %29, i32 0, i32 2
%31 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %8
%32 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %31, i32 0, i32 0
%33 = call i64 @memcmp(i32* %30, i32* %32, i32 16)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %28, %23, %17, %4
store i32 -1, i32* %5
br label %88
36: ; preds = %28
%37 = load i64, i64* %7
%38 = sub i64 %37, 24
store i64 %38, i64* %7
%39 = load i64*, i64** %6
%40 = getelementptr inbounds i64, i64* %39, i64 24
store i64* %40, i64** %11
br label %41
41: ; preds = %69, %36
%42 = load i64, i64* %7
%43 = icmp uge i64 %42, 8
br i1 %43, label %44, label %87
44: ; preds = %41
%45 = load i64*, i64** %11
%46 = getelementptr inbounds i64, i64* %45, i64 1
%47 = load i64, i64* %46
%48 = trunc i64 %47 to i32
%49 = shl i32 %48, 3
store i32 %49, i32* %12
%50 = load i32, i32* %12
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %44
br label %87
53: ; preds = %44
%54 = load i64, i64* %7
%55 = load i32, i32* %12
%56 = sext i32 %55 to i64
%57 = icmp ult i64 %54, %56
br i1 %57, label %58, label %59
58: ; preds = %53
br label %87
59: ; preds = %53
%60 = load i32, i32* %12
%61 = sext i32 %60 to i64
%62 = load i64, i64* %7
%63 = sub i64 %62, %61
store i64 %63, i64* %7
%64 = load i64*, i64** %11
%65 = getelementptr inbounds i64, i64* %64, i64 0
%66 = load i64, i64* %65
%67 = load i64, i64* @ND_OPT_TARGET_LINKADDR
%68 = icmp ne i64 %66, %67
br i1 %68, label %69, label %74
69: ; preds = %59
%70 = load i32, i32* %12
%71 = load i64*, i64** %11
%72 = sext i32 %70 to i64
%73 = getelementptr inbounds i64, i64* %71, i64 %72
store i64* %73, i64** %11
br label %41
74: ; preds = %59
%75 = load i64*, i64** %11
%76 = getelementptr inbounds i64, i64* %75, i64 2
store i64* %76, i64** %11
%77 = load i32, i32* %12
%78 = sub nsw i32 %77, 2
store i32 %78, i32* %12
%79 = load i32, i32* %9
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %74
%82 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %83
83: ; preds = %81, %74
%84 = load i64*, i64** %11
%85 = load i32, i32* %12
%86 = call i32 @print_mac_address(i64* %84, i32 %85)
store i32 0, i32* %5
br label %88
87: ; preds = %58, %52, %41
store i32 -1, i32* %5
br label %88
88: ; preds = %87, %83, %35
%89 = load i32, i32* %5
ret i32 %89
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
@ND_NEIGHBOR_ADVERT = global i64 0
@ND_OPT_TARGET_LINKADDR = constant i64 0
@.str = [28 x i8] c"Target link-layer address: \00"
define i32 @parse_advert(i64* %0, i64 %1, %struct.sockaddr_in6* %2, i32 %3) {
%5 = icmp ult i64 %1, 24
br i1 %5, label %51, label %6
6: ; preds = %4
%7 = load i64, i64* %0
%8 = load i64, i64* @ND_NEIGHBOR_ADVERT
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %51
10: ; preds = %6
%11 = getelementptr inbounds i64, i64* %0, i64 1
%12 = load i64, i64* %11
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %51
14: ; preds = %10
%15 = getelementptr inbounds i64, i64* %0, i64 2
%16 = bitcast i64* %15 to i32*
%17 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
%18 = tail call i64 @memcmp(i32* %16, i32* %17, i32 16)
%19 = icmp eq i64 %18, 0
%20 = add i64 %1, -24
%21 = icmp ugt i64 %20, 7
%22 = and i1 %19, %21
br i1 %22, label %23, label %51
23: ; preds = %14
%24 = getelementptr inbounds i64, i64* %0, i64 24
br label %25
25: ; preds = %23, %47
%26 = phi i64* [ %49, %47 ], [ %24, %23 ]
%27 = phi i64 [ %48, %47 ], [ %20, %23 ]
%28 = getelementptr inbounds i64, i64* %26, i64 1
%29 = load i64, i64* %28
%30 = trunc i64 %29 to i32
%31 = shl i32 %30, 3
%32 = icmp eq i32 %31, 0
br i1 %32, label %51, label %33
33: ; preds = %25
%34 = sext i32 %31 to i64
%35 = icmp ult i64 %27, %34
br i1 %35, label %51, label %36
36: ; preds = %33
%37 = load i64, i64* %26
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = getelementptr inbounds i64, i64* %26, i64 2
%41 = add nsw i32 %31, -2
%42 = icmp eq i32 %3, 0
br i1 %42, label %45, label %43
43: ; preds = %39
%44 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %39, %43
%46 = tail call i32 @print_mac_address(i64* %40, i32 %41)
br label %51
47: ; preds = %36
%48 = sub i64 %27, %34
%49 = getelementptr inbounds i64, i64* %26, i64 %34
%50 = icmp ugt i64 %48, 7
br i1 %50, label %25, label %51
51: ; preds = %47, %25, %33, %45, %4, %6, %10, %14
%52 = phi i32 [ -1, %14 ], [ -1, %10 ], [ -1, %6 ], [ -1, %4 ], [ 0, %45 ], [ -1, %33 ], [ -1, %25 ], [ -1, %47 ]
ret i32 %52
}