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


x86 assembly (clang, -O0):
<copy_from_pascal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1147 <copy_from_pascal+0x27>
movb   $0x0,-0x1(%rbp)
jmp    1190 <copy_from_pascal+0x70>
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x1d(%rbp)
mov    -0x1c(%rbp),%eax
movzbl -0x1d(%rbp),%ecx
cmp    %ecx,%eax
jae    1165 <copy_from_pascal+0x45>
mov    -0x1c(%rbp),%eax
mov    %al,-0x1d(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    -0x1d(%rbp),%al
movzbl %al,%edx
call   1040 <memcpy@plt>
mov    -0x1d(%rbp),%al
mov    %al,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_from_pascal>:
push   %rbp
push   %r14
push   %rbx
test   %rsi,%rsi
je     1153 <copy_from_pascal+0x33>
mov    %rsi,%rbx
mov    %rdi,%r14
movzbl (%rsi),%ebp
cmp    %edx,%ebp
cmova  %edx,%ebp
xor    %esi,%esi
call   1030 <memset@plt>
add    $0x1,%rbx
movzbl %bpl,%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <memcpy@plt>
jmp    1155 <copy_from_pascal+0x35>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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