#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 */
int /*<<< orphan*/ free(char *);
size_t iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char const *, char const *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
char *strdup(char const *);
int code_convert(const char *from_charset, const char *to_charset,
                 const char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
  iconv_t cd;
  size_t rc;
  char *tmp = strdup(inbuf);
  char *tmp_p = tmp;
  if (tmp == NULL)
    return -1;
  cd = iconv_open(to_charset, from_charset);
  if (cd == 0) {
    free(tmp);
    return -1;
  }
  memset(outbuf, 0, outlen);
  rc = iconv(cd, &tmp_p, &inlen, &outbuf, &outlen);
  free(tmp);
  iconv_close(cd);
  return (rc == (size_t)-1) ? -1 : 0;
}


x86 assembly (clang, -O0):
<code_convert>:
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    %r9,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    11ac <code_convert+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <code_convert+0xd0>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <iconv_open@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11dd <code_convert+0x7d>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <code_convert+0xd0>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x40(%rbp),%rdi
lea    -0x58(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   1040 <iconv@plt>
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1060 <iconv_close@plt>
mov    -0x48(%rbp),%rdx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0xffffffffffffffff,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<code_convert>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r14
mov    %r8,%r15
mov    %rsi,%r13
mov    %rdi,%r12
mov    %rcx,0x20(%rsp)
mov    %r8,0x18(%rsp)
mov    %r9,0x10(%rsp)
mov    %rdx,%rdi
call   1070 <strdup@plt>
mov    %rax,0x8(%rsp)
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1208 <code_convert+0xa8>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %r12,%rsi
call   1080 <iconv_open@plt>
test   %rax,%rax
je     1200 <code_convert+0xa0>
mov    %rax,%r12
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1050 <memset@plt>
lea    0x8(%rsp),%rsi
lea    0x20(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %r12,%rdi
call   1040 <iconv@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1060 <iconv_close@plt>
xor    %ebp,%ebp
cmp    $0xffffffffffffffff,%r14
sete   %bpl
neg    %ebp
jmp    1208 <code_convert+0xa8>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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