#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 */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char *, char const *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
scalar_t__ safe_malloc(size_t);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t strlen(char *);
char *utf8(const char *fromcode, char *inputbuf) {
  iconv_t cd;
  char *outputptr;
  char *outputbuf;
  size_t outbytesleft;
  size_t inbytesleft;
  inbytesleft = strlen(inputbuf);
  cd = iconv_open("UTF-8", fromcode);
  if (cd == (iconv_t)-1) {
    fprintf(stderr, "utf8: iconv_open: %s\n", strerror(errno));
    return NULL;
  }
  outbytesleft = inbytesleft * 3 + 1;
  outputbuf = (char *)safe_malloc(outbytesleft);
  if (!outputbuf) {
    fprintf(stderr, "utf8: malloc: %s\n", strerror(errno));
    return NULL;
  }
  memset(outputbuf, 0, outbytesleft);
  outputptr = outputbuf;
  if (-1 == iconv(cd, &inputbuf, &inbytesleft, &outputptr, &outbytesleft)) {
    fprintf(stderr, "utf8: %s", strerror(errno));
    return NULL;
  }
  iconv_close(cd);
  return outputbuf;
}


x86 assembly (clang, -O0):
<utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   10a0 <iconv_open@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11f5 <utf8+0x75>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xe23(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <utf8+0x15c>
imul   $0x3,-0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <safe_malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1253 <utf8+0xd3>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xddb(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <utf8+0x15c>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   1040 <iconv@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12cb <utf8+0x14b>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xd75(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <utf8+0x15c>
mov    -0x20(%rbp),%rdi
call   1080 <iconv_close@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
mov    %rsi,0x18(%rsp)
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %rax,(%rsp)
lea    0xe59(%rip),%rdi
mov    %r14,%rsi
call   10a0 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     1210 <utf8+0x90>
mov    %rax,%r14
lea    (%rbx,%rbx,2),%rbx
add    $0x1,%rbx
mov    %rbx,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <safe_malloc@plt>
test   %rax,%rax
je     1230 <utf8+0xb0>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1060 <memset@plt>
mov    %r15,0x10(%rsp)
lea    0x18(%rsp),%rsi
mov    %rsp,%rdx
lea    0x10(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %r14,%rdi
call   1040 <iconv@plt>
cmp    $0xffffffff,%eax
je     1250 <utf8+0xd0>
mov    %r14,%rdi
call   1080 <iconv_close@plt>
jmp    127b <utf8+0xfb>
mov    0x2dc9(%rip),%rax
mov    (%rax),%ebx
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd8(%rip),%rsi
jmp    126e <utf8+0xee>
mov    0x2da9(%rip),%rax
mov    (%rax),%ebx
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdce(%rip),%rsi
jmp    126e <utf8+0xee>
mov    0x2d89(%rip),%rax
mov    (%rax),%ebx
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdc0(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"UTF-8\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"utf8: iconv_open: %s\0A\00"
@errno = global i32 0
@.str.2 = [18 x i8] c"utf8: malloc: %s\0A\00"
@.str.3 = [9 x i8] c"utf8: %s\00"
define i8* @utf8(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %10
%13 = load i8*, i8** %4
%14 = call i64 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %13)
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i8* null, i8** %3
br label %53
22: ; preds = %2
%23 = load i64, i64* %10
%24 = mul i64 %23, 3
%25 = add i64 %24, 1
store i64 %25, i64* %9
%26 = load i64, i64* %9
%27 = call i64 @safe_malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = icmp ne i8* %29, null
br i1 %30, label %36, label %31
31: ; preds = %22
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i8* null, i8** %3
br label %53
36: ; preds = %22
%37 = load i8*, i8** %8
%38 = load i64, i64* %9
%39 = call i32 @memset(i8* %37, i32 0, i64 %38)
%40 = load i8*, i8** %8
store i8* %40, i8** %7
%41 = load i64, i64* %6
%42 = call i32 @iconv(i64 %41, i8** %5, i64* %10, i8** %7, i64* %9)
%43 = icmp eq i32 -1, %42
br i1 %43, label %44, label %49
44: ; preds = %36
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %47)
store i8* null, i8** %3
br label %53
49: ; preds = %36
%50 = load i64, i64* %6
%51 = call i32 @iconv_close(i64 %50)
%52 = load i8*, i8** %8
store i8* %52, i8** %3
br label %53
53: ; preds = %49, %44, %31, %17
%54 = load i8*, i8** %3
ret i8* %54
}

LLVM IR (-O2):
@.str = [6 x i8] c"UTF-8\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"utf8: iconv_open: %s\0A\00"
@errno = global i32 0
@.str.2 = [18 x i8] c"utf8: malloc: %s\0A\00"
@.str.3 = [9 x i8] c"utf8: %s\00"
define i8* @utf8(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %1, i8** %3
%7 = bitcast i8** %4 to i8*
%8 = bitcast i64* %5 to i8*
%9 = bitcast i64* %6 to i8*
%10 = tail call i64 @strlen(i8* dereferenceable(1) %1)
store i64 %10, i64* %6
%11 = tail call i64 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = icmp eq i64 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %40
18: ; preds = %2
%19 = mul i64 %10, 3
%20 = add i64 %19, 1
store i64 %20, i64* %5
%21 = tail call i64 @safe_malloc(i64 %20)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %27)
br label %40
29: ; preds = %18
%30 = tail call i32 @memset(i8* %22, i32 0, i64 %20)
store i8* %22, i8** %4
%31 = call i32 @iconv(i64 %11, i8** %3, i64* %6, i8** %4, i64* %5)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %36)
br label %40
38: ; preds = %29
%39 = call i32 @iconv_close(i64 %11)
br label %40
40: ; preds = %38, %33, %24, %13
%41 = phi i8* [ null, %13 ], [ null, %33 ], [ %22, %38 ], [ null, %24 ]
ret i8* %41
}