#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;
  scalar_t__ icmp6_code;
};
typedef int ssize_t;

/* Variables and functions */
scalar_t__ ICMP6_ECHO_REQUEST;
int /*<<< orphan*/ getpid();
scalar_t__ htons(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
ssize_t parse_echo_error(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_REQUEST) ||
      (pih->icmp6_code) || (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_error>:
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   $0x20,-0x18(%rbp)
jb     11af <parse_echo_error+0x7f>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    0x2e7b(%rip),%rcx
cmp    (%rcx),%rax
jne    11af <parse_echo_error+0x7f>
mov    -0x38(%rbp),%rax
cmpq   $0x0,0x18(%rax)
jne    11af <parse_echo_error+0x7f>
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     11bb <parse_echo_error+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <parse_echo_error+0xc3>
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_error>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%ebp
cmp    $0x20,%rsi
jb     1197 <parse_echo_error+0x67>
mov    %rcx,%r14
mov    %rdi,%rbx
mov    (%rdi),%rax
mov    0x2e95(%rip),%rcx
cmp    (%rcx),%rax
jne    1197 <parse_echo_error+0x67>
cmpq   $0x0,0x18(%rbx)
jne    1197 <parse_echo_error+0x67>
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    1197 <parse_echo_error+0x67>
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, i64 }
@ICMP6_ECHO_REQUEST = global i64 0
define i32 @parse_echo_error(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, 32
br i1 %16, label %35, 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_REQUEST
%22 = icmp ne i64 %20, %21
br i1 %22, label %35, 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 3
%26 = load i64, i64* %25
%27 = icmp ne i64 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %23
%29 = load %struct.icmp6_hdr*, %struct.icmp6_hdr** %12
%30 = getelementptr inbounds %struct.icmp6_hdr, %struct.icmp6_hdr* %29, i32 0, i32 1
%31 = load i64, i64* %30
%32 = call i32 (...) @getpid()
%33 = call i64 @htons(i32 %32)
%34 = icmp ne i64 %31, %33
br i1 %34, label %35, label %36
35: ; preds = %28, %23, %17, %5
store i32 -1, i32* %6
br label %50
36: ; preds = %28
%37 = load i32, i32* %11
%38 = load %struct.icmp6_hdr*, %struct.icmp6_hdr** %12
%39 = getelementptr inbounds %struct.icmp6_hdr, %struct.icmp6_hdr* %38, i32 0, i32 2
%40 = load i32, i32* %39
%41 = call i32 @ntohs(i32 %40)
%42 = ashr i32 %41, 8
%43 = load i32*, i32** %9
store i32 %42, i32* %43
%44 = load %struct.icmp6_hdr*, %struct.icmp6_hdr** %12
%45 = getelementptr inbounds %struct.icmp6_hdr, %struct.icmp6_hdr* %44, i32 0, i32 2
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = and i32 %47, 255
%49 = load i32*, i32** %10
store i32 %48, i32* %49
store i32 0, i32* %6
br label %50
50: ; preds = %36, %35
%51 = load i32, i32* %6
ret i32 %51
}

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