#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 *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
char *strdup(char *);
size_t strlen(char const *);
char *r_str_iconv(const char *str, int *len) {
  iconv_t cd;
  char *inbuf = (char *)str;
  size_t inbytesleft = strlen(str);
  char data[1024];
  char *outbuf = (char *)&data;
  size_t rest, outbytesleft = 1024;
  rest = outbytesleft;
  cd = iconv_open("UTF-8", "UTF-8");
  if (cd == (iconv_t)-1) {
    perror("iconv_open");
    return NULL;
  }
  size_t s = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
  iconv(cd, NULL, NULL, &outbuf, &outbytesleft);
  rest -= outbytesleft;
  data[rest] = 0;
  printf("S = %d (%s)\n", (int)rest, data);
  iconv_close(cd);
  if (len)
    *len = rest;
  return strdup(data);
}


x86 assembly (clang, -O0):
<r_str_iconv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x438(%rbp)
movq   $0x400,-0x448(%rbp)
mov    -0x448(%rbp),%rax
mov    %rax,-0x440(%rbp)
lea    0xe3a(%rip),%rsi
mov    %rsi,%rdi
call   1090 <iconv_open@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11f6 <r_str_iconv+0x86>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b0 <r_str_iconv+0x140>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x438(%rbp),%rcx
lea    -0x448(%rbp),%r8
call   1030 <iconv@plt>
mov    %rax,-0x450(%rbp)
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x438(%rbp),%rcx
lea    -0x448(%rbp),%r8
mov    %rdx,%rsi
call   1030 <iconv@plt>
mov    -0x448(%rbp),%rcx
mov    -0x440(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x440(%rbp)
mov    -0x440(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
mov    -0x440(%rbp),%rax
mov    %eax,%esi
lea    -0x430(%rbp),%rdx
lea    0xd99(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1060 <iconv_close@plt>
cmpq   $0x0,-0x18(%rbp)
je     12a0 <r_str_iconv+0x130>
mov    -0x440(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x430(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<r_str_iconv>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%r14
mov    %rdi,0x18(%rsp)
call   1040 <strlen@plt>
mov    %rax,0x10(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x8(%rsp)
movq   $0x400,(%rsp)
lea    0xe57(%rip),%rdi
mov    %rdi,%rsi
call   1090 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     1236 <r_str_iconv+0xc6>
mov    %rax,%r15
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rbx
mov    %rsp,%r12
mov    %rax,%rdi
mov    %rbx,%rcx
mov    %r12,%r8
call   1030 <iconv@plt>
mov    %r15,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %rbx,%rcx
mov    %r12,%r8
call   1030 <iconv@plt>
mov    $0x400,%ebx
sub    (%rsp),%rbx
movb   $0x0,0x20(%rsp,%rbx,1)
lea    0xe10(%rip),%rdi
lea    0x20(%rsp),%rdx
mov    %ebx,%esi
call   1050 <printf@plt>
mov    %r15,%rdi
call   1060 <iconv_close@plt>
test   %r14,%r14
je     121d <r_str_iconv+0xad>
mov    %ebx,(%r14)
lea    0x20(%rsp),%rdi
call   1080 <strdup@plt>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdc9(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
jmp    1227 <r_str_iconv+0xb7>

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

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