#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 *utf8tostr(char *cs_to, char *str) {
  char *result = NULL;
  if (str == NULL) {
    return result;
  }
  if (cs_to == NULL) {
    return result;
  }
  size_t len_in = strlen(str) + 1;
  if (strcmp(cs_to, "UTF-8") == 0) {
    result = (char *)xmalloc(len_in);
    strcpy(result, str);
  } else {
    iconv_t cd = iconv_open(cs_to, "UTF-8");
    if (cd == (iconv_t)-1) {
    } else {
      size_t len_out = len_in;
      result = (char *)xzalloc(len_out);
      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):
<utf8tostr>:
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 <utf8tostr+0x30>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12bb <utf8tostr+0x12b>
cmpq   $0x0,-0x10(%rbp)
jne    11d8 <utf8tostr+0x48>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12bb <utf8tostr+0x12b>
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 <utf8tostr+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    12b3 <utf8tostr+0x123>
mov    -0x10(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   10b0 <iconv_open@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1247 <utf8tostr+0xb7>
jmp    12ae <utf8tostr+0x11e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%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    12a5 <utf8tostr+0x115>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <iconv_close@plt>
jmp    12b3 <utf8tostr+0x123>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8tostr>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
xor    %r12d,%r12d
test   %rdi,%rdi
je     1261 <utf8tostr+0xd1>
mov    %rsi,%r14
test   %rsi,%rsi
je     1261 <utf8tostr+0xd1>
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     1246 <utf8tostr+0xb6>
lea    0xe1c(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     125e <utf8tostr+0xce>
mov    %rax,%r15
mov    %r12,0x18(%rsp)
mov    %r12,%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    123c <utf8tostr+0xac>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r15,%rdi
call   1090 <iconv_close@plt>
jmp    1261 <utf8tostr+0xd1>
mov    %r12,%rdi
call   1070 <xmalloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
jmp    1261 <utf8tostr+0xd1>
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* @utf8tostr(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 %63
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 %63
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 %61
37: ; preds = %22
%38 = load i8*, i8** %4
%39 = call i64 @iconv_open(i8* %38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
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 %60
43: ; preds = %37
%44 = load i64, i64* %7
store i64 %44, i64* %9
%45 = load i64, i64* %9
%46 = call i64 @xzalloc(i64 %45)
%47 = inttoptr i64 %46 to i8*
store i8* %47, i8** %6
%48 = load i8*, i8** %5
store i8* %48, i8** %10
%49 = load i8*, i8** %6
store i8* %49, i8** %11
%50 = load i64, i64* %8
%51 = call i64 @iconv(i64 %50, i8** %10, i64* %7, i8** %11, i64* %9)
store i64 %51, i64* %12
%52 = load i64, i64* %12
%53 = icmp eq i64 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %43
%55 = load i8*, i8** %6
%56 = call i32 @free(i8* %55)
store i8* null, i8** %6
br label %57
57: ; preds = %54, %43
%58 = load i64, i64* %8
%59 = call i32 @iconv_close(i64 %58)
br label %60
60: ; preds = %57, %42
br label %61
61: ; preds = %60, %30
%62 = load i8*, i8** %6
store i8* %62, i8** %3
br label %63
63: ; preds = %61, %20, %15
%64 = load i8*, i8** %3
ret i8* %64
}

LLVM IR (-O2):
@.str = [6 x i8] c"UTF-8\00"
define i8* @utf8tostr(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 %39, 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 %37
21: ; preds = %10
%22 = tail call i64 @iconv_open(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i64 %22, -1
br i1 %23, label %37, label %24
24: ; preds = %21
%25 = bitcast i64* %4 to i8*
store i64 %14, i64* %4
%26 = tail call i64 @xzalloc(i64 %14)
%27 = inttoptr i64 %26 to i8*
%28 = bitcast i8** %5 to i8*
store i8* %1, i8** %5
%29 = bitcast i8** %6 to i8*
store i8* %27, i8** %6
%30 = call i64 @iconv(i64 %22, i8** %5, i64* %3, i8** %6, i64* %4)
%31 = icmp eq i64 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = call i32 @free(i8* %27)
br label %34
34: ; preds = %32, %24
%35 = phi i8* [ null, %32 ], [ %27, %24 ]
%36 = call i32 @iconv_close(i64 %22)
br label %37
37: ; preds = %34, %21, %17
%38 = phi i8* [ %19, %17 ], [ null, %21 ], [ %35, %34 ]
br label %39
39: ; preds = %2, %37
%40 = phi i8* [ %38, %37 ], [ null, %2 ]
ret i8* %40
}