#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 */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct timeval {
  int dummy;
};
struct TYPE_4__ {
  int /*<<< orphan*/ s_addr;
};
struct TYPE_3__ {
  int /*<<< orphan*/ s_addr;
};
struct ip {
  int ip_hl;
  TYPE_2__ ip_dst;
  TYPE_1__ ip_src;
  int /*<<< orphan*/ ip_len;
};

/* Variables and functions */
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ ntohl(int /*<<< orphan*/);
size_t ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ process_tcp_packet(struct timeval const *,
                                      int /*<<< orphan*/, int /*<<< orphan*/,
                                      char const *, size_t);
void process_ip_packet(const struct timeval *ts, const char *buffer,
                       size_t size) {
  if (size < sizeof(struct ip)) {
    die("invalid ip packet length");
  }
  const struct ip *ip_header = (struct ip *)buffer;
  size_t ip_len = ntohs(ip_header->ip_len);
  size_t ip_header_len = ip_header->ip_hl * 4;
  if (size < ip_len || ip_len < ip_header_len) {
    die("invalid ip packet length");
  }
  process_tcp_packet(ts, ntohl(ip_header->ip_src.s_addr),
                     ntohl(ip_header->ip_dst.s_addr), buffer + ip_header_len,
                     ip_len - ip_header_len);
}


x86 assembly (clang, -O0):
<process_ip_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x10,-0x18(%rbp)
jae    116b <process_ip_packet+0x2b>
lea    0xe9a(%rip),%rdi
call   1050 <die@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%edi
call   1040 <ntohs@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
shl    $0x2,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     11ae <process_ip_packet+0x6e>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    11ba <process_ip_packet+0x7a>
lea    0xe4b(%rip),%rdi
call   1050 <die@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
call   1060 <ntohl@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <ntohl@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
add    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%r8
sub    -0x30(%rbp),%r8
call   1030 <process_tcp_packet@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_ip_packet>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
cmp    $0xf,%rdx
ja     1163 <process_ip_packet+0x23>
lea    0xea2(%rip),%rdi
call   1050 <die@plt>
mov    0xc(%rbx),%edi
call   1040 <ntohs@plt>
mov    %rax,%r15
movslq (%rbx),%rbp
shl    $0x2,%rbp
cmp    %r12,%rax
ja     117f <process_ip_packet+0x3f>
cmp    %rbp,%r15
jae    118b <process_ip_packet+0x4b>
lea    0xe7a(%rip),%rdi
call   1050 <die@plt>
mov    0x8(%rbx),%edi
call   1060 <ntohl@plt>
mov    %eax,%r12d
mov    0x4(%rbx),%edi
call   1060 <ntohl@plt>
add    %rbp,%rbx
sub    %rbp,%r15
mov    %r14,%rdi
mov    %r12d,%esi
mov    %eax,%edx
mov    %rbx,%rcx
mov    %r15,%r8
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <process_tcp_packet@plt>

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.ip = type { i32, %struct.TYPE_4__, %struct.TYPE_3__, i32 }
%struct.TYPE_4__ = type { i32 }
%struct.TYPE_3__ = type { i32 }
@.str = [25 x i8] c"invalid ip packet length\00"
define void @process_ip_packet(%struct.timeval* %0, i8* %1, i64 %2) {
%4 = alloca %struct.timeval*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.ip*
%8 = alloca i64
%9 = alloca i64
store %struct.timeval* %0, %struct.timeval** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i64, i64* %6
%11 = icmp ult i64 %10, 16
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @die(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %3
%15 = load i8*, i8** %5
%16 = bitcast i8* %15 to %struct.ip*
store %struct.ip* %16, %struct.ip** %7
%17 = load %struct.ip*, %struct.ip** %7
%18 = getelementptr inbounds %struct.ip, %struct.ip* %17, i32 0, i32 3
%19 = load i32, i32* %18
%20 = call i64 @ntohs(i32 %19)
store i64 %20, i64* %8
%21 = load %struct.ip*, %struct.ip** %7
%22 = getelementptr inbounds %struct.ip, %struct.ip* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = mul nsw i32 %23, 4
%25 = sext i32 %24 to i64
store i64 %25, i64* %9
%26 = load i64, i64* %6
%27 = load i64, i64* %8
%28 = icmp ult i64 %26, %27
br i1 %28, label %33, label %29
29: ; preds = %14
%30 = load i64, i64* %8
%31 = load i64, i64* %9
%32 = icmp ult i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29, %14
%34 = call i32 @die(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = load %struct.timeval*, %struct.timeval** %4
%37 = load %struct.ip*, %struct.ip** %7
%38 = getelementptr inbounds %struct.ip, %struct.ip* %37, i32 0, i32 2
%39 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @ntohl(i32 %40)
%42 = load %struct.ip*, %struct.ip** %7
%43 = getelementptr inbounds %struct.ip, %struct.ip* %42, i32 0, i32 1
%44 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @ntohl(i32 %45)
%47 = load i8*, i8** %5
%48 = load i64, i64* %9
%49 = getelementptr inbounds i8, i8* %47, i64 %48
%50 = load i64, i64* %8
%51 = load i64, i64* %9
%52 = sub i64 %50, %51
%53 = call i32 @process_tcp_packet(%struct.timeval* %36, i32 %41, i32 %46, i8* %49, i64 %52)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@.str = [25 x i8] c"invalid ip packet length\00"
define void @process_ip_packet(%struct.timeval* %0, i8* %1, i64 %2) {
%4 = icmp ult i64 %2, 16
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @die(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %3
%8 = getelementptr inbounds i8, i8* %1, i64 12
%9 = bitcast i8* %8 to i32*
%10 = load i32, i32* %9
%11 = tail call i64 @ntohs(i32 %10)
%12 = bitcast i8* %1 to i32*
%13 = load i32, i32* %12
%14 = shl nsw i32 %13, 2
%15 = sext i32 %14 to i64
%16 = icmp ugt i64 %11, %2
%17 = icmp ult i64 %11, %15
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %19, label %21
19: ; preds = %7
%20 = tail call i32 @die(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %7, %19
%22 = getelementptr inbounds i8, i8* %1, i64 8
%23 = bitcast i8* %22 to i32*
%24 = load i32, i32* %23
%25 = tail call i32 @ntohl(i32 %24)
%26 = getelementptr inbounds i8, i8* %1, i64 4
%27 = bitcast i8* %26 to i32*
%28 = load i32, i32* %27
%29 = tail call i32 @ntohl(i32 %28)
%30 = getelementptr inbounds i8, i8* %1, i64 %15
%31 = sub i64 %11, %15
%32 = tail call i32 @process_tcp_packet(%struct.timeval* %0, i32 %25, i32 %29, i8* %30, i64 %31)
ret void
}