#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 /*<<< orphan*/);
unsigned char *OPENSSL_malloc(int);
int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it) {
  if (out && !*out) {
    unsigned char *p, *buf;
    int len;
    len = ASN1_item_ex_i2d(&val, NULL, it, -1, 0);
    if (len <= 0)
      return len;
    buf = OPENSSL_malloc(len);
    if (!buf)
      return -1;
    p = buf;
    ASN1_item_ex_i2d(&val, &p, it, -1, 0);
    *out = buf;
    return len;
  }
  return ASN1_item_ex_i2d(&val, out, it, -1, 0);
}


x86 assembly (clang, -O0):
<ASN1_item_i2d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d8 <ASN1_item_i2d+0xb8>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11d8 <ASN1_item_i2d+0xb8>
mov    -0x20(%rbp),%rdx
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   1030 <ASN1_item_ex_i2d@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     117e <ASN1_item_i2d+0x5e>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f4 <ASN1_item_i2d+0xd4>
mov    -0x34(%rbp),%edi
call   1040 <OPENSSL_malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a1 <ASN1_item_i2d+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f4 <ASN1_item_i2d+0xd4>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   1030 <ASN1_item_ex_i2d@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f4 <ASN1_item_i2d+0xd4>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x10(%rbp),%rdi
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
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_i2d>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,(%rsp)
test   %rsi,%rsi
je     1141 <ASN1_item_i2d+0x21>
cmpq   $0x0,(%rbx)
je     1168 <ASN1_item_i2d+0x48>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   1030 <ASN1_item_ex_i2d@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%rdi
mov    $0xffffffff,%ebp
xor    %esi,%esi
mov    %r14,%rdx
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   1030 <ASN1_item_ex_i2d@plt>
mov    %eax,%r15d
test   %eax,%eax
jle    11b9 <ASN1_item_i2d+0x99>
mov    %r15d,%edi
call   1040 <OPENSSL_malloc@plt>
test   %rax,%rax
je     1159 <ASN1_item_i2d+0x39>
mov    %rax,%r12
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %r14,%rdx
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   1030 <ASN1_item_ex_i2d@plt>
mov    %r12,(%rbx)
mov    %r15d,%ebp
jmp    1159 <ASN1_item_i2d+0x39>

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

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