#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 */
#define E2BIG 130
#define EILSEQ 129
#define EINVAL 128
int errno;
int /*<<< orphan*/ free(char *);
size_t iconv(int /*<<< orphan*/, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(int /*<<< orphan*/);
int /*<<< orphan*/ iconv_open(char const *, char const *);
char *malloc(size_t);
int /*<<< orphan*/ memset(char *, char, int);
char *realloc(char *, size_t);
char *mz_convert(const char *string, size_t length, const char *to_charset,
                 const char *from_charset, size_t *bytes_read,
                 size_t *bytes_written) {
  iconv_t cd;
  char *dest = NULL;
  const char *src;
  char *outp;
  size_t src_left, dest_left;
  size_t ret;
  size_t outbuf_size;
  bool error = false;
  cd = iconv_open(to_charset, from_charset);
  src = string;
  outbuf_size = length + 1;
  dest_left = outbuf_size - 1;
  src_left = length;
  outp = dest = malloc(outbuf_size);
  do {
    ret = iconv(cd, (char **)&src, &src_left, &outp, &dest_left);
    if (ret == -1) {
      switch (errno) {
      case E2BIG: {
        size_t used = outp - dest;
        outbuf_size *= 2;
        dest = realloc(dest, outbuf_size);
        outp = dest + used;
        dest_left = outbuf_size - used - 1;
      } break;
      case EINVAL:
      case EILSEQ:
      default:
        error = true;
        break;
      }
    }
  } while (ret == -1 && !error);
  iconv_close(cd);
  if (ret == -1) {
    free(dest);
    return NULL;
  }
  memset(outp, '\0', 1);
  return dest;
}


x86 assembly (clang, -O0):
<mz_convert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%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)
movq   $0x0,-0x48(%rbp)
movl   $0x0,-0x7c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <iconv_open@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x78(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    %rax,-0x58(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x50(%rbp),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x58(%rbp),%rcx
lea    -0x68(%rbp),%r8
call   1040 <iconv@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0xffffffffffffffff,-0x70(%rbp)
jne    12af <mz_convert+0x13f>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
add    $0xffffff80,%eax
sub    $0x2,%eax
jb     129e <mz_convert+0x12e>
jmp    1232 <mz_convert+0xc2>
mov    -0x8c(%rbp),%eax
sub    $0x82,%eax
jne    12a3 <mz_convert+0x133>
jmp    1248 <mz_convert+0xd8>
mov    -0x58(%rbp),%rax
mov    -0x48(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x88(%rbp)
mov    -0x78(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x78(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x78(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
add    -0x88(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x78(%rbp),%rax
sub    -0x88(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x68(%rbp)
jmp    12aa <mz_convert+0x13a>
jmp    12a3 <mz_convert+0x133>
movl   $0x1,-0x7c(%rbp)
jmp    12af <mz_convert+0x13f>
jmp    12b4 <mz_convert+0x144>
xor    %eax,%eax
cmpq   $0xffffffffffffffff,-0x70(%rbp)
mov    %al,-0x8d(%rbp)
jne    12d6 <mz_convert+0x166>
cmpl   $0x0,-0x7c(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x8d(%rbp)
mov    -0x8d(%rbp),%al
test   $0x1,%al
jne    11eb <mz_convert+0x7b>
mov    -0x3c(%rbp),%edi
call   1080 <iconv_close@plt>
cmpq   $0xffffffffffffffff,-0x70(%rbp)
jne    130d <mz_convert+0x19d>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1325 <mz_convert+0x1b5>
mov    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1050 <memset@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mz_convert>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rdx,%rdi
mov    %rcx,%rsi
call   1090 <iconv_open@plt>
mov    %eax,%r14d
mov    %rbp,0x20(%rsp)
lea    0x1(%rbx),%rbp
mov    %rbx,0x10(%rsp)
mov    %rbx,0x18(%rsp)
mov    %rbp,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,0x8(%rsp)
lea    0x20(%rsp),%rsi
lea    0x18(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %r14d,%edi
call   1040 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
jne    1249 <mz_convert+0xd9>
lea    0x10(%rsp),%r15
nopl   0x0(%rax)
mov    0x2df9(%rip),%rax
cmpl   $0x82,(%rax)
jne    1264 <mz_convert+0xf4>
mov    0x8(%rsp),%r13
sub    %rbx,%r13
mov    %rbp,%r12
add    %rbp,%r12
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <realloc@plt>
mov    %rax,%rbx
add    %r13,%rax
mov    %rax,0x8(%rsp)
not    %r13
lea    0x0(,%rbp,2),%rax
add    %r13,%rax
mov    %rax,0x10(%rsp)
mov    %r14d,%edi
lea    0x20(%rsp),%rsi
lea    0x18(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %r15,%r8
call   1040 <iconv@plt>
mov    %r12,%rbp
cmp    $0xffffffffffffffff,%rax
je     11e0 <mz_convert+0x70>
mov    %r14d,%edi
call   1080 <iconv_close@plt>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1050 <memset@plt>
jmp    1276 <mz_convert+0x106>
mov    %r14d,%edi
call   1080 <iconv_close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i8* @mz_convert(i8* %0, i64 %1, i8* %2, i8* %3, i64* %4, i64* %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64*
%13 = alloca i64*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i8*
%18 = alloca i64
%19 = alloca i64
%20 = alloca i64
%21 = alloca i64
%22 = alloca i32
%23 = alloca i64
store i8* %0, i8** %8
store i64 %1, i64* %9
store i8* %2, i8** %10
store i8* %3, i8** %11
store i64* %4, i64** %12
store i64* %5, i64** %13
store i8* null, i8** %15
store i32 0, i32* %22
%24 = load i8*, i8** %10
%25 = load i8*, i8** %11
%26 = call i32 @iconv_open(i8* %24, i8* %25)
store i32 %26, i32* %14
%27 = load i8*, i8** %8
store i8* %27, i8** %16
%28 = load i64, i64* %9
%29 = add i64 %28, 1
store i64 %29, i64* %21
%30 = load i64, i64* %21
%31 = sub i64 %30, 1
store i64 %31, i64* %19
%32 = load i64, i64* %9
store i64 %32, i64* %18
%33 = load i64, i64* %21
%34 = call i8* @malloc(i64 %33)
store i8* %34, i8** %15
store i8* %34, i8** %17
br label %35
35: ; preds = %71, %6
%36 = load i32, i32* %14
%37 = call i64 @iconv(i32 %36, i8** %16, i64* %18, i8** %17, i64* %19)
store i64 %37, i64* %20
%38 = load i64, i64* %20
%39 = icmp eq i64 %38, -1
br i1 %39, label %40, label %63
40: ; preds = %35
%41 = load i32, i32* @errno
switch i32 %41, label %61 [
i32 130, label %42
i32 128, label %60
i32 129, label %60
]
42: ; preds = %40
%43 = load i8*, i8** %17
%44 = load i8*, i8** %15
%45 = ptrtoint i8* %43 to i64
%46 = ptrtoint i8* %44 to i64
%47 = sub i64 %45, %46
store i64 %47, i64* %23
%48 = load i64, i64* %21
%49 = mul i64 %48, 2
store i64 %49, i64* %21
%50 = load i8*, i8** %15
%51 = load i64, i64* %21
%52 = call i8* @realloc(i8* %50, i64 %51)
store i8* %52, i8** %15
%53 = load i8*, i8** %15
%54 = load i64, i64* %23
%55 = getelementptr inbounds i8, i8* %53, i64 %54
store i8* %55, i8** %17
%56 = load i64, i64* %21
%57 = load i64, i64* %23
%58 = sub i64 %56, %57
%59 = sub i64 %58, 1
store i64 %59, i64* %19
br label %62
60: ; preds = %40, %40
br label %61
61: ; preds = %40, %60
store i32 1, i32* %22
br label %62
62: ; preds = %61, %42
br label %63
63: ; preds = %62, %35
br label %64
64: ; preds = %63
%65 = load i64, i64* %20
%66 = icmp eq i64 %65, -1
br i1 %66, label %67, label %71
67: ; preds = %64
%68 = load i32, i32* %22
%69 = icmp ne i32 %68, 0
%70 = xor i1 %69, true
br label %71
71: ; preds = %67, %64
%72 = phi i1 [ false, %64 ], [ %70, %67 ]
br i1 %72, label %35, label %73
73: ; preds = %71
%74 = load i32, i32* %14
%75 = call i32 @iconv_close(i32 %74)
%76 = load i64, i64* %20
%77 = icmp eq i64 %76, -1
br i1 %77, label %78, label %81
78: ; preds = %73
%79 = load i8*, i8** %15
%80 = call i32 @free(i8* %79)
store i8* null, i8** %7
br label %85
81: ; preds = %73
%82 = load i8*, i8** %17
%83 = call i32 @memset(i8* %82, i8 signext 0, i32 1)
%84 = load i8*, i8** %15
store i8* %84, i8** %7
br label %85
85: ; preds = %81, %78
%86 = load i8*, i8** %7
ret i8* %86
}

LLVM IR (-O2):
@errno = global i32 0
define i8* @mz_convert(i8* %0, i64 %1, i8* %2, i8* %3, i64* nocapture readnone %4, i64* nocapture readnone %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = bitcast i8** %7 to i8*
%12 = bitcast i8** %8 to i8*
%13 = bitcast i64* %9 to i8*
%14 = bitcast i64* %10 to i8*
%15 = tail call i32 @iconv_open(i8* %2, i8* %3)
store i8* %0, i8** %7
%16 = add i64 %1, 1
store i64 %1, i64* %10
store i64 %1, i64* %9
%17 = tail call i8* @malloc(i64 %16)
store i8* %17, i8** %8
%18 = call i64 @iconv(i32 %15, i8** %7, i64* %9, i8** %8, i64* %10)
%19 = icmp eq i64 %18, -1
br i1 %19, label %20, label %40
20: ; preds = %6, %28
%21 = phi i64 [ %33, %28 ], [ %16, %6 ]
%22 = phi i8* [ %34, %28 ], [ %17, %6 ]
%23 = load i32, i32* @errno
%24 = icmp eq i32 %23, 130
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = call i32 @iconv_close(i32 %15)
%27 = call i32 @free(i8* %22)
br label %45
28: ; preds = %20
%29 = load i8*, i8** %8
%30 = ptrtoint i8* %29 to i64
%31 = ptrtoint i8* %22 to i64
%32 = sub i64 %30, %31
%33 = shl i64 %21, 1
%34 = call i8* @realloc(i8* %22, i64 %33)
%35 = getelementptr inbounds i8, i8* %34, i64 %32
store i8* %35, i8** %8
%36 = xor i64 %32, -1
%37 = add i64 %33, %36
store i64 %37, i64* %10
%38 = call i64 @iconv(i32 %15, i8** %7, i64* %9, i8** %8, i64* %10)
%39 = icmp eq i64 %38, -1
br i1 %39, label %20, label %40
40: ; preds = %28, %6
%41 = phi i8* [ %17, %6 ], [ %34, %28 ]
%42 = call i32 @iconv_close(i32 %15)
%43 = load i8*, i8** %8
%44 = call i32 @memset(i8* %43, i8 signext 0, i32 1)
br label %45
45: ; preds = %40, %25
%46 = phi i8* [ null, %25 ], [ %41, %40 ]
ret i8* %46
}