#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, scalar_t__);
scalar_t__ strlen(char const *);
char *utf8_decode(const char *src) {
  char *ret = calloc(1, strlen(src) + 1);
  char *aux = ret;
  while (*src) {
    unsigned char lead = *src++;
    if ((lead & 0xe0) == 0xc0) {
      unsigned char ch2 = *src++;
      *aux = ((lead & 0x1f) << 6) | (ch2 & 0x3f);
    } else {
      *aux = lead;
    }
    aux++;
  }
  return ret;
}


x86 assembly (clang, -O0):
<utf8_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rsi
add    $0x1,%rsi
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     11d5 <utf8_decode+0xb5>
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
and    $0xe0,%eax
cmp    $0xc0,%eax
jne    11bb <utf8_decode+0x9b>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x1a(%rbp)
movzbl -0x19(%rbp),%eax
and    $0x1f,%eax
shl    $0x6,%eax
movzbl -0x1a(%rbp),%ecx
and    $0x3f,%ecx
or     %ecx,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
jmp    11c4 <utf8_decode+0xa4>
mov    -0x19(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1152 <utf8_decode+0x32>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_decode>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
lea    0x1(%rax),%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    (%r14),%bl
test   %bl,%bl
je     117f <utf8_decode+0x5f>
mov    %rax,%rdx
jmp    116f <utf8_decode+0x4f>
cs nopw 0x0(%rax,%rax,1)
movzbl 0x1(%r14),%ecx
add    $0x2,%r14
shl    $0x6,%bl
and    $0x3f,%cl
or     %cl,%bl
mov    %bl,(%rdx)
add    $0x1,%rdx
movzbl (%r14),%ebx
test   %bl,%bl
je     117f <utf8_decode+0x5f>
mov    %ebx,%ecx
and    $0xe0,%cl
cmp    $0xc0,%cl
je     1150 <utf8_decode+0x30>
add    $0x1,%r14
jmp    1161 <utf8_decode+0x41>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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