#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 length;
typedef int chunk_size;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ rhash_byte_to_base64(char *, unsigned char const *, size_t);
size_t rhash_urlencode(char *, char *, int, int);
size_t rhash_base64_url_encoded_helper(char *dst, const unsigned char *src,
                                       size_t length, int url_encode,
                                       int upper_case) {
  char buffer[164];
  assert((((((120) + 2) / 3) * 4) + 4) <= sizeof(buffer));
  assert((120 % 6) == 0);
  if (url_encode) {
    size_t result_length = 0;
    for (; length > 0; src += 120) {
      size_t chunk_size = (length < 120 ? length : 120);
      size_t encoded_length;
      rhash_byte_to_base64(buffer, src, chunk_size);
      encoded_length = rhash_urlencode(
          dst, buffer, ((((chunk_size) + 2) / 3) * 4), upper_case);
      result_length += encoded_length;
      dst += encoded_length;
      length -= chunk_size;
    }
    return result_length;
  }
  rhash_byte_to_base64(dst, src, length);
  return ((((length) + 2) / 3) * 4);
}


x86 assembly (clang, -O0):
<rhash_base64_url_encoded_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    $0x1,%edi
call   1040 <assert@plt>
mov    $0x1,%edi
call   1040 <assert@plt>
cmpl   $0x0,-0x24(%rbp)
je     125f <rhash_base64_url_encoded_helper+0x12f>
movq   $0x0,-0xd8(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    124f <rhash_base64_url_encoded_helper+0x11f>
cmpq   $0x78,-0x20(%rbp)
jae    119d <rhash_base64_url_encoded_helper+0x6d>
mov    -0x20(%rbp),%rax
mov    %rax,-0xf0(%rbp)
jmp    11ae <rhash_base64_url_encoded_helper+0x7e>
mov    $0x78,%eax
mov    %rax,-0xf0(%rbp)
jmp    11ae <rhash_base64_url_encoded_helper+0x7e>
mov    -0xf0(%rbp),%rax
mov    %rax,-0xe0(%rbp)
lea    -0xd0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xe0(%rbp),%rdx
call   1030 <rhash_byte_to_base64@plt>
mov    -0x10(%rbp),%rdi
lea    -0xd0(%rbp),%rsi
mov    -0xe0(%rbp),%rax
add    $0x2,%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
shl    $0x2,%rax
mov    %eax,%edx
mov    -0x28(%rbp),%ecx
call   1050 <rhash_urlencode@plt>
mov    %rax,-0xe8(%rbp)
mov    -0xe8(%rbp),%rax
add    -0xd8(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    -0xe8(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0xe0(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x78,%rax
mov    %rax,-0x18(%rbp)
jmp    1177 <rhash_base64_url_encoded_helper+0x47>
mov    -0xd8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    128a <rhash_base64_url_encoded_helper+0x15a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <rhash_byte_to_base64@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
shl    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rhash_base64_url_encoded_helper>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    %r8d,%r12d
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%rbp
mov    $0x1,%edi
call   1040 <assert@plt>
mov    $0x1,%edi
call   1040 <assert@plt>
test   %r15d,%r15d
je     11d3 <rhash_base64_url_encoded_helper+0xa3>
test   %rbx,%rbx
je     1201 <rhash_base64_url_encoded_helper+0xd1>
mov    %r12d,0xc(%rsp)
xor    %r12d,%r12d
lea    0x10(%rsp),%r13
nop
cmp    $0x78,%rbx
mov    $0x78,%r15d
cmovb  %rbx,%r15
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <rhash_byte_to_base64@plt>
lea    0x2(%r15),%eax
movzbl %al,%eax
imul   $0xab,%eax,%eax
shr    $0x8,%eax
add    %al,%al
and    $0xfc,%al
movzbl %al,%edx
mov    %rbp,%rdi
mov    %r13,%rsi
mov    0xc(%rsp),%ecx
call   1050 <rhash_urlencode@plt>
add    %rax,%r12
add    %rax,%rbp
add    $0x78,%r14
sub    %r15,%rbx
jne    1180 <rhash_base64_url_encoded_helper+0x50>
jmp    1204 <rhash_base64_url_encoded_helper+0xd4>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <rhash_byte_to_base64@plt>
add    $0x2,%rbx
movabs $0xaaaaaaaaaaaaaaab,%rcx
aa aa aa
mov    %rbx,%rax
mul    %rcx
mov    %rdx,%r12
add    %rdx,%r12
and    $0xfffffffffffffffc,%r12
jmp    1204 <rhash_base64_url_encoded_helper+0xd4>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i64 @rhash_base64_url_encoded_helper(i8* %0, i8* %1, i64 %2, i32 %3, i32 %4) {
%6 = alloca [164 x i8]
%7 = getelementptr inbounds [164 x i8], [164 x i8]* %6, i64 0, i64 0
%8 = tail call i32 @assert(i32 1)
%9 = tail call i32 @assert(i32 1)
%10 = icmp eq i32 %3, 0
br i1 %10, label %32, label %11
11: ; preds = %5
%12 = icmp eq i64 %2, 0
br i1 %12, label %37, label %13
13: ; preds = %11, %13
%14 = phi i8* [ %28, %13 ], [ %0, %11 ]
%15 = phi i64 [ %27, %13 ], [ 0, %11 ]
%16 = phi i8* [ %30, %13 ], [ %1, %11 ]
%17 = phi i64 [ %29, %13 ], [ %2, %11 ]
%18 = icmp ult i64 %17, 120
%19 = select i1 %18, i64 %17, i64 120
%20 = call i32 @rhash_byte_to_base64(i8* %7, i8* %16, i64 %19)
%21 = trunc i64 %19 to i8
%22 = add nuw nsw i8 %21, 2
%23 = udiv i8 %22, 3
%24 = shl nuw i8 %23, 2
%25 = zext i8 %24 to i32
%26 = call i64 @rhash_urlencode(i8* %14, i8* %7, i32 %25, i32 %4)
%27 = add i64 %26, %15
%28 = getelementptr inbounds i8, i8* %14, i64 %26
%29 = sub i64 %17, %19
%30 = getelementptr inbounds i8, i8* %16, i64 120
%31 = icmp eq i64 %29, 0
br i1 %31, label %37, label %13
32: ; preds = %5
%33 = tail call i32 @rhash_byte_to_base64(i8* %0, i8* %1, i64 %2)
%34 = add i64 %2, 2
%35 = udiv i64 %34, 3
%36 = shl i64 %35, 2
br label %37
37: ; preds = %13, %11, %32
%38 = phi i64 [ %36, %32 ], [ 0, %11 ], [ %27, %13 ]
ret i64 %38
}