#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 int /*<<< orphan*/ iconv_t;

/* Variables and functions */
char *alloca(size_t);
size_t convert_utf8_to_local(int /*<<< orphan*/, char const *, size_t, char *,
                             size_t);
size_t strlen(char const *);
char *xstrdup(char *);
char *convert_utf8_to_local_heap(iconv_t hnd, const char *str) {
  size_t inlen, buflen, convlen;
  char *buf = 0;
  if (!str)
    str = "";
  if (!*str)
    return xstrdup("");
  inlen = strlen(str);
  buflen = 4 * inlen + 16;
  buf = alloca(buflen);
  convlen = convert_utf8_to_local(hnd, str, inlen, buf, buflen);
  buf[convlen] = 0;
  return xstrdup(buf);
}


x86 assembly (clang, -O0):
<convert_utf8_to_local_heap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116d <convert_utf8_to_local_heap+0x2d>
lea    0xe97(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    118f <convert_utf8_to_local_heap+0x4f>
lea    0xe7f(%rip),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11ee <convert_utf8_to_local_heap+0xae>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    $0x10,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <alloca@plt>
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1060 <convert_utf8_to_local@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_utf8_to_local_heap>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
test   %rsi,%rsi
lea    0xeab(%rip),%rbp
cmove  %rbp,%rbx
cmpb   $0x0,(%rbx)
je     1198 <convert_utf8_to_local_heap+0x58>
mov    %edi,%r14d
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
lea    0x10(,%rax,4),%r12
mov    %r12,%rdi
call   1050 <alloca@plt>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %rax,%rcx
mov    %r12,%r8
call   1060 <convert_utf8_to_local@plt>
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <xstrdup@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @convert_utf8_to_local_heap(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* null, i8** %9
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %5
br label %13
13: ; preds = %12, %2
%14 = load i8*, i8** %5
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = call i8* @xstrdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %18, i8** %3
br label %38
19: ; preds = %13
%20 = load i8*, i8** %5
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = mul i64 4, %22
%24 = add i64 %23, 16
store i64 %24, i64* %7
%25 = load i64, i64* %7
%26 = call i8* @alloca(i64 %25)
store i8* %26, i8** %9
%27 = load i32, i32* %4
%28 = load i8*, i8** %5
%29 = load i64, i64* %6
%30 = load i8*, i8** %9
%31 = load i64, i64* %7
%32 = call i64 @convert_utf8_to_local(i32 %27, i8* %28, i64 %29, i8* %30, i64 %31)
store i64 %32, i64* %8
%33 = load i8*, i8** %9
%34 = load i64, i64* %8
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %9
%37 = call i8* @xstrdup(i8* %36)
store i8* %37, i8** %3
br label %38
38: ; preds = %19, %17
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @convert_utf8_to_local_heap(i32 %0, i8* %1) {
%3 = icmp eq i8* %1, null
%4 = select i1 %3, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %1
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %2
%8 = tail call i64 @strlen(i8* %4)
%9 = shl i64 %8, 2
%10 = add i64 %9, 16
%11 = tail call i8* @alloca(i64 %10)
%12 = tail call i64 @convert_utf8_to_local(i32 %0, i8* %4, i64 %8, i8* %11, i64 %10)
%13 = getelementptr inbounds i8, i8* %11, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %2, %7
%15 = phi i8* [ %11, %7 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %2 ]
%16 = tail call i8* @xstrdup(i8* %15)
ret i8* %16
}