#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 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*/, int);
int code_convert(const char *from_charset, const char *to_charset, char *inbuf,
                 int inlen, char *outbuf, int outlen) {
  iconv_t cd;
  int rc;
  char **pin = &inbuf;
  char **pout = &outbuf;
  size_t iSrcLen = inlen;
  size_t iOutLen = outlen;
  cd = iconv_open(to_charset, from_charset);
  if (cd == 0)
    return -1;
  memset(outbuf, 0, outlen);
  if (iconv(cd, pin, &iSrcLen, pout, &iOutLen) == -1)
    return -1;
  iconv_close(cd);
  return 0;
}


x86 assembly (clang, -O0):
<code_convert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
movslq -0x24(%rbp),%rax
mov    %rax,-0x60(%rbp)
movslq -0x34(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <iconv_open@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11a7 <code_convert+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <code_convert+0xb3>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rcx
lea    -0x60(%rbp),%rdx
lea    -0x68(%rbp),%r8
call   1030 <iconv@plt>
cmp    $0xffffffff,%eax
jne    11e3 <code_convert+0xa3>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <code_convert+0xb3>
mov    -0x40(%rbp),%rdi
call   1050 <iconv_close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<code_convert>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %r9d,%r15d
mov    %r8,%rbp
mov    %rdi,%rax
mov    %rdx,0x20(%rsp)
mov    %r8,0x18(%rsp)
movslq %ecx,%rcx
mov    %rcx,0x10(%rsp)
movslq %r9d,%rcx
mov    %rcx,0x8(%rsp)
mov    %rsi,%rdi
mov    %rax,%rsi
call   1060 <iconv_open@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11bf <code_convert+0x7f>
mov    %rax,%rbx
mov    %rbp,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1040 <memset@plt>
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rbx,%rdi
call   1030 <iconv@plt>
cmp    $0xffffffff,%eax
je     11bf <code_convert+0x7f>
mov    %rbx,%rdi
call   1050 <iconv_close@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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