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

/* Variables and functions */
size_t iconv(int /*<<< orphan*/, char **, size_t *, char **, size_t *);
int iconv_close(int /*<<< orphan*/);
int /*<<< orphan*/ iconv_open(char *, char *);
int /*<<< orphan*/ printf(char *);
size_t strlen(char *);
void koi_to_utf8(char *str_i, char *str_o) {
  iconv_t cd;
  size_t len_i, len_o = 255 * 2;
  size_t i;
  if ((cd = iconv_open("UTF-8", "KOI8-R")) == (iconv_t)-1) {
    printf("koi_to_utf8: iconv_open error\n");
    return;
  }
  len_i = strlen(str_i);
  if ((i = iconv(cd, &str_i, &len_i, &str_o, &len_o)) == (size_t)-1) {
    printf("koi_to_utf8: iconv error\n");
  }
  if (iconv_close(cd) == -1) {
    printf("koi_to_utf8: iconv_close error\n");
    return;
  }
}


x86 assembly (clang, -O0):
<koi_to_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x1fe,-0x28(%rbp)
lea    0xe91(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1070 <iconv_open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1198 <koi_to_utf8+0x48>
lea    0xe7f(%rip),%rdi
call   1050 <printf@plt>
jmp    11f4 <koi_to_utf8+0xa4>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x10(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1030 <iconv@plt>
mov    %rax,-0x30(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    11d7 <koi_to_utf8+0x87>
lea    0xe5a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <iconv_close@plt>
cmp    $0xffffffff,%eax
jne    11f4 <koi_to_utf8+0xa4>
lea    0xe57(%rip),%rdi
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<koi_to_utf8>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    %rdi,0x20(%rsp)
mov    %rsi,0x18(%rsp)
movq   $0x1fe,0x8(%rsp)
lea    0xe8d(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1070 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     11df <koi_to_utf8+0x8f>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,0x10(%rsp)
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %ebp,%edi
call   1030 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
jne    11c0 <koi_to_utf8+0x70>
lea    0xe71(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1060 <iconv_close@plt>
cmp    $0xffffffff,%eax
jne    11d8 <koi_to_utf8+0x88>
lea    0xe3a(%rip),%rdi
call   1040 <puts@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5f(%rip),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <puts@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"UTF-8\00"
@.str.1 = [7 x i8] c"KOI8-R\00"
@.str.2 = [31 x i8] c"koi_to_utf8: iconv_open error\0A\00"
@.str.3 = [26 x i8] c"koi_to_utf8: iconv error\0A\00"
@.str.4 = [32 x i8] c"koi_to_utf8: iconv_close error\0A\00"
define void @koi_to_utf8(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
store i64 510, i64* %7
%9 = call i32 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 %9, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %27
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %6
%16 = load i32, i32* %5
%17 = call i64 @iconv(i32 %16, i8** %3, i64* %6, i8** %4, i64* %7)
store i64 %17, i64* %8
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = load i32, i32* %5
%23 = call i32 @iconv_close(i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %11, %25, %21
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"UTF-8\00"
@.str.1 = [7 x i8] c"KOI8-R\00"
@str = [31 x i8] c"koi_to_utf8: iconv_close error\00"
@str.5 = [25 x i8] c"koi_to_utf8: iconv error\00"
@str.6 = [30 x i8] c"koi_to_utf8: iconv_open error\00"
define void @koi_to_utf8(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = bitcast i64* %5 to i8*
%8 = bitcast i64* %6 to i8*
store i64 510, i64* %6
%9 = tail call i32 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.6, i64 0, i64 0))
br label %24
13: ; preds = %2
%14 = tail call i64 @strlen(i8* dereferenceable(1) %0)
store i64 %14, i64* %5
%15 = call i64 @iconv(i32 %9, i8** %3, i64* %5, i8** %4, i64* %6)
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
%20 = call i32 @iconv_close(i32 %9)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %19, %22, %11
ret void
}