#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*/ uint16_t;
struct icmp6_hdr {
  scalar_t__ icmp6_type;
  scalar_t__ icmp6_id;
  int /*<<< orphan*/ icmp6_seq;
};
typedef int ssize_t;

/* Variables and functions */
scalar_t__ ICMP6_ECHO_REPLY;
int /*<<< orphan*/ getpid();
scalar_t__ htons(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
ssize_t parse_echo_reply(const void *data, size_t len, int *ttl, unsigned *n,
                         uint16_t port) {
  const struct icmp6_hdr *pih = (const struct icmp6_hdr *)data;
  if ((len < sizeof(*pih)) || (pih->icmp6_type != ICMP6_ECHO_REPLY) ||
      (pih->icmp6_id != htons(getpid())))
    return -1;
  (void)port;
  *ttl = ntohs(pih->icmp6_seq) >> 8;
  *n = ntohs(pih->icmp6_seq) & 0xff;
  return 0;
}


x86 assembly (clang, -O0):
<parse_echo_reply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x18,-0x18(%rbp)
jb     11a0 <parse_echo_reply+0x70>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    0x2e73(%rip),%rcx
cmp    (%rcx),%rax
jne    11a0 <parse_echo_reply+0x70>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
je     11ac <parse_echo_reply+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <parse_echo_reply+0xb4>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
sar    $0x8,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
and    $0xff,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_echo_reply>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%ebp
cmp    $0x18,%rsi
jb     1190 <parse_echo_reply+0x60>
mov    %rcx,%r14
mov    %rdi,%rbx
mov    (%rdi),%rax
mov    0x2e8d(%rip),%rcx
cmp    (%rcx),%rax
jne    1190 <parse_echo_reply+0x60>
mov    %rdx,%r15
mov    0x8(%rbx),%r12
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%edi
call   1040 <htons@plt>
cmp    %rax,%r12
jne    1190 <parse_echo_reply+0x60>
mov    0x10(%rbx),%edi
call   1050 <ntohs@plt>
sar    $0x8,%eax
mov    %eax,(%r15)
mov    0x10(%rbx),%edi
call   1050 <ntohs@plt>
movzbl %al,%eax
mov    %eax,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.icmp6_hdr = type { i64, i64, i32 }
@ICMP6_ECHO_REPLY = global i64 0
define i32 @parse_echo_reply(i8* %0, i64 %1, i32* %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca %struct.icmp6_hdr*
store i8* %0, i8** %7
store i64 %1, i64* %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store i32 %4, i32* %11
%13 = load i8*, i8** %7
%14 = bitcast i8* %13 to %struct.icmp6_hdr*
store %struct.icmp6_hdr* %14, %struct.icmp6_hdr** %12
%15 = load i64, i64* %8
%16 = icmp ult i64 %15, 24
br i1 %16, label %30, label %17
17: ; preds = %5
%18 = load %struct.icmp6_hdr*, %struct.icmp6_hdr** %12
%19 = getelementptr inbounds %struct.icmp6_hdr, %struct.icmp6_hdr* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @ICMP6_ECHO_REPLY
%22 = icmp ne i64 %20, %21
br i1 %22, label %30, label %23
23: ; preds = %17
%24 = load %struct.icmp6_hdr*, %struct.icmp6_hdr** %12
%25 = getelementptr inbounds %struct.icmp6_hdr, %struct.icmp6_hdr* %24, i32 0, i32 1
%26 = load i64, i64* %25
%27 = call i32 (...) @getpid()
%28 = call i64 @htons(i32 %27)
%29 = icmp ne i64 %26, %28
br i1 %29, label %30, label %31
30: ; preds = %23, %17, %5
store i32 -1, i32* %6
br label %45
31: ; preds = %23
%32 = load i32, i32* %11
%33 = load %struct.icmp6_hdr*, %struct.icmp6_hdr** %12
%34 = getelementptr inbounds %struct.icmp6_hdr, %struct.icmp6_hdr* %33, i32 0, i32 2
%35 = load i32, i32* %34
%36 = call i32 @ntohs(i32 %35)
%37 = ashr i32 %36, 8
%38 = load i32*, i32** %9
store i32 %37, i32* %38
%39 = load %struct.icmp6_hdr*, %struct.icmp6_hdr** %12
%40 = getelementptr inbounds %struct.icmp6_hdr, %struct.icmp6_hdr* %39, i32 0, i32 2
%41 = load i32, i32* %40
%42 = call i32 @ntohs(i32 %41)
%43 = and i32 %42, 255
%44 = load i32*, i32** %10
store i32 %43, i32* %44
store i32 0, i32* %6
br label %45
45: ; preds = %31, %30
%46 = load i32, i32* %6
ret i32 %46
}

LLVM IR (-O2):
@ICMP6_ECHO_REPLY = global i64 0
define i32 @parse_echo_reply(i8* nocapture %0, i64 %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3, i32 %4) {
%6 = icmp ult i64 %1, 24
br i1 %6, label %28, label %7
7: ; preds = %5
%8 = bitcast i8* %0 to i64*
%9 = load i64, i64* %8
%10 = load i64, i64* @ICMP6_ECHO_REPLY
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %28
12: ; preds = %7
%13 = getelementptr inbounds i8, i8* %0, i64 8
%14 = bitcast i8* %13 to i64*
%15 = load i64, i64* %14
%16 = tail call i32 (...) @getpid()
%17 = tail call i64 @htons(i32 %16)
%18 = icmp eq i64 %15, %17
br i1 %18, label %19, label %28
19: ; preds = %12
%20 = getelementptr inbounds i8, i8* %0, i64 16
%21 = bitcast i8* %20 to i32*
%22 = load i32, i32* %21
%23 = tail call i32 @ntohs(i32 %22)
%24 = ashr i32 %23, 8
store i32 %24, i32* %2
%25 = load i32, i32* %21
%26 = tail call i32 @ntohs(i32 %25)
%27 = and i32 %26, 255
store i32 %27, i32* %3
br label %28
28: ; preds = %5, %7, %12, %19
%29 = phi i32 [ 0, %19 ], [ -1, %12 ], [ -1, %7 ], [ -1, %5 ]
ret i32 %29
}