#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 */
scalar_t__ realloc(char *, int);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *rstrcpy(char *Dest, const char *Src, int Max) {
  int len = 0;
  if (Src)
    len = strlen(Src);
  if (Max && (len > Max))
    len = Max;
  Dest = (char *)realloc(Dest, len + 1);
  strncpy(Dest, Src, len);
  Dest[len] = '\0';
  return (Dest);
}


x86 assembly (clang, -O0):
<rstrcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1161 <rstrcpy+0x31>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     117d <rstrcpy+0x4d>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    117d <rstrcpy+0x4d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rstrcpy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
test   %rsi,%rsi
je     114e <rstrcpy+0x1e>
mov    %r15,%rdi
call   1040 <strlen@plt>
jmp    1150 <rstrcpy+0x20>
xor    %eax,%eax
cmp    %ebp,%eax
mov    %eax,%ebx
cmovg  %ebp,%ebx
test   %ebp,%ebp
cmove  %eax,%ebx
lea    0x1(%rbx),%esi
mov    %r14,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @rstrcpy(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%8 = load i8*, i8** %5
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %7
br label %13
13: ; preds = %10, %3
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i32, i32* %7
%18 = load i32, i32* %6
%19 = icmp sgt i32 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i32, i32* %6
store i32 %21, i32* %7
br label %22
22: ; preds = %20, %16, %13
%23 = load i8*, i8** %4
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
%26 = call i64 @realloc(i8* %23, i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %4
%28 = load i8*, i8** %4
%29 = load i8*, i8** %5
%30 = load i32, i32* %7
%31 = call i32 @strncpy(i8* %28, i8* %29, i32 %30)
%32 = load i8*, i8** %4
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %4
ret i8* %36
}

LLVM IR (-O2):
define i8* @rstrcpy(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = tail call i32 @strlen(i8* %1)
br label %7
7: ; preds = %5, %3
%8 = phi i32 [ %6, %5 ], [ 0, %3 ]
%9 = icmp ne i32 %2, 0
%10 = icmp sgt i32 %8, %2
%11 = select i1 %9, i1 %10, i1 false
%12 = select i1 %11, i32 %2, i32 %8
%13 = add nsw i32 %12, 1
%14 = tail call i64 @realloc(i8* %0, i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i32 @strncpy(i8* %15, i8* %1, i32 %12)
%17 = sext i32 %12 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 0, i8* %18
ret i8* %15
}