#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 */
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
unsigned char *realloc(unsigned char *, int);
int strlen(unsigned char *);
int /*<<< orphan*/ utf8ToLatin1(unsigned char *);
int validateUtf8Char(unsigned char *);
unsigned char *utf8StrToLatin1Dup(unsigned char *utf8) {
  int len = strlen(utf8) + 1;
  unsigned char *ret = malloc(len);
  unsigned char *cp = ret;
  int count;
  memset(ret, 0, len);
  len = 0;
  while (*utf8) {
    count = validateUtf8Char(utf8);
    if (!count) {
      free(ret);
      return NULL;
    }
    *(cp++) = utf8ToLatin1(utf8);
    utf8 += count;
    len++;
  }
  return realloc(ret, len + 1);
}


x86 assembly (clang, -O0):
<utf8StrToLatin1Dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1228 <utf8StrToLatin1Dup+0xb8>
mov    -0x10(%rbp),%rdi
call   1050 <validateUtf8Char@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11ed <utf8StrToLatin1Dup+0x7d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    123b <utf8StrToLatin1Dup+0xcb>
mov    -0x10(%rbp),%rdi
call   1040 <utf8ToLatin1@plt>
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b4 <utf8StrToLatin1Dup+0x44>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1090 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8StrToLatin1Dup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
call   1060 <strlen@plt>
lea    0x1(%rax),%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1070 <memset@plt>
cmpb   $0x0,(%r15)
je     11d1 <utf8StrToLatin1Dup+0x61>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1050 <validateUtf8Char@plt>
test   %eax,%eax
je     11ea <utf8StrToLatin1Dup+0x7a>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <utf8ToLatin1@plt>
mov    %al,(%r14,%rbx,1)
movslq %ebp,%rax
add    $0x1,%rbx
cmpb   $0x0,(%r15,%rax,1)
lea    (%r15,%rax,1),%r15
jne    11a0 <utf8StrToLatin1Dup+0x30>
add    $0x1,%ebx
jmp    11d6 <utf8StrToLatin1Dup+0x66>
mov    $0x1,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <realloc@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @utf8StrToLatin1Dup(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = tail call i32 @memset(i8* %4, i32 0, i32 %3)
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %25, label %8
8: ; preds = %1, %16
%9 = phi i8* [ %19, %16 ], [ %4, %1 ]
%10 = phi i8* [ %21, %16 ], [ %0, %1 ]
%11 = phi i32 [ %22, %16 ], [ 1, %1 ]
%12 = tail call i32 @validateUtf8Char(i8* %10)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @free(i8* %4)
br label %28
16: ; preds = %8
%17 = tail call i32 @utf8ToLatin1(i8* %10)
%18 = trunc i32 %17 to i8
%19 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %18, i8* %9
%20 = sext i32 %12 to i64
%21 = getelementptr inbounds i8, i8* %10, i64 %20
%22 = add i32 %11, 1
%23 = load i8, i8* %21
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %8
25: ; preds = %16, %1
%26 = phi i32 [ 1, %1 ], [ %22, %16 ]
%27 = tail call i8* @realloc(i8* %4, i32 %26)
br label %28
28: ; preds = %25, %14
%29 = phi i8* [ null, %14 ], [ %27, %25 ]
ret i8* %29
}