#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 *);
char *strncpy(char *, char *, unsigned int);
char *strmncpy(char *dest, char *source, unsigned int len) {
  char *pt;
  if (dest == NULL | source == NULL)
    return NULL;
  pt = strncpy(dest, source, len - 1);
  if (strlen(source) + 1 >= len)
    dest[len - 1] = 0;
  return pt;
}


x86 assembly (clang, -O0):
<strmncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
cmpq   $0x0,-0x18(%rbp)
sete   %cl
and    $0x1,%cl
movzbl %cl,%ecx
or     %ecx,%eax
cmp    $0x0,%eax
je     1166 <strmncpy+0x46>
movq   $0x0,-0x8(%rbp)
jmp    11aa <strmncpy+0x8a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jb     11a2 <strmncpy+0x82>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strmncpy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r14d,%r14d
test   %rdi,%rdi
je     1166 <strmncpy+0x46>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1166 <strmncpy+0x46>
mov    %edx,%r15d
mov    %rdi,%r12
lea    -0x1(%r15),%ebp
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cmp    %r15d,%eax
jb     1166 <strmncpy+0x46>
mov    %ebp,%eax
movb   $0x0,(%r12,%rax,1)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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