#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 ssize_t;

/* Variables and functions */
int a_strlen(char const *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
ssize_t a_strcpy(char *dst, ssize_t n, const char *src) {
  ssize_t len = a_strlen(src);
  if (n > 0) {
    ssize_t dlen = ((n - 1) < (len) ? (n - 1) : (len));
    memcpy(dst, src, dlen);
    dst[dlen] = '\0';
  }
  return len;
}


x86 assembly (clang, -O0):
<a_strcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <a_strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    118e <a_strcpy+0x6e>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jge    1166 <a_strcpy+0x46>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116c <a_strcpy+0x4c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<a_strcpy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
mov    %rdx,%rdi
call   1040 <a_strlen@plt>
mov    %eax,%ebx
test   %ebp,%ebp
jle    115a <a_strcpy+0x3a>
add    $0xffffffff,%ebp
cmp    %ebx,%ebp
cmovge %ebx,%ebp
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <memcpy@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @a_strcpy(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = load i8*, i8** %6
%10 = call i32 @a_strlen(i8* %9)
store i32 %10, i32* %7
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %3
%14 = load i32, i32* %5
%15 = sub nsw i32 %14, 1
%16 = load i32, i32* %7
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* %5
%20 = sub nsw i32 %19, 1
br label %23
21: ; preds = %13
%22 = load i32, i32* %7
br label %23
23: ; preds = %21, %18
%24 = phi i32 [ %20, %18 ], [ %22, %21 ]
store i32 %24, i32* %8
%25 = load i8*, i8** %4
%26 = load i8*, i8** %6
%27 = load i32, i32* %8
%28 = call i32 @memcpy(i8* %25, i8* %26, i32 %27)
%29 = load i8*, i8** %4
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %23, %3
%34 = load i32, i32* %7
ret i32 %34
}

LLVM IR (-O2):
define i32 @a_strcpy(i8* %0, i32 %1, i8* %2) {
%4 = tail call i32 @a_strlen(i8* %2)
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %13
6: ; preds = %3
%7 = add nsw i32 %1, -1
%8 = icmp slt i32 %7, %4
%9 = select i1 %8, i32 %7, i32 %4
%10 = tail call i32 @memcpy(i8* %0, i8* %2, i32 %9)
%11 = sext i32 %9 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %6, %3
ret i32 %4
}