#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_Utf8ToLatin1(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 + srclen);
  dst2 = dst = calloc(dstlen, 1);
  if (!dst)
    return (NULL);
  iconv_t conv = iconv_open("LATIN1//TRANSLIT", "UTF-8");
  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_Utf8ToLatin1>:
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_Utf8ToLatin1+0x2c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    118e <U_Utf8ToLatin1+0x3e>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%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    11c8 <U_Utf8ToLatin1+0x78>
movq   $0x0,-0x8(%rbp)
jmp    125b <U_Utf8ToLatin1+0x10b>
lea    0xe31(%rip),%rdi
lea    0xe3b(%rip),%rsi
call   1070 <iconv_open@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0xffffffffffffffff,-0x50(%rbp)
jne    11f7 <U_Utf8ToLatin1+0xa7>
movq   $0x0,-0x8(%rbp)
jmp    125b <U_Utf8ToLatin1+0x10b>
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    1235 <U_Utf8ToLatin1+0xe5>
movq   $0x0,-0x8(%rbp)
jmp    125b <U_Utf8ToLatin1+0x10b>
cmpq   $0x0,-0x20(%rbp)
je     1253 <U_Utf8ToLatin1+0x103>
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_Utf8ToLatin1>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rdi,0x20(%rsp)
test   %rsi,%rsi
jne    1173 <U_Utf8ToLatin1+0x23>
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rsi
mov    %rsi,0x10(%rsp)
add    $0x1,%rsi
mov    %rsi,0x8(%rsp)
mov    %rsi,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,0x18(%rsp)
test   %rax,%rax
je     11f8 <U_Utf8ToLatin1+0xa8>
mov    %rax,%rbx
lea    0xe5e(%rip),%rdi
lea    0xe68(%rip),%rsi
call   1070 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     11f8 <U_Utf8ToLatin1+0xa8>
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     11f8 <U_Utf8ToLatin1+0xa8>
test   %r14,%r14
je     11fa <U_Utf8ToLatin1+0xaa>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,(%r14)
jmp    11fa <U_Utf8ToLatin1+0xaa>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [17 x i8] c"LATIN1//TRANSLIT\00"
@.str.1 = [6 x i8] c"UTF-8\00"
define i8* @U_Utf8ToLatin1(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 = add i64 1, %24
store i64 %25, i64* %11
%26 = load i64, i64* %11
%27 = call i8* @calloc(i64 %26, i32 1)
store i8* %27, i8** %8
store i8* %27, i8** %9
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %31, label %30
30: ; preds = %23
store i8* null, i8** %4
br label %54
31: ; preds = %23
%32 = call i64 @iconv_open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i64 %32, i64* %13
%33 = load i64, i64* %13
%34 = icmp eq i64 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %31
store i8* null, i8** %4
br label %54
36: ; preds = %31
%37 = load i64, i64* %13
%38 = call i64 @iconv(i64 %37, i8** %5, i64* %10, i8** %8, i64* %11)
store i64 %38, i64* %12
%39 = load i64, i64* %13
%40 = call i32 @iconv_close(i64 %39)
%41 = load i64, i64* %12
%42 = icmp eq i64 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %36
store i8* null, i8** %4
br label %54
44: ; preds = %36
%45 = load i64*, i64** %7
%46 = icmp ne i64* %45, null
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = load i8*, i8** %9
%49 = call i32 @strlen(i8* %48)
%50 = sext i32 %49 to i64
%51 = load i64*, i64** %7
store i64 %50, i64* %51
br label %52
52: ; preds = %47, %44
%53 = load i8*, i8** %9
store i8* %53, i8** %4
br label %54
54: ; preds = %52, %43, %35, %30
%55 = load i8*, i8** %4
ret i8* %55
}

LLVM IR (-O2):
@.str = [17 x i8] c"LATIN1//TRANSLIT\00"
@.str.1 = [6 x i8] c"UTF-8\00"
define i8* @U_Utf8ToLatin1(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 = add i64 %17, 1
store i64 %18, i64* %7
%19 = tail call i8* @calloc(i64 %18, i32 1)
store i8* %19, i8** %5
%20 = icmp eq i8* %19, null
br i1 %20, label %33, label %21
21: ; preds = %16
%22 = tail call i64 @iconv_open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, -1
br i1 %23, label %33, label %24
24: ; preds = %21
%25 = call i64 @iconv(i64 %22, i8** %4, i64* %6, i8** %5, i64* %7)
%26 = call i32 @iconv_close(i64 %22)
%27 = icmp eq i64 %25, -1
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = icmp eq i64* %2, null
br i1 %29, label %33, label %30
30: ; preds = %28
%31 = call i32 @strlen(i8* %19)
%32 = sext i32 %31 to i64
store i64 %32, i64* %2
br label %33
33: ; preds = %21, %24, %30, %28, %16
%34 = phi i8* [ null, %16 ], [ null, %21 ], [ null, %24 ], [ %19, %30 ], [ %19, %28 ]
ret i8* %34
}