#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 */
int iconv(int /*<<< orphan*/, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(int /*<<< orphan*/);
int /*<<< orphan*/ iconv_open(char *, char *);
size_t convert_string(char *in, size_t in_length, char *out, size_t out_length,
                      char *from, char *to) {
  int res;
  size_t inbytesleft = in_length;
  size_t outbytesleft = out_length;
  char *in_ptr = &in[0];
  char *out_ptr = &out[0];
  iconv_t cd;
  if (out_length - in_length < 0) {
    return -1;
  }
  if ((cd = iconv_open(to, from)) == (iconv_t)(-1)) {
    return -2;
  }
  res = iconv(cd, &in_ptr, &inbytesleft, &out_ptr, &outbytesleft);
  if (res < 0) {
    return -3;
  }
  iconv_close(cd);
  out_ptr[0] = '\0';
  return (out_length - outbytesleft);
}


x86 assembly (clang, -O0):
<convert_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x18(%rbp),%rax
cmp    $0x0,%rax
jae    118f <convert_string+0x5f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1202 <convert_string+0xd2>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <iconv_open@plt>
mov    %eax,-0x64(%rbp)
cmp    $0xffffffff,%eax
jne    11b5 <convert_string+0x85>
movq   $0xfffffffffffffffe,-0x8(%rbp)
ff
jmp    1202 <convert_string+0xd2>
mov    -0x64(%rbp),%edi
lea    -0x58(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x60(%rbp),%rcx
lea    -0x50(%rbp),%r8
call   1030 <iconv@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11e7 <convert_string+0xb7>
movq   $0xfffffffffffffffd,-0x8(%rbp)
ff
jmp    1202 <convert_string+0xd2>
mov    -0x64(%rbp),%edi
call   1040 <iconv_close@plt>
mov    -0x60(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
sub    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_string>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%rbx
mov    %rsi,0x20(%rsp)
mov    %rcx,0x10(%rsp)
mov    %rdi,0x18(%rsp)
mov    %rdx,0x8(%rsp)
mov    %r9,%rdi
mov    %r8,%rsi
call   1050 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     1194 <convert_string+0x64>
mov    %eax,%ebp
lea    0x18(%rsp),%rsi
lea    0x20(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %eax,%edi
call   1030 <iconv@plt>
test   %eax,%eax
js     119d <convert_string+0x6d>
mov    %ebp,%edi
call   1040 <iconv_close@plt>
mov    0x8(%rsp),%rax
movb   $0x0,(%rax)
sub    0x10(%rsp),%rbx
jmp    11a4 <convert_string+0x74>
mov    $0xfffffffffffffffe,%rbx
jmp    11a4 <convert_string+0x74>
mov    $0xfffffffffffffffd,%rbx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i64 @convert_string(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4, i8* %5) {
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i64
%16 = alloca i64
%17 = alloca i8*
%18 = alloca i8*
%19 = alloca i32
store i8* %0, i8** %8
store i64 %1, i64* %9
store i8* %2, i8** %10
store i64 %3, i64* %11
store i8* %4, i8** %12
store i8* %5, i8** %13
%20 = load i64, i64* %9
store i64 %20, i64* %15
%21 = load i64, i64* %11
store i64 %21, i64* %16
%22 = load i8*, i8** %8
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8* %23, i8** %17
%24 = load i8*, i8** %10
%25 = getelementptr inbounds i8, i8* %24, i64 0
store i8* %25, i8** %18
%26 = load i64, i64* %11
%27 = load i64, i64* %9
%28 = sub i64 %26, %27
%29 = icmp ult i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %6
store i64 -1, i64* %7
br label %51
31: ; preds = %6
%32 = load i8*, i8** %13
%33 = load i8*, i8** %12
%34 = call i32 @iconv_open(i8* %32, i8* %33)
store i32 %34, i32* %19
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %31
store i64 -2, i64* %7
br label %51
37: ; preds = %31
%38 = load i32, i32* %19
%39 = call i32 @iconv(i32 %38, i8** %17, i64* %15, i8** %18, i64* %16)
store i32 %39, i32* %14
%40 = load i32, i32* %14
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %37
store i64 -3, i64* %7
br label %51
43: ; preds = %37
%44 = load i32, i32* %19
%45 = call i32 @iconv_close(i32 %44)
%46 = load i8*, i8** %18
%47 = getelementptr inbounds i8, i8* %46, i64 0
store i8 0, i8* %47
%48 = load i64, i64* %11
%49 = load i64, i64* %16
%50 = sub i64 %48, %49
store i64 %50, i64* %7
br label %51
51: ; preds = %43, %42, %36, %30
%52 = load i64, i64* %7
ret i64 %52
}

LLVM IR (-O2):
define i64 @convert_string(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4, i8* %5) {
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = bitcast i64* %7 to i8*
store i64 %1, i64* %7
%12 = bitcast i64* %8 to i8*
store i64 %3, i64* %8
%13 = bitcast i8** %9 to i8*
store i8* %0, i8** %9
%14 = bitcast i8** %10 to i8*
store i8* %2, i8** %10
%15 = tail call i32 @iconv_open(i8* %5, i8* %4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %25, label %17
17: ; preds = %6
%18 = call i32 @iconv(i32 %15, i8** %9, i64* %7, i8** %10, i64* %8)
%19 = icmp slt i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = call i32 @iconv_close(i32 %15)
%22 = load i8*, i8** %10
store i8 0, i8* %22
%23 = load i64, i64* %8
%24 = sub i64 %3, %23
br label %25
25: ; preds = %17, %6, %20
%26 = phi i64 [ %24, %20 ], [ -2, %6 ], [ -3, %17 ]
ret i64 %26
}