#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);
int /*<<< orphan*/ free(char *);
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;
  char *src2 = (char *)src;
  size_t srclen, dstlen, status;
  if (max) {
    srclen = max;
  } else {
    srclen = strlen(src) + 1;
  }
  dstlen = (1 + srclen);
  dst2 = dst = calloc(dstlen, 1);
  if (dst) {
    iconv_t conv = iconv_open("LATIN1//TRANSLIT", "UTF-8");
    if (conv == (iconv_t)-1) {
      free(dst2);
      dst2 = NULL;
    } else {
      status = iconv(conv, (char **)&src2, &srclen, &dst, &dstlen);
      iconv_close(conv);
      if (status == (size_t)-1) {
        free(dst2);
        dst2 = NULL;
      } else if (len) {
        *len = strlen(dst2);
      }
    }
  }
  return ((char *)dst2);
}


x86 assembly (clang, -O0):
<U_Utf8ToLatin1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1194 <U_Utf8ToLatin1+0x34>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11a6 <U_Utf8ToLatin1+0x46>
mov    -0x8(%rbp),%rdi
call   1050 <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   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     127f <U_Utf8ToLatin1+0x11f>
lea    0xe26(%rip),%rdi
lea    0xe30(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0xffffffffffffffff,-0x50(%rbp)
jne    120b <U_Utf8ToLatin1+0xab>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
jmp    127a <U_Utf8ToLatin1+0x11a>
mov    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x20(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1040 <iconv@plt>
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
call   1070 <iconv_close@plt>
cmpq   $0xffffffffffffffff,-0x48(%rbp)
jne    1252 <U_Utf8ToLatin1+0xf2>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
jmp    1275 <U_Utf8ToLatin1+0x115>
cmpq   $0x0,-0x18(%rbp)
je     1270 <U_Utf8ToLatin1+0x110>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1275 <U_Utf8ToLatin1+0x115>
jmp    127a <U_Utf8ToLatin1+0x11a>
jmp    127f <U_Utf8ToLatin1+0x11f>
mov    -0x28(%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,0x18(%rsp)
test   %rsi,%rsi
jne    1183 <U_Utf8ToLatin1+0x23>
call   1050 <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   1060 <calloc@plt>
mov    %rax,0x20(%rsp)
test   %rax,%rax
je     1210 <U_Utf8ToLatin1+0xb0>
mov    %rax,%rbx
lea    0xe4e(%rip),%rdi
lea    0xe58(%rip),%rsi
call   1080 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     1208 <U_Utf8ToLatin1+0xa8>
mov    %rax,%r15
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x20(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rax,%rdi
call   1040 <iconv@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1070 <iconv_close@plt>
cmp    $0xffffffffffffffff,%r12
je     1208 <U_Utf8ToLatin1+0xa8>
test   %r14,%r14
je     1212 <U_Utf8ToLatin1+0xb2>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
mov    %rax,(%r14)
jmp    1212 <U_Utf8ToLatin1+0xb2>
mov    %rbx,%rdi
call   1030 <free@plt>
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 i64
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64* %2, i64** %6
%14 = load i8*, i8** %4
store i8* %14, i8** %9
%15 = load i64, i64* %5
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i64, i64* %5
store i64 %18, i64* %10
br label %24
19: ; preds = %3
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
store i64 %23, i64* %10
br label %24
24: ; preds = %19, %17
%25 = load i64, i64* %10
%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** %7
store i8* %28, i8** %8
%29 = load i8*, i8** %7
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %59
31: ; preds = %24
%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 %38
35: ; preds = %31
%36 = load i8*, i8** %8
%37 = call i32 @free(i8* %36)
store i8* null, i8** %8
br label %58
38: ; preds = %31
%39 = load i64, i64* %13
%40 = call i64 @iconv(i64 %39, i8** %9, i64* %10, i8** %7, i64* %11)
store i64 %40, i64* %12
%41 = load i64, i64* %13
%42 = call i32 @iconv_close(i64 %41)
%43 = load i64, i64* %12
%44 = icmp eq i64 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = load i8*, i8** %8
%47 = call i32 @free(i8* %46)
store i8* null, i8** %8
br label %57
48: ; preds = %38
%49 = load i64*, i64** %6
%50 = icmp ne i64* %49, null
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i8*, i8** %8
%53 = call i32 @strlen(i8* %52)
%54 = sext i32 %53 to i64
%55 = load i64*, i64** %6
store i64 %54, i64* %55
br label %56
56: ; preds = %51, %48
br label %57
57: ; preds = %56, %45
br label %58
58: ; preds = %57, %35
br label %59
59: ; preds = %58, %24
%60 = load i8*, i8** %8
ret i8* %60
}

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