#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*/ errno;
int iconv(int /*<<< orphan*/ *, char **, size_t *, char **, size_t *);
int iconv_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *iconv_open(char const *, char const *);
int /*<<< orphan*/ printf(char *, char const *, char const *, char *);
char *strerror(int /*<<< orphan*/);
int convert(const char *tocode, const char *fromcode, char *inbufp,
            size_t inbytesleft, char *outbufp, size_t outbytesleft) {
  iconv_t *ic;
  size_t outbytes = outbytesleft;
  int ret;
  ic = iconv_open(tocode, fromcode);
  if (ic == (iconv_t *)-1) {
    printf("iconv_open failed: from: %s, to: %s: %s", fromcode, tocode,
           strerror(errno));
    return -1;
  }
  while (inbytesleft > 0) {
    ret = iconv(ic, &inbufp, &inbytesleft, &outbufp, &outbytes);
    if (ret == -1) {
      printf("iconv failed: from: %s, to: %s: %s", fromcode, tocode,
             strerror(errno));
      return -1;
    }
  }
  ret = iconv_close(ic);
  if (ret == -1) {
    printf("iconv_close failed: from: %s, to: %s: %s", fromcode, tocode,
           strerror(errno));
    return -1;
  }
  return outbytesleft - outbytes;
}


x86 assembly (clang, -O0):
<convert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%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    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <iconv_open@plt>
mov    %rax,-0x40(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x40(%rbp)
jne    11de <convert+0x8e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x60(%rbp),%rsi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xe33(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <convert+0x16c>
jmp    11e3 <convert+0x93>
cmpq   $0x0,-0x28(%rbp)
jbe    125a <convert+0x10a>
mov    -0x40(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   1030 <iconv@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    1255 <convert+0x105>
mov    -0x18(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x70(%rbp),%rsi
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xde4(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <convert+0x16c>
jmp    11e3 <convert+0x93>
mov    -0x40(%rbp),%rdi
call   1050 <iconv_close@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    12b1 <convert+0x161>
mov    -0x18(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x80(%rbp),%rsi
mov    -0x78(%rbp),%rdx
mov    %rax,%rcx
lea    0xdab(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <convert+0x16c>
mov    -0x38(%rbp),%rax
sub    -0x48(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9,%r12
mov    %rcx,%rbp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rdx,0x30(%rsp)
mov    %rcx,0x20(%rsp)
mov    %r8,0x28(%rsp)
mov    %r9,0x18(%rsp)
call   1070 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     11e5 <convert+0x95>
mov    %rax,%rbx
mov    %r15,0x8(%rsp)
mov    %r14,0x10(%rsp)
test   %rbp,%rbp
je     11d1 <convert+0x81>
lea    0x30(%rsp),%r13
lea    0x20(%rsp),%rbp
lea    0x28(%rsp),%r14
lea    0x18(%rsp),%r15
nop
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
mov    %r15,%r8
call   1030 <iconv@plt>
cmp    $0xffffffff,%eax
je     1202 <convert+0xb2>
cmpq   $0x0,0x20(%rsp)
jne    11b0 <convert+0x60>
mov    %rbx,%rdi
call   1050 <iconv_close@plt>
cmp    $0xffffffff,%eax
je     1219 <convert+0xc9>
sub    0x18(%rsp),%r12d
jmp    1246 <convert+0xf6>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe06(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
jmp    1238 <convert+0xe8>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe11(%rip),%rdi
jmp    122e <convert+0xde>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe1d(%rip),%rdi
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
mov    %rax,%rcx
call   1040 <printf@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"iconv_open failed: from: %s, to: %s: %s\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"iconv failed: from: %s, to: %s: %s\00"
@.str.2 = [41 x i8] c"iconv_close failed: from: %s, to: %s: %s\00"
define i32 @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 i32*
%15 = alloca i64
%16 = alloca i32
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
%17 = load i64, i64* %13
store i64 %17, i64* %15
%18 = load i8*, i8** %8
%19 = load i8*, i8** %9
%20 = call i32* @iconv_open(i8* %18, i8* %19)
store i32* %20, i32** %14
%21 = load i32*, i32** %14
%22 = icmp eq i32* %21, inttoptr (i64 -1 to i32*)
br i1 %22, label %23, label %29
23: ; preds = %6
%24 = load i8*, i8** %9
%25 = load i8*, i8** %8
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %24, i8* %25, i8* %27)
store i32 -1, i32* %7
br label %61
29: ; preds = %6
br label %30
30: ; preds = %44, %29
%31 = load i64, i64* %11
%32 = icmp ugt i64 %31, 0
br i1 %32, label %33, label %45
33: ; preds = %30
%34 = load i32*, i32** %14
%35 = call i32 @iconv(i32* %34, i8** %10, i64* %11, i8** %12, i64* %15)
store i32 %35, i32* %16
%36 = load i32, i32* %16
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i8*, i8** %9
%40 = load i8*, i8** %8
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %39, i8* %40, i8* %42)
store i32 -1, i32* %7
br label %61
44: ; preds = %33
br label %30
45: ; preds = %30
%46 = load i32*, i32** %14
%47 = call i32 @iconv_close(i32* %46)
store i32 %47, i32* %16
%48 = load i32, i32* %16
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %56
50: ; preds = %45
%51 = load i8*, i8** %9
%52 = load i8*, i8** %8
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %51, i8* %52, i8* %54)
store i32 -1, i32* %7
br label %61
56: ; preds = %45
%57 = load i64, i64* %13
%58 = load i64, i64* %15
%59 = sub i64 %57, %58
%60 = trunc i64 %59 to i32
store i32 %60, i32* %7
br label %61
61: ; preds = %56, %50, %38, %23
%62 = load i32, i32* %7
ret i32 %62
}

LLVM IR (-O2):
@.str = [40 x i8] c"iconv_open failed: from: %s, to: %s: %s\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"iconv failed: from: %s, to: %s: %s\00"
@.str.2 = [41 x i8] c"iconv_close failed: from: %s, to: %s: %s\00"
define i32 @convert(i8* %0, i8* %1, i8* %2, i64 %3, i8* %4, i64 %5) {
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
store i8* %2, i8** %7
store i64 %3, i64* %8
store i8* %4, i8** %9
%11 = bitcast i64* %10 to i8*
store i64 %5, i64* %10
%12 = tail call i32* @iconv_open(i8* %0, i8* %1)
%13 = icmp eq i32* %12, inttoptr (i64 -1 to i32*)
br i1 %13, label %16, label %14
14: ; preds = %6
%15 = icmp eq i64 %3, 0
br i1 %15, label %30, label %20
16: ; preds = %6
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %0, i8* %18)
br label %41
20: ; preds = %14, %23
%21 = call i32 @iconv(i32* %12, i8** %7, i64* %8, i8** %9, i64* %10)
%22 = icmp eq i32 %21, -1
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i64, i64* %8
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %20
26: ; preds = %20
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0, i8* %28)
br label %41
30: ; preds = %23, %14
%31 = call i32 @iconv_close(i32* %12)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %0, i8* %35)
br label %41
37: ; preds = %30
%38 = load i64, i64* %10
%39 = sub i64 %5, %38
%40 = trunc i64 %39 to i32
br label %41
41: ; preds = %37, %33, %26, %16
%42 = phi i32 [ -1, %16 ], [ -1, %26 ], [ -1, %33 ], [ %40, %37 ]
ret i32 %42
}