#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 mg_base64_final(char *, int);
int mg_base64_update(unsigned char const, char *, int);
int mg_base64_encode(const unsigned char *p, int n, char *to) {
  int i, len = 0;
  for (i = 0; i < n; i++)
    len = mg_base64_update(p[i], to, len);
  len = mg_base64_final(to, len);
  return len;
}


x86 assembly (clang, -O0):
<mg_base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1178 <mg_base64_encode+0x58>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
movzbl %al,%edi
call   1040 <mg_base64_update@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1141 <mg_base64_encode+0x21>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1030 <mg_base64_final@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mg_base64_encode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %esi,%esi
jle    115a <mg_base64_encode+0x3a>
mov    %rdi,%r15
mov    %esi,%r12d
xor    %ebx,%ebx
xor    %eax,%eax
nopl   0x0(%rax)
movzbl (%r15,%rbx,1),%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <mg_base64_update@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1140 <mg_base64_encode+0x20>
jmp    115c <mg_base64_encode+0x3c>
xor    %eax,%eax
mov    %r14,%rdi
mov    %eax,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <mg_base64_final@plt>

LLVM IR (-O0):
define i32 @mg_base64_encode(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 0, i32* %8
store i32 0, i32* %7
br label %9
9: ; preds = %22, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %25
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = load i8*, i8** %6
%20 = load i32, i32* %8
%21 = call i32 @mg_base64_update(i8 zeroext %18, i8* %19, i32 %20)
store i32 %21, i32* %8
br label %22
22: ; preds = %13
%23 = load i32, i32* %7
%24 = add nsw i32 %23, 1
store i32 %24, i32* %7
br label %9
25: ; preds = %9
%26 = load i8*, i8** %6
%27 = load i32, i32* %8
%28 = call i32 @mg_base64_final(i8* %26, i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
ret i32 %29
}

LLVM IR (-O2):
define i32 @mg_base64_encode(i8* nocapture %0, i32 %1, i8* %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %15
5: ; preds = %3
%6 = zext i32 %1 to i64
br label %7
7: ; preds = %5, %7
%8 = phi i64 [ 0, %5 ], [ %13, %7 ]
%9 = phi i32 [ 0, %5 ], [ %12, %7 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %8
%11 = load i8, i8* %10
%12 = tail call i32 @mg_base64_update(i8 zeroext %11, i8* %2, i32 %9)
%13 = add nuw nsw i64 %8, 1
%14 = icmp eq i64 %13, %6
br i1 %14, label %15, label %7
15: ; preds = %7, %3
%16 = phi i32 [ 0, %3 ], [ %12, %7 ]
%17 = tail call i32 @mg_base64_final(i8* %2, i32 %16)
ret i32 %17
}