#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*/ ASN1_VALUE;
typedef int /*<<< orphan*/ ASN1_ITEM;

/* Variables and functions */
int ASN1_item_ex_i2d(int /*<<< orphan*/ **, unsigned char **,
                     int /*<<< orphan*/ const *, int, int);
unsigned char *OPENSSL_malloc(int);
int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
                        const ASN1_ITEM *it, int flags) {
  if (out && !*out) {
    unsigned char *p, *buf;
    int len;
    len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
    if (len <= 0)
      return len;
    buf = OPENSSL_malloc(len);
    if (!buf)
      return -1;
    p = buf;
    ASN1_item_ex_i2d(&val, &p, it, -1, flags);
    *out = buf;
    return len;
  }
  return ASN1_item_ex_i2d(&val, out, it, -1, flags);
}


x86 assembly (clang, -O0):
<asn1_item_flags_i2d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11dd <asn1_item_flags_i2d+0xbd>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11dd <asn1_item_flags_i2d+0xbd>
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%r8d
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xffffffff,%ecx
call   1030 <ASN1_item_ex_i2d@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     1182 <asn1_item_flags_i2d+0x62>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fa <asn1_item_flags_i2d+0xda>
mov    -0x3c(%rbp),%edi
call   1040 <OPENSSL_malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a5 <asn1_item_flags_i2d+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <asn1_item_flags_i2d+0xda>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%r8d
lea    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0xffffffff,%ecx
call   1030 <ASN1_item_ex_i2d@plt>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fa <asn1_item_flags_i2d+0xda>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%r8d
lea    -0x10(%rbp),%rdi
mov    $0xffffffff,%ecx
call   1030 <ASN1_item_ex_i2d@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<asn1_item_flags_i2d>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,0x8(%rsp)
test   %rsi,%rsi
je     1147 <asn1_item_flags_i2d+0x27>
cmpq   $0x0,(%rbx)
je     1172 <asn1_item_flags_i2d+0x52>
lea    0x8(%rsp),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    $0xffffffff,%ecx
mov    %r14d,%r8d
call   1030 <ASN1_item_ex_i2d@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x8(%rsp),%rdi
mov    $0xffffffff,%ebp
xor    %esi,%esi
mov    %r15,%rdx
mov    $0xffffffff,%ecx
mov    %r14d,%r8d
call   1030 <ASN1_item_ex_i2d@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    11c7 <asn1_item_flags_i2d+0xa7>
mov    %r12d,%edi
call   1040 <OPENSSL_malloc@plt>
test   %rax,%rax
je     1161 <asn1_item_flags_i2d+0x41>
mov    %rax,%r13
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %r15,%rdx
mov    $0xffffffff,%ecx
mov    %r14d,%r8d
call   1030 <ASN1_item_ex_i2d@plt>
mov    %r13,(%rbx)
mov    %r12d,%ebp
jmp    1161 <asn1_item_flags_i2d+0x41>

LLVM IR (-O0):
define i32 @asn1_item_flags_i2d(i32* %0, i8** %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i32* %0, i32** %6
store i8** %1, i8*** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%13 = load i8**, i8*** %7
%14 = icmp ne i8** %13, null
br i1 %14, label %15, label %41
15: ; preds = %4
%16 = load i8**, i8*** %7
%17 = load i8*, i8** %16
%18 = icmp ne i8* %17, null
br i1 %18, label %41, label %19
19: ; preds = %15
%20 = load i32*, i32** %8
%21 = load i32, i32* %9
%22 = call i32 @ASN1_item_ex_i2d(i32** %6, i8** null, i32* %20, i32 -1, i32 %21)
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = load i32, i32* %12
store i32 %26, i32* %5
br label %46
27: ; preds = %19
%28 = load i32, i32* %12
%29 = call i8* @OPENSSL_malloc(i32 %28)
store i8* %29, i8** %11
%30 = load i8*, i8** %11
%31 = icmp ne i8* %30, null
br i1 %31, label %33, label %32
32: ; preds = %27
store i32 -1, i32* %5
br label %46
33: ; preds = %27
%34 = load i8*, i8** %11
store i8* %34, i8** %10
%35 = load i32*, i32** %8
%36 = load i32, i32* %9
%37 = call i32 @ASN1_item_ex_i2d(i32** %6, i8** %10, i32* %35, i32 -1, i32 %36)
%38 = load i8*, i8** %11
%39 = load i8**, i8*** %7
store i8* %38, i8** %39
%40 = load i32, i32* %12
store i32 %40, i32* %5
br label %46
41: ; preds = %15, %4
%42 = load i8**, i8*** %7
%43 = load i32*, i32** %8
%44 = load i32, i32* %9
%45 = call i32 @ASN1_item_ex_i2d(i32** %6, i8** %42, i32* %43, i32 -1, i32 %44)
store i32 %45, i32* %5
br label %46
46: ; preds = %41, %33, %32, %25
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
define i32 @asn1_item_flags_i2d(i32* %0, i8** %1, i32* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i8*
store i32* %0, i32** %5
%7 = icmp eq i8** %1, null
br i1 %7, label %22, label %8
8: ; preds = %4
%9 = load i8*, i8** %1
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %22
11: ; preds = %8
%12 = bitcast i8** %6 to i8*
%13 = call i32 @ASN1_item_ex_i2d(i32** %5, i8** null, i32* %2, i32 -1, i32 %3)
%14 = icmp slt i32 %13, 1
br i1 %14, label %20, label %15
15: ; preds = %11
%16 = call i8* @OPENSSL_malloc(i32 %13)
%17 = icmp eq i8* %16, null
br i1 %17, label %20, label %18
18: ; preds = %15
store i8* %16, i8** %6
%19 = call i32 @ASN1_item_ex_i2d(i32** %5, i8** %6, i32* %2, i32 -1, i32 %3)
store i8* %16, i8** %1
br label %20
20: ; preds = %15, %11, %18
%21 = phi i32 [ %13, %18 ], [ %13, %11 ], [ -1, %15 ]
br label %24
22: ; preds = %8, %4
%23 = call i32 @ASN1_item_ex_i2d(i32** %5, i8** %1, i32* %2, i32 -1, i32 %3)
br label %24
24: ; preds = %22, %20
%25 = phi i32 [ %23, %22 ], [ %21, %20 ]
ret i32 %25
}