#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 */
struct ip {
  int ip_hl;
};
struct icmp {
  scalar_t__ icmp_type;
  scalar_t__ icmp_id;
};

/* Variables and functions */
scalar_t__ ICMP_ECHOREPLY;
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *);
int nc_unpack(char *buf, int len) {
  int iphdrlen;
  struct ip *ip;
  struct icmp *icmp;
  ip = (struct ip *)buf;
  iphdrlen = ip->ip_hl << 2;
  icmp = (struct icmp *)(buf + iphdrlen);
  len -= iphdrlen;
  if (len < 8) {
    printf("ICMP packets\'s length is less than 8\n");
    return -1;
  }
  if ((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == getpid())) {
    return 0;
  } else {
    printf("Not my response\n");
    return -1;
  }
}


x86 assembly (clang, -O0):
<nc_unpack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
shl    $0x2,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x8,-0x14(%rbp)
jge    117f <nc_unpack+0x5f>
lea    0xe92(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <nc_unpack+0xb8>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
jne    11c5 <nc_unpack+0xa5>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jne    11c5 <nc_unpack+0xa5>
movl   $0x0,-0x4(%rbp)
jmp    11d8 <nc_unpack+0xb8>
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nc_unpack>:
push   %rbp
push   %r14
push   %rbx
mov    (%rdi),%eax
shl    $0x2,%eax
sub    %eax,%esi
cmp    $0x8,%esi
jge    1139 <nc_unpack+0x19>
lea    0xed9(%rip),%rbx
jmp    1165 <nc_unpack+0x45>
cltq
mov    (%rdi,%rax,1),%rcx
mov    0x2e9a(%rip),%rdx
lea    0xeb3(%rip),%rbx
cmp    (%rdx),%rcx
jne    1165 <nc_unpack+0x45>
mov    0x8(%rdi,%rax,1),%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %rax,%r14
je     1172 <nc_unpack+0x52>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ip = type { i32 }
%struct.icmp = type { i64, i64 }
@.str = [38 x i8] c"ICMP packets's length is less than 8\0A\00"
@ICMP_ECHOREPLY = global i64 0
@.str.1 = [17 x i8] c"Not my response\0A\00"
define i32 @nc_unpack(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ip*
%8 = alloca %struct.icmp*
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = bitcast i8* %9 to %struct.ip*
store %struct.ip* %10, %struct.ip** %7
%11 = load %struct.ip*, %struct.ip** %7
%12 = getelementptr inbounds %struct.ip, %struct.ip* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = shl i32 %13, 2
store i32 %14, i32* %6
%15 = load i8*, i8** %4
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = bitcast i8* %18 to %struct.icmp*
store %struct.icmp* %19, %struct.icmp** %8
%20 = load i32, i32* %6
%21 = load i32, i32* %5
%22 = sub nsw i32 %21, %20
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp slt i32 %23, 8
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %42
27: ; preds = %2
%28 = load %struct.icmp*, %struct.icmp** %8
%29 = getelementptr inbounds %struct.icmp, %struct.icmp* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = load i64, i64* @ICMP_ECHOREPLY
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %40
33: ; preds = %27
%34 = load %struct.icmp*, %struct.icmp** %8
%35 = getelementptr inbounds %struct.icmp, %struct.icmp* %34, i32 0, i32 1
%36 = load i64, i64* %35
%37 = call i64 (...) @getpid()
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 0, i32* %3
br label %42
40: ; preds = %33, %27
%41 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %42
42: ; preds = %40, %39, %25
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@ICMP_ECHOREPLY = global i64 0
@str = [16 x i8] c"Not my response\00"
@str.2 = [37 x i8] c"ICMP packets's length is less than 8\00"
define i32 @nc_unpack(i8* nocapture %0, i32 %1) {
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = shl i32 %4, 2
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = sub nsw i32 %1, %5
%9 = icmp slt i32 %8, 8
br i1 %9, label %21, label %10
10: ; preds = %2
%11 = bitcast i8* %7 to i64*
%12 = load i64, i64* %11
%13 = load i64, i64* @ICMP_ECHOREPLY
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %21
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %7, i64 8
%17 = bitcast i8* %16 to i64*
%18 = load i64, i64* %17
%19 = tail call i64 (...) @getpid()
%20 = icmp eq i64 %18, %19
br i1 %20, label %24, label %21
21: ; preds = %10, %15, %2
%22 = phi i8* [ getelementptr inbounds ([37 x i8], [37 x i8]* @str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0), %15 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0), %10 ]
%23 = tail call i32 @puts(i8* dereferenceable(1) %22)
br label %24
24: ; preds = %21, %15
%25 = phi i32 [ 0, %15 ], [ -1, %21 ]
ret i32 %25
}