#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 */
scalar_t__ g_utf8_validate(char const *, int, int /*<<< orphan*/ *);
char *iconv_string_full(char const *, int, char const *, char *,
                        int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
char *strdup(char const *);
char *str_legacy_to_utf8_internal(const char *string,
                                  const char *legacy_encoding,
                                  int replacement_char) {
  if (string == NULL || legacy_encoding == NULL) {
    return NULL;
  }
  if (g_utf8_validate(string, -1, NULL)) {
    return strdup(string);
  }
  char *utf8_string = iconv_string_full(string, -1, legacy_encoding, "UTF-8",
                                        NULL, NULL, replacement_char);
  return utf8_string;
}


x86 assembly (clang, -O0):
<str_legacy_to_utf8_internal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1159 <str_legacy_to_utf8_internal+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    1166 <str_legacy_to_utf8_internal+0x36>
movq   $0x0,-0x8(%rbp)
jmp    11c8 <str_legacy_to_utf8_internal+0x98>
mov    -0x10(%rbp),%rdi
mov    $0xffffffff,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <g_utf8_validate@plt>
cmp    $0x0,%rax
je     1194 <str_legacy_to_utf8_internal+0x64>
mov    -0x10(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11c8 <str_legacy_to_utf8_internal+0x98>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%eax
mov    $0xffffffff,%esi
lea    0xe55(%rip),%rcx
xor    %r8d,%r8d
mov    %r8d,%r9d
mov    %r9,%r8
mov    %eax,(%rsp)
call   1030 <iconv_string_full@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_legacy_to_utf8_internal>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     116b <str_legacy_to_utf8_internal+0x3b>
mov    %rsi,%r14
test   %rsi,%rsi
je     116b <str_legacy_to_utf8_internal+0x3b>
mov    %edx,%ebp
mov    %rdi,%rbx
mov    $0xffffffff,%esi
xor    %edx,%edx
call   1050 <g_utf8_validate@plt>
test   %rax,%rax
je     116f <str_legacy_to_utf8_internal+0x3f>
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <strdup@plt>
xor    %eax,%eax
jmp    118f <str_legacy_to_utf8_internal+0x5f>
mov    %ebp,(%rsp)
lea    0xe87(%rip),%rcx
mov    %rbx,%rdi
mov    $0xffffffff,%esi
mov    %r14,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <iconv_string_full@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"UTF-8\00"
define i8* @str_legacy_to_utf8_internal(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %11, %3
store i8* null, i8** %4
br label %28
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = call i64 @g_utf8_validate(i8* %16, i32 -1, i32* null)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i8*, i8** %5
%21 = call i8* @strdup(i8* %20)
store i8* %21, i8** %4
br label %28
22: ; preds = %15
%23 = load i8*, i8** %5
%24 = load i8*, i8** %6
%25 = load i32, i32* %7
%26 = call i8* @iconv_string_full(i8* %23, i32 -1, i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* null, i32* null, i32 %25)
store i8* %26, i8** %8
%27 = load i8*, i8** %8
store i8* %27, i8** %4
br label %28
28: ; preds = %22, %19, %14
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
@.str = [6 x i8] c"UTF-8\00"
define i8* @str_legacy_to_utf8_internal(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i8* %1, null
%6 = or i1 %4, %5
br i1 %6, label %14, label %7
7: ; preds = %3
%8 = tail call i64 @g_utf8_validate(i8* %0, i32 -1, i32* null)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i8* @strdup(i8* %0)
br label %14
12: ; preds = %7
%13 = tail call i8* @iconv_string_full(i8* %0, i32 -1, i8* %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* null, i32* null, i32 %2)
br label %14
14: ; preds = %3, %12, %10
%15 = phi i8* [ %11, %10 ], [ %13, %12 ], [ null, %3 ]
ret i8* %15
}