#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 */
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 *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ xmalloc(size_t);
scalar_t__ xzalloc(size_t);
char *strtoutf8(char *cs_from, char *str) {
  char *result = NULL;
  if (str == NULL) {
    return result;
  }
  if (cs_from == NULL) {
    return result;
  }
  size_t len_in = strlen(str) + 1;
  if (strcmp(cs_from, "UTF-8") == 0) {
    result = (char *)xmalloc(len_in);
    strcpy(result, str);
  } else {
    iconv_t cd = iconv_open("UTF-8", cs_from);
    if (cd == (iconv_t)-1) {
    } else {
      size_t len_out = len_in * 4;
      result = (char *)xzalloc(len_out + 1);
      char *p_in = str, *p_out = result;
      size_t nconv = iconv(cd, &p_in, &len_in, &p_out, &len_out);
      if (nconv == (size_t)-1) {
        free(result);
        result = NULL;
      }
      iconv_close(cd);
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<strtoutf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c0 <strtoutf8+0x30>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c3 <strtoutf8+0x133>
cmpq   $0x0,-0x10(%rbp)
jne    11d8 <strtoutf8+0x48>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c3 <strtoutf8+0x133>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1223 <strtoutf8+0x93>
mov    -0x28(%rbp),%rdi
call   1070 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    12bb <strtoutf8+0x12b>
mov    -0x10(%rbp),%rsi
lea    0xdd2(%rip),%rdi
call   10b0 <iconv_open@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1247 <strtoutf8+0xb7>
jmp    12b6 <strtoutf8+0x126>
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
call   10a0 <xzalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x48(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   1050 <iconv@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0xffffffffffffffff,-0x50(%rbp)
jne    12ad <strtoutf8+0x11d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <iconv_close@plt>
jmp    12bb <strtoutf8+0x12b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtoutf8>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
xor    %r12d,%r12d
test   %rdi,%rdi
je     126e <strtoutf8+0xde>
mov    %rsi,%r14
test   %rsi,%rsi
je     126e <strtoutf8+0xde>
mov    %rdi,%rbx
mov    %r14,%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r12
mov    %r12,0x20(%rsp)
lea    0xe30(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1253 <strtoutf8+0xc3>
lea    0xe1c(%rip),%rdi
mov    %rbx,%rsi
call   10b0 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     126b <strtoutf8+0xdb>
mov    %rax,%r15
lea    0x0(,%r12,4),%rax
mov    %rax,0x18(%rsp)
lea    0x1(,%r12,4),%rdi
call   10a0 <xzalloc@plt>
mov    %rax,%r12
mov    %r14,0x10(%rsp)
mov    %rax,0x8(%rsp)
lea    0x10(%rsp),%rsi
lea    0x20(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x18(%rsp),%r8
mov    %r15,%rdi
call   1050 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
jne    1249 <strtoutf8+0xb9>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r15,%rdi
call   1090 <iconv_close@plt>
jmp    126e <strtoutf8+0xde>
mov    %r12,%rdi
call   1070 <xmalloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
jmp    126e <strtoutf8+0xde>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"UTF-8\00"
define i8* @strtoutf8(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i8*, i8** %6
store i8* %16, i8** %3
br label %65
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i8*, i8** %6
store i8* %21, i8** %3
br label %65
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %24, 1
%26 = sext i32 %25 to i64
store i64 %26, i64* %7
%27 = load i8*, i8** %4
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %22
%31 = load i64, i64* %7
%32 = call i64 @xmalloc(i64 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = load i8*, i8** %5
%36 = call i32 @strcpy(i8* %34, i8* %35)
br label %63
37: ; preds = %22
%38 = load i8*, i8** %4
%39 = call i64 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %38)
store i64 %39, i64* %8
%40 = load i64, i64* %8
%41 = icmp eq i64 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %37
br label %62
43: ; preds = %37
%44 = load i64, i64* %7
%45 = mul i64 %44, 4
store i64 %45, i64* %9
%46 = load i64, i64* %9
%47 = add i64 %46, 1
%48 = call i64 @xzalloc(i64 %47)
%49 = inttoptr i64 %48 to i8*
store i8* %49, i8** %6
%50 = load i8*, i8** %5
store i8* %50, i8** %10
%51 = load i8*, i8** %6
store i8* %51, i8** %11
%52 = load i64, i64* %8
%53 = call i64 @iconv(i64 %52, i8** %10, i64* %7, i8** %11, i64* %9)
store i64 %53, i64* %12
%54 = load i64, i64* %12
%55 = icmp eq i64 %54, -1
br i1 %55, label %56, label %59
56: ; preds = %43
%57 = load i8*, i8** %6
%58 = call i32 @free(i8* %57)
store i8* null, i8** %6
br label %59
59: ; preds = %56, %43
%60 = load i64, i64* %8
%61 = call i32 @iconv_close(i64 %60)
br label %62
62: ; preds = %59, %42
br label %63
63: ; preds = %62, %30
%64 = load i8*, i8** %6
store i8* %64, i8** %3
br label %65
65: ; preds = %63, %20, %15
%66 = load i8*, i8** %3
ret i8* %66
}

LLVM IR (-O2):
@.str = [6 x i8] c"UTF-8\00"
define i8* @strtoutf8(i8* %0, i8* %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = icmp eq i8* %1, null
%8 = icmp eq i8* %0, null
%9 = or i1 %8, %7
br i1 %9, label %41, label %10
10: ; preds = %2
%11 = bitcast i64* %3 to i8*
%12 = tail call i32 @strlen(i8* %1)
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
store i64 %14, i64* %3
%15 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = tail call i64 @xmalloc(i64 %14)
%19 = inttoptr i64 %18 to i8*
%20 = tail call i32 @strcpy(i8* %19, i8* %1)
br label %39
21: ; preds = %10
%22 = tail call i64 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0)
%23 = icmp eq i64 %22, -1
br i1 %23, label %39, label %24
24: ; preds = %21
%25 = bitcast i64* %4 to i8*
%26 = shl nsw i64 %14, 2
store i64 %26, i64* %4
%27 = or i64 %26, 1
%28 = tail call i64 @xzalloc(i64 %27)
%29 = inttoptr i64 %28 to i8*
%30 = bitcast i8** %5 to i8*
store i8* %1, i8** %5
%31 = bitcast i8** %6 to i8*
store i8* %29, i8** %6
%32 = call i64 @iconv(i64 %22, i8** %5, i64* %3, i8** %6, i64* %4)
%33 = icmp eq i64 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = call i32 @free(i8* %29)
br label %36
36: ; preds = %34, %24
%37 = phi i8* [ null, %34 ], [ %29, %24 ]
%38 = call i32 @iconv_close(i64 %22)
br label %39
39: ; preds = %36, %21, %17
%40 = phi i8* [ %19, %17 ], [ null, %21 ], [ %37, %36 ]
br label %41
41: ; preds = %2, %39
%42 = phi i8* [ %40, %39 ], [ null, %2 ]
ret i8* %42
}