#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*/ buf;

/* Variables and functions */
scalar_t__ R_CharAvailable(unsigned short);
int /*<<< orphan*/ memcpy(unsigned short *, unsigned short *, int);
int qwcslen(unsigned short *);
unsigned short *transliterate_char(unsigned short);
unsigned short *maybe_transliterate(unsigned short *src) {
  unsigned short *dst, *trans;
  static unsigned short buf[2048];
  int len;
  dst = buf;
  while (*src && dst < buf + (sizeof(buf) / sizeof(buf[0])) - 1) {
    if (R_CharAvailable(*src))
      *dst++ = *src;
    else {
      trans = transliterate_char(*src);
      len = ((qwcslen(trans)) < (buf + (sizeof(buf) / sizeof(buf[0])) - 1 - dst)
                 ? (qwcslen(trans))
                 : (buf + (sizeof(buf) / sizeof(buf[0])) - 1 - dst));
      memcpy(dst, trans, len * sizeof(unsigned short));
      dst += len;
    }
    src++;
  }
  *dst = 0;
  return buf;
}


x86 assembly (clang, -O0):
<maybe_transliterate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0x2efd(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movzwl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
je     1187 <maybe_transliterate+0x47>
lea    0x2edd(%rip),%rax
add    $0x1000,%rax
add    $0xfffffffffffffffe,%rax
cmp    %rax,-0x10(%rbp)
setb   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1197 <maybe_transliterate+0x57>
jmp    1284 <maybe_transliterate+0x144>
mov    -0x8(%rbp),%rax
movzwl (%rax),%edi
call   1060 <R_CharAvailable@plt>
cmp    $0x0,%rax
je     11cb <maybe_transliterate+0x8b>
mov    -0x8(%rbp),%rax
mov    (%rax),%cx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x2,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cx,(%rax)
jmp    1273 <maybe_transliterate+0x133>
mov    -0x8(%rbp),%rax
movzwl (%rax),%edi
call   1050 <transliterate_char@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <qwcslen@plt>
cltq
mov    -0x10(%rbp),%rdx
lea    0x2e5f(%rip),%rcx
add    $0x1000,%rcx
add    $0xfffffffffffffffe,%rcx
sub    %rdx,%rcx
sar    $0x1,%rcx
cmp    %rcx,%rax
jge    1220 <maybe_transliterate+0xe0>
mov    -0x18(%rbp),%rdi
call   1030 <qwcslen@plt>
cltq
mov    %rax,-0x28(%rbp)
jmp    1240 <maybe_transliterate+0x100>
mov    -0x10(%rbp),%rcx
lea    0x2e25(%rip),%rax
add    $0x1000,%rax
add    $0xfffffffffffffffe,%rax
sub    %rcx,%rax
sar    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x1,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
jmp    1157 <maybe_transliterate+0x17>
mov    -0x10(%rbp),%rax
movw   $0x0,(%rax)
lea    0x2dbc(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<maybe_transliterate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movzwl (%rdi),%eax
test   %ax,%ax
je     11ed <maybe_transliterate+0xad>
mov    %rdi,%rbx
add    $0x2,%rbx
lea    0x2eeb(%rip),%r12
lea    0x3ee2(%rip),%r13
mov    %r12,%r14
nop
movzwl %ax,%edi
call   1060 <R_CharAvailable@plt>
movzwl -0x2(%rbx),%ecx
test   %rax,%rax
je     1190 <maybe_transliterate+0x50>
mov    %cx,(%r14)
add    $0x2,%r14
jmp    11da <maybe_transliterate+0x9a>
nopl   0x0(%rax,%rax,1)
movzwl %cx,%edi
call   1050 <transliterate_char@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1030 <qwcslen@plt>
cltq
mov    %r12,%rbp
sub    %r14,%rbp
add    $0xffe,%rbp
sar    %rbp
cmp    %rax,%rbp
jle    11c4 <maybe_transliterate+0x84>
mov    %r15,%rdi
call   1030 <qwcslen@plt>
mov    %eax,%ebp
lea    0x0(%rbp,%rbp,1),%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <memcpy@plt>
movslq %ebp,%rax
lea    (%r14,%rax,2),%r14
movzwl (%rbx),%eax
test   %ax,%ax
je     11f4 <maybe_transliterate+0xb4>
add    $0x2,%rbx
cmp    %r13,%r14
jb     1170 <maybe_transliterate+0x30>
jmp    11f4 <maybe_transliterate+0xb4>
lea    0x2e5c(%rip),%r14
movw   $0x0,(%r14)
lea    0x2e4f(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@maybe_transliterate.buf = internal global [2048 x i16] zeroinitializer
define i16* @maybe_transliterate(i16* %0) {
%2 = alloca i16*
%3 = alloca i16*
%4 = alloca i16*
%5 = alloca i32
store i16* %0, i16** %2
store i16* getelementptr inbounds ([2048 x i16], [2048 x i16]* @maybe_transliterate.buf, i64 0, i64 0), i16** %3
br label %6
6: ; preds = %61, %1
%7 = load i16*, i16** %2
%8 = load i16, i16* %7
%9 = zext i16 %8 to i32
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i16*, i16** %3
%13 = icmp ult i16* %12, getelementptr inbounds ([2048 x i16], [2048 x i16]* @maybe_transliterate.buf, i64 1, i64 -1)
br label %14
14: ; preds = %11, %6
%15 = phi i1 [ false, %6 ], [ %13, %11 ]
br i1 %15, label %16, label %64
16: ; preds = %14
%17 = load i16*, i16** %2
%18 = load i16, i16* %17
%19 = call i64 @R_CharAvailable(i16 zeroext %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i16*, i16** %2
%23 = load i16, i16* %22
%24 = load i16*, i16** %3
%25 = getelementptr inbounds i16, i16* %24, i32 1
store i16* %25, i16** %3
store i16 %23, i16* %24
br label %61
26: ; preds = %16
%27 = load i16*, i16** %2
%28 = load i16, i16* %27
%29 = call i16* @transliterate_char(i16 zeroext %28)
store i16* %29, i16** %4
%30 = load i16*, i16** %4
%31 = call i32 @qwcslen(i16* %30)
%32 = sext i32 %31 to i64
%33 = load i16*, i16** %3
%34 = ptrtoint i16* %33 to i64
%35 = sub i64 ptrtoint (i16* getelementptr inbounds ([2048 x i16], [2048 x i16]* @maybe_transliterate.buf, i64 1, i64 -1) to i64), %34
%36 = sdiv exact i64 %35, 2
%37 = icmp slt i64 %32, %36
br i1 %37, label %38, label %42
38: ; preds = %26
%39 = load i16*, i16** %4
%40 = call i32 @qwcslen(i16* %39)
%41 = sext i32 %40 to i64
br label %47
42: ; preds = %26
%43 = load i16*, i16** %3
%44 = ptrtoint i16* %43 to i64
%45 = sub i64 ptrtoint (i16* getelementptr inbounds ([2048 x i16], [2048 x i16]* @maybe_transliterate.buf, i64 1, i64 -1) to i64), %44
%46 = sdiv exact i64 %45, 2
br label %47
47: ; preds = %42, %38
%48 = phi i64 [ %41, %38 ], [ %46, %42 ]
%49 = trunc i64 %48 to i32
store i32 %49, i32* %5
%50 = load i16*, i16** %3
%51 = load i16*, i16** %4
%52 = load i32, i32* %5
%53 = sext i32 %52 to i64
%54 = mul i64 %53, 2
%55 = trunc i64 %54 to i32
%56 = call i32 @memcpy(i16* %50, i16* %51, i32 %55)
%57 = load i32, i32* %5
%58 = load i16*, i16** %3
%59 = sext i32 %57 to i64
%60 = getelementptr inbounds i16, i16* %58, i64 %59
store i16* %60, i16** %3
br label %61
61: ; preds = %47, %21
%62 = load i16*, i16** %2
%63 = getelementptr inbounds i16, i16* %62, i32 1
store i16* %63, i16** %2
br label %6
64: ; preds = %14
%65 = load i16*, i16** %3
store i16 0, i16* %65
ret i16* getelementptr inbounds ([2048 x i16], [2048 x i16]* @maybe_transliterate.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@maybe_transliterate.buf = internal global [2048 x i16] zeroinitializer
define i16* @maybe_transliterate(i16* nocapture %0) {
%2 = load i16, i16* %0
%3 = icmp eq i16 %2, 0
br i1 %3, label %39, label %4
4: ; preds = %1, %32
%5 = phi i16 [ %35, %32 ], [ %2, %1 ]
%6 = phi i16* [ %34, %32 ], [ %0, %1 ]
%7 = phi i16* [ %33, %32 ], [ getelementptr inbounds ([2048 x i16], [2048 x i16]* @maybe_transliterate.buf, i64 0, i64 0), %1 ]
%8 = tail call i64 @R_CharAvailable(i16 zeroext %5)
%9 = icmp eq i64 %8, 0
%10 = load i16, i16* %6
br i1 %9, label %13, label %11
11: ; preds = %4
%12 = getelementptr inbounds i16, i16* %7, i64 1
store i16 %10, i16* %7
br label %32
13: ; preds = %4
%14 = tail call i16* @transliterate_char(i16 zeroext %10)
%15 = tail call i32 @qwcslen(i16* %14)
%16 = sext i32 %15 to i64
%17 = ptrtoint i16* %7 to i64
%18 = sub i64 ptrtoint (i16* getelementptr inbounds ([2048 x i16], [2048 x i16]* @maybe_transliterate.buf, i64 0, i64 2047) to i64), %17
%19 = ashr exact i64 %18, 1
%20 = icmp sgt i64 %19, %16
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = tail call i32 @qwcslen(i16* %14)
%23 = zext i32 %22 to i64
br label %24
24: ; preds = %13, %21
%25 = phi i64 [ %23, %21 ], [ %19, %13 ]
%26 = shl i64 %25, 32
%27 = ashr exact i64 %26, 32
%28 = lshr exact i64 %26, 31
%29 = trunc i64 %28 to i32
%30 = tail call i32 @memcpy(i16* %7, i16* %14, i32 %29)
%31 = getelementptr inbounds i16, i16* %7, i64 %27
br label %32
32: ; preds = %24, %11
%33 = phi i16* [ %12, %11 ], [ %31, %24 ]
%34 = getelementptr inbounds i16, i16* %6, i64 1
%35 = load i16, i16* %34
%36 = icmp ne i16 %35, 0
%37 = icmp ult i16* %33, getelementptr inbounds ([2048 x i16], [2048 x i16]* @maybe_transliterate.buf, i64 0, i64 2047)
%38 = select i1 %36, i1 %37, i1 false
br i1 %38, label %4, label %39
39: ; preds = %32, %1
%40 = phi i16* [ getelementptr inbounds ([2048 x i16], [2048 x i16]* @maybe_transliterate.buf, i64 0, i64 0), %1 ], [ %33, %32 ]
store i16 0, i16* %40
ret i16* getelementptr inbounds ([2048 x i16], [2048 x i16]* @maybe_transliterate.buf, i64 0, i64 0)
}