#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 */
char *calloc(int, int);
int strlen(char const *);
char *utf8_encode(const char *src) {
  char *ret = calloc(1, (strlen(src) * 2) + 1);
  char *aux = ret;
  while (*src) {
    unsigned char ch = *src++;
    if (ch < 0x80) {
      *aux = ch;
    } else {
      *aux++ = 0xc0 | (ch >> 6 & 0x1f);
      *aux = 0xc0 | (0x80 | (ch & 0x3f));
    }
    aux++;
  }
  return ret;
}


x86 assembly (clang, -O0):
<utf8_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%esi
shl    $0x1,%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11dd <utf8_encode+0xbd>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x19(%rbp)
movzbl -0x19(%rbp),%eax
cmp    $0x80,%eax
jge    1191 <utf8_encode+0x71>
mov    -0x19(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
jmp    11cc <utf8_encode+0xac>
movzbl -0x19(%rbp),%eax
sar    $0x6,%eax
and    $0x1f,%eax
or     $0xc0,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movzbl -0x19(%rbp),%eax
and    $0x3f,%eax
or     $0x80,%eax
or     $0xc0,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1153 <utf8_encode+0x33>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_encode>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    (%r14),%bl
test   %bl,%bl
je     117c <utf8_encode+0x5c>
add    $0x1,%r14
mov    %rax,%rdx
jmp    1174 <utf8_encode+0x54>
nopl   0x0(%rax)
mov    %ebx,%ecx
shr    $0x6,%cl
or     $0xc0,%cl
mov    %cl,(%rdx)
or     $0xc0,%bl
mov    %bl,0x1(%rdx)
add    $0x1,%rdx
add    $0x1,%rdx
movzbl (%r14),%ebx
add    $0x1,%r14
test   %bl,%bl
je     117c <utf8_encode+0x5c>
test   %bl,%bl
js     1150 <utf8_encode+0x30>
mov    %bl,(%rdx)
jmp    1164 <utf8_encode+0x44>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @utf8_encode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
%8 = mul nsw i32 %7, 2
%9 = add nsw i32 %8, 1
%10 = call i8* @calloc(i32 1, i32 %9)
store i8* %10, i8** %3
%11 = load i8*, i8** %3
store i8* %11, i8** %4
br label %12
12: ; preds = %42, %1
%13 = load i8*, i8** %2
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %16, label %45
16: ; preds = %12
%17 = load i8*, i8** %2
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %2
%19 = load i8, i8* %17
store i8 %19, i8* %5
%20 = load i8, i8* %5
%21 = zext i8 %20 to i32
%22 = icmp slt i32 %21, 128
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i8, i8* %5
%25 = load i8*, i8** %4
store i8 %24, i8* %25
br label %42
26: ; preds = %16
%27 = load i8, i8* %5
%28 = zext i8 %27 to i32
%29 = ashr i32 %28, 6
%30 = and i32 %29, 31
%31 = or i32 192, %30
%32 = trunc i32 %31 to i8
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %4
store i8 %32, i8* %33
%35 = load i8, i8* %5
%36 = zext i8 %35 to i32
%37 = and i32 %36, 63
%38 = or i32 128, %37
%39 = or i32 192, %38
%40 = trunc i32 %39 to i8
%41 = load i8*, i8** %4
store i8 %40, i8* %41
br label %42
42: ; preds = %26, %23
%43 = load i8*, i8** %4
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %4
br label %12
45: ; preds = %12
%46 = load i8*, i8** %3
ret i8* %46
}

LLVM IR (-O2):
define noalias i8* @utf8_encode(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = shl nsw i32 %2, 1
%4 = or i32 %3, 1
%5 = tail call i8* @calloc(i32 1, i32 %4)
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %25, label %8
8: ; preds = %1, %20
%9 = phi i8 [ %23, %20 ], [ %6, %1 ]
%10 = phi i8* [ %12, %20 ], [ %0, %1 ]
%11 = phi i8* [ %22, %20 ], [ %5, %1 ]
%12 = getelementptr inbounds i8, i8* %10, i64 1
%13 = icmp sgt i8 %9, -1
br i1 %13, label %14, label %15
14: ; preds = %8
store i8 %9, i8* %11
br label %20
15: ; preds = %8
%16 = lshr i8 %9, 6
%17 = or i8 %16, -64
%18 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %17, i8* %11
%19 = or i8 %9, -64
store i8 %19, i8* %18
br label %20
20: ; preds = %15, %14
%21 = phi i8* [ %11, %14 ], [ %18, %15 ]
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = load i8, i8* %12
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %8
25: ; preds = %20, %1
ret i8* %5
}