#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 sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct ip {
  int ip_hl;
};
struct icmp {
  scalar_t__ icmp_type;
  int icmp_id;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
scalar_t__ ICMP_ECHOREPLY;
int /*<<< orphan*/ memmove(unsigned char *, unsigned char *, int);
int recvfrom(int, unsigned char *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int get_icmp(int sock, unsigned char *buff, int buff_len,
             struct sockaddr_in *addr, socklen_t *len) {
  struct ip *ip;
  struct icmp *icmp;
  int ret, iphl;
  ret = recvfrom(sock, buff, buff_len, 0, (struct sockaddr *)addr, len);
  if (ret) {
    ip = (struct ip *)buff;
    iphl = ip->ip_hl << 2;
    icmp = (struct icmp *)(buff + iphl);
    if ((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == 65534)) {
      memmove(buff, buff + (iphl + 8), ret - (iphl + 8));
      return (ret - (iphl + 8));
    }
  }
  return (-1);
}


x86 assembly (clang, -O0):
<get_icmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
xor    %ecx,%ecx
call   1030 <recvfrom@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     11e6 <get_icmp+0xc6>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
shl    $0x2,%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x40(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
jne    11e1 <get_icmp+0xc1>
mov    -0x38(%rbp),%rax
cmpl   $0xfffe,0x8(%rax)
jne    11e1 <get_icmp+0xc1>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%eax
add    $0x8,%eax
cltq
add    %rax,%rsi
mov    -0x3c(%rbp),%edx
mov    -0x40(%rbp),%eax
add    $0x8,%eax
sub    %eax,%edx
call   1040 <memmove@plt>
mov    -0x3c(%rbp),%eax
mov    -0x40(%rbp),%ecx
add    $0x8,%ecx
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11ed <get_icmp+0xcd>
jmp    11e6 <get_icmp+0xc6>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_icmp>:
push   %rbp
push   %rbx
push   %rax
mov    %r8,%r9
mov    %rcx,%r8
mov    %rsi,%rbx
xor    %ecx,%ecx
call   1030 <recvfrom@plt>
mov    %eax,%ebp
mov    $0xffffffff,%eax
test   %ebp,%ebp
je     1179 <get_icmp+0x59>
mov    (%rbx),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rbx,%rcx,1),%rdx
mov    0x2e8f(%rip),%rsi
cmp    (%rsi),%rdx
jne    1179 <get_icmp+0x59>
cmpl   $0xfffe,0x8(%rbx,%rcx,1)
jne    1179 <get_icmp+0x59>
lea    (%rcx,%rbx,1),%rsi
add    $0x8,%rsi
sub    %ecx,%ebp
add    $0xfffffff8,%ebp
mov    %rbx,%rdi
mov    %ebp,%edx
call   1040 <memmove@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.ip = type { i32 }
%struct.icmp = type { i64, i32 }
%struct.sockaddr = type { i32 }
@ICMP_ECHOREPLY = global i64 0
define i32 @get_icmp(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr_in*
%11 = alloca i32*
%12 = alloca %struct.ip*
%13 = alloca %struct.icmp*
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %10
store i32* %4, i32** %11
%16 = load i32, i32* %7
%17 = load i8*, i8** %8
%18 = load i32, i32* %9
%19 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%20 = bitcast %struct.sockaddr_in* %19 to %struct.sockaddr*
%21 = load i32*, i32** %11
%22 = call i32 @recvfrom(i32 %16, i8* %17, i32 %18, i32 0, %struct.sockaddr* %20, i32* %21)
store i32 %22, i32* %14
%23 = load i32, i32* %14
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %64
25: ; preds = %5
%26 = load i8*, i8** %8
%27 = bitcast i8* %26 to %struct.ip*
store %struct.ip* %27, %struct.ip** %12
%28 = load %struct.ip*, %struct.ip** %12
%29 = getelementptr inbounds %struct.ip, %struct.ip* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = shl i32 %30, 2
store i32 %31, i32* %15
%32 = load i8*, i8** %8
%33 = load i32, i32* %15
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = bitcast i8* %35 to %struct.icmp*
store %struct.icmp* %36, %struct.icmp** %13
%37 = load %struct.icmp*, %struct.icmp** %13
%38 = getelementptr inbounds %struct.icmp, %struct.icmp* %37, i32 0, i32 0
%39 = load i64, i64* %38
%40 = load i64, i64* @ICMP_ECHOREPLY
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %63
42: ; preds = %25
%43 = load %struct.icmp*, %struct.icmp** %13
%44 = getelementptr inbounds %struct.icmp, %struct.icmp* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = icmp eq i32 %45, 65534
br i1 %46, label %47, label %63
47: ; preds = %42
%48 = load i8*, i8** %8
%49 = load i8*, i8** %8
%50 = load i32, i32* %15
%51 = add nsw i32 %50, 8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %49, i64 %52
%54 = load i32, i32* %14
%55 = load i32, i32* %15
%56 = add nsw i32 %55, 8
%57 = sub nsw i32 %54, %56
%58 = call i32 @memmove(i8* %48, i8* %53, i32 %57)
%59 = load i32, i32* %14
%60 = load i32, i32* %15
%61 = add nsw i32 %60, 8
%62 = sub nsw i32 %59, %61
store i32 %62, i32* %6
br label %65
63: ; preds = %42, %25
br label %64
64: ; preds = %63, %5
store i32 -1, i32* %6
br label %65
65: ; preds = %64, %47
%66 = load i32, i32* %6
ret i32 %66
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@ICMP_ECHOREPLY = global i64 0
define i32 @get_icmp(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3, i32* %4) {
%6 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%7 = tail call i32 @recvfrom(i32 %0, i8* %1, i32 %2, i32 0, %struct.sockaddr* %6, i32* %4)
%8 = icmp eq i32 %7, 0
br i1 %8, label %30, label %9
9: ; preds = %5
%10 = bitcast i8* %1 to i32*
%11 = load i32, i32* %10
%12 = shl i32 %11, 2
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
%15 = bitcast i8* %14 to i64*
%16 = load i64, i64* %15
%17 = load i64, i64* @ICMP_ECHOREPLY
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %30
19: ; preds = %9
%20 = getelementptr inbounds i8, i8* %14, i64 8
%21 = bitcast i8* %20 to i32*
%22 = load i32, i32* %21
%23 = icmp eq i32 %22, 65534
br i1 %23, label %24, label %30
24: ; preds = %19
%25 = add nsw i32 %12, 8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %1, i64 %26
%28 = sub nsw i32 %7, %25
%29 = tail call i32 @memmove(i8* %1, i8* %27, i32 %28)
br label %30
30: ; preds = %5, %19, %9, %24
%31 = phi i32 [ %28, %24 ], [ -1, %9 ], [ -1, %19 ], [ -1, %5 ]
ret i32 %31
}