#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 */
typedef scalar_t__ u_int8_t;
typedef int u_int16_t;
typedef int /*<<< orphan*/ u_char;
typedef scalar_t__ ptr;

/* Variables and functions */
scalar_t__ *pkt_get_offset(int /*<<< orphan*/ const *, int const);
int /*<<< orphan*/ printf(char *);
u_int16_t pkt_get_uint16(const u_char *packet_data, const int offset) {
  const u_int8_t *ptr;
  if (offset < 0 || NULL == packet_data) {
    printf("ERR: Invalid input packet_data\n");
    return 0;
  }
  ptr = pkt_get_offset(packet_data, offset);
  if (!ptr) {
    return 0;
  } else {
    return ((u_int16_t)((u_int16_t) * ((const u_int8_t *)(ptr) + 0) << 8 |
                        (u_int16_t) * ((const u_int8_t *)(ptr) + 1) << 0));
  }
}


x86 assembly (clang, -O0):
<pkt_get_uint16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1145 <pkt_get_uint16+0x25>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    115d <pkt_get_uint16+0x3d>
lea    0xeb4(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    119e <pkt_get_uint16+0x7e>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <pkt_get_offset@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1184 <pkt_get_uint16+0x64>
movl   $0x0,-0x4(%rbp)
jmp    119e <pkt_get_uint16+0x7e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
shl    $0x8,%eax
mov    -0x20(%rbp),%rcx
mov    0x8(%rcx),%rcx
shl    $0x0,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkt_get_uint16>:
push   %rax
test   %rdi,%rdi
je     1141 <pkt_get_uint16+0x21>
test   %esi,%esi
js     1141 <pkt_get_uint16+0x21>
call   1030 <pkt_get_offset@plt>
test   %rax,%rax
je     114d <pkt_get_uint16+0x2d>
mov    %rax,%rcx
mov    (%rax),%eax
shl    $0x8,%eax
or     0x8(%rcx),%eax
pop    %rcx
ret
lea    0xeb8(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [32 x i8] c"ERR: Invalid input packet_data\0A\00"
define i32 @pkt_get_uint16(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i64*
store i32* %0, i32** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i32*, i32** %4
%11 = icmp eq i32* null, %10
br i1 %11, label %12, label %14
12: ; preds = %9, %2
%13 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %33
14: ; preds = %9
%15 = load i32*, i32** %4
%16 = load i32, i32* %5
%17 = call i64* @pkt_get_offset(i32* %15, i32 %16)
store i64* %17, i64** %6
%18 = load i64*, i64** %6
%19 = icmp ne i64* %18, null
br i1 %19, label %21, label %20
20: ; preds = %14
store i32 0, i32* %3
br label %33
21: ; preds = %14
%22 = load i64*, i64** %6
%23 = getelementptr inbounds i64, i64* %22, i64 0
%24 = load i64, i64* %23
%25 = trunc i64 %24 to i32
%26 = shl i32 %25, 8
%27 = load i64*, i64** %6
%28 = getelementptr inbounds i64, i64* %27, i64 1
%29 = load i64, i64* %28
%30 = trunc i64 %29 to i32
%31 = shl i32 %30, 0
%32 = or i32 %26, %31
store i32 %32, i32* %3
br label %33
33: ; preds = %21, %20, %12
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@str = [31 x i8] c"ERR: Invalid input packet_data\00"
define i32 @pkt_get_uint16(i32* %0, i32 %1) {
%3 = icmp slt i32 %1, 0
%4 = icmp eq i32* %0, null
%5 = or i1 %4, %3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %19
8: ; preds = %2
%9 = tail call i64* @pkt_get_offset(i32* %0, i32 %1)
%10 = icmp eq i64* %9, null
br i1 %10, label %19, label %11
11: ; preds = %8
%12 = load i64, i64* %9
%13 = trunc i64 %12 to i32
%14 = shl i32 %13, 8
%15 = getelementptr inbounds i64, i64* %9, i64 1
%16 = load i64, i64* %15
%17 = trunc i64 %16 to i32
%18 = or i32 %14, %17
br label %19
19: ; preds = %8, %11, %6
%20 = phi i32 [ 0, %6 ], [ %18, %11 ], [ 0, %8 ]
ret i32 %20
}