#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 */
unsigned short *Q_malloc(int);
unsigned short TextEncodingDecodeUTF8(char *, int *);
int strlen(char *);
unsigned short *decode_utf8(char *str) {
  unsigned short *buf, *out;
  int length = strlen(str);
  int i = 0;
  buf = out = Q_malloc((length + 1) * sizeof(unsigned short));
  for (i = 0; i < length; ++i) {
    *out = TextEncodingDecodeUTF8(str, &i);
    out++;
  }
  *out = 0;
  return buf;
}


x86 assembly (clang, -O0):
<decode_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x1,%rax
mov    %eax,%edi
call   1050 <Q_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11ae <decode_utf8+0x7e>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <TextEncodingDecodeUTF8@plt>
mov    %ax,%cx
mov    -0x18(%rbp),%rax
mov    %cx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1171 <decode_utf8+0x41>
mov    -0x18(%rbp),%rax
movw   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_utf8>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%r13d
lea    0x2(%r13,%r13,1),%edi
call   1050 <Q_malloc@plt>
mov    %rax,%r15
movl   $0x0,0xc(%rsp)
mov    %rax,%rbx
test   %r13d,%r13d
jle    1192 <decode_utf8+0x62>
lea    0xc(%rsp),%r12
mov    %r15,%rbx
nopl   (%rax)
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <TextEncodingDecodeUTF8@plt>
mov    %ax,(%rbx)
add    $0x2,%rbx
mov    0xc(%rsp),%eax
add    $0x1,%eax
mov    %eax,0xc(%rsp)
cmp    %r13d,%eax
jl     1170 <decode_utf8+0x40>
movw   $0x0,(%rbx)
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i16* @decode_utf8(i8* %0) {
%2 = alloca i8*
%3 = alloca i16*
%4 = alloca i16*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %5
store i32 0, i32* %6
%9 = load i32, i32* %5
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = mul i64 %11, 2
%13 = trunc i64 %12 to i32
%14 = call i16* @Q_malloc(i32 %13)
store i16* %14, i16** %4
store i16* %14, i16** %3
store i32 0, i32* %6
br label %15
15: ; preds = %25, %1
%16 = load i32, i32* %6
%17 = load i32, i32* %5
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %28
19: ; preds = %15
%20 = load i8*, i8** %2
%21 = call zeroext i16 @TextEncodingDecodeUTF8(i8* %20, i32* %6)
%22 = load i16*, i16** %4
store i16 %21, i16* %22
%23 = load i16*, i16** %4
%24 = getelementptr inbounds i16, i16* %23, i32 1
store i16* %24, i16** %4
br label %25
25: ; preds = %19
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
store i32 %27, i32* %6
br label %15
28: ; preds = %15
%29 = load i16*, i16** %4
store i16 0, i16* %29
%30 = load i16*, i16** %3
ret i16* %30
}

LLVM IR (-O2):
define i16* @decode_utf8(i8* %0) {
%2 = alloca i32
%3 = tail call i32 @strlen(i8* %0)
%4 = bitcast i32* %2 to i8*
%5 = shl i32 %3, 1
%6 = add i32 %5, 2
%7 = tail call i16* @Q_malloc(i32 %6)
store i32 0, i32* %2
%8 = icmp sgt i32 %3, 0
br i1 %8, label %9, label %16
9: ; preds = %1, %9
%10 = phi i16* [ %12, %9 ], [ %7, %1 ]
%11 = call zeroext i16 @TextEncodingDecodeUTF8(i8* %0, i32* %2)
store i16 %11, i16* %10
%12 = getelementptr inbounds i16, i16* %10, i64 1
%13 = load i32, i32* %2
%14 = add nsw i32 %13, 1
store i32 %14, i32* %2
%15 = icmp slt i32 %14, %3
br i1 %15, label %9, label %16
16: ; preds = %9, %1
%17 = phi i16* [ %7, %1 ], [ %12, %9 ]
store i16 0, i16* %17
ret i16* %7
}