#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*/ uint16_t;
typedef scalar_t__ iconv_t;

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ iconv(scalar_t__, char **, size_t *, char **, size_t *);
scalar_t__ iconv_open(char *, char *);
int /*<<< orphan*/ *malloc(size_t);
int time_iconv(char *srcbuf, size_t srcbufsize) {
  uint16_t *destbuf = NULL;
  size_t destbufsize;
  static uint16_t *origdestbuf;
  static size_t origdestbufsize;
  iconv_t ic = (iconv_t)-1;
  int ret = 0;
  if (ic == (iconv_t)-1) {
    ic = iconv_open("UTF-16LE", "UTF-8");
    if (ic == (iconv_t)-1) {
      ret = -1;
      goto done;
    }
  }
  destbufsize = srcbufsize * sizeof(uint16_t);
  if (destbufsize > origdestbufsize) {
    free(origdestbuf);
    origdestbuf = destbuf = malloc(origdestbufsize = destbufsize);
  } else {
    destbuf = origdestbuf;
  }
  iconv(ic, &srcbuf, &srcbufsize, (char **)&destbuf, &destbufsize);
done:
  return ret;
}


x86 assembly (clang, -O0):
<time_iconv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
movl   $0x0,-0x2c(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11a5 <time_iconv+0x65>
lea    0xe87(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1060 <iconv_open@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11a0 <time_iconv+0x60>
movl   $0xffffffff,-0x2c(%rbp)
jmp    1212 <time_iconv+0xd2>
jmp    11a5 <time_iconv+0x65>
mov    -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    0x2e94(%rip),%rax
jbe    11ee <time_iconv+0xae>
mov    0x2e7f(%rip),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
mov    %rdi,0x2e77(%rip)
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    %rax,0x2e5f(%rip)
jmp    11f9 <time_iconv+0xb9>
mov    0x2e53(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x18(%rbp),%rcx
lea    -0x8(%rbp),%rsi
lea    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%r8
call   1040 <iconv@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_iconv>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %rdi,0x20(%rsp)
mov    %rsi,0x18(%rsp)
lea    0xea5(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1060 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     11ab <time_iconv+0x6b>
mov    %rax,%r14
shl    $0x2,%rbx
mov    %rbx,0x10(%rsp)
mov    0x2ec8(%rip),%rdi
cmp    0x2ec9(%rip),%rbx
jbe    11b2 <time_iconv+0x72>
call   1030 <free@plt>
mov    %rbx,0x2ebb(%rip)
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,0x8(%rsp)
mov    %rax,0x2e9f(%rip)
jmp    11b7 <time_iconv+0x77>
mov    $0xffffffff,%eax
jmp    11d5 <time_iconv+0x95>
mov    %rdi,0x8(%rsp)
lea    0x20(%rsp),%rsi
lea    0x18(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %r14,%rdi
call   1040 <iconv@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@time_iconv.origdestbuf = internal global i32* null
@time_iconv.origdestbufsize = internal global i64 0
@.str = [9 x i8] c"UTF-16LE\00"
@.str.1 = [6 x i8] c"UTF-8\00"
define i32 @time_iconv(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32* null, i32** %5
store i64 -1, i64* %7
store i32 0, i32* %8
%9 = load i64, i64* %7
%10 = icmp eq i64 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = call i64 @iconv_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i64 %12, i64* %7
%13 = load i64, i64* %7
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 -1, i32* %8
br label %34
16: ; preds = %11
br label %17
17: ; preds = %16, %2
%18 = load i64, i64* %4
%19 = mul i64 %18, 4
store i64 %19, i64* %6
%20 = load i64, i64* %6
%21 = load i64, i64* @time_iconv.origdestbufsize
%22 = icmp ugt i64 %20, %21
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i32*, i32** @time_iconv.origdestbuf
%25 = call i32 @free(i32* %24)
%26 = load i64, i64* %6
store i64 %26, i64* @time_iconv.origdestbufsize
%27 = call i32* @malloc(i64 %26)
store i32* %27, i32** %5
store i32* %27, i32** @time_iconv.origdestbuf
br label %30
28: ; preds = %17
%29 = load i32*, i32** @time_iconv.origdestbuf
store i32* %29, i32** %5
br label %30
30: ; preds = %28, %23
%31 = load i64, i64* %7
%32 = bitcast i32** %5 to i8**
%33 = call i32 @iconv(i64 %31, i8** %3, i64* %4, i8** %32, i64* %6)
br label %34
34: ; preds = %30, %15
%35 = load i32, i32* %8
ret i32 %35
}

LLVM IR (-O2):
@time_iconv.origdestbuf = internal unnamed_addr global i32* null
@time_iconv.origdestbufsize = internal unnamed_addr global i64 0
@.str = [9 x i8] c"UTF-16LE\00"
@.str.1 = [6 x i8] c"UTF-8\00"
define i32 @time_iconv(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = bitcast i32** %5 to i8*
%8 = bitcast i64* %6 to i8*
%9 = tail call i64 @iconv_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i64 %9, -1
br i1 %10, label %23, label %11
11: ; preds = %2
%12 = shl i64 %1, 2
store i64 %12, i64* %6
%13 = load i64, i64* @time_iconv.origdestbufsize
%14 = icmp ugt i64 %12, %13
%15 = load i32*, i32** @time_iconv.origdestbuf
br i1 %14, label %16, label %19
16: ; preds = %11
%17 = tail call i32 @free(i32* %15)
store i64 %12, i64* @time_iconv.origdestbufsize
%18 = tail call i32* @malloc(i64 %12)
store i32* %18, i32** %5
store i32* %18, i32** @time_iconv.origdestbuf
br label %20
19: ; preds = %11
store i32* %15, i32** %5
br label %20
20: ; preds = %19, %16
%21 = bitcast i32** %5 to i8**
%22 = call i32 @iconv(i64 %9, i8** %3, i64* %4, i8** %21, i64* %6)
br label %23
23: ; preds = %2, %20
%24 = phi i32 [ 0, %20 ], [ -1, %2 ]
ret i32 %24
}