#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 **);
  int /*<<< orphan*/ 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_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*/ X509V3error(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, 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);
  X509V3error(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
cmpl   $0x0,0x8(%rax)
je     11bf <do_ext_i2d+0x5f>
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
lea    -0x28(%rbp),%rsi
call   1080 <ASN1_item_i2d@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11ba <do_ext_i2d+0x5a>
jmp    1273 <do_ext_i2d+0x113>
jmp    1207 <do_ext_i2d+0xa7>
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    11ee <do_ext_i2d+0x8e>
jmp    1273 <do_ext_i2d+0x113>
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   1030 <ASN1_OCTET_STRING_new@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1221 <do_ext_i2d+0xc1>
jmp    1273 <do_ext_i2d+0x113>
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    125d <do_ext_i2d+0xfd>
jmp    1273 <do_ext_i2d+0x113>
mov    -0x38(%rbp),%rdi
call   1040 <ASN1_OCTET_STRING_free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1292 <do_ext_i2d+0x132>
mov    -0x38(%rbp),%rdi
call   1040 <ASN1_OCTET_STRING_free@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1050 <X509V3error@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,%rbp
mov    %edx,%r14d
mov    %esi,%r15d
mov    0x8(%rdi),%edx
test   %edx,%edx
je     1198 <do_ext_i2d+0x38>
movq   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %rbp,%rdi
call   1080 <ASN1_item_i2d@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    11c4 <do_ext_i2d+0x64>
jmp    1201 <do_ext_i2d+0xa1>
mov    %rdi,%rbx
mov    %rbp,%rdi
xor    %esi,%esi
call   *(%rbx)
mov    %eax,%r12d
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     1201 <do_ext_i2d+0xa1>
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   *(%rbx)
xor    %eax,%eax
call   1030 <ASN1_OCTET_STRING_new@plt>
test   %rax,%rax
je     1201 <do_ext_i2d+0xa1>
mov    %rax,%rbp
mov    (%rsp),%rax
mov    %rax,0x0(%rbp)
mov    %r12d,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     1203 <do_ext_i2d+0xa3>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1040 <ASN1_OCTET_STRING_free@plt>
jmp    121b <do_ext_i2d+0xbb>
xor    %ebp,%ebp
mov    %rbp,%rdi
call   1040 <ASN1_OCTET_STRING_free@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1050 <X509V3error@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**)*, i32 }
%struct.TYPE_8__ = type { i8*, i32 }
@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 i32, i32* %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %29
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 i32, i32* %22
%24 = call i32 @ASN1_item_i2d(i8* %20, i8** %10, i32 %23)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %19
br label %68
28: ; preds = %19
br label %46
29: ; preds = %4
%30 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%31 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %30, i32 0, i32 0
%32 = load i32 (i8*, i8**)*, i32 (i8*, i8**)** %31
%33 = load i8*, i8** %9
%34 = call i32 %32(i8* %33, i8** null)
store i32 %34, i32* %11
%35 = load i32, i32* %11
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %10
%37 = icmp ne i8* %36, null
br i1 %37, label %39, label %38
38: ; preds = %29
br label %68
39: ; preds = %29
%40 = load i8*, i8** %10
store i8* %40, i8** %14
%41 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%42 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %41, i32 0, i32 0
%43 = load i32 (i8*, i8**)*, i32 (i8*, i8**)** %42
%44 = load i8*, i8** %9
%45 = call i32 %43(i8* %44, i8** %14)
br label %46
46: ; preds = %39, %28
%47 = call %struct.TYPE_8__* (...) @ASN1_OCTET_STRING_new()
store %struct.TYPE_8__* %47, %struct.TYPE_8__** %12
%48 = icmp ne %struct.TYPE_8__* %47, null
br i1 %48, label %50, label %49
49: ; preds = %46
br label %68
50: ; preds = %46
%51 = load i8*, i8** %10
%52 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%53 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %52, i32 0, i32 0
store i8* %51, i8** %53
%54 = load i32, i32* %11
%55 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%56 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %55, i32 0, i32 1
store i32 %54, i32* %56
%57 = load i32, i32* %7
%58 = load i32, i32* %8
%59 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%60 = call i32* @X509_EXTENSION_create_by_NID(i32* null, i32 %57, i32 %58, %struct.TYPE_8__* %59)
store i32* %60, i32** %13
%61 = load i32*, i32** %13
%62 = icmp ne i32* %61, null
br i1 %62, label %64, label %63
63: ; preds = %50
br label %68
64: ; preds = %50
%65 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%66 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_8__* %65)
%67 = load i32*, i32** %13
store i32* %67, i32** %5
br label %73
68: ; preds = %63, %49, %38, %27
%69 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%70 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_8__* %69)
%71 = load i32, i32* @ERR_R_MALLOC_FAILURE
%72 = call i32 @X509V3error(i32 %71)
store i32* null, i32** %5
br label %73
73: ; preds = %68, %64
%74 = load i32*, i32** %5
ret i32* %74
}

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