#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);
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;
  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)
    return (NULL);
  iconv_t conv = iconv_open("UTF-8", "LATIN1");
  if (conv == (iconv_t)-1)
    return (NULL);
  status = iconv(conv, (char **)&src, &srclen, &dst, &dstlen);
  iconv_close(conv);
  if (status == (size_t)-1)
    return (NULL);
  if (len)
    *len = strlen(dst2);
  return ((char *)dst2);
}


x86 assembly (clang, -O0):
<U_Latin1ToUtf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     117c <U_Latin1ToUtf8+0x2c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    118e <U_Latin1ToUtf8+0x3e>
mov    -0x10(%rbp),%rdi
call   1040 <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   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cc <U_Latin1ToUtf8+0x7c>
movq   $0x0,-0x8(%rbp)
jmp    125f <U_Latin1ToUtf8+0x10f>
lea    0xe2d(%rip),%rdi
lea    0xe2c(%rip),%rsi
call   1070 <iconv_open@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0xffffffffffffffff,-0x50(%rbp)
jne    11fb <U_Latin1ToUtf8+0xab>
movq   $0x0,-0x8(%rbp)
jmp    125f <U_Latin1ToUtf8+0x10f>
mov    -0x50(%rbp),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1030 <iconv@plt>
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
call   1060 <iconv_close@plt>
cmpq   $0xffffffffffffffff,-0x48(%rbp)
jne    1239 <U_Latin1ToUtf8+0xe9>
movq   $0x0,-0x8(%rbp)
jmp    125f <U_Latin1ToUtf8+0x10f>
cmpq   $0x0,-0x20(%rbp)
je     1257 <U_Latin1ToUtf8+0x107>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%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,0x20(%rsp)
test   %rsi,%rsi
jne    1173 <U_Latin1ToUtf8+0x23>
call   1040 <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   1050 <calloc@plt>
mov    %rax,0x18(%rsp)
test   %rax,%rax
je     11f9 <U_Latin1ToUtf8+0xa9>
mov    %rax,%rbx
lea    0xe5d(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   1070 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     11f9 <U_Latin1ToUtf8+0xa9>
mov    %rax,%r15
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rax,%rdi
call   1030 <iconv@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1060 <iconv_close@plt>
cmp    $0xffffffffffffffff,%r12
je     11f9 <U_Latin1ToUtf8+0xa9>
test   %r14,%r14
je     11fb <U_Latin1ToUtf8+0xab>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,(%r14)
jmp    11fb <U_Latin1ToUtf8+0xab>
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 i8*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
%14 = load i64, i64* %6
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i64, i64* %6
store i64 %17, i64* %10
br label %23
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
%21 = add nsw i32 %20, 1
%22 = sext i32 %21 to i64
store i64 %22, i64* %10
br label %23
23: ; preds = %18, %16
%24 = load i64, i64* %10
%25 = mul i64 2, %24
%26 = add i64 1, %25
store i64 %26, i64* %11
%27 = load i64, i64* %11
%28 = call i8* @calloc(i64 %27, i32 1)
store i8* %28, i8** %8
store i8* %28, i8** %9
%29 = load i8*, i8** %8
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %23
store i8* null, i8** %4
br label %55
32: ; preds = %23
%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 %37
36: ; preds = %32
store i8* null, i8** %4
br label %55
37: ; preds = %32
%38 = load i64, i64* %13
%39 = call i64 @iconv(i64 %38, i8** %5, i64* %10, i8** %8, i64* %11)
store i64 %39, i64* %12
%40 = load i64, i64* %13
%41 = call i32 @iconv_close(i64 %40)
%42 = load i64, i64* %12
%43 = icmp eq i64 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %37
store i8* null, i8** %4
br label %55
45: ; preds = %37
%46 = load i64*, i64** %7
%47 = icmp ne i64* %46, null
br i1 %47, label %48, label %53
48: ; preds = %45
%49 = load i8*, i8** %9
%50 = call i32 @strlen(i8* %49)
%51 = sext i32 %50 to i64
%52 = load i64*, i64** %7
store i64 %51, i64* %52
br label %53
53: ; preds = %48, %45
%54 = load i8*, i8** %9
store i8* %54, i8** %4
br label %55
55: ; preds = %53, %44, %36, %31
%56 = load i8*, i8** %4
ret i8* %56
}

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
store i8* %0, i8** %4
%8 = bitcast i8** %5 to i8*
%9 = bitcast i64* %6 to i8*
%10 = bitcast i64* %7 to i8*
%11 = icmp eq i64 %1, 0
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = tail call i32 @strlen(i8* %0)
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
br label %16
16: ; preds = %3, %12
%17 = phi i64 [ %15, %12 ], [ %1, %3 ]
store i64 %17, i64* %6
%18 = shl i64 %17, 1
%19 = or i64 %18, 1
store i64 %19, i64* %7
%20 = tail call i8* @calloc(i64 %19, i32 1)
store i8* %20, i8** %5
%21 = icmp eq i8* %20, null
br i1 %21, label %34, label %22
22: ; preds = %16
%23 = 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))
%24 = icmp eq i64 %23, -1
br i1 %24, label %34, label %25
25: ; preds = %22
%26 = call i64 @iconv(i64 %23, i8** %4, i64* %6, i8** %5, i64* %7)
%27 = call i32 @iconv_close(i64 %23)
%28 = icmp eq i64 %26, -1
br i1 %28, label %34, label %29
29: ; preds = %25
%30 = icmp eq i64* %2, null
br i1 %30, label %34, label %31
31: ; preds = %29
%32 = call i32 @strlen(i8* %20)
%33 = sext i32 %32 to i64
store i64 %33, i64* %2
br label %34
34: ; preds = %22, %25, %31, %29, %16
%35 = phi i8* [ null, %16 ], [ null, %22 ], [ null, %25 ], [ %20, %31 ], [ %20, %29 ]
ret i8* %35
}