#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*/ string_convbase64_8x3_to_6x4(char const *, char *);
void string_encode_base64(const char *from, int length, char *to) {
  const char *ptr_from;
  char *ptr_to;
  if (!from || !to)
    return;
  ptr_from = from;
  ptr_to = to;
  while (length >= 3) {
    string_convbase64_8x3_to_6x4(ptr_from, ptr_to);
    ptr_from += 3 * sizeof(*ptr_from);
    ptr_to += 4 * sizeof(*ptr_to);
    length -= 3;
  }
  if (length > 0) {
    char rest[3] = {0, 0, 0};
    switch (length) {
    case 1:
      rest[0] = ptr_from[0];
      string_convbase64_8x3_to_6x4(rest, ptr_to);
      ptr_to[2] = ptr_to[3] = '=';
      break;
    case 2:
      rest[0] = ptr_from[0];
      rest[1] = ptr_from[1];
      string_convbase64_8x3_to_6x4(rest, ptr_to);
      ptr_to[3] = '=';
      break;
    }
    ptr_to[4] = 0;
  } else
    ptr_to[0] = '\0';
}


x86 assembly (clang, -O0):
<string_encode_base64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1139 <string_encode_base64+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    113e <string_encode_base64+0x2e>
jmp    122b <string_encode_base64+0x11b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x3,-0xc(%rbp)
jl     118b <string_encode_base64+0x7b>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <string_convbase64_8x3_to_6x4@plt>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0xc(%rbp)
jmp    114e <string_encode_base64+0x3e>
cmpl   $0x0,-0xc(%rbp)
jle    1224 <string_encode_base64+0x114>
movb   $0x0,-0x2a(%rbp)
movw   $0x0,-0x2c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0x1,%eax
je     11c4 <string_encode_base64+0xb4>
jmp    11b3 <string_encode_base64+0xa3>
mov    -0x30(%rbp),%eax
sub    $0x2,%eax
je     11ef <string_encode_base64+0xdf>
jmp    1217 <string_encode_base64+0x107>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <string_convbase64_8x3_to_6x4@plt>
mov    -0x28(%rbp),%rax
movb   $0x3d,0x3(%rax)
mov    -0x28(%rbp),%rax
movb   $0x3d,0x2(%rax)
jmp    1217 <string_encode_base64+0x107>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x2b(%rbp)
lea    -0x2c(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <string_convbase64_8x3_to_6x4@plt>
mov    -0x28(%rbp),%rax
movb   $0x3d,0x3(%rax)
mov    -0x28(%rbp),%rax
movb   $0x0,0x4(%rax)
jmp    122b <string_encode_base64+0x11b>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_encode_base64>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11ce <string_encode_base64+0xbe>
mov    %rdx,%r14
test   %rdx,%rdx
je     11ce <string_encode_base64+0xbe>
mov    %esi,%ebx
mov    %rdi,%r15
cmp    $0x3,%esi
jl     1197 <string_encode_base64+0x87>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <string_convbase64_8x3_to_6x4@plt>
add    $0x3,%r15
add    $0x4,%r14
lea    -0x3(%rbx),%eax
cmp    $0x5,%ebx
mov    %eax,%ebx
ja     1140 <string_encode_base64+0x30>
test   %eax,%eax
jle    119d <string_encode_base64+0x8d>
movb   $0x0,0xe(%rsp)
movw   $0x0,0xc(%rsp)
cmp    $0x2,%eax
je     11a3 <string_encode_base64+0x93>
cmp    $0x1,%eax
jne    11c9 <string_encode_base64+0xb9>
mov    (%r15),%al
mov    %al,0xc(%rsp)
lea    0xc(%rsp),%rdi
mov    %r14,%rsi
call   1030 <string_convbase64_8x3_to_6x4@plt>
movb   $0x3d,0x3(%r14)
mov    $0x2,%eax
jmp    11c4 <string_encode_base64+0xb4>
mov    %ebx,%eax
test   %eax,%eax
jg     1161 <string_encode_base64+0x51>
movb   $0x0,(%r14)
jmp    11ce <string_encode_base64+0xbe>
mov    (%r15),%al
mov    %al,0xc(%rsp)
mov    0x1(%r15),%al
mov    %al,0xd(%rsp)
lea    0xc(%rsp),%rdi
mov    %r14,%rsi
call   1030 <string_convbase64_8x3_to_6x4@plt>
mov    $0x3,%eax
movb   $0x3d,(%r14,%rax,1)
movb   $0x0,0x4(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @string_encode_base64(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [3 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12, %3
br label %70
16: ; preds = %12
%17 = load i8*, i8** %4
store i8* %17, i8** %7
%18 = load i8*, i8** %6
store i8* %18, i8** %8
br label %19
19: ; preds = %22, %16
%20 = load i32, i32* %5
%21 = icmp sge i32 %20, 3
br i1 %21, label %22, label %32
22: ; preds = %19
%23 = load i8*, i8** %7
%24 = load i8*, i8** %8
%25 = call i32 @string_convbase64_8x3_to_6x4(i8* %23, i8* %24)
%26 = load i8*, i8** %7
%27 = getelementptr inbounds i8, i8* %26, i64 3
store i8* %27, i8** %7
%28 = load i8*, i8** %8
%29 = getelementptr inbounds i8, i8* %28, i64 4
store i8* %29, i8** %8
%30 = load i32, i32* %5
%31 = sub nsw i32 %30, 3
store i32 %31, i32* %5
br label %19
32: ; preds = %19
%33 = load i32, i32* %5
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %67
35: ; preds = %32
%36 = bitcast [3 x i8]* %9 to i8*
%37 = load i32, i32* %5
switch i32 %37, label %64 [
i32 1, label %38
i32 2, label %50
]
38: ; preds = %35
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i64 0
%41 = load i8, i8* %40
%42 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
store i8 %41, i8* %42
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%44 = load i8*, i8** %8
%45 = call i32 @string_convbase64_8x3_to_6x4(i8* %43, i8* %44)
%46 = load i8*, i8** %8
%47 = getelementptr inbounds i8, i8* %46, i64 3
store i8 61, i8* %47
%48 = load i8*, i8** %8
%49 = getelementptr inbounds i8, i8* %48, i64 2
store i8 61, i8* %49
br label %64
50: ; preds = %35
%51 = load i8*, i8** %7
%52 = getelementptr inbounds i8, i8* %51, i64 0
%53 = load i8, i8* %52
%54 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
store i8 %53, i8* %54
%55 = load i8*, i8** %7
%56 = getelementptr inbounds i8, i8* %55, i64 1
%57 = load i8, i8* %56
%58 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 1
store i8 %57, i8* %58
%59 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%60 = load i8*, i8** %8
%61 = call i32 @string_convbase64_8x3_to_6x4(i8* %59, i8* %60)
%62 = load i8*, i8** %8
%63 = getelementptr inbounds i8, i8* %62, i64 3
store i8 61, i8* %63
br label %64
64: ; preds = %35, %50, %38
%65 = load i8*, i8** %8
%66 = getelementptr inbounds i8, i8* %65, i64 4
store i8 0, i8* %66
br label %70
67: ; preds = %32
%68 = load i8*, i8** %8
%69 = getelementptr inbounds i8, i8* %68, i64 0
store i8 0, i8* %69
br label %70
70: ; preds = %15, %67, %64
ret void
}

LLVM IR (-O2):
define void @string_encode_base64(i8* %0, i32 %1, i8* %2) {
%4 = alloca [3 x i8]
%5 = icmp ne i8* %0, null
%6 = icmp ne i8* %2, null
%7 = and i1 %5, %6
br i1 %7, label %8, label %42
8: ; preds = %3
%9 = icmp sgt i32 %1, 2
br i1 %9, label %10, label %19
10: ; preds = %8, %10
%11 = phi i8* [ %16, %10 ], [ %2, %8 ]
%12 = phi i8* [ %15, %10 ], [ %0, %8 ]
%13 = phi i32 [ %17, %10 ], [ %1, %8 ]
%14 = tail call i32 @string_convbase64_8x3_to_6x4(i8* %12, i8* %11)
%15 = getelementptr inbounds i8, i8* %12, i64 3
%16 = getelementptr inbounds i8, i8* %11, i64 4
%17 = add nsw i32 %13, -3
%18 = icmp ugt i32 %13, 5
br i1 %18, label %10, label %19
19: ; preds = %10, %8
%20 = phi i32 [ %1, %8 ], [ %17, %10 ]
%21 = phi i8* [ %0, %8 ], [ %15, %10 ]
%22 = phi i8* [ %2, %8 ], [ %16, %10 ]
%23 = icmp sgt i32 %20, 0
br i1 %23, label %24, label %41
24: ; preds = %19
%25 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
switch i32 %20, label %39 [
i32 1, label %26
i32 2, label %30
]
26: ; preds = %24
%27 = load i8, i8* %21
store i8 %27, i8* %25
%28 = call i32 @string_convbase64_8x3_to_6x4(i8* %25, i8* %22)
%29 = getelementptr inbounds i8, i8* %22, i64 3
store i8 61, i8* %29
br label %36
30: ; preds = %24
%31 = load i8, i8* %21
store i8 %31, i8* %25
%32 = getelementptr inbounds i8, i8* %21, i64 1
%33 = load i8, i8* %32
%34 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
store i8 %33, i8* %34
%35 = call i32 @string_convbase64_8x3_to_6x4(i8* %25, i8* %22)
br label %36
36: ; preds = %26, %30
%37 = phi i64 [ 3, %30 ], [ 2, %26 ]
%38 = getelementptr inbounds i8, i8* %22, i64 %37
store i8 61, i8* %38
br label %39
39: ; preds = %36, %24
%40 = getelementptr inbounds i8, i8* %22, i64 4
store i8 0, i8* %40
br label %42
41: ; preds = %19
store i8 0, i8* %22
br label %42
42: ; preds = %39, %41, %3
ret void
}