#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 */
typedef int /*<<< orphan*/ buf2;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int b64_ntop(unsigned char *, size_t, char *, int);
int /*<<< orphan*/ errx(int, char *);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
void base64_encode(void) {
  unsigned char buf[3];
  char buf2[sizeof(buf) * 2 + 1];
  size_t n;
  int rv, sequence;
  sequence = 0;
  while ((n = fread(buf, 1, sizeof(buf), stdin))) {
    ++sequence;
    rv = b64_ntop(buf, n, buf2, (sizeof(buf2) / sizeof(buf2[0])));
    if (rv == -1)
      errx(1, "b64_ntop: error encoding base64");
    printf("%s%s", buf2, (sequence % ((70 / 4) - 1)) ? "" : "\n");
  }
  if (sequence % ((70 / 4) - 1))
    printf("\n");
}


x86 assembly (clang, -O0):
<base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x20(%rbp)
lea    -0x3(%rbp),%rdi
mov    0x2e8e(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x3,%edx
call   1030 <fread@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11ef <base64_encode+0xaf>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
lea    -0x3(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0xa(%rbp),%rdx
mov    $0x7,%ecx
call   1060 <b64_ntop@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b6 <base64_encode+0x76>
mov    $0x1,%edi
lea    0xe4f(%rip),%rsi
call   1040 <errx@plt>
lea    -0xa(%rbp),%rsi
mov    -0x20(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %edx,%ecx
lea    0xe57(%rip),%rdx
lea    0xe51(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    114f <base64_encode+0xf>
mov    -0x20(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1211 <base64_encode+0xd1>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e83(%rip),%rax
mov    (%rax),%ecx
lea    0xe(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
call   1040 <fread@plt>
test   %rax,%rax
je     1227 <base64_encode+0xd7>
lea    0xe(%rsp),%r14
lea    0x11(%rsp),%rbx
lea    0xe90(%rip),%rbp
lea    0xe8a(%rip),%r15
lea    0xe7d(%rip),%r12
xor    %r13d,%r13d
jmp    11e8 <base64_encode+0x98>
nopl   0x0(%rax,%rax,1)
test   $0xf,%r13b
mov    %r15,%rdx
cmove  %rbp,%rdx
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x3,%edx
call   1040 <fread@plt>
test   %rax,%rax
je     1217 <base64_encode+0xc7>
add    $0x1,%r13d
mov    %r14,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
mov    $0x7,%ecx
call   1070 <b64_ntop@plt>
cmp    $0xffffffff,%eax
jne    11b0 <base64_encode+0x60>
mov    $0x1,%edi
lea    0xdf0(%rip),%rsi
call   1050 <errx@plt>
jmp    11b0 <base64_encode+0x60>
test   $0xf,%r13b
je     1227 <base64_encode+0xd7>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [32 x i8] c"b64_ntop: error encoding base64\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [2 x i8] c"\0A\00"
define void @base64_encode() {
%1 = alloca [3 x i8]
%2 = alloca [7 x i8]
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %5
br label %6
6: ; preds = %22, %0
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%8 = load i32, i32* @stdin
%9 = call i64 @fread(i8* %7, i32 1, i32 3, i32 %8)
store i64 %9, i64* %3
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %30
11: ; preds = %6
%12 = load i32, i32* %5
%13 = add nsw i32 %12, 1
store i32 %13, i32* %5
%14 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%15 = load i64, i64* %3
%16 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
%17 = call i32 @b64_ntop(i8* %14, i64 %15, i8* %16, i32 7)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = call i32 @errx(i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %11
%23 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
%24 = load i32, i32* %5
%25 = srem i32 %24, 16
%26 = icmp ne i32 %25, 0
%27 = zext i1 %26 to i64
%28 = select i1 %26, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %28)
br label %6
30: ; preds = %6
%31 = load i32, i32* %5
%32 = srem i32 %31, 16
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [32 x i8] c"b64_ntop: error encoding base64\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [2 x i8] c"\0A\00"
define void @base64_encode() {
%1 = alloca [3 x i8]
%2 = alloca [7 x i8]
%3 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @stdin
%6 = call i64 @fread(i8* %3, i32 1, i32 3, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %29, label %8
8: ; preds = %0, %16
%9 = phi i64 [ %22, %16 ], [ %6, %0 ]
%10 = phi i32 [ %11, %16 ], [ 0, %0 ]
%11 = add nuw nsw i32 %10, 1
%12 = call i32 @b64_ntop(i8* %3, i64 %9, i8* %4, i32 7)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = call i32 @errx(i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %8
%17 = and i32 %11, 15
%18 = icmp eq i32 %17, 0
%19 = select i1 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %19)
%21 = load i32, i32* @stdin
%22 = call i64 @fread(i8* %3, i32 1, i32 3, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %8
24: ; preds = %16
%25 = and i32 %11, 15
%26 = icmp eq i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 @putchar(i32 10)
br label %29
29: ; preds = %0, %27, %24
ret void
}