#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*/ free(char *);
char *str_legacy_to_utf8(char const *, char const *);
scalar_t__ str_need_unescape_unicode(char *);
char *str_unescape_unicode(char *);
char *str_convert_to_unescaped_utf8(const char *string,
                                    const char *legacy_encoding) {
  if (string == NULL || legacy_encoding == NULL)
    return NULL;
  char *utf8_string = str_legacy_to_utf8(string, legacy_encoding);
  if (utf8_string) {
    if (str_need_unescape_unicode(utf8_string)) {
      char *unescaped_utf8 = str_unescape_unicode(utf8_string);
      free(utf8_string);
      return unescaped_utf8;
    } else
      return utf8_string;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<str_convert_to_unescaped_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1166 <str_convert_to_unescaped_utf8+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1173 <str_convert_to_unescaped_utf8+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11da <str_convert_to_unescaped_utf8+0x9a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <str_legacy_to_utf8@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d2 <str_convert_to_unescaped_utf8+0x92>
mov    -0x20(%rbp),%rdi
call   1050 <str_need_unescape_unicode@plt>
cmp    $0x0,%rax
je     11c5 <str_convert_to_unescaped_utf8+0x85>
mov    -0x20(%rbp),%rdi
call   1060 <str_unescape_unicode@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11da <str_convert_to_unescaped_utf8+0x9a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11da <str_convert_to_unescaped_utf8+0x9a>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_convert_to_unescaped_utf8>:
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     1184 <str_convert_to_unescaped_utf8+0x44>
test   %rsi,%rsi
je     1184 <str_convert_to_unescaped_utf8+0x44>
call   1040 <str_legacy_to_utf8@plt>
test   %rax,%rax
je     1182 <str_convert_to_unescaped_utf8+0x42>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <str_need_unescape_unicode@plt>
test   %rax,%rax
je     1184 <str_convert_to_unescaped_utf8+0x44>
mov    %rbx,%rdi
call   1060 <str_unescape_unicode@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
jmp    1187 <str_convert_to_unescaped_utf8+0x47>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @str_convert_to_unescaped_utf8(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %10, %2
store i8* null, i8** %3
br label %33
14: ; preds = %10
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = call i8* @str_legacy_to_utf8(i8* %15, i8* %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %32
20: ; preds = %14
%21 = load i8*, i8** %6
%22 = call i64 @str_need_unescape_unicode(i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = call i8* @str_unescape_unicode(i8* %25)
store i8* %26, i8** %7
%27 = load i8*, i8** %6
%28 = call i32 @free(i8* %27)
%29 = load i8*, i8** %7
store i8* %29, i8** %3
br label %33
30: ; preds = %20
%31 = load i8*, i8** %6
store i8* %31, i8** %3
br label %33
32: ; preds = %14
store i8* null, i8** %3
br label %33
33: ; preds = %32, %30, %24, %13
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
define i8* @str_convert_to_unescaped_utf8(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i8* %1, null
%5 = or i1 %3, %4
br i1 %5, label %15, label %6
6: ; preds = %2
%7 = tail call i8* @str_legacy_to_utf8(i8* %0, i8* %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = tail call i64 @str_need_unescape_unicode(i8* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i8* @str_unescape_unicode(i8* %7)
%14 = tail call i32 @free(i8* %7)
br label %15
15: ; preds = %12, %9, %6, %2
%16 = phi i8* [ null, %2 ], [ %13, %12 ], [ %7, %9 ], [ null, %6 ]
ret i8* %16
}