#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);
int /*<<< orphan*/ koi2wc(int /*<<< orphan*/);
int strlen(char *);
unsigned short *decode_koi8q(char *str) {
  unsigned short *buf, *out;
  buf = out = Q_malloc((strlen(str) + 1) * sizeof(unsigned short));
  while (*str)
    *out++ = koi2wc(*str++);
  *out = 0;
  return buf;
}


x86 assembly (clang, -O0):
<decode_koi8q>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x1,%rax
mov    %eax,%edi
call   1050 <Q_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     119b <decode_koi8q+0x6b>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%edi
call   1040 <koi2wc@plt>
mov    %ax,%cx
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x2,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cx,(%rax)
jmp    115d <decode_koi8q+0x2d>
mov    -0x18(%rbp),%rax
movw   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_koi8q>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%edi
add    $0x2,%edi
call   1050 <Q_malloc@plt>
mov    %rax,%r14
mov    (%r15),%al
mov    %r14,%rbx
test   %al,%al
je     117b <decode_koi8q+0x4b>
add    $0x1,%r15
mov    %r14,%rbx
nopl   0x0(%rax)
movsbl %al,%edi
call   1040 <koi2wc@plt>
mov    %ax,(%rbx)
add    $0x2,%rbx
movzbl (%r15),%eax
add    $0x1,%r15
test   %al,%al
jne    1160 <decode_koi8q+0x30>
movw   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i16* @decode_koi8q(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = shl i32 %2, 1
%4 = add i32 %3, 2
%5 = tail call i16* @Q_malloc(i32 %4)
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %19, label %8
8: ; preds = %1, %8
%9 = phi i8 [ %17, %8 ], [ %6, %1 ]
%10 = phi i16* [ %16, %8 ], [ %5, %1 ]
%11 = phi i8* [ %12, %8 ], [ %0, %1 ]
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = sext i8 %9 to i32
%14 = tail call i32 @koi2wc(i32 %13)
%15 = trunc i32 %14 to i16
%16 = getelementptr inbounds i16, i16* %10, i64 1
store i16 %15, i16* %10
%17 = load i8, i8* %12
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %8
19: ; preds = %8, %1
%20 = phi i16* [ %5, %1 ], [ %16, %8 ]
store i16 0, i16* %20
ret i16* %5
}