#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 */
char *calloc(size_t, int);
int /*<<< orphan*/ free(char *);
size_t iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char *, char *);
int strlen(char const *);
char *U_Latin1ToUtf8(const char *src, size_t max, size_t *len) {
  char *dst, *dst2;
  char *src2 = (char *)src;
  size_t srclen, dstlen, status;
  if (max) {
    srclen = max;
  } else {
    srclen = strlen(src) + 1;
  }
  dstlen = (1 + 2 * srclen);
  dst2 = dst = calloc(dstlen, 1);
  if (dst) {
    iconv_t conv = iconv_open("UTF-8", "LATIN1");
    if (conv == (iconv_t)-1) {
      free(dst2);
      dst2 = NULL;
    } else {
      status = iconv(conv, (char **)&src2, &srclen, &dst, &dstlen);
      iconv_close(conv);
      if (status == (size_t)-1) {
        free(dst2);
        dst2 = NULL;
      } else if (len) {
        *len = strlen(dst2);
      }
    }
  }
  return ((char *)dst2);
}


x86 assembly (clang, -O0):
<U_Latin1ToUtf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1194 <U_Latin1ToUtf8+0x34>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11a6 <U_Latin1ToUtf8+0x46>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1283 <U_Latin1ToUtf8+0x123>
lea    0xe22(%rip),%rdi
lea    0xe21(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0xffffffffffffffff,-0x50(%rbp)
jne    120f <U_Latin1ToUtf8+0xaf>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
jmp    127e <U_Latin1ToUtf8+0x11e>
mov    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x20(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1040 <iconv@plt>
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
call   1070 <iconv_close@plt>
cmpq   $0xffffffffffffffff,-0x48(%rbp)
jne    1256 <U_Latin1ToUtf8+0xf6>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
jmp    1279 <U_Latin1ToUtf8+0x119>
cmpq   $0x0,-0x18(%rbp)
je     1274 <U_Latin1ToUtf8+0x114>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1279 <U_Latin1ToUtf8+0x119>
jmp    127e <U_Latin1ToUtf8+0x11e>
jmp    1283 <U_Latin1ToUtf8+0x123>
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<U_Latin1ToUtf8>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rdi,0x18(%rsp)
test   %rsi,%rsi
jne    1183 <U_Latin1ToUtf8+0x23>
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rsi
mov    %rsi,0x10(%rsp)
lea    (%rsi,%rsi,1),%rdi
add    $0x1,%rdi
mov    %rdi,0x8(%rsp)
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,0x20(%rsp)
test   %rax,%rax
je     1211 <U_Latin1ToUtf8+0xb1>
mov    %rax,%rbx
lea    0xe4d(%rip),%rdi
lea    0xe4c(%rip),%rsi
call   1080 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     1209 <U_Latin1ToUtf8+0xa9>
mov    %rax,%r15
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x20(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rax,%rdi
call   1040 <iconv@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1070 <iconv_close@plt>
cmp    $0xffffffffffffffff,%r12
je     1209 <U_Latin1ToUtf8+0xa9>
test   %r14,%r14
je     1213 <U_Latin1ToUtf8+0xb3>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
mov    %rax,(%r14)
jmp    1213 <U_Latin1ToUtf8+0xb3>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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