#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 */

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
int check_nping_hdr(unsigned char *data, unsigned int len) {
  if (data == NULL) {
    printf("NULL pointer as supplied for check_nping_hdr().\n");
    fflush(stdout);
    return (-1);
  }
  if ((data[0] & 0xf0) == 0x00) {
    int pkt_len = 0;
    pkt_len = (int)(data[1] << 8);
    pkt_len += data[2];
    if (pkt_len != len - 5) {
      printf("nPing (data) header had wrong length.\n");
      fflush(stdout);
      return (-1);
    }
    if (data[3] != 0xfe) {
      printf("Source port number != 254 -> not a nPing packet.\n");
      fflush(stdout);
      return (-1);
    }
    return (1);
  }
  return (-1);
}


x86 assembly (clang, -O0):
<check_nping_hdr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1160 <check_nping_hdr+0x40>
lea    0xebf(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <check_nping_hdr+0xfb>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xf0,%eax
cmp    $0x0,%eax
jne    1214 <check_nping_hdr+0xf4>
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
shl    $0x8,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x5,%ecx
cmp    %ecx,%eax
je     11cf <check_nping_hdr+0xaf>
lea    0xe81(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <check_nping_hdr+0xfb>
mov    -0x10(%rbp),%rax
movzbl 0x3(%rax),%eax
cmp    $0xfe,%eax
je     1208 <check_nping_hdr+0xe8>
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <check_nping_hdr+0xfb>
movl   $0x1,-0x4(%rbp)
jmp    121b <check_nping_hdr+0xfb>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_nping_hdr>:
push   %rax
test   %rdi,%rdi
je     1154 <check_nping_hdr+0x34>
mov    $0xffffffff,%eax
cmpb   $0xf,(%rdi)
ja     117c <check_nping_hdr+0x5c>
movzwl 0x1(%rdi),%eax
shl    $0x10,%eax
bswap  %eax
add    $0xfffffffb,%esi
cmp    %esi,%eax
jne    115d <check_nping_hdr+0x3d>
mov    $0x1,%eax
cmpb   $0xfe,0x3(%rdi)
je     117c <check_nping_hdr+0x5c>
lea    0xeae(%rip),%rdi
jmp    1164 <check_nping_hdr+0x44>
lea    0xefc(%rip),%rdi
jmp    1164 <check_nping_hdr+0x44>
lea    0xecd(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [49 x i8] c"NULL pointer as supplied for check_nping_hdr().\0A\00"
@stdout = global i32 0
@.str.1 = [39 x i8] c"nPing (data) header had wrong length.\0A\00"
@.str.2 = [50 x i8] c"Source port number != 254 -> not a nPing packet.\0A\00"
define i32 @check_nping_hdr(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @stdout
%12 = call i32 @fflush(i32 %11)
store i32 -1, i32* %3
br label %52
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = and i32 %17, 240
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %51
20: ; preds = %13
store i32 0, i32* %6
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = shl i32 %24, 8
store i32 %25, i32* %6
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 2
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = load i32, i32* %6
%31 = add nsw i32 %30, %29
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = load i32, i32* %5
%34 = sub i32 %33, 5
%35 = icmp ne i32 %32, %34
br i1 %35, label %36, label %40
36: ; preds = %20
%37 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* @stdout
%39 = call i32 @fflush(i32 %38)
store i32 -1, i32* %3
br label %52
40: ; preds = %20
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i64 3
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = icmp ne i32 %44, 254
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @stdout
%49 = call i32 @fflush(i32 %48)
store i32 -1, i32* %3
br label %52
50: ; preds = %40
store i32 1, i32* %3
br label %52
51: ; preds = %13
store i32 -1, i32* %3
br label %52
52: ; preds = %51, %50, %46, %36, %9
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@stdout = global i32 0
@str = [49 x i8] c"Source port number != 254 -> not a nPing packet.\00"
@str.3 = [38 x i8] c"nPing (data) header had wrong length.\00"
@str.4 = [48 x i8] c"NULL pointer as supplied for check_nping_hdr().\00"
define i32 @check_nping_hdr(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %22, label %4
4: ; preds = %2
%5 = load i8, i8* %0
%6 = icmp ult i8 %5, 16
br i1 %6, label %7, label %27
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %0, i64 1
%9 = load i8, i8* %8
%10 = zext i8 %9 to i32
%11 = shl nuw nsw i32 %10, 8
%12 = getelementptr inbounds i8, i8* %0, i64 2
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = or i32 %11, %14
%16 = add i32 %1, -5
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %7
%19 = getelementptr inbounds i8, i8* %0, i64 3
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, -2
br i1 %21, label %27, label %22
22: ; preds = %18, %7, %2
%23 = phi i8* [ getelementptr inbounds ([48 x i8], [48 x i8]* @str.4, i64 0, i64 0), %2 ], [ getelementptr inbounds ([38 x i8], [38 x i8]* @str.3, i64 0, i64 0), %7 ], [ getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0), %18 ]
%24 = tail call i32 @puts(i8* dereferenceable(1) %23)
%25 = load i32, i32* @stdout
%26 = tail call i32 @fflush(i32 %25)
br label %27
27: ; preds = %22, %4, %18
%28 = phi i32 [ 1, %18 ], [ -1, %4 ], [ -1, %22 ]
ret i32 %28
}