#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 /*<<< orphan*/ err(int, char *, size_t, size_t);
int /*<<< orphan*/ free(char *);
size_t iconv(int /*<<< orphan*/, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *xmalloc(int);
char *xrealloc(char *, size_t);
char *string_do_convert_length(const char *in, iconv_t ic, size_t in_bytes) {
  char *my_in = xmalloc(in_bytes);
  char *my_in_mem = my_in;
  char *out = xmalloc(6 * in_bytes);
  char *ret = out;
  size_t out_bytes = 6 * in_bytes + 1;
  size_t out_bytes_start = out_bytes;
  size_t got;
  memcpy(my_in, in, in_bytes);
  got = iconv(ic, &my_in, &in_bytes, &out, &out_bytes);
  if (in_bytes)
    err(1, "String conversion left %zd bytes, returned %zd", in_bytes, got);
  free(my_in_mem);
  return xrealloc(ret, out_bytes_start - out_bytes);
}


x86 assembly (clang, -O0):
<string_do_convert_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%edi
call   1060 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
imul   $0x6,-0x18(%rbp),%rax
mov    %eax,%edi
call   1060 <xmalloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
imul   $0x6,-0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1040 <iconv@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1208 <string_do_convert_length+0xa8>
mov    -0x18(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    $0x1,%edi
lea    0xdfd(%rip),%rsi
call   1080 <err@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
sub    -0x40(%rbp),%rsi
call   1050 <xrealloc@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_do_convert_length>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%rbp
mov    %esi,%r12d
mov    %rdi,%r13
mov    %rdx,0x10(%rsp)
mov    %ebp,%edi
call   1060 <xmalloc@plt>
mov    %rax,%r15
mov    %rax,0x20(%rsp)
lea    0x0(%rbp,%rbp,1),%eax
lea    (%rax,%rax,2),%edi
call   1060 <xmalloc@plt>
mov    %rax,%r14
mov    %rax,0x18(%rsp)
mov    %rbp,%rax
add    %rbp,%rax
lea    (%rax,%rax,2),%rbx
or     $0x1,%rbx
mov    %rbx,0x8(%rsp)
mov    %r15,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1070 <memcpy@plt>
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %r12d,%edi
call   1040 <iconv@plt>
mov    0x10(%rsp),%rdx
test   %rdx,%rdx
je     11fa <string_do_convert_length+0x9a>
lea    0xe13(%rip),%rsi
mov    $0x1,%edi
mov    %rax,%rcx
call   1080 <err@plt>
mov    %r15,%rdi
call   1030 <free@plt>
sub    0x8(%rsp),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <xrealloc@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [47 x i8] c"String conversion left %zd bytes, returned %zd\00"
define i8* @string_do_convert_length(i8* %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%14 = load i64, i64* %6
%15 = trunc i64 %14 to i32
%16 = call i8* @xmalloc(i32 %15)
store i8* %16, i8** %7
%17 = load i8*, i8** %7
store i8* %17, i8** %8
%18 = load i64, i64* %6
%19 = mul i64 6, %18
%20 = trunc i64 %19 to i32
%21 = call i8* @xmalloc(i32 %20)
store i8* %21, i8** %9
%22 = load i8*, i8** %9
store i8* %22, i8** %10
%23 = load i64, i64* %6
%24 = mul i64 6, %23
%25 = add i64 %24, 1
store i64 %25, i64* %11
%26 = load i64, i64* %11
store i64 %26, i64* %12
%27 = load i8*, i8** %7
%28 = load i8*, i8** %4
%29 = load i64, i64* %6
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
%31 = load i32, i32* %5
%32 = call i64 @iconv(i32 %31, i8** %7, i64* %6, i8** %9, i64* %11)
store i64 %32, i64* %13
%33 = load i64, i64* %6
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %3
%36 = load i64, i64* %6
%37 = load i64, i64* %13
%38 = call i32 @err(i32 1, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i64 %36, i64 %37)
br label %39
39: ; preds = %35, %3
%40 = load i8*, i8** %8
%41 = call i32 @free(i8* %40)
%42 = load i8*, i8** %10
%43 = load i64, i64* %12
%44 = load i64, i64* %11
%45 = sub i64 %43, %44
%46 = call i8* @xrealloc(i8* %42, i64 %45)
ret i8* %46
}

LLVM IR (-O2):
@.str = [47 x i8] c"String conversion left %zd bytes, returned %zd\00"
define i8* @string_do_convert_length(i8* %0, i32 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i64 %2, i64* %4
%8 = bitcast i8** %5 to i8*
%9 = trunc i64 %2 to i32
%10 = tail call i8* @xmalloc(i32 %9)
store i8* %10, i8** %5
%11 = bitcast i8** %6 to i8*
%12 = mul i32 %9, 6
%13 = tail call i8* @xmalloc(i32 %12)
store i8* %13, i8** %6
%14 = bitcast i64* %7 to i8*
%15 = mul i64 %2, 6
%16 = or i64 %15, 1
store i64 %16, i64* %7
%17 = tail call i32 @memcpy(i8* %10, i8* %0, i64 %2)
%18 = call i64 @iconv(i32 %1, i8** %5, i64* %4, i8** %6, i64* %7)
%19 = load i64, i64* %4
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %3
%22 = call i32 @err(i32 1, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i64 %19, i64 %18)
br label %23
23: ; preds = %21, %3
%24 = call i32 @free(i8* %10)
%25 = load i64, i64* %7
%26 = sub i64 %16, %25
%27 = call i8* @xrealloc(i8* %13, i64 %26)
ret i8* %27
}