#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
char *realloc(char *, size_t);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int strlen(char const *const);
int /*<<< orphan*/ ut_handle_error_message(char *, unsigned long,
                                           int /*<<< orphan*/);
const char *latin1ToUtf8(const char *const latin1String) {
  static char *utf8String = NULL;
  static size_t bufSize = 0;
  size_t size;
  const unsigned char *in;
  unsigned char *out;
  assert(latin1String != NULL);
  size = 2 * strlen(latin1String) + 1;
  if (size > bufSize) {
    char *buf = realloc(utf8String, size);
    if (buf != NULL) {
      utf8String = buf;
      bufSize = size;
    } else {
      ut_handle_error_message("Couldn't allocate %ld-byte buffer: %s",
                              (unsigned long)size, strerror(errno));
      return NULL;
    }
  }
  for (in = (const unsigned char *)latin1String,
      out = (unsigned char *)utf8String;
       *in; ++in) {
    if ((((*in) & 0x80) == 0)) {
      *out++ = *in;
    } else {
      *out++ = 0xC0 | ((0xC0 & *in) >> 6);
      *out++ = 0x80 | (0x3F & *in);
    }
  }
  *out = 0;
  return utf8String;
}


x86 assembly (clang, -O0):
<latin1ToUtf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
shl    $0x1,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    0x2eca(%rip),%rax
jbe    1208 <latin1ToUtf8+0xb8>
mov    0x2eb5(%rip),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11ce <latin1ToUtf8+0x7e>
mov    -0x30(%rbp),%rax
mov    %rax,0x2e92(%rip)
mov    -0x18(%rbp),%rax
mov    %rax,0x2e8f(%rip)
jmp    1203 <latin1ToUtf8+0xb3>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe0f(%rip),%rdi
call   1060 <ut_handle_error_message@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ca <latin1ToUtf8+0x17a>
jmp    1208 <latin1ToUtf8+0xb8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e39(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     12b8 <latin1ToUtf8+0x168>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1259 <latin1ToUtf8+0x109>
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    12a2 <latin1ToUtf8+0x152>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
and    $0xc0,%eax
sar    $0x6,%eax
or     $0xc0,%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    12a7 <latin1ToUtf8+0x157>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    121b <latin1ToUtf8+0xcb>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2d8a(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<latin1ToUtf8>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %eax,%eax
add    $0x1,%eax
movslq %eax,%r14
mov    0x2ed4(%rip),%rax
cmp    %r14,0x2ed5(%rip)
jae    11a3 <latin1ToUtf8+0x53>
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     11f5 <latin1ToUtf8+0xa5>
mov    %rax,0x2eb4(%rip)
mov    %r14,0x2eb5(%rip)
mov    (%rbx),%dl
mov    %rax,%rcx
test   %dl,%dl
je     11f0 <latin1ToUtf8+0xa0>
add    $0x1,%rbx
mov    %rax,%rcx
jmp    11e4 <latin1ToUtf8+0x94>
cs nopw 0x0(%rax,%rax,1)
nop
shr    $0x6,%dl
or     $0xc0,%dl
mov    %dl,(%rcx)
movzbl -0x1(%rbx),%edx
and    $0x3f,%dl
or     $0x80,%dl
mov    %dl,0x1(%rcx)
add    $0x2,%rcx
movzbl (%rbx),%edx
add    $0x1,%rbx
test   %dl,%dl
je     11f0 <latin1ToUtf8+0xa0>
test   %dl,%dl
js     11c0 <latin1ToUtf8+0x70>
mov    %dl,(%rcx)
add    $0x1,%rcx
jmp    11d9 <latin1ToUtf8+0x89>
movb   $0x0,(%rcx)
jmp    1216 <latin1ToUtf8+0xc6>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdf6(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <ut_handle_error_message@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@latin1ToUtf8.utf8String = internal global i8* null
@latin1ToUtf8.bufSize = internal global i64 0
@.str = [38 x i8] c"Couldn't allocate %ld-byte buffer: %s\00"
@errno = global i32 0
define i8* @latin1ToUtf8(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
%14 = mul nsw i32 2, %13
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
store i64 %16, i64* %4
%17 = load i64, i64* %4
%18 = load i64, i64* @latin1ToUtf8.bufSize
%19 = icmp ugt i64 %17, %18
br i1 %19, label %20, label %35
20: ; preds = %1
%21 = load i8*, i8** @latin1ToUtf8.utf8String
%22 = load i64, i64* %4
%23 = call i8* @realloc(i8* %21, i64 %22)
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i8*, i8** %7
store i8* %27, i8** @latin1ToUtf8.utf8String
%28 = load i64, i64* %4
store i64 %28, i64* @latin1ToUtf8.bufSize
br label %34
29: ; preds = %20
%30 = load i64, i64* %4
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @ut_handle_error_message(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i64 %30, i32 %32)
store i8* null, i8** %2
br label %78
34: ; preds = %26
br label %35
35: ; preds = %34, %1
%36 = load i8*, i8** %3
store i8* %36, i8** %5
%37 = load i8*, i8** @latin1ToUtf8.utf8String
store i8* %37, i8** %6
br label %38
38: ; preds = %72, %35
%39 = load i8*, i8** %5
%40 = load i8, i8* %39
%41 = icmp ne i8 %40, 0
br i1 %41, label %42, label %75
42: ; preds = %38
%43 = load i8*, i8** %5
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = and i32 %45, 128
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %42
%49 = load i8*, i8** %5
%50 = load i8, i8* %49
%51 = load i8*, i8** %6
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %6
store i8 %50, i8* %51
br label %71
53: ; preds = %42
%54 = load i8*, i8** %5
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = and i32 192, %56
%58 = ashr i32 %57, 6
%59 = or i32 192, %58
%60 = trunc i32 %59 to i8
%61 = load i8*, i8** %6
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %6
store i8 %60, i8* %61
%63 = load i8*, i8** %5
%64 = load i8, i8* %63
%65 = zext i8 %64 to i32
%66 = and i32 63, %65
%67 = or i32 128, %66
%68 = trunc i32 %67 to i8
%69 = load i8*, i8** %6
%70 = getelementptr inbounds i8, i8* %69, i32 1
store i8* %70, i8** %6
store i8 %68, i8* %69
br label %71
71: ; preds = %53, %48
br label %72
72: ; preds = %71
%73 = load i8*, i8** %5
%74 = getelementptr inbounds i8, i8* %73, i32 1
store i8* %74, i8** %5
br label %38
75: ; preds = %38
%76 = load i8*, i8** %6
store i8 0, i8* %76
%77 = load i8*, i8** @latin1ToUtf8.utf8String
store i8* %77, i8** %2
br label %78
78: ; preds = %75, %29
%79 = load i8*, i8** %2
ret i8* %79
}

LLVM IR (-O2):
@latin1ToUtf8.utf8String = internal unnamed_addr global i8* null
@latin1ToUtf8.bufSize = internal unnamed_addr global i64 0
@.str = [38 x i8] c"Couldn't allocate %ld-byte buffer: %s\00"
@errno = global i32 0
define i8* @latin1ToUtf8(i8* %0) {
%2 = icmp ne i8* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32 @strlen(i8* %0)
%6 = shl nsw i32 %5, 1
%7 = or i32 %6, 1
%8 = sext i32 %7 to i64
%9 = load i64, i64* @latin1ToUtf8.bufSize
%10 = icmp ult i64 %9, %8
%11 = load i8*, i8** @latin1ToUtf8.utf8String
br i1 %10, label %12, label %20
12: ; preds = %1
%13 = tail call i8* @realloc(i8* %11, i64 %8)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 @ut_handle_error_message(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i64 %8, i32 %17)
br label %46
19: ; preds = %12
store i8* %13, i8** @latin1ToUtf8.utf8String
store i64 %8, i64* @latin1ToUtf8.bufSize
br label %20
20: ; preds = %1, %19
%21 = phi i8* [ %13, %19 ], [ %11, %1 ]
%22 = load i8, i8* %0
%23 = icmp eq i8 %22, 0
br i1 %23, label %44, label %24
24: ; preds = %20, %39
%25 = phi i8 [ %42, %39 ], [ %22, %20 ]
%26 = phi i8* [ %40, %39 ], [ %21, %20 ]
%27 = phi i8* [ %41, %39 ], [ %0, %20 ]
%28 = icmp sgt i8 %25, -1
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = getelementptr inbounds i8, i8* %26, i64 1
store i8 %25, i8* %26
br label %39
31: ; preds = %24
%32 = lshr i8 %25, 6
%33 = or i8 %32, -64
%34 = getelementptr inbounds i8, i8* %26, i64 1
store i8 %33, i8* %26
%35 = load i8, i8* %27
%36 = and i8 %35, 63
%37 = or i8 %36, -128
%38 = getelementptr inbounds i8, i8* %26, i64 2
store i8 %37, i8* %34
br label %39
39: ; preds = %29, %31
%40 = phi i8* [ %30, %29 ], [ %38, %31 ]
%41 = getelementptr inbounds i8, i8* %27, i64 1
%42 = load i8, i8* %41
%43 = icmp eq i8 %42, 0
br i1 %43, label %44, label %24
44: ; preds = %39, %20
%45 = phi i8* [ %21, %20 ], [ %40, %39 ]
store i8 0, i8* %45
br label %46
46: ; preds = %15, %44
%47 = phi i8* [ %21, %44 ], [ null, %15 ]
ret i8* %47
}