#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 */
size_t strlen(char *);
scalar_t__ utilStrNCpy(char *, char *, size_t);
char *utilStrMCpy(char *sDst, char *sSrc, size_t ulCnt) {
  size_t ulLen;
  if (utilStrNCpy(sDst, sSrc, ulCnt) == 0)
    return 0;
  ulLen = strlen(sSrc);
  if (ulLen >= ulCnt)
    sDst[ulCnt - 1] = '\0';
  return sDst;
}


x86 assembly (clang, -O0):
<utilStrMCpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <utilStrNCpy@plt>
cmp    $0x0,%rax
jne    115c <utilStrMCpy+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    118f <utilStrMCpy+0x6f>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1187 <utilStrMCpy+0x67>
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    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utilStrMCpy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
call   1030 <utilStrNCpy@plt>
test   %rax,%rax
je     114d <utilStrMCpy+0x2d>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %r14,%rax
jb     1150 <utilStrMCpy+0x30>
movb   $0x0,-0x1(%r14,%r15,1)
jmp    1150 <utilStrMCpy+0x30>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @utilStrMCpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %5
%10 = load i8*, i8** %6
%11 = load i64, i64* %7
%12 = call i64 @utilStrNCpy(i8* %9, i8* %10, i64 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i8* null, i8** %4
br label %28
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = load i64, i64* %7
%20 = icmp uge i64 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %15
%22 = load i8*, i8** %5
%23 = load i64, i64* %7
%24 = sub i64 %23, 1
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %21, %15
%27 = load i8*, i8** %5
store i8* %27, i8** %4
br label %28
28: ; preds = %26, %14
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
define i8* @utilStrMCpy(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @utilStrNCpy(i8* %0, i8* %1, i64 %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = icmp ult i64 %7, %2
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = add i64 %2, -1
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %6, %9, %3
%13 = phi i8* [ null, %3 ], [ %0, %9 ], [ %0, %6 ]
ret i8* %13
}