#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 *malloc(int);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ otrl_base64_encode(char *, unsigned char const *, size_t);
char *otrl_base64_otr_encode(const unsigned char *buf, size_t buflen) {
  char *base64buf;
  size_t base64len;
  base64len = ((buflen + 2) / 3) * 4;
  base64buf = malloc(5 + base64len + 1 + 1);
  if (base64buf == NULL) {
    return NULL;
  }
  memmove(base64buf, "?OTR:", 5);
  otrl_base64_encode(base64buf + 5, buf, buflen);
  base64buf[5 + base64len] = '.';
  base64buf[5 + base64len + 1] = '\0';
  return base64buf;
}


x86 assembly (clang, -O0):
<otrl_base64_otr_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
shl    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x5,%rax
add    $0x1,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118d <otrl_base64_otr_encode+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <otrl_base64_otr_encode+0xb0>
mov    -0x20(%rbp),%rdi
lea    0xe68(%rip),%rsi
mov    $0x5,%edx
call   1040 <memmove@plt>
mov    -0x20(%rbp),%rdi
add    $0x5,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <otrl_base64_encode@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
add    $0x5,%rcx
movb   $0x2e,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
add    $0x5,%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<otrl_base64_otr_encode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0x2(%rsi),%rax
movabs $0xaaaaaaaaaaaaaaab,%rcx
aa aa aa
mul    %rcx
mov    %rdx,%rbx
mov    %rdi,%r15
add    %rdx,%rbx
and    $0xfffffffffffffffc,%rbx
lea    0x7(%rbx),%edi
call   1030 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     1197 <otrl_base64_otr_encode+0x67>
lea    0xe90(%rip),%rsi
mov    %r12,%rdi
mov    $0x5,%edx
call   1040 <memmove@plt>
mov    %r12,%rdi
add    $0x5,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <otrl_base64_encode@plt>
movw   $0x2e,0x5(%rbx,%r12,1)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"?OTR:\00"
define i8* @otrl_base64_otr_encode(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i64, i64* %5
%9 = add i64 %8, 2
%10 = udiv i64 %9, 3
%11 = mul i64 %10, 4
store i64 %11, i64* %7
%12 = load i64, i64* %7
%13 = add i64 5, %12
%14 = add i64 %13, 1
%15 = add i64 %14, 1
%16 = trunc i64 %15 to i32
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %2
store i8* null, i8** %3
br label %39
21: ; preds = %2
%22 = load i8*, i8** %6
%23 = call i32 @memmove(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 5
%26 = load i8*, i8** %4
%27 = load i64, i64* %5
%28 = call i32 @otrl_base64_encode(i8* %25, i8* %26, i64 %27)
%29 = load i8*, i8** %6
%30 = load i64, i64* %7
%31 = add i64 5, %30
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 46, i8* %32
%33 = load i8*, i8** %6
%34 = load i64, i64* %7
%35 = add i64 5, %34
%36 = add i64 %35, 1
%37 = getelementptr inbounds i8, i8* %33, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %6
store i8* %38, i8** %3
br label %39
39: ; preds = %21, %20
%40 = load i8*, i8** %3
ret i8* %40
}

LLVM IR (-O2):
@.str = [6 x i8] c"?OTR:\00"
define i8* @otrl_base64_otr_encode(i8* %0, i64 %1) {
%3 = add i64 %1, 2
%4 = udiv i64 %3, 3
%5 = shl i64 %4, 2
%6 = trunc i64 %5 to i32
%7 = add i32 %6, 7
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %18, label %10
10: ; preds = %2
%11 = add i64 %5, 6
%12 = add i64 %5, 5
%13 = tail call i32 @memmove(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%14 = getelementptr inbounds i8, i8* %8, i64 5
%15 = tail call i32 @otrl_base64_encode(i8* %14, i8* %0, i64 %1)
%16 = getelementptr inbounds i8, i8* %8, i64 %12
store i8 46, i8* %16
%17 = getelementptr inbounds i8, i8* %8, i64 %11
store i8 0, i8* %17
br label %18
18: ; preds = %2, %10
ret i8* %8
}