#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*/ assert(int);
char *strncpy(char *, char const *, size_t);
char *StrCopyMax(char *target, size_t max, const char *source) {
  assert((0 != (target)));
  assert((0 != (source)));
  assert(max > 0);
  target = strncpy(target, source, max - 1);
  target[max - 1] = (char)0;
  return target;
}


x86 assembly (clang, -O0):
<StrCopyMax>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrCopyMax>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
add    $0xffffffffffffffff,%r14
setb   %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r14,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @StrCopyMax(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%7 = load i8*, i8** %4
%8 = icmp ne i8* null, %7
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i8*, i8** %6
%12 = icmp ne i8* null, %11
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i64, i64* %5
%16 = icmp ugt i64 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i8*, i8** %4
%20 = load i8*, i8** %6
%21 = load i64, i64* %5
%22 = sub i64 %21, 1
%23 = call i8* @strncpy(i8* %19, i8* %20, i64 %22)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = load i64, i64* %5
%26 = sub i64 %25, 1
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
%28 = load i8*, i8** %4
ret i8* %28
}

LLVM IR (-O2):
define i8* @StrCopyMax(i8* returned %0, i64 %1, i8* %2) {
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i8* %2, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i64 %1, 0
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = add i64 %1, -1
%14 = tail call i8* @strncpy(i8* dereferenceable(1) %0, i8* %2, i64 %13)
%15 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 0, i8* %15
ret i8* %0
}