#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*/ strcpy(char *, char *);
int strlen(char *);
char *cstrcpy(char *dest, char *src, int buflen) {
  if (dest == NULL || src == NULL || buflen <= 0)
    return NULL;
  if (buflen < strlen(src) + 1) {
    *dest = 0;
    return NULL;
  }
  strcpy(dest, src);
  return dest;
}


x86 assembly (clang, -O0):
<cstrcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1153 <cstrcpy+0x33>
cmpq   $0x0,-0x18(%rbp)
je     1153 <cstrcpy+0x33>
cmpl   $0x0,-0x1c(%rbp)
jg     1160 <cstrcpy+0x40>
movq   $0x0,-0x8(%rbp)
jmp    11a8 <cstrcpy+0x88>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1193 <cstrcpy+0x73>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11a8 <cstrcpy+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cstrcpy>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
test   %rdi,%rdi
je     115e <cstrcpy+0x3e>
mov    %rsi,%rbx
test   %rsi,%rsi
je     115e <cstrcpy+0x3e>
mov    %edx,%ebp
test   %edx,%edx
jle    115e <cstrcpy+0x3e>
mov    %rdi,%r14
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %ebp,%eax
jge    1158 <cstrcpy+0x38>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rax
jmp    115e <cstrcpy+0x3e>
movb   $0x0,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @cstrcpy(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%8 = load i8*, i8** %5
%9 = icmp eq i8* %8, null
br i1 %9, label %16, label %10
10: ; preds = %3
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = load i32, i32* %7
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13, %10, %3
store i8* null, i8** %4
br label %30
17: ; preds = %13
%18 = load i32, i32* %7
%19 = load i8*, i8** %6
%20 = call i32 @strlen(i8* %19)
%21 = add nsw i32 %20, 1
%22 = icmp slt i32 %18, %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i8*, i8** %5
store i8 0, i8* %24
store i8* null, i8** %4
br label %30
25: ; preds = %17
%26 = load i8*, i8** %5
%27 = load i8*, i8** %6
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %5
store i8* %29, i8** %4
br label %30
30: ; preds = %25, %23, %16
%31 = load i8*, i8** %4
ret i8* %31
}

LLVM IR (-O2):
define i8* @cstrcpy(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i8* %1, null
%6 = or i1 %4, %5
%7 = icmp slt i32 %2, 1
%8 = or i1 %6, %7
br i1 %8, label %15, label %9
9: ; preds = %3
%10 = tail call i32 @strlen(i8* %1)
%11 = icmp slt i32 %10, %2
br i1 %11, label %13, label %12
12: ; preds = %9
store i8 0, i8* %0
br label %15
13: ; preds = %9
%14 = tail call i32 @strcpy(i8* %0, i8* %1)
br label %15
15: ; preds = %3, %13, %12
%16 = phi i8* [ null, %12 ], [ %0, %13 ], [ null, %3 ]
ret i8* %16
}