#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 scalar_t__ iconv_t;

/* Variables and functions */
size_t iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char *, char *);
char *lisys_calloc(size_t, int);
int /*<<< orphan*/ lisys_free(char *);
size_t strlen(char const *);
char *lisys_string_convert_sys_to_utf8(const char *str) {
  char *tmp;
  char *inbuf;
  char *outbuf;
  size_t n;
  size_t ninbytes;
  size_t noutbytes;
  iconv_t cd;
  ninbytes = strlen(str);
  noutbytes = 4 * (ninbytes + 10);
  tmp = lisys_calloc(noutbytes, sizeof(char));
  if (tmp == NULL)
    return NULL;
  inbuf = (char *)str;
  outbuf = tmp;
  cd = iconv_open("UTF-8", "char");
  if (cd == (iconv_t)-1)
    cd = iconv_open("UTF-8", "");
  if (cd == (iconv_t)-1) {
    lisys_free(tmp);
    return NULL;
  }
  n = iconv(cd, &inbuf, &ninbytes, &outbuf, &noutbytes);
  iconv_close(cd);
  if (n == (size_t)-1) {
    lisys_free(tmp);
    return NULL;
  }
  return tmp;
}


x86 assembly (clang, -O0):
<lisys_string_convert_sys_to_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0xa,%rax
shl    $0x2,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    $0x1,%esi
call   1050 <lisys_calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b3 <lisys_string_convert_sys_to_utf8+0x53>
movq   $0x0,-0x8(%rbp)
jmp    126c <lisys_string_convert_sys_to_utf8+0x10c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    0xe36(%rip),%rdi
lea    0xe35(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0xffffffffffffffff,-0x48(%rbp)
jne    11fc <lisys_string_convert_sys_to_utf8+0x9c>
lea    0xe14(%rip),%rdi
lea    0xe12(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0xffffffffffffffff,-0x48(%rbp)
jne    121d <lisys_string_convert_sys_to_utf8+0xbd>
mov    -0x18(%rbp),%rdi
call   1070 <lisys_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    126c <lisys_string_convert_sys_to_utf8+0x10c>
mov    -0x48(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1030 <iconv@plt>
mov    %rax,-0x30(%rbp)
mov    -0x48(%rbp),%rdi
call   1060 <iconv_close@plt>
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1264 <lisys_string_convert_sys_to_utf8+0x104>
mov    -0x18(%rbp),%rdi
call   1070 <lisys_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    126c <lisys_string_convert_sys_to_utf8+0x10c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lisys_string_convert_sys_to_utf8>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rax,0x8(%rsp)
lea    0x28(,%rax,4),%rdi
mov    %rdi,(%rsp)
mov    $0x1,%esi
call   1050 <lisys_calloc@plt>
test   %rax,%rax
je     1209 <lisys_string_convert_sys_to_utf8+0xa9>
mov    %rax,%r14
mov    %rbx,0x18(%rsp)
mov    %rax,0x10(%rsp)
lea    0xe5b(%rip),%rdi
lea    0xe5a(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
jne    11d6 <lisys_string_convert_sys_to_utf8+0x76>
lea    0xe3f(%rip),%rdi
lea    0xe3d(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     1201 <lisys_string_convert_sys_to_utf8+0xa1>
lea    0x18(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %rsp,%r8
mov    %rbx,%rdi
call   1030 <iconv@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <iconv_close@plt>
cmp    $0xffffffffffffffff,%r15
jne    120c <lisys_string_convert_sys_to_utf8+0xac>
mov    %r14,%rdi
call   1070 <lisys_free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"UTF-8\00"
@.str.1 = [5 x i8] c"char\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @lisys_string_convert_sys_to_utf8(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = add i64 %13, 10
%15 = mul i64 4, %14
store i64 %15, i64* %9
%16 = load i64, i64* %9
%17 = call i8* @lisys_calloc(i64 %16, i32 1)
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %1
store i8* null, i8** %2
br label %47
21: ; preds = %1
%22 = load i8*, i8** %3
store i8* %22, i8** %5
%23 = load i8*, i8** %4
store i8* %23, i8** %6
%24 = call i64 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i64 %24, i64* %10
%25 = load i64, i64* %10
%26 = icmp eq i64 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i64 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
store i64 %28, i64* %10
br label %29
29: ; preds = %27, %21
%30 = load i64, i64* %10
%31 = icmp eq i64 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8*, i8** %4
%34 = call i32 @lisys_free(i8* %33)
store i8* null, i8** %2
br label %47
35: ; preds = %29
%36 = load i64, i64* %10
%37 = call i64 @iconv(i64 %36, i8** %5, i64* %8, i8** %6, i64* %9)
store i64 %37, i64* %7
%38 = load i64, i64* %10
%39 = call i32 @iconv_close(i64 %38)
%40 = load i64, i64* %7
%41 = icmp eq i64 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = load i8*, i8** %4
%44 = call i32 @lisys_free(i8* %43)
store i8* null, i8** %2
br label %47
45: ; preds = %35
%46 = load i8*, i8** %4
store i8* %46, i8** %2
br label %47
47: ; preds = %45, %42, %32, %20
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
@.str = [6 x i8] c"UTF-8\00"
@.str.1 = [5 x i8] c"char\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @lisys_string_convert_sys_to_utf8(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = bitcast i8** %2 to i8*
%7 = bitcast i8** %3 to i8*
%8 = bitcast i64* %4 to i8*
%9 = bitcast i64* %5 to i8*
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
store i64 %10, i64* %4
%11 = shl i64 %10, 2
%12 = add i64 %11, 40
store i64 %12, i64* %5
%13 = tail call i8* @lisys_calloc(i64 %12, i32 1)
%14 = icmp eq i8* %13, null
br i1 %14, label %30, label %15
15: ; preds = %1
store i8* %0, i8** %2
store i8* %13, i8** %3
%16 = tail call i64 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = tail call i64 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @lisys_free(i8* %13)
br label %30
23: ; preds = %15, %18
%24 = phi i64 [ %19, %18 ], [ %16, %15 ]
%25 = call i64 @iconv(i64 %24, i8** %2, i64* %4, i8** %3, i64* %5)
%26 = call i32 @iconv_close(i64 %24)
%27 = icmp eq i64 %25, -1
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @lisys_free(i8* %13)
br label %30
30: ; preds = %23, %1, %28, %21
%31 = phi i8* [ null, %21 ], [ null, %28 ], [ null, %1 ], [ %13, %23 ]
ret i8* %31
}