#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  char *data;
};
typedef TYPE_1__ BUF_MEM;
typedef int /*<<< orphan*/ BIO;
typedef int /*<<< orphan*/ ASN1_INTEGER;

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_get_mem_ptr(int /*<<< orphan*/ *, TYPE_1__ **);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_s_mem();
int i2a_ASN1_INTEGER(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *);
void ASN1IntToCString(ASN1_INTEGER *asnInt, char *casnInt, size_t bufsize) {
  if (asnInt) {
    BIO *mbio = BIO_new(BIO_s_mem());
    if (mbio) {
      int bytesWritten;
      if ((bytesWritten = i2a_ASN1_INTEGER(mbio, asnInt)) != -1) {
        BUF_MEM *out = 0;
        BIO_get_mem_ptr(mbio, &out);
        if (out) {
          snprintf(casnInt, bufsize, "%s", out->data);
          if (bytesWritten < (int)bufsize)
            memset(casnInt + bytesWritten, 0, bufsize - bytesWritten);
        }
      }
      BIO_free(mbio);
    }
  }
}


x86 assembly (clang, -O0):
<ASN1IntToCString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1244 <ASN1IntToCString+0xd4>
mov    $0x0,%al
call   1080 <BIO_s_mem@plt>
mov    %eax,%edi
call   1050 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     123f <ASN1IntToCString+0xcf>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1090 <i2a_ASN1_INTEGER@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1236 <ASN1IntToCString+0xc6>
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <BIO_get_mem_ptr@plt>
cmpq   $0x0,-0x30(%rbp)
je     1231 <ASN1IntToCString+0xc1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xe05(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
jge    122c <ASN1IntToCString+0xbc>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
movslq -0x24(%rbp),%rax
sub    %rax,%rdx
xor    %esi,%esi
call   1040 <memset@plt>
jmp    1231 <ASN1IntToCString+0xc1>
jmp    1236 <ASN1IntToCString+0xc6>
mov    -0x20(%rbp),%rdi
call   1060 <BIO_free@plt>
jmp    1244 <ASN1IntToCString+0xd4>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ASN1IntToCString>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1205 <ASN1IntToCString+0x95>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbp
xor    %eax,%eax
call   1080 <BIO_s_mem@plt>
mov    %eax,%edi
call   1050 <BIO_new@plt>
test   %rax,%rax
je     1205 <ASN1IntToCString+0x95>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1090 <i2a_ASN1_INTEGER@plt>
cmp    $0xffffffff,%eax
je     11fd <ASN1IntToCString+0x8d>
mov    %eax,%ebp
movq   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <BIO_get_mem_ptr@plt>
mov    (%rsp),%rax
test   %rax,%rax
je     11fd <ASN1IntToCString+0x8d>
mov    (%rax),%rcx
lea    0xe29(%rip),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <snprintf@plt>
cmp    %r15d,%ebp
jge    11fd <ASN1IntToCString+0x8d>
movslq %ebp,%rax
add    %rax,%r14
sub    %rax,%r15
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1040 <memset@plt>
mov    %rbx,%rdi
call   1060 <BIO_free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i8* }
@.str = [3 x i8] c"%s\00"
define void @ASN1IntToCString(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.TYPE_3__*
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %54
12: ; preds = %3
%13 = call i32 (...) @BIO_s_mem()
%14 = call i32* @BIO_new(i32 %13)
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %53
17: ; preds = %12
%18 = load i32*, i32** %7
%19 = load i32*, i32** %4
%20 = call i32 @i2a_ASN1_INTEGER(i32* %18, i32* %19)
store i32 %20, i32* %8
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %50
22: ; preds = %17
store %struct.TYPE_3__* null, %struct.TYPE_3__** %9
%23 = load i32*, i32** %7
%24 = call i32 @BIO_get_mem_ptr(i32* %23, %struct.TYPE_3__** %9)
%25 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%26 = icmp ne %struct.TYPE_3__* %25, null
br i1 %26, label %27, label %49
27: ; preds = %22
%28 = load i8*, i8** %5
%29 = load i64, i64* %6
%30 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%31 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @snprintf(i8* %28, i64 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = load i32, i32* %8
%35 = load i64, i64* %6
%36 = trunc i64 %35 to i32
%37 = icmp slt i32 %34, %36
br i1 %37, label %38, label %48
38: ; preds = %27
%39 = load i8*, i8** %5
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i64, i64* %6
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = sub i64 %43, %45
%47 = call i32 @memset(i8* %42, i32 0, i64 %46)
br label %48
48: ; preds = %38, %27
br label %49
49: ; preds = %48, %22
br label %50
50: ; preds = %49, %17
%51 = load i32*, i32** %7
%52 = call i32 @BIO_free(i32* %51)
br label %53
53: ; preds = %50, %12
br label %54
54: ; preds = %53, %3
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i8* }
@.str = [3 x i8] c"%s\00"
define void @ASN1IntToCString(i32* %0, i8* %1, i64 %2) {
%4 = alloca %struct.TYPE_3__*
%5 = icmp eq i32* %0, null
br i1 %5, label %32, label %6
6: ; preds = %3
%7 = tail call i32 (...) @BIO_s_mem()
%8 = tail call i32* @BIO_new(i32 %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %32, label %10
10: ; preds = %6
%11 = tail call i32 @i2a_ASN1_INTEGER(i32* %8, i32* %0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %30, label %13
13: ; preds = %10
%14 = bitcast %struct.TYPE_3__** %4 to i8*
store %struct.TYPE_3__* null, %struct.TYPE_3__** %4
%15 = call i32 @BIO_get_mem_ptr(i32* %8, %struct.TYPE_3__** %4)
%16 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%17 = icmp eq %struct.TYPE_3__* %16, null
br i1 %17, label %29, label %18
18: ; preds = %13
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %16, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = trunc i64 %2 to i32
%23 = icmp slt i32 %11, %22
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = sext i32 %11 to i64
%26 = getelementptr inbounds i8, i8* %1, i64 %25
%27 = sub i64 %2, %25
%28 = call i32 @memset(i8* %26, i32 0, i64 %27)
br label %29
29: ; preds = %18, %24, %13
br label %30
30: ; preds = %29, %10
%31 = call i32 @BIO_free(i32* %8)
br label %32
32: ; preds = %6, %30, %3
ret void
}