#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 ssize_t;
typedef scalar_t__ iconv_t;

/* Variables and functions */
int iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char *, char *);
ssize_t convert_string(const char *in, size_t in_length, char *out,
                       size_t out_size, char *from, char *to) {
  char *in_ptr = (char *)in;
  size_t in_bytesleft = in_length;
  char *out_ptr = out;
  size_t out_bytesleft = out_size - 1;
  iconv_t cd = (iconv_t)-1;
  ssize_t res;
  cd = iconv_open(to, from);
  if (cd == (iconv_t)-1) {
    return -2;
  }
  res = iconv(cd, &in_ptr, &in_bytesleft, &out_ptr, &out_bytesleft);
  if (res < 0) {
    iconv_close(cd);
    return -3;
  }
  iconv_close(cd);
  *out_ptr = '\0';
  return (out_ptr - out);
}


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    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x58(%rbp)
movq   $0xffffffffffffffff,-0x60(%rbp)
ff
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <iconv_open@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0xffffffffffffffff,-0x60(%rbp)
jne    11a4 <convert_string+0x74>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11fd <convert_string+0xcd>
mov    -0x60(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
lea    -0x58(%rbp),%r8
call   1030 <iconv@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    11df <convert_string+0xaf>
mov    -0x60(%rbp),%rdi
call   1040 <iconv_close@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    11fd <convert_string+0xcd>
mov    -0x60(%rbp),%rdi
call   1040 <iconv_close@plt>
mov    -0x50(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x50(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

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

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

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