#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 int len1;

/* Variables and functions */
unsigned int base64_encode_alloc(char const *, unsigned int const, char **);
scalar_t__ calloc(unsigned int, int);
int /*<<< orphan*/ free(char *);
unsigned int mbstowcs(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
void *strlen(char const *);
int /*<<< orphan*/ strncat(char *, char *, int);
unsigned int non_ascii_alloc(const char *source, char **encoded) {
  const unsigned int len1 = strlen(source);
  unsigned int len2 = mbstowcs(NULL, source, 0);
  unsigned int len3 = 0;
  char *tmp = NULL;
  if (len1 == len2) {
    return 0;
  }
  len2 = ((((len1) + 2) / 3) * 4) + 10 + 2 + 1;
  *encoded = (char *)calloc(len2, sizeof(char));
  len3 = base64_encode_alloc(source, len1, &tmp);
  strncat(*encoded, "=?utf-8?B?", 10);
  strncat(*encoded, tmp, len3);
  strncat(*encoded, "?=", 2);
  free(tmp);
  len2 = strlen(*encoded);
  return len2;
}


x86 assembly (clang, -O0):
<non_ascii_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
xor    %edx,%edx
call   1040 <mbstowcs@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    11b5 <non_ascii_alloc+0x55>
movl   $0x0,-0x4(%rbp)
jmp    125e <non_ascii_alloc+0xfe>
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    $0x3,%ecx
xor    %edx,%edx
div    %ecx
shl    $0x2,%eax
add    $0xa,%eax
add    $0x2,%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
lea    -0x30(%rbp),%rdx
call   1080 <base64_encode_alloc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf5(%rip),%rsi
mov    $0xa,%edx
call   1060 <strncat@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <strncat@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdd5(%rip),%rsi
mov    $0x2,%edx
call   1060 <strncat@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<non_ascii_alloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1050 <strlen@plt>
mov    %rax,%r15
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1040 <mbstowcs@plt>
movq   $0x0,(%rsp)
cmp    %r15d,%eax
je     1216 <non_ascii_alloc+0xb6>
lea    0x2(%r15),%eax
mov    $0xaaaaaaab,%ecx
imul   %rax,%rcx
shr    $0x20,%rcx
and    $0xfffffffe,%ecx
lea    (%rcx,%rcx,1),%edi
add    $0xd,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,(%r14)
mov    %rsp,%rdx
mov    %r12,%rdi
mov    %r15d,%esi
call   1080 <base64_encode_alloc@plt>
mov    %eax,%ebx
mov    (%r14),%rdi
lea    0xe2a(%rip),%rsi
mov    $0xa,%edx
call   1060 <strncat@plt>
mov    (%r14),%rdi
mov    (%rsp),%rsi
mov    %ebx,%edx
call   1060 <strncat@plt>
mov    (%r14),%rdi
lea    0xe13(%rip),%rsi
mov    $0x2,%edx
call   1060 <strncat@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    (%r14),%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"=?utf-8?B?\00"
@.str.1 = [3 x i8] c"?=\00"
define i32 @non_ascii_alloc(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%10 = load i8*, i8** %4
%11 = call i8* @strlen(i8* %10)
%12 = ptrtoint i8* %11 to i32
store i32 %12, i32* %6
%13 = load i8*, i8** %4
%14 = call i32 @mbstowcs(i32* null, i8* %13, i32 0)
store i32 %14, i32* %7
store i32 0, i32* %8
store i8* null, i8** %9
%15 = load i32, i32* %6
%16 = load i32, i32* %7
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 0, i32* %3
br label %52
19: ; preds = %2
%20 = load i32, i32* %6
%21 = add i32 %20, 2
%22 = udiv i32 %21, 3
%23 = mul i32 %22, 4
%24 = add i32 %23, 10
%25 = add i32 %24, 2
%26 = add i32 %25, 1
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = call i64 @calloc(i32 %27, i32 1)
%29 = inttoptr i64 %28 to i8*
%30 = load i8**, i8*** %5
store i8* %29, i8** %30
%31 = load i8*, i8** %4
%32 = load i32, i32* %6
%33 = call i32 @base64_encode_alloc(i8* %31, i32 %32, i8** %9)
store i32 %33, i32* %8
%34 = load i8**, i8*** %5
%35 = load i8*, i8** %34
%36 = call i32 @strncat(i8* %35, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 10)
%37 = load i8**, i8*** %5
%38 = load i8*, i8** %37
%39 = load i8*, i8** %9
%40 = load i32, i32* %8
%41 = call i32 @strncat(i8* %38, i8* %39, i32 %40)
%42 = load i8**, i8*** %5
%43 = load i8*, i8** %42
%44 = call i32 @strncat(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%45 = load i8*, i8** %9
%46 = call i32 @free(i8* %45)
%47 = load i8**, i8*** %5
%48 = load i8*, i8** %47
%49 = call i8* @strlen(i8* %48)
%50 = ptrtoint i8* %49 to i32
store i32 %50, i32* %7
%51 = load i32, i32* %7
store i32 %51, i32* %3
br label %52
52: ; preds = %19, %18
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [11 x i8] c"=?utf-8?B?\00"
@.str.1 = [3 x i8] c"?=\00"
define i32 @non_ascii_alloc(i8* %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = tail call i8* @strlen(i8* %0)
%5 = ptrtoint i8* %4 to i64
%6 = trunc i64 %5 to i32
%7 = tail call i32 @mbstowcs(i32* null, i8* %0, i32 0)
%8 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%9 = icmp eq i32 %7, %6
br i1 %9, label %31, label %10
10: ; preds = %2
%11 = add i32 %6, 2
%12 = udiv i32 %11, 3
%13 = shl i32 %12, 2
%14 = add i32 %13, 13
%15 = tail call i64 @calloc(i32 %14, i32 1)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %1
%17 = call i32 @base64_encode_alloc(i8* %0, i32 %6, i8** %3)
%18 = load i8*, i8** %1
%19 = call i32 @strncat(i8* %18, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 10)
%20 = load i8*, i8** %1
%21 = load i8*, i8** %3
%22 = call i32 @strncat(i8* %20, i8* %21, i32 %17)
%23 = load i8*, i8** %1
%24 = call i32 @strncat(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%25 = load i8*, i8** %3
%26 = call i32 @free(i8* %25)
%27 = load i8*, i8** %1
%28 = call i8* @strlen(i8* %27)
%29 = ptrtoint i8* %28 to i64
%30 = trunc i64 %29 to i32
br label %31
31: ; preds = %2, %10
%32 = phi i32 [ %30, %10 ], [ 0, %2 ]
ret i32 %32
}