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

/* Variables and functions */
int evl_checkBufSpace(size_t, size_t, size_t);
size_t evl_memcpy(char *, int /*<<< orphan*/ *, size_t);
int wcslen(int /*<<< orphan*/ *);
int evl_packWstring(char *d, wchar_t *s, size_t buf_maxlen,
                    size_t total_used_len, size_t *tk_len_added) {
  size_t slen = (wcslen(s) + 1) * sizeof(wchar_t);
  size_t len = 0;
  int status = evl_checkBufSpace(buf_maxlen, total_used_len, slen);
  if (status == 0) {
    len = evl_memcpy(d, (wchar_t *)s, slen);
  } else {
    return status;
  }
  *tk_len_added = len;
  return 0;
}


x86 assembly (clang, -O0):
<evl_packWstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <wcslen@plt>
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1050 <evl_checkBufSpace@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    11a2 <evl_packWstring+0x72>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <evl_memcpy@plt>
mov    %rax,-0x40(%rbp)
jmp    11ad <evl_packWstring+0x7d>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <evl_packWstring+0x8f>
mov    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evl_packWstring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r13
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <wcslen@plt>
cltq
lea    0x4(,%rax,4),%rbx
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1050 <evl_checkBufSpace@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1181 <evl_packWstring+0x51>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <evl_memcpy@plt>
mov    %rax,(%r14)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @evl_packWstring(i8* %0, i32* %1, i64 %2, i64 %3, i64* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32
store i8* %0, i8** %7
store i32* %1, i32** %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i64* %4, i64** %11
%15 = load i32*, i32** %8
%16 = call i32 @wcslen(i32* %15)
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 4
store i64 %19, i64* %12
store i64 0, i64* %13
%20 = load i64, i64* %9
%21 = load i64, i64* %10
%22 = load i64, i64* %12
%23 = call i32 @evl_checkBufSpace(i64 %20, i64 %21, i64 %22)
store i32 %23, i32* %14
%24 = load i32, i32* %14
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %5
%27 = load i8*, i8** %7
%28 = load i32*, i32** %8
%29 = load i64, i64* %12
%30 = call i64 @evl_memcpy(i8* %27, i32* %28, i64 %29)
store i64 %30, i64* %13
br label %33
31: ; preds = %5
%32 = load i32, i32* %14
store i32 %32, i32* %6
br label %36
33: ; preds = %26
%34 = load i64, i64* %13
%35 = load i64*, i64** %11
store i64 %34, i64* %35
store i32 0, i32* %6
br label %36
36: ; preds = %33, %31
%37 = load i32, i32* %6
ret i32 %37
}

LLVM IR (-O2):
define i32 @evl_packWstring(i8* %0, i32* %1, i64 %2, i64 %3, i64* nocapture writeonly %4) {
%6 = tail call i32 @wcslen(i32* %1)
%7 = add nsw i32 %6, 1
%8 = sext i32 %7 to i64
%9 = shl nsw i64 %8, 2
%10 = tail call i32 @evl_checkBufSpace(i64 %2, i64 %3, i64 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = tail call i64 @evl_memcpy(i8* %0, i32* %1, i64 %9)
store i64 %13, i64* %4
br label %14
14: ; preds = %5, %12
ret i32 %10
}