#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 */

/* Variables and functions */
void *cduck_dispatch(unsigned short, unsigned char const *, unsigned int,
                     unsigned int *);
int /*<<< orphan*/ memcpy(unsigned short *, unsigned char const *, int);
unsigned short ntohs(unsigned short);
int /*<<< orphan*/ printf(char *, unsigned short);
void *handle_packet(const void *source, unsigned int len,
                    unsigned int *outlen) {
  const unsigned char *cbuf;
  unsigned short msgid;
  cbuf = (const unsigned char *)source;
  memcpy(&msgid, cbuf, 2);
  cbuf += 2;
  msgid = ntohs(msgid);
  printf("Dispatching message 0x%04hX\n", msgid);
  return cduck_dispatch(msgid, cbuf, len - 2, outlen);
}


x86 assembly (clang, -O0):
<handle_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    -0x22(%rbp),%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
movzwl -0x22(%rbp),%edi
call   1050 <ntohs@plt>
mov    %ax,-0x22(%rbp)
lea    0xe73(%rip),%rdi
movzwl -0x22(%rbp),%esi
call   1030 <printf@plt>
mov    -0x22(%rbp),%ax
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
sub    $0x2,%edx
mov    -0x18(%rbp),%rcx
movzwl %ax,%edi
call   1060 <cduck_dispatch@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_packet>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
add    $0x2,%rbx
movzwl 0xe(%rsp),%edi
call   1050 <ntohs@plt>
mov    %ax,0xe(%rsp)
movzwl %ax,%esi
lea    0xe81(%rip),%rdi
call   1030 <printf@plt>
movzwl 0xe(%rsp),%edi
add    $0xfffffffe,%ebp
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %r14,%rcx
call   1060 <cduck_dispatch@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Dispatching message 0x%04hX\0A\00"
define i8* @handle_packet(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i16
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%9 = load i8*, i8** %4
store i8* %9, i8** %7
%10 = load i8*, i8** %7
%11 = call i32 @memcpy(i16* %8, i8* %10, i32 2)
%12 = load i8*, i8** %7
%13 = getelementptr inbounds i8, i8* %12, i64 2
store i8* %13, i8** %7
%14 = load i16, i16* %8
%15 = call zeroext i16 @ntohs(i16 zeroext %14)
store i16 %15, i16* %8
%16 = load i16, i16* %8
%17 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i16 zeroext %16)
%18 = load i16, i16* %8
%19 = load i8*, i8** %7
%20 = load i32, i32* %5
%21 = sub i32 %20, 2
%22 = load i32*, i32** %6
%23 = call i8* @cduck_dispatch(i16 zeroext %18, i8* %19, i32 %21, i32* %22)
ret i8* %23
}

LLVM IR (-O2):
@.str = [29 x i8] c"Dispatching message 0x%04hX\0A\00"
define i8* @handle_packet(i8* %0, i32 %1, i32* %2) {
%4 = alloca i16
%5 = bitcast i16* %4 to i8*
%6 = call i32 @memcpy(i16* %4, i8* %0, i32 2)
%7 = getelementptr inbounds i8, i8* %0, i64 2
%8 = load i16, i16* %4
%9 = call zeroext i16 @ntohs(i16 zeroext %8)
store i16 %9, i16* %4
%10 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i16 zeroext %9)
%11 = load i16, i16* %4
%12 = add i32 %1, -2
%13 = call i8* @cduck_dispatch(i16 zeroext %11, i8* %7, i32 %12, i32* %2)
ret i8* %13
}