#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*/ uint8_t;

/* Variables and functions */
size_t BRBase58Encode(char *, size_t, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ BRSHA256_2(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                              size_t);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(size_t);
int /*<<< orphan*/ mem_clean(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          size_t);
size_t BRBase58CheckEncode(char *str, size_t strLen, const uint8_t *data,
                           size_t dataLen) {
  size_t len = 0, bufLen = dataLen + 256 / 8;
  uint8_t _buf[(bufLen <= 0x1000) ? bufLen : 0],
      *buf = (bufLen <= 0x1000) ? _buf : malloc(bufLen);
  assert(buf != NULL);
  assert(data != NULL || dataLen == 0);
  if (data || dataLen == 0) {
    memcpy(buf, data, dataLen);
    BRSHA256_2(&buf[dataLen], data, dataLen);
    len = BRBase58Encode(str, strLen, buf, dataLen + 4);
  }
  mem_clean(buf, bufLen);
  if (buf != _buf)
    free(buf);
  return len;
}


x86 assembly (clang, -O0):
<BRBase58CheckEncode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x1000,-0x30(%rbp)
ja     11b7 <BRBase58CheckEncode+0x47>
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    11c2 <BRBase58CheckEncode+0x52>
xor    %eax,%eax
mov    %rax,-0x50(%rbp)
jmp    11c2 <BRBase58CheckEncode+0x52>
mov    -0x50(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
cmpq   $0x1000,-0x30(%rbp)
ja     1205 <BRBase58CheckEncode+0x95>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    1212 <BRBase58CheckEncode+0xa2>
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    $0x1,%al
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x61(%rbp)
jne    1247 <BRBase58CheckEncode+0xd7>
cmpq   $0x0,-0x20(%rbp)
sete   %al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
jne    126a <BRBase58CheckEncode+0xfa>
cmpq   $0x0,-0x20(%rbp)
jne    12b4 <BRBase58CheckEncode+0x144>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <BRSHA256_2@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x20(%rbp),%rcx
add    $0x4,%rcx
call   1090 <BRBase58Encode@plt>
mov    %rax,-0x28(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <mem_clean@plt>
mov    -0x58(%rbp),%rax
cmp    %rax,-0x48(%rbp)
je     12d8 <BRBase58CheckEncode+0x168>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BRBase58CheckEncode>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%r13
mov    %rsi,-0x40(%rbp)
mov    %rdi,-0x38(%rbp)
lea    0x20(%rcx),%r14
xor    %eax,%eax
cmp    $0x1001,%r14
cmovb  %r14,%rax
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r12
sub    %rax,%r12
mov    %r12,%rsp
mov    %r12,%rbx
cmp    $0x1001,%r14
jb     11cc <BRBase58CheckEncode+0x5c>
mov    %r14,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
test   %r13,%r13
setne  %al
test   %r15,%r15
sete   %cl
or     %al,%cl
mov    %r13,-0x30(%rbp)
movzbl %cl,%r13d
mov    %r13d,%edi
call   1050 <assert@plt>
cmp    $0x1,%r13b
jne    123b <BRBase58CheckEncode+0xcb>
mov    %rbx,%rdi
mov    -0x30(%rbp),%r13
mov    %r13,%rsi
mov    %r15,%rdx
call   1070 <memcpy@plt>
lea    (%rbx,%r15,4),%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1040 <BRSHA256_2@plt>
add    $0x4,%r15
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1090 <BRBase58Encode@plt>
mov    %rax,%r15
jmp    123e <BRBase58CheckEncode+0xce>
xor    %r15d,%r15d
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <mem_clean@plt>
cmp    %r12,%rbx
je     1256 <BRBase58CheckEncode+0xe6>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @BRBase58CheckEncode(i8* %0, i64 %1, i32* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32* %2, i32** %7
store i64 %3, i64* %8
store i64 0, i64* %9
%14 = load i64, i64* %8
%15 = add i64 %14, 32
store i64 %15, i64* %10
%16 = load i64, i64* %10
%17 = icmp ule i64 %16, 4096
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = load i64, i64* %10
br label %21
20: ; preds = %4
br label %21
21: ; preds = %20, %18
%22 = phi i64 [ %19, %18 ], [ 0, %20 ]
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %11
%24 = alloca i32, i64 %22
store i64 %22, i64* %12
%25 = load i64, i64* %10
%26 = icmp ule i64 %25, 4096
br i1 %26, label %27, label %28
27: ; preds = %21
br label %31
28: ; preds = %21
%29 = load i64, i64* %10
%30 = call i32* @malloc(i64 %29)
br label %31
31: ; preds = %28, %27
%32 = phi i32* [ %24, %27 ], [ %30, %28 ]
store i32* %32, i32** %13
%33 = load i32*, i32** %13
%34 = icmp ne i32* %33, null
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = load i32*, i32** %7
%38 = icmp ne i32* %37, null
br i1 %38, label %42, label %39
39: ; preds = %31
%40 = load i64, i64* %8
%41 = icmp eq i64 %40, 0
br label %42
42: ; preds = %39, %31
%43 = phi i1 [ true, %31 ], [ %41, %39 ]
%44 = zext i1 %43 to i32
%45 = call i32 @assert(i32 %44)
%46 = load i32*, i32** %7
%47 = icmp ne i32* %46, null
br i1 %47, label %51, label %48
48: ; preds = %42
%49 = load i64, i64* %8
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %68
51: ; preds = %48, %42
%52 = load i32*, i32** %13
%53 = load i32*, i32** %7
%54 = load i64, i64* %8
%55 = call i32 @memcpy(i32* %52, i32* %53, i64 %54)
%56 = load i32*, i32** %13
%57 = load i64, i64* %8
%58 = getelementptr inbounds i32, i32* %56, i64 %57
%59 = load i32*, i32** %7
%60 = load i64, i64* %8
%61 = call i32 @BRSHA256_2(i32* %58, i32* %59, i64 %60)
%62 = load i8*, i8** %5
%63 = load i64, i64* %6
%64 = load i32*, i32** %13
%65 = load i64, i64* %8
%66 = add i64 %65, 4
%67 = call i64 @BRBase58Encode(i8* %62, i64 %63, i32* %64, i64 %66)
store i64 %67, i64* %9
br label %68
68: ; preds = %51, %48
%69 = load i32*, i32** %13
%70 = load i64, i64* %10
%71 = call i32 @mem_clean(i32* %69, i64 %70)
%72 = load i32*, i32** %13
%73 = icmp ne i32* %72, %24
br i1 %73, label %74, label %77
74: ; preds = %68
%75 = load i32*, i32** %13
%76 = call i32 @free(i32* %75)
br label %77
77: ; preds = %74, %68
%78 = load i64, i64* %9
%79 = load i8*, i8** %11
ret i64 %78
}

LLVM IR (-O2):
define i64 @BRBase58CheckEncode(i8* %0, i64 %1, i32* %2, i64 %3) {
%5 = add i64 %3, 32
%6 = icmp ult i64 %5, 4097
%7 = select i1 %6, i64 %5, i64 0
%8 = alloca i32, i64 %7
br i1 %6, label %11, label %9
9: ; preds = %4
%10 = tail call i32* @malloc(i64 %5)
br label %11
11: ; preds = %4, %9
%12 = phi i32* [ %10, %9 ], [ %8, %4 ]
%13 = icmp ne i32* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = icmp ne i32* %2, null
%17 = icmp eq i64 %3, 0
%18 = or i1 %16, %17
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
br i1 %18, label %21, label %27
21: ; preds = %11
%22 = call i32 @memcpy(i32* %12, i32* %2, i64 %3)
%23 = getelementptr inbounds i32, i32* %12, i64 %3
%24 = call i32 @BRSHA256_2(i32* %23, i32* %2, i64 %3)
%25 = add i64 %3, 4
%26 = call i64 @BRBase58Encode(i8* %0, i64 %1, i32* %12, i64 %25)
br label %27
27: ; preds = %11, %21
%28 = phi i64 [ %26, %21 ], [ 0, %11 ]
%29 = call i32 @mem_clean(i32* %12, i64 %5)
%30 = icmp eq i32* %12, %8
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @free(i32* %12)
br label %33
33: ; preds = %31, %27
ret i64 %28
}