#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*/ assert(int);
char *cbbaseencode(char const *, int);
char *cbmyfatal(char *);
char *cbquoteencode(char const *, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, ...);
int strlen(char const *);
char *cbmimeencode(const char *str, const char *encname, int base) {
  char *buf, *wp, *enc;
  int len;
  assert(str && encname);
  len = strlen(str);
  (((buf) = malloc(len * 3 + strlen(encname) + 16))
       ? (buf)
       : cbmyfatal("out of memory"));
  wp = buf;
  wp += sprintf(wp, "=?%s?%c?", encname, base ? 'B' : 'Q');
  enc = base ? cbbaseencode(str, len) : cbquoteencode(str, len);
  wp += sprintf(wp, "%s?=", enc);
  free(enc);
  return buf;
}


x86 assembly (clang, -O0):
<cbmimeencode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x35(%rbp)
je     11ae <cbmimeencode+0x2e>
cmpq   $0x0,-0x10(%rbp)
setne  %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x34(%rbp)
imul   $0x3,-0x34(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
add    %eax,%edi
add    $0x10,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11fb <cbmimeencode+0x7b>
mov    -0x20(%rbp),%rax
jmp    1207 <cbmimeencode+0x87>
lea    0xdfe(%rip),%rdi
call   1080 <cbmyfatal@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%esi
mov    $0x51,%ecx
mov    $0x42,%eax
cmp    $0x0,%esi
cmovne %eax,%ecx
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1267 <cbmimeencode+0xe7>
mov    -0x8(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10a0 <cbbaseencode@plt>
mov    %rax,-0x48(%rbp)
jmp    1277 <cbmimeencode+0xf7>
mov    -0x8(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1040 <cbquoteencode@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xd89(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbmimeencode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
test   %rdi,%rdi
setne  %al
test   %rsi,%rsi
setne  %cl
and    %al,%cl
movzbl %cl,%edi
call   1060 <assert@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
lea    (%r12,%r12,2),%ebx
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbx,1),%edi
add    $0x10,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e0 <cbmimeencode+0x60>
lea    0xe25(%rip),%rdi
call   1080 <cbmyfatal@plt>
test   %r15d,%r15d
mov    $0x51,%eax
mov    $0x42,%ecx
cmove  %eax,%ecx
lea    0xe17(%rip),%rsi
mov    %rbx,%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %eax,%r13d
mov    %r14,%rdi
mov    %r12d,%esi
test   %r15d,%r15d
je     1219 <cbmimeencode+0x99>
call   10a0 <cbbaseencode@plt>
jmp    121e <cbmimeencode+0x9e>
call   1040 <cbquoteencode@plt>
mov    %rax,%rbp
movslq %r13d,%rax
mov    %rbx,%rdi
add    %rax,%rdi
lea    0xde6(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"out of memory\00"
@.str.1 = [9 x i8] c"=?%s?%c?\00"
@.str.2 = [5 x i8] c"%s?=\00"
define i8* @cbmimeencode(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br label %16
16: ; preds = %13, %3
%17 = phi i1 [ false, %3 ], [ %15, %13 ]
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = mul nsw i32 %22, 3
%24 = load i8*, i8** %5
%25 = call i32 @strlen(i8* %24)
%26 = add nsw i32 %23, %25
%27 = add nsw i32 %26, 16
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %16
%31 = load i8*, i8** %7
br label %34
32: ; preds = %16
%33 = call i8* @cbmyfatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %30
%35 = phi i8* [ %31, %30 ], [ %33, %32 ]
%36 = load i8*, i8** %7
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = load i8*, i8** %5
%39 = load i32, i32* %6
%40 = icmp ne i32 %39, 0
%41 = zext i1 %40 to i64
%42 = select i1 %40, i32 66, i32 81
%43 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %37, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %38, i32 %42)
%44 = load i8*, i8** %8
%45 = sext i32 %43 to i64
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8* %46, i8** %8
%47 = load i32, i32* %6
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %34
%50 = load i8*, i8** %4
%51 = load i32, i32* %10
%52 = call i8* @cbbaseencode(i8* %50, i32 %51)
br label %57
53: ; preds = %34
%54 = load i8*, i8** %4
%55 = load i32, i32* %10
%56 = call i8* @cbquoteencode(i8* %54, i32 %55)
br label %57
57: ; preds = %53, %49
%58 = phi i8* [ %52, %49 ], [ %56, %53 ]
store i8* %58, i8** %9
%59 = load i8*, i8** %8
%60 = load i8*, i8** %9
%61 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %59, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %60)
%62 = load i8*, i8** %8
%63 = sext i32 %61 to i64
%64 = getelementptr inbounds i8, i8* %62, i64 %63
store i8* %64, i8** %8
%65 = load i8*, i8** %9
%66 = call i32 @free(i8* %65)
%67 = load i8*, i8** %7
ret i8* %67
}

LLVM IR (-O2):
@.str = [14 x i8] c"out of memory\00"
@.str.1 = [9 x i8] c"=?%s?%c?\00"
@.str.2 = [5 x i8] c"%s?=\00"
define noalias i8* @cbmimeencode(i8* %0, i8* %1, i32 %2) {
%4 = icmp ne i8* %0, null
%5 = icmp ne i8* %1, null
%6 = and i1 %4, %5
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i32 @strlen(i8* %0)
%10 = mul nsw i32 %9, 3
%11 = tail call i32 @strlen(i8* %1)
%12 = add i32 %11, 16
%13 = add i32 %12, %10
%14 = tail call i8* @malloc(i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = tail call i8* @cbmyfatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %3, %16
%19 = icmp eq i32 %2, 0
%20 = select i1 %19, i32 81, i32 66
%21 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %20)
br i1 %19, label %24, label %22
22: ; preds = %18
%23 = tail call i8* @cbbaseencode(i8* %0, i32 %9)
br label %26
24: ; preds = %18
%25 = tail call i8* @cbquoteencode(i8* %0, i32 %9)
br label %26
26: ; preds = %24, %22
%27 = phi i8* [ %23, %22 ], [ %25, %24 ]
%28 = sext i32 %21 to i64
%29 = getelementptr inbounds i8, i8* %14, i64 %28
%30 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %29, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%31 = tail call i32 @free(i8* %27)
ret i8* %14
}