#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 */
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ unicode_to_utf8(char *, size_t *, char *, size_t *);
int /*<<< orphan*/ utf8_to_unicode(char *, size_t *, char *, size_t *);
int main() {
  char *text = "汉";
  char inbuf[1024] = {};
  strcpy(inbuf, text);
  size_t inlen = strlen(inbuf);
  char outbuf[1024] = {};
  size_t outlen = 1024;
  utf8_to_unicode(inbuf, &inlen, outbuf, &outlen);
  printf("print outbuf: %s\n", outbuf);
  size_t outsize = strlen(outbuf);
  size_t insize = 1024;
  char instr[1024] = {};
  unicode_to_utf8(outbuf, &outsize, instr, &insize);
  printf("print buf: %s\n", instr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x418(%rbp)
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
movq   $0x400,-0x828(%rbp)
lea    -0x410(%rbp),%rdi
lea    -0x820(%rbp),%rdx
lea    -0x418(%rbp),%rsi
lea    -0x828(%rbp),%rcx
call   1070 <utf8_to_unicode@plt>
lea    -0x820(%rbp),%rsi
lea    0xe04(%rip),%rdi
call   1050 <printf@plt>
lea    -0x820(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x830(%rbp)
movq   $0x400,-0x838(%rbp)
lea    -0xc40(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x820(%rbp),%rdi
lea    -0xc40(%rbp),%rdx
lea    -0x830(%rbp),%rsi
lea    -0x838(%rbp),%rcx
call   1080 <unicode_to_utf8@plt>
lea    -0xc40(%rbp),%rsi
lea    0xdb1(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0xc40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0xc28,%rsp
lea    0x820(%rsp),%r14
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xe78(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,0x18(%rsp)
lea    0x420(%rsp),%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
movq   $0x400,0x10(%rsp)
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rcx
mov    %r14,%rdi
mov    %rbx,%rdx
call   1070 <utf8_to_unicode@plt>
lea    0xe2b(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,0x8(%rsp)
movq   $0x400,(%rsp)
lea    0x20(%rsp),%r14
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
lea    0x8(%rsp),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rdx
call   1080 <unicode_to_utf8@plt>
lea    0xdf2(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0xc28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"\E6\B1\89\00"
@.str.1 = [18 x i8] c"print outbuf: %s\0A\00"
@.str.2 = [15 x i8] c"print buf: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i64
%5 = alloca [1024 x i8]
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca [1024 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %2
%10 = bitcast [1024 x i8]* %3 to i8*
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = load i8*, i8** %2
%13 = call i32 @strcpy(i8* %11, i8* %12)
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %4
%16 = bitcast [1024 x i8]* %5 to i8*
store i64 1024, i64* %6
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%19 = call i32 @utf8_to_unicode(i8* %17, i64* %4, i8* %18, i64* %6)
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%21 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %7
store i64 1024, i64* %8
%24 = bitcast [1024 x i8]* %9 to i8*
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%27 = call i32 @unicode_to_utf8(i8* %25, i64* %7, i8* %26, i64* %8)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%29 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %28)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"\E6\B1\89\00"
@.str.1 = [18 x i8] c"print outbuf: %s\0A\00"
@.str.2 = [15 x i8] c"print buf: %s\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca i64
%3 = alloca [1024 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca [1024 x i8]
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%9 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = bitcast i64* %2 to i8*
%11 = call i64 @strlen(i8* %8)
store i64 %11, i64* %2
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%13 = bitcast i64* %4 to i8*
store i64 1024, i64* %4
%14 = call i32 @utf8_to_unicode(i8* %8, i64* %2, i8* %12, i64* %4)
%15 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%16 = bitcast i64* %5 to i8*
%17 = call i64 @strlen(i8* %12)
store i64 %17, i64* %5
%18 = bitcast i64* %6 to i8*
store i64 1024, i64* %6
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%20 = call i32 @unicode_to_utf8(i8* %12, i64* %5, i8* %19, i64* %6)
%21 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %19)
ret i32 0
}