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

/* Variables and functions */
size_t iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char *, char const *);
char *realloc(char *, size_t);
int convert_encoding(const char *encoding, char *intext, size_t insize,
                     char **buffer, size_t *outsize) {
  char *outbuf = *buffer;
  size_t outleft = *outsize;
  char *inbuf = intext;
  size_t inleft = insize;
  iconv_t icd = iconv_open("UTF-8", encoding);
  if (icd == (iconv_t)-1)
    return 0;
  if (iconv(icd, &inbuf, &inleft, &outbuf, &outleft) == (size_t)-1) {
    iconv_close(icd);
    return 0;
  }
  iconv_close(icd);
  *outsize -= outleft;
  char *tmp = realloc(*buffer, *outsize);
  if (tmp != NULL)
    *buffer = tmp;
  return 1;
}


x86 assembly (clang, -O0):
<convert_encoding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe73(%rip),%rdi
call   1060 <iconv_open@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0xffffffffffffffff,-0x58(%rbp)
jne    11ad <convert_encoding+0x6d>
movl   $0x0,-0x4(%rbp)
jmp    1233 <convert_encoding+0xf3>
mov    -0x58(%rbp),%rdi
lea    -0x48(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x38(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1030 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
jne    11e5 <convert_encoding+0xa5>
mov    -0x58(%rbp),%rdi
call   1050 <iconv_close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1233 <convert_encoding+0xf3>
mov    -0x58(%rbp),%rdi
call   1050 <iconv_close@plt>
mov    -0x40(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     122c <convert_encoding+0xec>
mov    -0x60(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_encoding>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8,%r12
mov    %rcx,%r14
mov    %rdi,%rax
mov    (%rcx),%rcx
mov    %rcx,0x18(%rsp)
mov    (%r8),%rcx
mov    %rcx,(%rsp)
mov    %rsi,0x10(%rsp)
mov    %rdx,0x8(%rsp)
lea    0xe8b(%rip),%rdi
mov    %rax,%rsi
call   1060 <iconv_open@plt>
xor    %r15d,%r15d
cmp    $0xffffffffffffffff,%rax
je     11d6 <convert_encoding+0x96>
mov    %rax,%rbp
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0x18(%rsp),%rcx
mov    %rsp,%r8
mov    %rax,%rdi
call   1030 <iconv@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1050 <iconv_close@plt>
cmp    $0xffffffffffffffff,%rbx
je     11d6 <convert_encoding+0x96>
mov    (%r12),%rsi
sub    (%rsp),%rsi
mov    %rsi,(%r12)
mov    (%r14),%rdi
call   1040 <realloc@plt>
mov    $0x1,%r15d
test   %rax,%rax
je     11d6 <convert_encoding+0x96>
mov    %rax,(%r14)
mov    %r15d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"UTF-8\00"
define i32 @convert_encoding(i8* %0, i8* %1, i64 %2, i8** nocapture %3, i64* nocapture %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = bitcast i8** %6 to i8*
%11 = load i8*, i8** %3
store i8* %11, i8** %6
%12 = bitcast i64* %7 to i8*
%13 = load i64, i64* %4
store i64 %13, i64* %7
%14 = bitcast i8** %8 to i8*
store i8* %1, i8** %8
%15 = bitcast i64* %9 to i8*
store i64 %2, i64* %9
%16 = tail call i64 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0)
%17 = icmp eq i64 %16, -1
br i1 %17, label %30, label %18
18: ; preds = %5
%19 = call i64 @iconv(i64 %16, i8** %8, i64* %9, i8** %6, i64* %7)
%20 = icmp eq i64 %19, -1
%21 = call i32 @iconv_close(i64 %16)
br i1 %20, label %30, label %22
22: ; preds = %18
%23 = load i64, i64* %7
%24 = load i64, i64* %4
%25 = sub i64 %24, %23
store i64 %25, i64* %4
%26 = load i8*, i8** %3
%27 = call i8* @realloc(i8* %26, i64 %25)
%28 = icmp eq i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %22
store i8* %27, i8** %3
br label %30
30: ; preds = %18, %22, %29, %5
%31 = phi i32 [ 0, %5 ], [ 1, %29 ], [ 1, %22 ], [ 0, %18 ]
ret i32 %31
}