#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[0x1000], *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    $0x4050,%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     11c0 <BRBase58CheckEncode+0x50>
lea    -0x4030(%rbp),%rax
mov    %rax,-0x4040(%rbp)
jmp    11d0 <BRBase58CheckEncode+0x60>
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x4040(%rbp)
mov    -0x4040(%rbp),%rax
mov    %rax,-0x4038(%rbp)
cmpq   $0x0,-0x4038(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    $0x1,%al
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x4041(%rbp)
jne    1214 <BRBase58CheckEncode+0xa4>
cmpq   $0x0,-0x20(%rbp)
sete   %al
mov    %al,-0x4041(%rbp)
mov    -0x4041(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
jne    123a <BRBase58CheckEncode+0xca>
cmpq   $0x0,-0x20(%rbp)
jne    128d <BRBase58CheckEncode+0x11d>
mov    -0x4038(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x4038(%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    -0x4038(%rbp),%rdx
mov    -0x20(%rbp),%rcx
add    $0x4,%rcx
call   1090 <BRBase58Encode@plt>
mov    %rax,-0x28(%rbp)
mov    -0x4038(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <mem_clean@plt>
mov    -0x4038(%rbp),%rax
lea    -0x4030(%rbp),%rcx
cmp    %rcx,%rax
je     12c0 <BRBase58CheckEncode+0x150>
mov    -0x4038(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x4050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BRBase58CheckEncode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4018,%rsp
mov    %rcx,%rbx
mov    %rdx,%rbp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r13
lea    0x20(%rcx),%r14
cmp    $0x1000,%r14
ja     11a3 <BRBase58CheckEncode+0x33>
lea    0x10(%rsp),%r15
jmp    11ae <BRBase58CheckEncode+0x3e>
mov    %r14,%rdi
call   1080 <malloc@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1050 <assert@plt>
test   %rbp,%rbp
setne  %al
test   %rbx,%rbx
sete   %cl
or     %al,%cl
movzbl %cl,%r12d
mov    %r12d,%edi
call   1050 <assert@plt>
cmp    $0x1,%r12b
jne    1215 <BRBase58CheckEncode+0xa5>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1070 <memcpy@plt>
lea    (%r15,%rbx,4),%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1040 <BRSHA256_2@plt>
add    $0x4,%rbx
mov    %r13,%rdi
mov    0x8(%rsp),%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1090 <BRBase58Encode@plt>
mov    %rax,%rbx
jmp    1217 <BRBase58CheckEncode+0xa7>
xor    %ebx,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <mem_clean@plt>
lea    0x10(%rsp),%rax
cmp    %rax,%r15
je     1234 <BRBase58CheckEncode+0xc4>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x4018,%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 [4096 x i32]
%12 = 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
%13 = load i64, i64* %8
%14 = add i64 %13, 32
store i64 %14, i64* %10
%15 = load i64, i64* %10
%16 = icmp ule i64 %15, 4096
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = getelementptr inbounds [4096 x i32], [4096 x i32]* %11, i64 0, i64 0
br label %22
19: ; preds = %4
%20 = load i64, i64* %10
%21 = call i32* @malloc(i64 %20)
br label %22
22: ; preds = %19, %17
%23 = phi i32* [ %18, %17 ], [ %21, %19 ]
store i32* %23, i32** %12
%24 = load i32*, i32** %12
%25 = icmp ne i32* %24, null
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i32*, i32** %7
%29 = icmp ne i32* %28, null
br i1 %29, label %33, label %30
30: ; preds = %22
%31 = load i64, i64* %8
%32 = icmp eq i64 %31, 0
br label %33
33: ; preds = %30, %22
%34 = phi i1 [ true, %22 ], [ %32, %30 ]
%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 = %33
%40 = load i64, i64* %8
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %59
42: ; preds = %39, %33
%43 = load i32*, i32** %12
%44 = load i32*, i32** %7
%45 = load i64, i64* %8
%46 = call i32 @memcpy(i32* %43, i32* %44, i64 %45)
%47 = load i32*, i32** %12
%48 = load i64, i64* %8
%49 = getelementptr inbounds i32, i32* %47, i64 %48
%50 = load i32*, i32** %7
%51 = load i64, i64* %8
%52 = call i32 @BRSHA256_2(i32* %49, i32* %50, i64 %51)
%53 = load i8*, i8** %5
%54 = load i64, i64* %6
%55 = load i32*, i32** %12
%56 = load i64, i64* %8
%57 = add i64 %56, 4
%58 = call i64 @BRBase58Encode(i8* %53, i64 %54, i32* %55, i64 %57)
store i64 %58, i64* %9
br label %59
59: ; preds = %42, %39
%60 = load i32*, i32** %12
%61 = load i64, i64* %10
%62 = call i32 @mem_clean(i32* %60, i64 %61)
%63 = load i32*, i32** %12
%64 = getelementptr inbounds [4096 x i32], [4096 x i32]* %11, i64 0, i64 0
%65 = icmp ne i32* %63, %64
br i1 %65, label %66, label %69
66: ; preds = %59
%67 = load i32*, i32** %12
%68 = call i32 @free(i32* %67)
br label %69
69: ; preds = %66, %59
%70 = load i64, i64* %9
ret i64 %70
}

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