#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 iphdr {
  int /*<<< orphan*/ tot_len;
};
struct TYPE_3__ {
  int /*<<< orphan*/ ip6_un1_plen;
};
struct TYPE_4__ {
  TYPE_1__ ip6_un1;
};
struct ip6_hdr {
  TYPE_2__ ip6_ctlun;
};

/* Variables and functions */
scalar_t__ dv_ip_is_v4(struct iphdr *);
size_t ntohs(int /*<<< orphan*/);
size_t dv_ip_datalen(void *h, size_t len) {
  struct iphdr *ip4 = h;
  struct ip6_hdr *ip6 = h;
  if (dv_ip_is_v4(ip4)) {
    if (len < sizeof(*ip4)) {
      return 0;
    }
    return ntohs(ip4->tot_len);
  }
  if (len < sizeof(*ip6)) {
    return 0;
  }
  return sizeof(*ip6) + ntohs(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
}


x86 assembly (clang, -O0):
<dv_ip_datalen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <dv_ip_is_v4@plt>
cmp    $0x0,%rax
je     117f <dv_ip_datalen+0x5f>
cmpq   $0x4,-0x18(%rbp)
jae    116b <dv_ip_datalen+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    11aa <dv_ip_datalen+0x8a>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %rax,-0x8(%rbp)
jmp    11aa <dv_ip_datalen+0x8a>
cmpq   $0x4,-0x18(%rbp)
jae    1197 <dv_ip_datalen+0x77>
movq   $0x0,-0x8(%rbp)
jmp    11aa <dv_ip_datalen+0x8a>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dv_ip_datalen>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <dv_ip_is_v4@plt>
test   %rax,%rax
je     1148 <dv_ip_datalen+0x28>
cmp    $0x4,%r14
jb     114e <dv_ip_datalen+0x2e>
mov    (%rbx),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <ntohs@plt>
cmp    $0x4,%r14
jae    1152 <dv_ip_datalen+0x32>
xor    %eax,%eax
jmp    115d <dv_ip_datalen+0x3d>
mov    (%rbx),%edi
call   1040 <ntohs@plt>
add    $0x4,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.iphdr = type { i32 }
%struct.ip6_hdr = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
define i64 @dv_ip_datalen(i8* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.iphdr*
%7 = alloca %struct.ip6_hdr*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = bitcast i8* %8 to %struct.iphdr*
store %struct.iphdr* %9, %struct.iphdr** %6
%10 = load i8*, i8** %4
%11 = bitcast i8* %10 to %struct.ip6_hdr*
store %struct.ip6_hdr* %11, %struct.ip6_hdr** %7
%12 = load %struct.iphdr*, %struct.iphdr** %6
%13 = call i64 @dv_ip_is_v4(%struct.iphdr* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %2
%16 = load i64, i64* %5
%17 = icmp ult i64 %16, 4
br i1 %17, label %18, label %19
18: ; preds = %15
store i64 0, i64* %3
br label %36
19: ; preds = %15
%20 = load %struct.iphdr*, %struct.iphdr** %6
%21 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i64 @ntohs(i32 %22)
store i64 %23, i64* %3
br label %36
24: ; preds = %2
%25 = load i64, i64* %5
%26 = icmp ult i64 %25, 4
br i1 %26, label %27, label %28
27: ; preds = %24
store i64 0, i64* %3
br label %36
28: ; preds = %24
%29 = load %struct.ip6_hdr*, %struct.ip6_hdr** %7
%30 = getelementptr inbounds %struct.ip6_hdr, %struct.ip6_hdr* %29, i32 0, i32 0
%31 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %30, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i64 @ntohs(i32 %33)
%35 = add i64 4, %34
store i64 %35, i64* %3
br label %36
36: ; preds = %28, %27, %19, %18
%37 = load i64, i64* %3
ret i64 %37
}

LLVM IR (-O2):
%struct.iphdr = type { i32 }
define i64 @dv_ip_datalen(i8* %0, i64 %1) {
%3 = bitcast i8* %0 to %struct.iphdr*
%4 = tail call i64 @dv_ip_is_v4(%struct.iphdr* %3)
%5 = icmp eq i64 %4, 0
%6 = icmp ult i64 %1, 4
br i1 %5, label %12, label %7
7: ; preds = %2
br i1 %6, label %18, label %8
8: ; preds = %7
%9 = bitcast i8* %0 to i32*
%10 = load i32, i32* %9
%11 = tail call i64 @ntohs(i32 %10)
br label %18
12: ; preds = %2
br i1 %6, label %18, label %13
13: ; preds = %12
%14 = bitcast i8* %0 to i32*
%15 = load i32, i32* %14
%16 = tail call i64 @ntohs(i32 %15)
%17 = add i64 %16, 4
br label %18
18: ; preds = %12, %7, %13, %8
%19 = phi i64 [ %11, %8 ], [ %17, %13 ], [ 0, %7 ], [ 0, %12 ]
ret i64 %19
}