#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__ u_is_valid(char const *);
int utf8_encode(char const *, char const *, char **);
char *xstrdup(char const *);
char *to_utf8(const char *str, const char *enc) {
  char *outbuf = NULL;
  int rc;
  if (u_is_valid(str)) {
    return xstrdup(str);
  } else {
    rc = utf8_encode(str, enc, &outbuf);
    return rc < 0 ? xstrdup(str) : outbuf;
  }
}


x86 assembly (clang, -O0):
<to_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <u_is_valid@plt>
cmp    $0x0,%rax
je     116d <to_utf8+0x3d>
mov    -0x10(%rbp),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11ad <to_utf8+0x7d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <utf8_encode@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    119d <to_utf8+0x6d>
mov    -0x10(%rbp),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x30(%rbp)
jmp    11a5 <to_utf8+0x75>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_utf8>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,(%rsp)
call   1050 <u_is_valid@plt>
test   %rax,%rax
je     115b <to_utf8+0x2b>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <xstrdup@plt>
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <utf8_encode@plt>
test   %eax,%eax
js     1173 <to_utf8+0x43>
mov    (%rsp),%rax
jmp    117b <to_utf8+0x4b>
mov    %rbx,%rdi
call   1040 <xstrdup@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @to_utf8(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
%8 = load i8*, i8** %4
%9 = call i64 @u_is_valid(i8* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i8* @xstrdup(i8* %12)
store i8* %13, i8** %3
br label %27
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = call i32 @utf8_encode(i8* %15, i8* %16, i8** %6)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i8*, i8** %4
%22 = call i8* @xstrdup(i8* %21)
br label %25
23: ; preds = %14
%24 = load i8*, i8** %6
br label %25
25: ; preds = %23, %20
%26 = phi i8* [ %22, %20 ], [ %24, %23 ]
store i8* %26, i8** %3
br label %27
27: ; preds = %25, %11
%28 = load i8*, i8** %3
ret i8* %28
}

LLVM IR (-O2):
define i8* @to_utf8(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = tail call i64 @u_is_valid(i8* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i8* @xstrdup(i8* %0)
br label %16
9: ; preds = %2
%10 = call i32 @utf8_encode(i8* %0, i8* %1, i8** %3)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i8* @xstrdup(i8* %0)
br label %16
14: ; preds = %9
%15 = load i8*, i8** %3
br label %16
16: ; preds = %12, %14, %7
%17 = phi i8* [ %8, %7 ], [ %13, %12 ], [ %15, %14 ]
ret i8* %17
}