#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 */

/* Variables and functions */
unsigned char *latin1ToUtf8(unsigned char);
unsigned char *malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
unsigned char *realloc(unsigned char *, int);
int strlen(unsigned char *);
unsigned char *latin1StrToUtf8Dup(unsigned char *latin1) {
  int len = strlen(latin1) * 2 + 1;
  unsigned char *ret = malloc(len);
  unsigned char *cp = ret;
  unsigned char *utf8;
  memset(ret, 0, len);
  len = 0;
  while (*latin1) {
    utf8 = latin1ToUtf8(*latin1);
    while (*utf8) {
      *(cp++) = *(utf8++);
      len++;
    }
    latin1++;
  }
  return realloc(ret, len + 1);
}


x86 assembly (clang, -O0):
<latin1StrToUtf8Dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
shl    $0x1,%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1202 <latin1StrToUtf8Dup+0xb2>
mov    -0x8(%rbp),%rax
movzbl (%rax),%edi
call   1070 <latin1ToUtf8@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     11f1 <latin1StrToUtf8Dup+0xa1>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11b4 <latin1StrToUtf8Dup+0x64>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1197 <latin1StrToUtf8Dup+0x47>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
call   1060 <realloc@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<latin1StrToUtf8Dup>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r12
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%r15d
add    $0x1,%r15d
mov    %r15d,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1040 <memset@plt>
mov    (%r12),%al
test   %al,%al
je     11d6 <latin1StrToUtf8Dup+0x86>
xor    %ebp,%ebp
mov    %r14,%rbx
jmp    11ad <latin1StrToUtf8Dup+0x5d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x1(%r12),%al
add    $0x1,%r12
test   %al,%al
je     11d6 <latin1StrToUtf8Dup+0x86>
movzbl %al,%edi
call   1070 <latin1ToUtf8@plt>
mov    (%rax),%cl
test   %cl,%cl
je     11a0 <latin1StrToUtf8Dup+0x50>
add    $0x1,%rax
nop
mov    %cl,(%rbx)
add    $0x1,%rbx
add    $0x1,%ebp
movzbl (%rax),%ecx
add    $0x1,%rax
test   %cl,%cl
jne    11c0 <latin1StrToUtf8Dup+0x70>
jmp    11a0 <latin1StrToUtf8Dup+0x50>
add    $0x1,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <realloc@plt>

LLVM IR (-O0):
define i8* @latin1StrToUtf8Dup(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = mul nsw i32 %8, 2
%10 = add nsw i32 %9, 1
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
store i8* %13, i8** %5
%14 = load i8*, i8** %4
%15 = load i32, i32* %3
%16 = call i32 @memset(i8* %14, i32 0, i32 %15)
store i32 0, i32* %3
br label %17
17: ; preds = %37, %1
%18 = load i8*, i8** %2
%19 = load i8, i8* %18
%20 = icmp ne i8 %19, 0
br i1 %20, label %21, label %40
21: ; preds = %17
%22 = load i8*, i8** %2
%23 = load i8, i8* %22
%24 = call i8* @latin1ToUtf8(i8 zeroext %23)
store i8* %24, i8** %6
br label %25
25: ; preds = %29, %21
%26 = load i8*, i8** %6
%27 = load i8, i8* %26
%28 = icmp ne i8 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %25
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %6
%32 = load i8, i8* %30
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %5
store i8 %32, i8* %33
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 1
store i32 %36, i32* %3
br label %25
37: ; preds = %25
%38 = load i8*, i8** %2
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %2
br label %17
40: ; preds = %17
%41 = load i8*, i8** %4
%42 = load i32, i32* %3
%43 = add nsw i32 %42, 1
%44 = call i8* @realloc(i8* %41, i32 %43)
ret i8* %44
}

LLVM IR (-O2):
define i8* @latin1StrToUtf8Dup(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = shl nsw i32 %2, 1
%4 = or i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = tail call i32 @memset(i8* %5, i32 0, i32 %4)
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %33, label %9
9: ; preds = %1, %27
%10 = phi i8 [ %31, %27 ], [ %7, %1 ]
%11 = phi i8* [ %29, %27 ], [ %5, %1 ]
%12 = phi i8* [ %30, %27 ], [ %0, %1 ]
%13 = phi i32 [ %28, %27 ], [ 0, %1 ]
%14 = tail call i8* @latin1ToUtf8(i8 zeroext %10)
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %9, %17
%18 = phi i8 [ %25, %17 ], [ %15, %9 ]
%19 = phi i8* [ %22, %17 ], [ %14, %9 ]
%20 = phi i8* [ %23, %17 ], [ %11, %9 ]
%21 = phi i32 [ %24, %17 ], [ %13, %9 ]
%22 = getelementptr inbounds i8, i8* %19, i64 1
%23 = getelementptr inbounds i8, i8* %20, i64 1
store i8 %18, i8* %20
%24 = add nsw i32 %21, 1
%25 = load i8, i8* %22
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %17
27: ; preds = %17, %9
%28 = phi i32 [ %13, %9 ], [ %24, %17 ]
%29 = phi i8* [ %11, %9 ], [ %23, %17 ]
%30 = getelementptr inbounds i8, i8* %12, i64 1
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %33, label %9
33: ; preds = %27, %1
%34 = phi i32 [ 0, %1 ], [ %28, %27 ]
%35 = add nsw i32 %34, 1
%36 = tail call i8* @realloc(i8* %5, i32 %35)
ret i8* %36
}