#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_8__ TYPE_2__;
typedef struct TYPE_7__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ X509_EXTENSION;
struct TYPE_7__ {
  int (*i2d)(void *, unsigned char **);
  scalar_t__ it;
};
typedef TYPE_1__ X509V3_EXT_METHOD;
struct TYPE_8__ {
  unsigned char *data;
  int length;
};
typedef TYPE_2__ ASN1_OCTET_STRING;

/* Variables and functions */
int /*<<< orphan*/ ASN1_ITEM_ptr(scalar_t__);
int /*<<< orphan*/ ASN1_OCTET_STRING_free(TYPE_2__ *);
TYPE_2__ *ASN1_OCTET_STRING_new();
int ASN1_item_i2d(void *, unsigned char **, int /*<<< orphan*/);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ X509V3_F_DO_EXT_I2D;
int /*<<< orphan*/ X509V3err(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *X509_EXTENSION_create_by_NID(int /*<<< orphan*/ *, int, int,
                                                 TYPE_2__ *);
unsigned char *malloc(int);
int stub1(void *, unsigned char **);
int stub2(void *, unsigned char **);
X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid,
                           int crit, void *ext_struc) {
  unsigned char *ext_der;
  int ext_len;
  ASN1_OCTET_STRING *ext_oct = NULL;
  X509_EXTENSION *ext;
  if (method->it) {
    ext_der = NULL;
    ext_len = ASN1_item_i2d(ext_struc, &ext_der, ASN1_ITEM_ptr(method->it));
    if (ext_len < 0)
      goto merr;
  } else {
    unsigned char *p;
    ext_len = method->i2d(ext_struc, NULL);
    if (!(ext_der = malloc(ext_len)))
      goto merr;
    p = ext_der;
    method->i2d(ext_struc, &p);
  }
  if (!(ext_oct = ASN1_OCTET_STRING_new()))
    goto merr;
  ext_oct->data = ext_der;
  ext_oct->length = ext_len;
  ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
  if (!ext)
    goto merr;
  ASN1_OCTET_STRING_free(ext_oct);
  return ext;
merr:
  ASN1_OCTET_STRING_free(ext_oct);
  X509V3err(X509V3_F_DO_EXT_I2D, ERR_R_MALLOC_FAILURE);
  return NULL;
}


x86 assembly (clang, -O0):
<do_ext_i2d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11e0 <do_ext_i2d+0x70>
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <ASN1_ITEM_ptr@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
lea    -0x28(%rbp),%rsi
call   1080 <ASN1_item_i2d@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11db <do_ext_i2d+0x6b>
jmp    1294 <do_ext_i2d+0x124>
jmp    1228 <do_ext_i2d+0xb8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rdi
xor    %ecx,%ecx
mov    %ecx,%esi
call   *%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    120f <do_ext_i2d+0x9f>
jmp    1294 <do_ext_i2d+0x124>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   *%rax
mov    $0x0,%al
call   1040 <ASN1_OCTET_STRING_new@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1242 <do_ext_i2d+0xd2>
jmp    1294 <do_ext_i2d+0x124>
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x38(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
call   1070 <X509_EXTENSION_create_by_NID@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    127e <do_ext_i2d+0x10e>
jmp    1294 <do_ext_i2d+0x124>
mov    -0x38(%rbp),%rdi
call   1050 <ASN1_OCTET_STRING_free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12bc <do_ext_i2d+0x14c>
mov    -0x38(%rbp),%rdi
call   1050 <ASN1_OCTET_STRING_free@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
call   1090 <X509V3err@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_ext_i2d>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r12
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbp
mov    0x8(%rdi),%rdi
test   %rdi,%rdi
je     11b3 <do_ext_i2d+0x43>
movq   $0x0,(%rsp)
call   1030 <ASN1_ITEM_ptr@plt>
mov    %rsp,%rsi
mov    %r12,%rdi
mov    %eax,%edx
call   1080 <ASN1_item_i2d@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11dd <do_ext_i2d+0x6d>
jmp    1219 <do_ext_i2d+0xa9>
mov    %r12,%rdi
xor    %esi,%esi
call   *0x0(%rbp)
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     1219 <do_ext_i2d+0xa9>
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   *0x0(%rbp)
xor    %eax,%eax
call   1040 <ASN1_OCTET_STRING_new@plt>
test   %rax,%rax
je     1219 <do_ext_i2d+0xa9>
mov    %rax,%rbp
mov    (%rsp),%rax
mov    %rax,0x0(%rbp)
mov    %ebx,0x8(%rbp)
xor    %edi,%edi
mov    %r15d,%esi
mov    %r14d,%edx
mov    %rbp,%rcx
call   1070 <X509_EXTENSION_create_by_NID@plt>
test   %rax,%rax
je     121b <do_ext_i2d+0xab>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1050 <ASN1_OCTET_STRING_free@plt>
jmp    123c <do_ext_i2d+0xcc>
xor    %ebp,%ebp
mov    %rbp,%rdi
call   1050 <ASN1_OCTET_STRING_free@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   1090 <X509V3err@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_7__ = type { i32 (i8*, i8**)*, i64 }
%struct.TYPE_8__ = type { i8*, i32 }
@X509V3_F_DO_EXT_I2D = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32* @do_ext_i2d(%struct.TYPE_7__* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32*
%6 = alloca %struct.TYPE_7__*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca %struct.TYPE_8__*
%13 = alloca i32*
%14 = alloca i8*
store %struct.TYPE_7__* %0, %struct.TYPE_7__** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store %struct.TYPE_8__* null, %struct.TYPE_8__** %12
%15 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%16 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %15, i32 0, i32 1
%17 = load i64, i64* %16
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %4
store i8* null, i8** %10
%20 = load i8*, i8** %9
%21 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%22 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %21, i32 0, i32 1
%23 = load i64, i64* %22
%24 = call i32 @ASN1_ITEM_ptr(i64 %23)
%25 = call i32 @ASN1_item_i2d(i8* %20, i8** %10, i32 %24)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %19
br label %69
29: ; preds = %19
br label %47
30: ; preds = %4
%31 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%32 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %31, i32 0, i32 0
%33 = load i32 (i8*, i8**)*, i32 (i8*, i8**)** %32
%34 = load i8*, i8** %9
%35 = call i32 %33(i8* %34, i8** null)
store i32 %35, i32* %11
%36 = load i32, i32* %11
%37 = call i8* @malloc(i32 %36)
store i8* %37, i8** %10
%38 = icmp ne i8* %37, null
br i1 %38, label %40, label %39
39: ; preds = %30
br label %69
40: ; preds = %30
%41 = load i8*, i8** %10
store i8* %41, i8** %14
%42 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%43 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %42, i32 0, i32 0
%44 = load i32 (i8*, i8**)*, i32 (i8*, i8**)** %43
%45 = load i8*, i8** %9
%46 = call i32 %44(i8* %45, i8** %14)
br label %47
47: ; preds = %40, %29
%48 = call %struct.TYPE_8__* (...) @ASN1_OCTET_STRING_new()
store %struct.TYPE_8__* %48, %struct.TYPE_8__** %12
%49 = icmp ne %struct.TYPE_8__* %48, null
br i1 %49, label %51, label %50
50: ; preds = %47
br label %69
51: ; preds = %47
%52 = load i8*, i8** %10
%53 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%54 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %53, i32 0, i32 0
store i8* %52, i8** %54
%55 = load i32, i32* %11
%56 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%57 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %56, i32 0, i32 1
store i32 %55, i32* %57
%58 = load i32, i32* %7
%59 = load i32, i32* %8
%60 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%61 = call i32* @X509_EXTENSION_create_by_NID(i32* null, i32 %58, i32 %59, %struct.TYPE_8__* %60)
store i32* %61, i32** %13
%62 = load i32*, i32** %13
%63 = icmp ne i32* %62, null
br i1 %63, label %65, label %64
64: ; preds = %51
br label %69
65: ; preds = %51
%66 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%67 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_8__* %66)
%68 = load i32*, i32** %13
store i32* %68, i32** %5
br label %75
69: ; preds = %64, %50, %39, %28
%70 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%71 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_8__* %70)
%72 = load i32, i32* @X509V3_F_DO_EXT_I2D
%73 = load i32, i32* @ERR_R_MALLOC_FAILURE
%74 = call i32 @X509V3err(i32 %72, i32 %73)
store i32* null, i32** %5
br label %75
75: ; preds = %69, %65
%76 = load i32*, i32** %5
ret i32* %76
}

LLVM IR (-O2):
%struct.TYPE_7__ = type { i32 (i8*, i8**)*, i64 }
%struct.TYPE_8__ = type { i8*, i32 }
@X509V3_F_DO_EXT_I2D = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32* @do_ext_i2d(%struct.TYPE_7__* nocapture %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = bitcast i8** %5 to i8*
%8 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %0, i64 0, i32 1
%9 = load i64, i64* %8
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %4
store i8* null, i8** %5
%12 = tail call i32 @ASN1_ITEM_ptr(i64 %9)
%13 = call i32 @ASN1_item_i2d(i8* %3, i8** %5, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %38, label %26
15: ; preds = %4
%16 = bitcast i8** %6 to i8*
%17 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %0, i64 0, i32 0
%18 = load i32 (i8*, i8**)*, i32 (i8*, i8**)** %17
%19 = tail call i32 %18(i8* %3, i8** null)
%20 = tail call i8* @malloc(i32 %19)
store i8* %20, i8** %5
%21 = icmp eq i8* %20, null
br i1 %21, label %25, label %22
22: ; preds = %15
store i8* %20, i8** %6
%23 = load i32 (i8*, i8**)*, i32 (i8*, i8**)** %17
%24 = call i32 %23(i8* %3, i8** %6)
br label %26
25: ; preds = %15
br label %38
26: ; preds = %22, %11
%27 = phi i32 [ %13, %11 ], [ %19, %22 ]
%28 = call %struct.TYPE_8__* (...) @ASN1_OCTET_STRING_new()
%29 = icmp eq %struct.TYPE_8__* %28, null
br i1 %29, label %38, label %30
30: ; preds = %26
%31 = load i8*, i8** %5
%32 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %28, i64 0, i32 0
store i8* %31, i8** %32
%33 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %28, i64 0, i32 1
store i32 %27, i32* %33
%34 = call i32* @X509_EXTENSION_create_by_NID(i32* null, i32 %1, i32 %2, %struct.TYPE_8__* %28)
%35 = icmp eq i32* %34, null
br i1 %35, label %38, label %36
36: ; preds = %30
%37 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_8__* %28)
br label %44
38: ; preds = %25, %30, %26, %11
%39 = phi %struct.TYPE_8__* [ null, %11 ], [ %28, %30 ], [ null, %26 ], [ null, %25 ]
%40 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_8__* %39)
%41 = load i32, i32* @X509V3_F_DO_EXT_I2D
%42 = load i32, i32* @ERR_R_MALLOC_FAILURE
%43 = call i32 @X509V3err(i32 %41, i32 %42)
br label %44
44: ; preds = %38, %36
%45 = phi i32* [ null, %38 ], [ %34, %36 ]
ret i32* %45
}