#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_5__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ X509_ALGOR;
struct TYPE_5__ {
  int /*<<< orphan*/ length;
  int /*<<< orphan*/ data;
};
typedef TYPE_1__ ASN1_OCTET_STRING;
typedef int /*<<< orphan*/ ASN1_ITEM;

/* Variables and functions */
int /*<<< orphan*/ ASN1_OCTET_STRING_free(TYPE_1__ *);
TYPE_1__ *ASN1_OCTET_STRING_new();
int ASN1_item_i2d(void *, unsigned char **, int /*<<< orphan*/ const *);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT;
int /*<<< orphan*/ PKCS12_R_ENCODE_ERROR;
int /*<<< orphan*/ PKCS12_R_ENCRYPT_ERROR;
int /*<<< orphan*/ PKCS12_pbe_crypt(int /*<<< orphan*/ *, char const *, int,
                                    unsigned char *, int, int /*<<< orphan*/ *,
                                    int /*<<< orphan*/ *, int);
int /*<<< orphan*/ PKCS12err(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ explicit_bzero(unsigned char *, int);
int /*<<< orphan*/ free(unsigned char *);
ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
                                           const ASN1_ITEM *it,
                                           const char *pass, int passlen,
                                           void *obj, int zbuf) {
  ASN1_OCTET_STRING *oct;
  unsigned char *in = NULL;
  int inlen;
  if (!(oct = ASN1_OCTET_STRING_new())) {
    PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, ERR_R_MALLOC_FAILURE);
    return NULL;
  }
  inlen = ASN1_item_i2d(obj, &in, it);
  if (!in) {
    PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, PKCS12_R_ENCODE_ERROR);
    goto err;
  }
  if (!PKCS12_pbe_crypt(algor, pass, passlen, in, inlen, &oct->data,
                        &oct->length, 1)) {
    PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, PKCS12_R_ENCRYPT_ERROR);
    goto err;
  }
  if (zbuf)
    explicit_bzero(in, inlen);
  free(in);
  return oct;
err:
  free(in);
  ASN1_OCTET_STRING_free(oct);
  return NULL;
}


x86 assembly (clang, -O0):
<PKCS12_item_i2d_encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
movq   $0x0,-0x48(%rbp)
mov    $0x0,%al
call   1040 <ASN1_OCTET_STRING_new@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11d0 <PKCS12_item_i2d_encrypt+0x60>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS12err@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a6 <PKCS12_item_i2d_encrypt+0x136>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x48(%rbp),%rsi
call   1090 <ASN1_item_i2d@plt>
mov    %eax,-0x4c(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    120b <PKCS12_item_i2d_encrypt+0x9b>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS12err@plt>
jmp    128c <PKCS12_item_i2d_encrypt+0x11c>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x48(%rbp),%rcx
mov    -0x4c(%rbp),%r8d
mov    -0x40(%rbp),%r9
add    $0x4,%r9
mov    -0x40(%rbp),%rax
mov    %rax,(%rsp)
movl   $0x1,0x8(%rsp)
call   1070 <PKCS12_pbe_crypt@plt>
cmp    $0x0,%eax
jne    1260 <PKCS12_item_i2d_encrypt+0xf0>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS12err@plt>
jmp    128c <PKCS12_item_i2d_encrypt+0x11c>
cmpl   $0x0,-0x34(%rbp)
je     1276 <PKCS12_item_i2d_encrypt+0x106>
mov    -0x48(%rbp),%rdi
mov    -0x4c(%rbp),%esi
call   1080 <explicit_bzero@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a6 <PKCS12_item_i2d_encrypt+0x136>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1050 <ASN1_OCTET_STRING_free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PKCS12_item_i2d_encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,0x14(%rsp)
mov    %r8,%r12
mov    %ecx,%r15d
mov    %rdx,%r13
mov    %rsi,%rbp
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   1040 <ASN1_OCTET_STRING_new@plt>
test   %rax,%rax
je     120a <PKCS12_item_i2d_encrypt+0x9a>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
mov    %rbp,%rdx
call   1090 <ASN1_item_i2d@plt>
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
je     1223 <PKCS12_item_i2d_encrypt+0xb3>
mov    %eax,%r12d
mov    %rbx,%r9
add    $0x4,%r9
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
mov    %eax,%r8d
push   $0x1
push   %rbx
call   1070 <PKCS12_pbe_crypt@plt>
add    $0x10,%rsp
test   %eax,%eax
je     122c <PKCS12_item_i2d_encrypt+0xbc>
cmpl   $0x0,0x14(%rsp)
je     11fe <PKCS12_item_i2d_encrypt+0x8e>
mov    0x8(%rsp),%rdi
mov    %r12d,%esi
call   1080 <explicit_bzero@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    1257 <PKCS12_item_i2d_encrypt+0xe7>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS12err@plt>
jmp    1255 <PKCS12_item_i2d_encrypt+0xe5>
mov    0x2dae(%rip),%rax
jmp    1233 <PKCS12_item_i2d_encrypt+0xc3>
mov    0x2d95(%rip),%rax
mov    0x2d96(%rip),%rcx
mov    (%rcx),%edi
mov    (%rax),%esi
call   1060 <PKCS12err@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <ASN1_OCTET_STRING_free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i32, i32 }
@PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@PKCS12_R_ENCODE_ERROR = global i32 0
@PKCS12_R_ENCRYPT_ERROR = global i32 0
define %struct.TYPE_5__* @PKCS12_item_i2d_encrypt(i32* %0, i32* %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca %struct.TYPE_5__*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca %struct.TYPE_5__*
%15 = alloca i8*
%16 = alloca i32
store i32* %0, i32** %8
store i32* %1, i32** %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
store i8* null, i8** %15
%17 = call %struct.TYPE_5__* (...) @ASN1_OCTET_STRING_new()
store %struct.TYPE_5__* %17, %struct.TYPE_5__** %14
%18 = icmp ne %struct.TYPE_5__* %17, null
br i1 %18, label %23, label %19
19: ; preds = %6
%20 = load i32, i32* @PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT
%21 = load i32, i32* @ERR_R_MALLOC_FAILURE
%22 = call i32 @PKCS12err(i32 %20, i32 %21)
store %struct.TYPE_5__* null, %struct.TYPE_5__** %7
br label %65
23: ; preds = %6
%24 = load i8*, i8** %12
%25 = load i32*, i32** %9
%26 = call i32 @ASN1_item_i2d(i8* %24, i8** %15, i32* %25)
store i32 %26, i32* %16
%27 = load i8*, i8** %15
%28 = icmp ne i8* %27, null
br i1 %28, label %33, label %29
29: ; preds = %23
%30 = load i32, i32* @PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT
%31 = load i32, i32* @PKCS12_R_ENCODE_ERROR
%32 = call i32 @PKCS12err(i32 %30, i32 %31)
br label %60
33: ; preds = %23
%34 = load i32*, i32** %8
%35 = load i8*, i8** %10
%36 = load i32, i32* %11
%37 = load i8*, i8** %15
%38 = load i32, i32* %16
%39 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%40 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %39, i32 0, i32 1
%41 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%42 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %41, i32 0, i32 0
%43 = call i32 @PKCS12_pbe_crypt(i32* %34, i8* %35, i32 %36, i8* %37, i32 %38, i32* %40, i32* %42, i32 1)
%44 = icmp ne i32 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %33
%46 = load i32, i32* @PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT
%47 = load i32, i32* @PKCS12_R_ENCRYPT_ERROR
%48 = call i32 @PKCS12err(i32 %46, i32 %47)
br label %60
49: ; preds = %33
%50 = load i32, i32* %13
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i8*, i8** %15
%54 = load i32, i32* %16
%55 = call i32 @explicit_bzero(i8* %53, i32 %54)
br label %56
56: ; preds = %52, %49
%57 = load i8*, i8** %15
%58 = call i32 @free(i8* %57)
%59 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
store %struct.TYPE_5__* %59, %struct.TYPE_5__** %7
br label %65
60: ; preds = %45, %29
%61 = load i8*, i8** %15
%62 = call i32 @free(i8* %61)
%63 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%64 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_5__* %63)
store %struct.TYPE_5__* null, %struct.TYPE_5__** %7
br label %65
65: ; preds = %60, %56, %19
%66 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
ret %struct.TYPE_5__* %66
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32, i32 }
@PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@PKCS12_R_ENCODE_ERROR = global i32 0
@PKCS12_R_ENCRYPT_ERROR = global i32 0
define %struct.TYPE_5__* @PKCS12_item_i2d_encrypt(i32* %0, i32* %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i8*
%8 = bitcast i8** %7 to i8*
store i8* null, i8** %7
%9 = tail call %struct.TYPE_5__* (...) @ASN1_OCTET_STRING_new()
%10 = icmp eq %struct.TYPE_5__* %9, null
br i1 %10, label %11, label %15
11: ; preds = %6
%12 = load i32, i32* @PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT
%13 = load i32, i32* @ERR_R_MALLOC_FAILURE
%14 = tail call i32 @PKCS12err(i32 %12, i32 %13)
br label %40
15: ; preds = %6
%16 = call i32 @ASN1_item_i2d(i8* %4, i8** %7, i32* %1)
%17 = load i8*, i8** %7
%18 = icmp eq i8* %17, null
br i1 %18, label %32, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %9, i64 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %9, i64 0, i32 0
%22 = call i32 @PKCS12_pbe_crypt(i32* %0, i8* %2, i32 %3, i8* %17, i32 %16, i32* %20, i32* %21, i32 1)
%23 = icmp eq i32 %22, 0
br i1 %23, label %32, label %24
24: ; preds = %19
%25 = icmp eq i32 %5, 0
br i1 %25, label %29, label %26
26: ; preds = %24
%27 = load i8*, i8** %7
%28 = call i32 @explicit_bzero(i8* %27, i32 %16)
br label %29
29: ; preds = %26, %24
%30 = load i8*, i8** %7
%31 = call i32 @free(i8* %30)
br label %40
32: ; preds = %19, %15
%33 = phi i32* [ @PKCS12_R_ENCODE_ERROR, %15 ], [ @PKCS12_R_ENCRYPT_ERROR, %19 ]
%34 = load i32, i32* @PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT
%35 = load i32, i32* %33
%36 = call i32 @PKCS12err(i32 %34, i32 %35)
%37 = load i8*, i8** %7
%38 = call i32 @free(i8* %37)
%39 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_5__* %9)
br label %40
40: ; preds = %32, %29, %11
%41 = phi %struct.TYPE_5__* [ %9, %29 ], [ null, %32 ], [ null, %11 ]
ret %struct.TYPE_5__* %41
}