#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*/ free(char *);
size_t iconv(int /*<<< orphan*/, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, unsigned int);
int _conv(iconv_t cd, char **p_str, unsigned int *p_len, char *ostr,
          unsigned int olen) {
  char *inbuf, *outbuf;
  size_t r, inlen, outlen;
  inbuf = *p_str;
  inlen = *p_len;
  outbuf = ostr;
  outlen = olen;
  iconv(cd, NULL, NULL, NULL, NULL);
  r = iconv(cd, &inbuf, &inlen, &outbuf, &outlen);
  if (r == (size_t)-1)
    return -1;
  *p_len = olen - outlen;
  free(*p_str);
  *p_str = ostr;
  outbuf = realloc(*p_str, *p_len + 1);
  if (!outbuf)
    perror("realloc");
  else
    *p_str = outbuf;
  (*p_str)[*p_len] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<_conv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rsi
mov    %r8,%rdx
mov    %r8,%rcx
call   1040 <iconv@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x38(%rbp),%rcx
lea    -0x50(%rbp),%r8
call   1040 <iconv@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0xffffffffffffffff,-0x40(%rbp)
jne    11c8 <_conv+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    1246 <_conv+0x106>
mov    -0x24(%rbp),%eax
sub    -0x50(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1223 <_conv+0xe3>
lea    0xde7(%rip),%rdi
call   1060 <perror@plt>
jmp    122e <_conv+0xee>
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_conv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8d,%ebp
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
mov    (%rsi),%rax
mov    %rax,0x18(%rsp)
mov    (%rdx),%eax
mov    %rax,0x10(%rsp)
mov    %rcx,0x8(%rsp)
mov    %r8d,%eax
mov    %rax,(%rsp)
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <iconv@plt>
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rsp,%r8
mov    %ebx,%edi
call   1040 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
je     11d9 <_conv+0x99>
sub    (%rsp),%ebp
mov    %ebp,(%r14)
mov    (%r15),%rdi
call   1030 <free@plt>
mov    %r12,(%r15)
mov    (%r14),%esi
add    $0x1,%esi
mov    %r12,%rdi
call   1050 <realloc@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     11eb <_conv+0xab>
mov    %rax,(%r15)
mov    (%r14),%ecx
movb   $0x0,(%rax,%rcx,1)
xor    %eax,%eax
jmp    11de <_conv+0x9e>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0e(%rip),%rdi
call   1060 <perror@plt>
mov    (%r15),%rax
jmp    11ce <_conv+0x8e>

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

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