#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 */
int /*<<< orphan*/ memcpy(char *, char *, unsigned short);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, unsigned int);
unsigned short ntohs(unsigned short);
unsigned short copy_from_pascal_two(char *dest, char *pascal,
                                    unsigned int max_len) {
  unsigned short *len_p = (unsigned short *)pascal;
  unsigned short len = ntohs(*len_p);
  if (max_len < len)
    len = max_len;
  if (len == 0)
    return 0;
  memset(dest, 0, max_len);
  memcpy(dest, pascal + 2, len);
  return len;
}


x86 assembly (clang, -O0):
<copy_from_pascal_two>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movzwl (%rax),%edi
call   1050 <ntohs@plt>
mov    %ax,-0x2a(%rbp)
mov    -0x1c(%rbp),%eax
movzwl -0x2a(%rbp),%ecx
cmp    %ecx,%eax
jae    1171 <copy_from_pascal_two+0x41>
mov    -0x1c(%rbp),%eax
mov    %ax,-0x2a(%rbp)
movzwl -0x2a(%rbp),%eax
cmp    $0x0,%eax
jne    1189 <copy_from_pascal_two+0x59>
movw   $0x0,-0x2(%rbp)
jmp    11b7 <copy_from_pascal_two+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x2,%rsi
mov    -0x2a(%rbp),%ax
movzwl %ax,%edx
call   1040 <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_from_pascal_two>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r14
movzwl (%rsi),%edi
call   1050 <ntohs@plt>
movzwl %ax,%ebp
cmp    %ebx,%ebp
cmova  %ebx,%ebp
test   %bp,%bp
je     1172 <copy_from_pascal_two+0x42>
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1030 <memset@plt>
add    $0x2,%r15
movzwl %bp,%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <memcpy@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define zeroext i16 @copy_from_pascal_two(i8* %0, i8* %1, i32 %2) {
%4 = bitcast i8* %1 to i16*
%5 = load i16, i16* %4
%6 = tail call zeroext i16 @ntohs(i16 zeroext %5)
%7 = zext i16 %6 to i32
%8 = icmp ugt i32 %7, %2
%9 = trunc i32 %2 to i16
%10 = select i1 %8, i16 %9, i16 %6
%11 = icmp eq i16 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %3
%13 = tail call i32 @memset(i8* %0, i32 0, i32 %2)
%14 = getelementptr inbounds i8, i8* %1, i64 2
%15 = tail call i32 @memcpy(i8* %0, i8* %14, i16 zeroext %10)
br label %16
16: ; preds = %3, %12
ret i16 %10
}