#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 char wchar_t;

/* Variables and functions */
scalar_t__ malloc(int);
char shortVal(short);
void convertUnicode(wchar_t *w, char *s, int len) {
  short *x;
  int i;
  x = (short *)s;
  w = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
  for (i = 0; i < len; i++) {
    w[i] = shortVal(x[i]);
  }
  w[len] = '\0';
}


x86 assembly (clang, -O0):
<convertUnicode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1191 <convertUnicode+0x71>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movswl (%rax,%rcx,2),%edi
call   1030 <shortVal@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1159 <convertUnicode+0x39>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convertUnicode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
lea    0x1(%r14),%edi
call   1040 <malloc@plt>
mov    %rax,%r12
test   %r14d,%r14d
jle    1167 <convertUnicode+0x47>
mov    %r14d,%r13d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movswl (%r15,%rbx,2),%edi
call   1030 <shortVal@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1150 <convertUnicode+0x30>
movslq %r14d,%rax
movb   $0x0,(%r12,%rax,1)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @convertUnicode(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i16*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
%10 = bitcast i8* %9 to i16*
store i16* %10, i16** %7
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 1
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %4
store i32 0, i32* %8
br label %18
18: ; preds = %33, %3
%19 = load i32, i32* %8
%20 = load i32, i32* %6
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %36
22: ; preds = %18
%23 = load i16*, i16** %7
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i16, i16* %23, i64 %25
%27 = load i16, i16* %26
%28 = call signext i8 @shortVal(i16 signext %27)
%29 = load i8*, i8** %4
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 %28, i8* %32
br label %33
33: ; preds = %22
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %18
36: ; preds = %18
%37 = load i8*, i8** %4
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
ret void
}

LLVM IR (-O2):
define void @convertUnicode(i8* nocapture readnone %0, i8* nocapture %1, i32 %2) {
%4 = bitcast i8* %1 to i16*
%5 = add nsw i32 %2, 1
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %19
9: ; preds = %3
%10 = zext i32 %2 to i64
br label %11
11: ; preds = %9, %11
%12 = phi i64 [ 0, %9 ], [ %17, %11 ]
%13 = getelementptr inbounds i16, i16* %4, i64 %12
%14 = load i16, i16* %13
%15 = tail call signext i8 @shortVal(i16 signext %14)
%16 = getelementptr inbounds i8, i8* %7, i64 %12
store i8 %15, i8* %16
%17 = add nuw nsw i64 %12, 1
%18 = icmp eq i64 %17, %10
br i1 %18, label %19, label %11
19: ; preds = %11, %3
%20 = sext i32 %2 to i64
%21 = getelementptr inbounds i8, i8* %7, i64 %20
store i8 0, i8* %21
ret void
}