#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_encrypt(unsigned char *, unsigned int, unsigned int *);
int /*<<< orphan*/ free(unsigned char *);
unsigned short htons(unsigned short);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned short *, int);
int /*<<< orphan*/ write(int, void *, unsigned int);
void send_packet(int socket, unsigned short msgid, const void *data,
                 unsigned int dlen) {
  void *newblock2;
  unsigned int newblock2len;
  unsigned char *newblock = malloc(dlen + 2);
  memcpy(newblock + 2, data, dlen);
  msgid = htons(msgid);
  memcpy(newblock, &msgid, 2);
  newblock2 = cduck_encrypt(newblock, dlen + 2, &newblock2len);
  write(socket, newblock2, newblock2len);
  free(newblock2);
  free(newblock);
}


x86 assembly (clang, -O0):
<send_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x2,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <memcpy@plt>
movzwl -0x6(%rbp),%edi
call   1050 <htons@plt>
mov    %ax,-0x6(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x6(%rbp),%rsi
mov    $0x2,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x2,%esi
lea    -0x24(%rbp),%rdx
call   1080 <cduck_encrypt@plt>
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <write@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_packet>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %esi,%r12d
mov    %edi,%r14d
lea    0x2(%rbp),%r13d
mov    %r13d,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0x2(%rax),%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
mov    %r12d,%edi
call   1050 <htons@plt>
mov    %ax,0x2(%rsp)
lea    0x2(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1060 <memcpy@plt>
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
call   1080 <cduck_encrypt@plt>
mov    %rax,%rbp
mov    0x4(%rsp),%edx
mov    %r14d,%edi
mov    %rax,%rsi
call   1040 <write@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @send_packet(i32 %0, i16 zeroext %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i16
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i32 %0, i32* %5
store i16 %1, i16* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%12 = load i32, i32* %8
%13 = add i32 %12, 2
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %11
%15 = load i8*, i8** %11
%16 = getelementptr inbounds i8, i8* %15, i64 2
%17 = load i8*, i8** %7
%18 = bitcast i8* %17 to i16*
%19 = load i32, i32* %8
%20 = call i32 @memcpy(i8* %16, i16* %18, i32 %19)
%21 = load i16, i16* %6
%22 = call zeroext i16 @htons(i16 zeroext %21)
store i16 %22, i16* %6
%23 = load i8*, i8** %11
%24 = call i32 @memcpy(i8* %23, i16* %6, i32 2)
%25 = load i8*, i8** %11
%26 = load i32, i32* %8
%27 = add i32 %26, 2
%28 = call i8* @cduck_encrypt(i8* %25, i32 %27, i32* %10)
store i8* %28, i8** %9
%29 = load i32, i32* %5
%30 = load i8*, i8** %9
%31 = load i32, i32* %10
%32 = call i32 @write(i32 %29, i8* %30, i32 %31)
%33 = load i8*, i8** %9
%34 = call i32 @free(i8* %33)
%35 = load i8*, i8** %11
%36 = call i32 @free(i8* %35)
ret void
}

LLVM IR (-O2):
define void @send_packet(i32 %0, i16 zeroext %1, i8* %2, i32 %3) {
%5 = alloca i16
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = add i32 %3, 2
%9 = tail call i8* @malloc(i32 %8)
%10 = getelementptr inbounds i8, i8* %9, i64 2
%11 = bitcast i8* %2 to i16*
%12 = tail call i32 @memcpy(i8* %10, i16* %11, i32 %3)
%13 = tail call zeroext i16 @htons(i16 zeroext %1)
store i16 %13, i16* %5
%14 = call i32 @memcpy(i8* %9, i16* %5, i32 2)
%15 = call i8* @cduck_encrypt(i8* %9, i32 %8, i32* %6)
%16 = load i32, i32* %6
%17 = call i32 @write(i32 %0, i8* %15, i32 %16)
%18 = call i32 @free(i8* %15)
%19 = call i32 @free(i8* %9)
ret void
}