#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 */
struct sspm_buffer {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ sspm_append_string(struct sspm_buffer *, char *);
int /*<<< orphan*/ sspm_write_base64(struct sspm_buffer *, char *, int);
void sspm_encode_base64(struct sspm_buffer *buf, char *data, size_t size) {
  char *p;
  char inbuf[3];
  int i = 0;
  int first = 1;
  int lpos = 0;
  inbuf[0] = inbuf[1] = inbuf[2] = 0;
  for (p = data; *p != 0; p++) {
    if (i % 3 == 0 && first == 0) {
      sspm_write_base64(buf, inbuf, 4);
      lpos += 4;
      inbuf[0] = inbuf[1] = inbuf[2] = 0;
    }
    assert(lpos % 4 == 0);
    if (lpos == 72) {
      sspm_append_string(buf, "\n");
      lpos = 0;
    }
    inbuf[i % 3] = *p;
    i++;
    first = 0;
  }
  if (i % 3 == 1 && first == 0) {
    sspm_write_base64(buf, inbuf, 2);
  } else if (i % 3 == 2 && first == 0) {
    sspm_write_base64(buf, inbuf, 3);
  }
}


x86 assembly (clang, -O0):
<sspm_encode_base64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movb   $0x0,-0x21(%rbp)
movb   $0x0,-0x22(%rbp)
movb   $0x0,-0x23(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1237 <sspm_encode_base64+0x107>
mov    -0x28(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11c2 <sspm_encode_base64+0x92>
cmpl   $0x0,-0x2c(%rbp)
jne    11c2 <sspm_encode_base64+0x92>
mov    -0x8(%rbp),%rdi
lea    -0x23(%rbp),%rsi
mov    $0x4,%edx
call   1030 <sspm_write_base64@plt>
mov    -0x30(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x30(%rbp)
movb   $0x0,-0x21(%rbp)
movb   $0x0,-0x22(%rbp)
movb   $0x0,-0x23(%rbp)
mov    -0x30(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpl   $0x48,-0x30(%rbp)
jne    11fe <sspm_encode_base64+0xce>
mov    -0x8(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   1040 <sspm_append_string@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x28(%rbp),%eax
mov    $0x3,%esi
cltd
idiv   %esi
movslq %edx,%rax
mov    %cl,-0x23(%rbp,%rax,1)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    116d <sspm_encode_base64+0x3d>
mov    -0x28(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    126c <sspm_encode_base64+0x13c>
cmpl   $0x0,-0x2c(%rbp)
jne    126c <sspm_encode_base64+0x13c>
mov    -0x8(%rbp),%rdi
lea    -0x23(%rbp),%rsi
mov    $0x2,%edx
call   1030 <sspm_write_base64@plt>
jmp    12a1 <sspm_encode_base64+0x171>
mov    -0x28(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x2,%edx
jne    129c <sspm_encode_base64+0x16c>
cmpl   $0x0,-0x2c(%rbp)
jne    129c <sspm_encode_base64+0x16c>
mov    -0x8(%rbp),%rdi
lea    -0x23(%rbp),%rsi
mov    $0x3,%edx
call   1030 <sspm_write_base64@plt>
jmp    12a1 <sspm_encode_base64+0x171>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sspm_encode_base64>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movw   $0x0,0x5(%rsp)
mov    (%rsi),%bpl
test   %bpl,%bpl
je     128b <sspm_encode_base64+0x15b>
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x1,%ebx
mov    $0x1,%edi
call   1050 <assert@plt>
mov    (%r15),%al
cmpb   $0x0,0x1(%r15)
mov    %al,0x4(%rsp)
je     1260 <sspm_encode_base64+0x130>
mov    %bpl,0x7(%rsp)
lea    0x5(%rsp),%r13
add    $0x2,%r15
xor    %r12d,%r12d
mov    $0x1,%ebx
jmp    11d0 <sspm_encode_base64+0xa0>
nopl   (%rax)
lea    0x0(,%rbp,2),%rax
add    %rbp,%rax
mov    %r13,%rbp
sub    %rax,%rbp
xor    %edi,%edi
test   $0x3,%r12b
sete   %dil
call   1050 <assert@plt>
cmp    $0x48,%r12d
je     1230 <sspm_encode_base64+0x100>
movzbl -0x1(%r15),%eax
mov    %al,0x0(%rbp)
add    $0x1,%rbx
add    $0x1,%r13
cmpb   $0x0,(%r15)
lea    0x1(%r15),%r15
je     1247 <sspm_encode_base64+0x117>
mov    %rbx,%rax
movabs $0xaaaaaaaaaaaaaaab,%rcx
aa aa aa
mul    %rcx
mov    %rdx,%rbp
shr    %rbp
mov    %ebx,%eax
mov    $0xaaaaaaab,%ecx
imul   %rcx,%rax
shr    $0x21,%rax
lea    (%rax,%rax,2),%eax
cmp    %eax,%ebx
jne    1190 <sspm_encode_base64+0x60>
mov    %r14,%rdi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1030 <sspm_write_base64@plt>
add    $0x4,%r12d
movb   $0x0,0x6(%rsp)
movw   $0x0,0x4(%rsp)
jmp    1190 <sspm_encode_base64+0x60>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
lea    0xdc6(%rip),%rsi
call   1040 <sspm_append_string@plt>
xor    %r12d,%r12d
jmp    11b6 <sspm_encode_base64+0x86>
mov    %ebx,%eax
mov    $0xaaaaaaab,%ecx
imul   %rax,%rcx
shr    $0x21,%rcx
lea    (%rcx,%rcx,2),%eax
sub    %eax,%ebx
mov    0x7(%rsp),%bpl
test   %bpl,%bpl
je     126f <sspm_encode_base64+0x13f>
mov    $0x2,%edx
cmp    $0x1,%ebx
je     127e <sspm_encode_base64+0x14e>
test   %bpl,%bpl
je     128b <sspm_encode_base64+0x15b>
mov    $0x3,%edx
cmp    $0x2,%ebx
jne    128b <sspm_encode_base64+0x15b>
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
call   1030 <sspm_write_base64@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sspm_buffer = type { i32 }
@.str = [2 x i8] c"\0A\00"
define void @sspm_encode_base64(%struct.sspm_buffer* %0, i8* %1, i64 %2) {
%4 = alloca %struct.sspm_buffer*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca [3 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store %struct.sspm_buffer* %0, %struct.sspm_buffer** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %9
store i32 1, i32* %10
store i32 0, i32* %11
%12 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
store i8 0, i8* %12
%13 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 0, i8* %13
%14 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
store i8 0, i8* %14
%15 = load i8*, i8** %5
store i8* %15, i8** %7
br label %16
16: ; preds = %57, %3
%17 = load i8*, i8** %7
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %60
21: ; preds = %16
%22 = load i32, i32* %9
%23 = srem i32 %22, 3
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %37
25: ; preds = %21
%26 = load i32, i32* %10
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %25
%29 = load %struct.sspm_buffer*, %struct.sspm_buffer** %4
%30 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%31 = call i32 @sspm_write_base64(%struct.sspm_buffer* %29, i8* %30, i32 4)
%32 = load i32, i32* %11
%33 = add nsw i32 %32, 4
store i32 %33, i32* %11
%34 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
store i8 0, i8* %34
%35 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 0, i8* %35
%36 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
store i8 0, i8* %36
br label %37
37: ; preds = %28, %25, %21
%38 = load i32, i32* %11
%39 = srem i32 %38, 4
%40 = icmp eq i32 %39, 0
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = load i32, i32* %11
%44 = icmp eq i32 %43, 72
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = load %struct.sspm_buffer*, %struct.sspm_buffer** %4
%47 = call i32 @sspm_append_string(%struct.sspm_buffer* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %11
br label %48
48: ; preds = %45, %37
%49 = load i8*, i8** %7
%50 = load i8, i8* %49
%51 = load i32, i32* %9
%52 = srem i32 %51, 3
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 %53
store i8 %50, i8* %54
%55 = load i32, i32* %9
%56 = add nsw i32 %55, 1
store i32 %56, i32* %9
store i32 0, i32* %10
br label %57
57: ; preds = %48
%58 = load i8*, i8** %7
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %7
br label %16
60: ; preds = %16
%61 = load i32, i32* %9
%62 = srem i32 %61, 3
%63 = icmp eq i32 %62, 1
br i1 %63, label %64, label %71
64: ; preds = %60
%65 = load i32, i32* %10
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %64
%68 = load %struct.sspm_buffer*, %struct.sspm_buffer** %4
%69 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%70 = call i32 @sspm_write_base64(%struct.sspm_buffer* %68, i8* %69, i32 2)
br label %83
71: ; preds = %64, %60
%72 = load i32, i32* %9
%73 = srem i32 %72, 3
%74 = icmp eq i32 %73, 2
br i1 %74, label %75, label %82
75: ; preds = %71
%76 = load i32, i32* %10
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %75
%79 = load %struct.sspm_buffer*, %struct.sspm_buffer** %4
%80 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%81 = call i32 @sspm_write_base64(%struct.sspm_buffer* %79, i8* %80, i32 3)
br label %82
82: ; preds = %78, %75, %71
br label %83
83: ; preds = %82, %67
ret void
}

LLVM IR (-O2):
%struct.sspm_buffer = type { i32 }
@.str = [2 x i8] c"\0A\00"
define void @sspm_encode_base64(%struct.sspm_buffer* %0, i8* nocapture %1, i64 %2) {
%4 = alloca [3 x i8]
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 2
store i8 0, i8* %6
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
store i8 0, i8* %7
%8 = load i8, i8* %1
%9 = icmp eq i8 %8, 0
br i1 %9, label %57, label %10
10: ; preds = %3
%11 = call i32 @assert(i32 1)
%12 = load i8, i8* %1
%13 = getelementptr inbounds i8, i8* %1, i64 1
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
store i8 %12, i8* %5
br i1 %15, label %47, label %16
16: ; preds = %10
%17 = getelementptr inbounds i8, i8* %1, i64 1
br label %18
18: ; preds = %16, %36
%19 = phi i32 [ %37, %36 ], [ 0, %16 ]
%20 = phi i32 [ %41, %36 ], [ 1, %16 ]
%21 = phi i8* [ %42, %36 ], [ %17, %16 ]
%22 = urem i32 %20, 3
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 @sspm_write_base64(%struct.sspm_buffer* %0, i8* %5, i32 4)
%26 = add nsw i32 %19, 4
store i8 0, i8* %6
store i8 0, i8* %7
store i8 0, i8* %5
br label %27
27: ; preds = %24, %18
%28 = phi i32 [ %26, %24 ], [ %19, %18 ]
%29 = and i32 %28, 3
%30 = icmp eq i32 %29, 0
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = icmp eq i32 %28, 72
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @sspm_append_string(%struct.sspm_buffer* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %34, %27
%37 = phi i32 [ 0, %34 ], [ %28, %27 ]
%38 = load i8, i8* %21
%39 = zext i32 %22 to i64
%40 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 %39
store i8 %38, i8* %40
%41 = add nuw nsw i32 %20, 1
%42 = getelementptr inbounds i8, i8* %21, i64 1
%43 = load i8, i8* %42
%44 = icmp eq i8 %43, 0
br i1 %44, label %45, label %18
45: ; preds = %36
%46 = urem i32 %41, 3
br label %47
47: ; preds = %45, %10
%48 = phi i32 [ 1, %10 ], [ %46, %45 ]
%49 = icmp ne i32 %48, 1
%50 = or i1 %9, %49
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = icmp ne i32 %48, 2
%53 = or i1 %9, %52
br i1 %53, label %57, label %54
54: ; preds = %51, %47
%55 = phi i32 [ 2, %47 ], [ 3, %51 ]
%56 = call i32 @sspm_write_base64(%struct.sspm_buffer* %0, i8* %5, i32 %55)
br label %57
57: ; preds = %54, %3, %51
ret void
}