#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 strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *StrMaxCpy(char *dst, const char *src, size_t maxLen) {
  if (maxLen < 1) {
    return "";
  }
  if (maxLen == 1) {
    dst[0] = '\0';
    return dst;
  }
  strncpy(dst, src, maxLen - 1);
  if ((strlen(src) + 1) >= maxLen) {
    dst[maxLen - 1] = '\0';
  }
  return dst;
}


x86 assembly (clang, -O0):
<StrMaxCpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x1,-0x20(%rbp)
jae    114f <StrMaxCpy+0x2f>
lea    0xeba(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11b3 <StrMaxCpy+0x93>
cmpq   $0x1,-0x20(%rbp)
jne    116e <StrMaxCpy+0x4e>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b3 <StrMaxCpy+0x93>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    -0x20(%rbp),%rax
jb     11ab <StrMaxCpy+0x8b>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrMaxCpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1169 <StrMaxCpy+0x49>
mov    %rdx,%rbx
mov    %rdi,%r14
mov    %rdi,%r15
cmp    $0x1,%rdx
je     1163 <StrMaxCpy+0x43>
mov    %rsi,%r12
lea    -0x1(%rbx),%r15
mov    %r14,%rdi
mov    %r15,%rdx
call   1030 <strncpy@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %rbx,%rax
jb     1170 <StrMaxCpy+0x50>
add    %r14,%r15
movb   $0x0,(%r15)
jmp    1170 <StrMaxCpy+0x50>
lea    0xe90(%rip),%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @StrMaxCpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%8 = load i64, i64* %7
%9 = icmp ult i64 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %3
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %4
br label %37
11: ; preds = %3
%12 = load i64, i64* %7
%13 = icmp eq i64 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 0
store i8 0, i8* %16
%17 = load i8*, i8** %5
store i8* %17, i8** %4
br label %37
18: ; preds = %11
%19 = load i8*, i8** %5
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = sub i64 %21, 1
%23 = call i32 @strncpy(i8* %19, i8* %20, i64 %22)
%24 = load i8*, i8** %6
%25 = call i32 @strlen(i8* %24)
%26 = add nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = load i64, i64* %7
%29 = icmp uge i64 %27, %28
br i1 %29, label %30, label %35
30: ; preds = %18
%31 = load i8*, i8** %5
%32 = load i64, i64* %7
%33 = sub i64 %32, 1
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %30, %18
%36 = load i8*, i8** %5
store i8* %36, i8** %4
br label %37
37: ; preds = %35, %14, %10
%38 = load i8*, i8** %4
ret i8* %38
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @StrMaxCpy(i8* %0, i8* %1, i64 %2) {
switch i64 %2, label %4 [
i64 0, label %15
i64 1, label %13
]
4: ; preds = %3
%5 = add i64 %2, -1
%6 = tail call i32 @strncpy(i8* %0, i8* %1, i64 %5)
%7 = tail call i32 @strlen(i8* %1)
%8 = add nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = icmp ult i64 %9, %2
br i1 %10, label %15, label %11
11: ; preds = %4
%12 = getelementptr inbounds i8, i8* %0, i64 %5
br label %13
13: ; preds = %3, %11
%14 = phi i8* [ %12, %11 ], [ %0, %3 ]
store i8 0, i8* %14
br label %15
15: ; preds = %13, %4, %3
%16 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %3 ], [ %0, %4 ], [ %0, %13 ]
ret i8* %16
}