#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*/ _write_base64_unit(char *, unsigned char *);
int HEX64_writeBase64(char *str, unsigned char *buffer, int len) {
  int sp, scnt, srmd;
  unsigned char temp[3];
  scnt = len / 3;
  srmd = len % 3;
  for (sp = 0; sp < scnt; sp++) {
    _write_base64_unit(str, &buffer[sp * 3]);
    str += 4;
  }
  if (srmd) {
    temp[0] = buffer[(sp * 3)];
    if (srmd > 1)
      temp[1] = buffer[(sp * 3) + 1];
    else
      temp[1] = 0;
    temp[2] = 0;
    _write_base64_unit(str, temp);
    str += 4;
    *(str - 1) = '=';
    if (srmd == 1)
      *(str - 2) = '=';
  }
  *str = '\0';
  return sp * 4;
}


x86 assembly (clang, -O0):
<HEX64_writeBase64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1182 <HEX64_writeBase64+0x72>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
imul   $0x3,-0x18(%rbp),%eax
cltq
add    %rax,%rsi
call   1030 <_write_base64_unit@plt>
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1146 <HEX64_writeBase64+0x36>
cmpl   $0x0,-0x20(%rbp)
je     1200 <HEX64_writeBase64+0xf0>
mov    -0x10(%rbp),%rax
imul   $0x3,-0x18(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x23(%rbp)
cmpl   $0x1,-0x20(%rbp)
jle    11c0 <HEX64_writeBase64+0xb0>
mov    -0x10(%rbp),%rax
imul   $0x3,-0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x22(%rbp)
jmp    11c4 <HEX64_writeBase64+0xb4>
movb   $0x0,-0x22(%rbp)
movb   $0x0,-0x21(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x23(%rbp),%rsi
call   1030 <_write_base64_unit@plt>
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x3d,-0x1(%rax)
cmpl   $0x1,-0x20(%rbp)
jne    11fb <HEX64_writeBase64+0xeb>
mov    -0x8(%rbp),%rax
movb   $0x3d,-0x2(%rax)
jmp    1200 <HEX64_writeBase64+0xf0>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
shl    $0x2,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HEX64_writeBase64>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r12
movslq %edx,%rax
imul   $0x55555556,%rax,%r14
mov    %r14,%rcx
shr    $0x3f,%rcx
shr    $0x20,%r14
add    %ecx,%r14d
lea    (%r14,%r14,2),%ecx
mov    %eax,%r13d
sub    %ecx,%r13d
cmp    $0x3,%eax
jl     1189 <HEX64_writeBase64+0x79>
mov    %r14d,%ebp
mov    %r15,%rbx
xchg   %ax,%ax
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <_write_base64_unit@plt>
add    $0x4,%r12
add    $0x3,%rbx
add    $0xffffffffffffffff,%rbp
jne    1150 <HEX64_writeBase64+0x40>
test   %r13d,%r13d
je     1191 <HEX64_writeBase64+0x81>
lea    (%r14,%r14,2),%eax
mov    (%r15,%rax,1),%cl
mov    %cl,0x5(%rsp)
cmp    $0x2,%r13d
jl     1196 <HEX64_writeBase64+0x86>
mov    %eax,%eax
mov    0x1(%rax,%r15,1),%al
jmp    1198 <HEX64_writeBase64+0x88>
xor    %r14d,%r14d
test   %r13d,%r13d
jne    116e <HEX64_writeBase64+0x5e>
mov    %r12,%rax
jmp    11c5 <HEX64_writeBase64+0xb5>
xor    %eax,%eax
mov    %al,0x6(%rsp)
movb   $0x0,0x7(%rsp)
lea    0x5(%rsp),%rsi
mov    %r12,%rdi
call   1030 <_write_base64_unit@plt>
lea    0x4(%r12),%rax
movb   $0x3d,0x3(%r12)
cmp    $0x1,%r13d
jne    11c5 <HEX64_writeBase64+0xb5>
movb   $0x3d,0x2(%r12)
movb   $0x0,(%rax)
shl    $0x2,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @HEX64_writeBase64(i8* %0, i8* %1, i32 %2) {
%4 = alloca [3 x i8]
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%6 = sdiv i32 %2, 3
%7 = srem i32 %2, 3
%8 = icmp sgt i32 %2, 2
br i1 %8, label %9, label %20
9: ; preds = %3
%10 = zext i32 %6 to i64
br label %11
11: ; preds = %9, %11
%12 = phi i64 [ 0, %9 ], [ %18, %11 ]
%13 = phi i8* [ %0, %9 ], [ %17, %11 ]
%14 = mul nuw nsw i64 %12, 3
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = tail call i32 @_write_base64_unit(i8* %13, i8* %15)
%17 = getelementptr inbounds i8, i8* %13, i64 4
%18 = add nuw nsw i64 %12, 1
%19 = icmp eq i64 %18, %10
br i1 %19, label %20, label %11
20: ; preds = %11, %3
%21 = phi i32 [ 0, %3 ], [ %6, %11 ]
%22 = phi i8* [ %0, %3 ], [ %17, %11 ]
%23 = icmp eq i32 %7, 0
br i1 %23, label %45, label %24
24: ; preds = %20
%25 = mul nsw i32 %21, 3
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %1, i64 %26
%28 = load i8, i8* %27
store i8 %28, i8* %5
%29 = icmp sgt i32 %7, 1
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = add nuw nsw i32 %25, 1
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %1, i64 %32
%34 = load i8, i8* %33
br label %35
35: ; preds = %24, %30
%36 = phi i8 [ %34, %30 ], [ 0, %24 ]
%37 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
store i8 %36, i8* %37
%38 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 2
store i8 0, i8* %38
%39 = call i32 @_write_base64_unit(i8* %22, i8* %5)
%40 = getelementptr inbounds i8, i8* %22, i64 4
%41 = getelementptr inbounds i8, i8* %22, i64 3
store i8 61, i8* %41
%42 = icmp eq i32 %7, 1
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = getelementptr inbounds i8, i8* %22, i64 2
store i8 61, i8* %44
br label %45
45: ; preds = %35, %43, %20
%46 = phi i8* [ %40, %43 ], [ %40, %35 ], [ %22, %20 ]
store i8 0, i8* %46
%47 = shl nsw i32 %21, 2
ret i32 %47
}