#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 *);
char *str_utf8_to_escaped_legacy(char *, char const *);
char *str_convert_to_escaped_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 == NULL)
    return NULL;
  char *legacy_fallback =
      str_utf8_to_escaped_legacy(utf8_string, legacy_encoding);
  free(utf8_string);
  if (legacy_fallback == NULL)
    return NULL;
  utf8_string = str_legacy_to_utf8(legacy_fallback, legacy_encoding);
  free(legacy_fallback);
  return utf8_string;
}


x86 assembly (clang, -O0):
<str_convert_to_escaped_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1156 <str_convert_to_escaped_utf8+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1163 <str_convert_to_escaped_utf8+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <str_convert_to_escaped_utf8+0xb0>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <str_legacy_to_utf8@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118c <str_convert_to_escaped_utf8+0x5c>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <str_convert_to_escaped_utf8+0xb0>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <str_utf8_to_escaped_legacy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11be <str_convert_to_escaped_utf8+0x8e>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <str_convert_to_escaped_utf8+0xb0>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <str_legacy_to_utf8@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_convert_to_escaped_utf8>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1188 <str_convert_to_escaped_utf8+0x58>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1188 <str_convert_to_escaped_utf8+0x58>
mov    %rbx,%rsi
call   1040 <str_legacy_to_utf8@plt>
test   %rax,%rax
je     1188 <str_convert_to_escaped_utf8+0x58>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <str_utf8_to_escaped_legacy@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1030 <free@plt>
test   %r14,%r14
je     1188 <str_convert_to_escaped_utf8+0x58>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <str_legacy_to_utf8@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
jmp    118a <str_convert_to_escaped_utf8+0x5a>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @str_convert_to_escaped_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 %37
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 eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %14
store i8* null, i8** %3
br label %37
21: ; preds = %14
%22 = load i8*, i8** %6
%23 = load i8*, i8** %5
%24 = call i8* @str_utf8_to_escaped_legacy(i8* %22, i8* %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %6
%26 = call i32 @free(i8* %25)
%27 = load i8*, i8** %7
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %21
store i8* null, i8** %3
br label %37
30: ; preds = %21
%31 = load i8*, i8** %7
%32 = load i8*, i8** %5
%33 = call i8* @str_legacy_to_utf8(i8* %31, i8* %32)
store i8* %33, i8** %6
%34 = load i8*, i8** %7
%35 = call i32 @free(i8* %34)
%36 = load i8*, i8** %6
store i8* %36, i8** %3
br label %37
37: ; preds = %30, %29, %20, %13
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
define i8* @str_convert_to_escaped_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 %16, 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 %16, label %9
9: ; preds = %6
%10 = tail call i8* @str_utf8_to_escaped_legacy(i8* %7, i8* %1)
%11 = tail call i32 @free(i8* %7)
%12 = icmp eq i8* %10, null
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = tail call i8* @str_legacy_to_utf8(i8* %10, i8* %1)
%15 = tail call i32 @free(i8* %10)
br label %16
16: ; preds = %6, %9, %13, %2
%17 = phi i8* [ null, %2 ], [ null, %6 ], [ %14, %13 ], [ null, %9 ]
ret i8* %17
}