#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*/ u_char;
struct sshbuf {
  int dummy;
};

/* Variables and functions */
int SIZE_MAX;
int b64_ntop(int /*<<< orphan*/ const *, size_t, char *, size_t);
int /*<<< orphan*/ bzero(char *, size_t);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
size_t sshbuf_len(struct sshbuf *);
int /*<<< orphan*/ *sshbuf_ptr(struct sshbuf *);
char *strdup(char *);
char *sshbuf_dtob64(struct sshbuf *buf) {
  size_t len = sshbuf_len(buf), plen;
  const u_char *p = sshbuf_ptr(buf);
  char *ret;
  int r;
  if (len == 0)
    return strdup("");
  plen = ((len + 2) / 3) * 4 + 1;
  if (SIZE_MAX / 2 <= len || (ret = malloc(plen)) == NULL)
    return NULL;
  if ((r = b64_ntop(p, len, ret, plen)) == -1) {
    bzero(ret, plen);
    free(ret);
    return NULL;
  }
  return ret;
}


x86 assembly (clang, -O0):
<sshbuf_dtob64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <sshbuf_len@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <sshbuf_ptr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b6 <sshbuf_dtob64+0x46>
lea    0xe58(%rip),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1261 <sshbuf_dtob64+0xf1>
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
shl    $0x2,%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
cmp    -0x18(%rbp),%rax
jbe    1208 <sshbuf_dtob64+0x98>
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1215 <sshbuf_dtob64+0xa5>
movq   $0x0,-0x8(%rbp)
jmp    1261 <sshbuf_dtob64+0xf1>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1080 <b64_ntop@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1259 <sshbuf_dtob64+0xe9>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <bzero@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1261 <sshbuf_dtob64+0xf1>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sshbuf_dtob64>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%rbx
call   1040 <sshbuf_len@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1060 <sshbuf_ptr@plt>
test   %r13,%r13
je     1218 <sshbuf_dtob64+0xa8>
mov    %rax,%r15
lea    0x2(%r13),%rax
movabs $0xaaaaaaaaaaaaaaab,%rcx
aa aa aa
mul    %rcx
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
movslq %ecx,%rax
cmp    %rax,%r13
jae    1208 <sshbuf_dtob64+0x98>
and    $0xfffffffffffffffe,%rdx
lea    (%rdx,%rdx,1),%r12
add    $0x1,%r12
mov    %r12,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1208 <sshbuf_dtob64+0x98>
mov    %rax,%r14
mov    %r15,%rdi
mov    %r13,%rsi
mov    %rax,%rdx
mov    %r12,%rcx
call   1080 <b64_ntop@plt>
cmp    $0xffffffff,%eax
jne    120b <sshbuf_dtob64+0x9b>
mov    %r14,%rdi
mov    %r12,%rsi
call   1070 <bzero@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xde1(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1090 <strdup@plt>

LLVM IR (-O0):
%struct.sshbuf = type { i32 }
@.str = [1 x i8] zeroinitializer
@SIZE_MAX = global i32 0
define i8* @sshbuf_dtob64(%struct.sshbuf* %0) {
%2 = alloca i8*
%3 = alloca %struct.sshbuf*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
store %struct.sshbuf* %0, %struct.sshbuf** %3
%9 = load %struct.sshbuf*, %struct.sshbuf** %3
%10 = call i64 @sshbuf_len(%struct.sshbuf* %9)
store i64 %10, i64* %4
%11 = load %struct.sshbuf*, %struct.sshbuf** %3
%12 = call i32* @sshbuf_ptr(%struct.sshbuf* %11)
store i32* %12, i32** %6
%13 = load i64, i64* %4
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %2
br label %48
17: ; preds = %1
%18 = load i64, i64* %4
%19 = add i64 %18, 2
%20 = udiv i64 %19, 3
%21 = mul i64 %20, 4
%22 = add i64 %21, 1
store i64 %22, i64* %5
%23 = load i32, i32* @SIZE_MAX
%24 = sdiv i32 %23, 2
%25 = sext i32 %24 to i64
%26 = load i64, i64* %4
%27 = icmp ule i64 %25, %26
br i1 %27, label %32, label %28
28: ; preds = %17
%29 = load i64, i64* %5
%30 = call i8* @malloc(i64 %29)
store i8* %30, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %28, %17
store i8* null, i8** %2
br label %48
33: ; preds = %28
%34 = load i32*, i32** %6
%35 = load i64, i64* %4
%36 = load i8*, i8** %7
%37 = load i64, i64* %5
%38 = call i32 @b64_ntop(i32* %34, i64 %35, i8* %36, i64 %37)
store i32 %38, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %46
40: ; preds = %33
%41 = load i8*, i8** %7
%42 = load i64, i64* %5
%43 = call i32 @bzero(i8* %41, i64 %42)
%44 = load i8*, i8** %7
%45 = call i32 @free(i8* %44)
store i8* null, i8** %2
br label %48
46: ; preds = %33
%47 = load i8*, i8** %7
store i8* %47, i8** %2
br label %48
48: ; preds = %46, %40, %32, %15
%49 = load i8*, i8** %2
ret i8* %49
}

LLVM IR (-O2):
%struct.sshbuf = type { i32 }
@.str = [1 x i8] zeroinitializer
@SIZE_MAX = global i32 0
define i8* @sshbuf_dtob64(%struct.sshbuf* %0) {
%2 = tail call i64 @sshbuf_len(%struct.sshbuf* %0)
%3 = tail call i32* @sshbuf_ptr(%struct.sshbuf* %0)
%4 = icmp eq i64 %2, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %25
7: ; preds = %1
%8 = add i64 %2, 2
%9 = udiv i64 %8, 3
%10 = shl i64 %9, 2
%11 = or i64 %10, 1
%12 = load i32, i32* @SIZE_MAX
%13 = sdiv i32 %12, 2
%14 = sext i32 %13 to i64
%15 = icmp ult i64 %2, %14
br i1 %15, label %16, label %25
16: ; preds = %7
%17 = tail call i8* @malloc(i64 %11)
%18 = icmp eq i8* %17, null
br i1 %18, label %25, label %19
19: ; preds = %16
%20 = tail call i32 @b64_ntop(i32* %3, i64 %2, i8* %17, i64 %11)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = tail call i32 @bzero(i8* %17, i64 %11)
%24 = tail call i32 @free(i8* %17)
br label %25
25: ; preds = %19, %7, %16, %22, %5
%26 = phi i8* [ %6, %5 ], [ null, %22 ], [ null, %16 ], [ null, %7 ], [ %17, %19 ]
ret i8* %26
}