#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 */
unsigned short htons(unsigned short);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned short);
scalar_t__ strlen(char const *);
unsigned short copy_to_pascal_two(char *dest, const char *src) {
  unsigned short *sendlen = (void *)dest;
  char *data = dest + 2;
  unsigned short len;
  if (!src) {
    dest[0] = 0;
    dest[1] = 0;
    return 2;
  }
  len = (unsigned short)strlen(src);
  *sendlen = htons(len);
  memcpy(data, src, len);
  return len;
}


x86 assembly (clang, -O0):
<copy_to_pascal_two>:
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
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1179 <copy_to_pascal_two+0x49>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
movw   $0x2,-0x2(%rbp)
jmp    11b5 <copy_to_pascal_two+0x85>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %ax,-0x2a(%rbp)
movzwl -0x2a(%rbp),%edi
call   1040 <htons@plt>
mov    %ax,%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x2a(%rbp),%ax
movzwl %ax,%edx
call   1050 <memcpy@plt>
mov    -0x2a(%rbp),%ax
mov    %ax,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_to_pascal_two>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
test   %rsi,%rsi
je     1170 <copy_to_pascal_two+0x40>
mov    %rsi,%rbx
lea    0x2(%r15),%r12
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%r14
movzwl %r14w,%ebp
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %ax,(%r15)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
jmp    117b <copy_to_pascal_two+0x4b>
movw   $0x0,(%r15)
mov    $0x2,%r14w
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i16 @copy_to_pascal_two(i8* %0, i8* %1) {
%3 = alloca i16
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i16*
%7 = alloca i8*
%8 = alloca i16
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = bitcast i8* %9 to i16*
store i16* %10, i16** %6
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 2
store i8* %12, i8** %7
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %20, label %15
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 0, i8* %17
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8 0, i8* %19
store i16 2, i16* %3
br label %32
20: ; preds = %2
%21 = load i8*, i8** %5
%22 = call i64 @strlen(i8* %21)
%23 = trunc i64 %22 to i16
store i16 %23, i16* %8
%24 = load i16, i16* %8
%25 = call zeroext i16 @htons(i16 zeroext %24)
%26 = load i16*, i16** %6
store i16 %25, i16* %26
%27 = load i8*, i8** %7
%28 = load i8*, i8** %5
%29 = load i16, i16* %8
%30 = call i32 @memcpy(i8* %27, i8* %28, i16 zeroext %29)
%31 = load i16, i16* %8
store i16 %31, i16* %3
br label %32
32: ; preds = %20, %15
%33 = load i16, i16* %3
ret i16 %33
}

LLVM IR (-O2):
define zeroext i16 @copy_to_pascal_two(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %4, label %6
4: ; preds = %2
store i8 0, i8* %0
%5 = getelementptr inbounds i8, i8* %0, i64 1
store i8 0, i8* %5
br label %13
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %0, i64 2
%8 = bitcast i8* %0 to i16*
%9 = tail call i64 @strlen(i8* %1)
%10 = trunc i64 %9 to i16
%11 = tail call zeroext i16 @htons(i16 zeroext %10)
store i16 %11, i16* %8
%12 = tail call i32 @memcpy(i8* %7, i8* %1, i16 zeroext %10)
br label %13
13: ; preds = %6, %4
%14 = phi i16 [ %10, %6 ], [ 2, %4 ]
ret i16 %14
}