#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 int uint16_t;

/* Variables and functions */
int htons(int);
int /*<<< orphan*/ memcpy(char *, ...);
int send(int, char *, int, int /*<<< orphan*/);
int strlen(char *);
int send_message(int sd, uint16_t code, char *message) {
  int n;
  uint16_t lon;
  uint16_t lon_nbo;
  char buffer[2 + 2 + 65535];
  lon = strlen(message) + 1;
  lon_nbo = htons(lon);
  code = htons(code);
  memcpy(buffer, &code, 2);
  memcpy(buffer + 2, &lon_nbo, 2);
  memcpy(buffer + 2 + 2, message, lon);
  n = send(sd, buffer, (2 + 2 + lon), 0);
  return n;
}


x86 assembly (clang, -O0):
<send_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x8(%rbp)
lea    -0x10020(%rbp),%rdi
xor    %eax,%eax
lea    -0x8(%rbp),%rsi
mov    $0x2,%edx
call   1060 <memcpy@plt>
lea    -0x10020(%rbp),%rdi
add    $0x2,%rdi
xor    %eax,%eax
lea    -0x1c(%rbp),%rsi
mov    $0x2,%edx
call   1060 <memcpy@plt>
lea    -0x10020(%rbp),%rdi
add    $0x2,%rdi
add    $0x2,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
xor    %eax,%eax
call   1060 <memcpy@plt>
mov    -0x4(%rbp),%edi
lea    -0x10020(%rbp),%rsi
mov    -0x18(%rbp),%edx
add    $0x4,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x10020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_message>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10020,%rsp
mov    %rdx,%r15
mov    %esi,%ebp
mov    %edi,%r14d
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%r12d
mov    %r12d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0x10(%rsp),%rbp
lea    0xc(%rsp),%rsi
mov    %rbp,%rdi
mov    $0x2,%edx
xor    %eax,%eax
call   1060 <memcpy@plt>
lea    0x12(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1060 <memcpy@plt>
lea    0x14(%rsp),%rdi
mov    %r15,%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <memcpy@plt>
add    $0x5,%ebx
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
add    $0x10020,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @send_message(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [65539 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%11 = load i8*, i8** %6
%12 = call i32 @strlen(i8* %11)
%13 = add nsw i32 %12, 1
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = call i32 @htons(i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %5
%17 = call i32 @htons(i32 %16)
store i32 %17, i32* %5
%18 = getelementptr inbounds [65539 x i8], [65539 x i8]* %10, i64 0, i64 0
%19 = call i32 (i8*, ...) @memcpy(i8* %18, i32* %5, i32 2)
%20 = getelementptr inbounds [65539 x i8], [65539 x i8]* %10, i64 0, i64 0
%21 = getelementptr inbounds i8, i8* %20, i64 2
%22 = call i32 (i8*, ...) @memcpy(i8* %21, i32* %9, i32 2)
%23 = getelementptr inbounds [65539 x i8], [65539 x i8]* %10, i64 0, i64 0
%24 = getelementptr inbounds i8, i8* %23, i64 2
%25 = getelementptr inbounds i8, i8* %24, i64 2
%26 = load i8*, i8** %6
%27 = load i32, i32* %8
%28 = call i32 (i8*, ...) @memcpy(i8* %25, i8* %26, i32 %27)
%29 = load i32, i32* %4
%30 = getelementptr inbounds [65539 x i8], [65539 x i8]* %10, i64 0, i64 0
%31 = load i32, i32* %8
%32 = add nsw i32 4, %31
%33 = call i32 @send(i32 %29, i8* %30, i32 %32, i32 0)
store i32 %33, i32* %7
%34 = load i32, i32* %7
ret i32 %34
}

LLVM IR (-O2):
define i32 @send_message(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca [65539 x i8]
%7 = bitcast i32* %5 to i8*
%8 = getelementptr inbounds [65539 x i8], [65539 x i8]* %6, i64 0, i64 0
%9 = tail call i32 @strlen(i8* %2)
%10 = add nsw i32 %9, 1
%11 = tail call i32 @htons(i32 %10)
store i32 %11, i32* %5
%12 = tail call i32 @htons(i32 %1)
store i32 %12, i32* %4
%13 = call i32 (i8*, ...) @memcpy(i8* %8, i32* %4, i32 2)
%14 = getelementptr inbounds [65539 x i8], [65539 x i8]* %6, i64 0, i64 2
%15 = call i32 (i8*, ...) @memcpy(i8* %14, i32* %5, i32 2)
%16 = getelementptr inbounds [65539 x i8], [65539 x i8]* %6, i64 0, i64 4
%17 = call i32 (i8*, ...) @memcpy(i8* %16, i8* %2, i32 %10)
%18 = add nsw i32 %9, 5
%19 = call i32 @send(i32 %0, i8* %8, i32 %18, i32 0)
ret i32 %19
}