#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 */
typedef scalar_t__ wchar_t;
typedef int /*<<< orphan*/ locale_t;

/* Variables and functions */
size_t wcslen(scalar_t__ const *);
int /*<<< orphan*/ wmemcpy(scalar_t__ *, scalar_t__ const *, size_t);
size_t __wcsxfrm_l(wchar_t *restrict dest, const wchar_t *restrict src,
                   size_t n, locale_t loc) {
  size_t l = wcslen(src);
  if (l < n) {
    wmemcpy(dest, src, l + 1);
  } else if (n) {
    wmemcpy(dest, src, n - 1);
    dest[n - 1] = 0;
  }
  return l;
}


x86 assembly (clang, -O0):
<__wcsxfrm_l>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <wcslen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    116c <__wcsxfrm_l+0x4c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1030 <wmemcpy@plt>
jmp    11a5 <__wcsxfrm_l+0x85>
cmpq   $0x0,-0x18(%rbp)
je     11a0 <__wcsxfrm_l+0x80>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <wmemcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movq   $0x0,(%rax,%rcx,8)
jmp    11a5 <__wcsxfrm_l+0x85>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__wcsxfrm_l>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <wcslen@plt>
mov    %rax,%rbx
cmp    %r12,%rax
jae    1155 <__wcsxfrm_l+0x35>
mov    %rbx,%rdx
add    $0x1,%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <wmemcpy@plt>
jmp    1173 <__wcsxfrm_l+0x53>
test   %r12,%r12
je     1173 <__wcsxfrm_l+0x53>
lea    -0x1(%r12),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <wmemcpy@plt>
movq   $0x0,-0x8(%r14,%r12,8)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @__wcsxfrm_l(i64* noalias %0, i64* noalias %1, i64 %2, i32 %3) {
%5 = alloca i64*
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
store i64* %0, i64** %5
store i64* %1, i64** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%10 = load i64*, i64** %6
%11 = call i64 @wcslen(i64* %10)
store i64 %11, i64* %9
%12 = load i64, i64* %9
%13 = load i64, i64* %7
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %21
15: ; preds = %4
%16 = load i64*, i64** %5
%17 = load i64*, i64** %6
%18 = load i64, i64* %9
%19 = add i64 %18, 1
%20 = call i32 @wmemcpy(i64* %16, i64* %17, i64 %19)
br label %35
21: ; preds = %4
%22 = load i64, i64* %7
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = load i64*, i64** %5
%26 = load i64*, i64** %6
%27 = load i64, i64* %7
%28 = sub i64 %27, 1
%29 = call i32 @wmemcpy(i64* %25, i64* %26, i64 %28)
%30 = load i64*, i64** %5
%31 = load i64, i64* %7
%32 = sub i64 %31, 1
%33 = getelementptr inbounds i64, i64* %30, i64 %32
store i64 0, i64* %33
br label %34
34: ; preds = %24, %21
br label %35
35: ; preds = %34, %15
%36 = load i64, i64* %9
ret i64 %36
}

LLVM IR (-O2):
define i64 @__wcsxfrm_l(i64* noalias %0, i64* noalias %1, i64 %2, i32 %3) {
%5 = tail call i64 @wcslen(i64* %1)
%6 = icmp ult i64 %5, %2
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = add nuw i64 %5, 1
%9 = tail call i32 @wmemcpy(i64* %0, i64* %1, i64 %8)
br label %16
10: ; preds = %4
%11 = icmp eq i64 %2, 0
br i1 %11, label %16, label %12
12: ; preds = %10
%13 = add i64 %2, -1
%14 = tail call i32 @wmemcpy(i64* %0, i64* %1, i64 %13)
%15 = getelementptr inbounds i64, i64* %0, i64 %13
store i64 0, i64* %15
br label %16
16: ; preds = %10, %12, %7
ret i64 %5
}