#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*/ _DIAGASSERT(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
size_t strxfrm(char *dst, const char *src, size_t n) {
  size_t srclen, copysize;
  _DIAGASSERT(src != NULL);
  srclen = strlen(src);
  if (n != 0) {
    _DIAGASSERT(dst != NULL);
    copysize = srclen < n ? srclen : n - 1;
    (void)memcpy(dst, src, copysize);
    dst[copysize] = 0;
  }
  return (srclen);
}


x86 assembly (clang, -O0):
<strxfrm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <_DIAGASSERT@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11cc <strxfrm+0x9c>
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <_DIAGASSERT@plt>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    119b <strxfrm+0x6b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11a7 <strxfrm+0x77>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strxfrm>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
call   1050 <_DIAGASSERT@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
test   %r15,%r15
je     118c <strxfrm+0x5c>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <_DIAGASSERT@plt>
lea    -0x1(%r15),%rbx
cmp    %r15,%r13
cmovb  %r13,%rbx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r14,%rbx,1)
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i64 @strxfrm(i8* %0, i8* %1, i64 %2) {
%4 = icmp ne i8* %1, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @_DIAGASSERT(i32 %5)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = icmp eq i64 %2, 0
br i1 %8, label %18, label %9
9: ; preds = %3
%10 = icmp ne i8* %0, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @_DIAGASSERT(i32 %11)
%13 = icmp ult i64 %7, %2
%14 = add i64 %2, -1
%15 = select i1 %13, i64 %7, i64 %14
%16 = tail call i32 @memcpy(i8* %0, i8* %1, i64 %15)
%17 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 0, i8* %17
br label %18
18: ; preds = %9, %3
ret i64 %7
}