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

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

/* Variables and functions */
int ASN1_item_i2d(void *, unsigned char **, int /*<<< orphan*/ const *);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
TYPE_1__ *M_ASN1_OCTET_STRING_new();
int /*<<< orphan*/ OPENSSL_cleanse(unsigned char *, int);
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*/ 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 = M_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);
    return NULL;
  }
  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);
    free(in);
    return NULL;
  }
  if (zbuf)
    OPENSSL_cleanse(in, inlen);
  free(in);
  return oct;
}


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   1070 <M_ASN1_OCTET_STRING_new@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11c0 <PKCS12_item_i2d_encrypt+0x60>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1050 <PKCS12err@plt>
movq   $0x0,-0x8(%rbp)
jmp    1290 <PKCS12_item_i2d_encrypt+0x130>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x48(%rbp),%rsi
call   1080 <ASN1_item_i2d@plt>
mov    %eax,-0x4c(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1203 <PKCS12_item_i2d_encrypt+0xa3>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
call   1050 <PKCS12err@plt>
movq   $0x0,-0x8(%rbp)
jmp    1290 <PKCS12_item_i2d_encrypt+0x130>
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   1060 <PKCS12_pbe_crypt@plt>
cmp    $0x0,%eax
jne    1269 <PKCS12_item_i2d_encrypt+0x109>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
call   1050 <PKCS12err@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1290 <PKCS12_item_i2d_encrypt+0x130>
cmpl   $0x0,-0x34(%rbp)
je     127f <PKCS12_item_i2d_encrypt+0x11f>
mov    -0x48(%rbp),%rdi
mov    -0x4c(%rbp),%esi
call   1040 <OPENSSL_cleanse@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-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,%r15
mov    %ecx,%r12d
mov    %rdx,%r13
mov    %rsi,%rbp
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   1070 <M_ASN1_OCTET_STRING_new@plt>
test   %rax,%rax
je     11fa <PKCS12_item_i2d_encrypt+0x9a>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
mov    %rbp,%rdx
call   1080 <ASN1_item_i2d@plt>
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
je     120c <PKCS12_item_i2d_encrypt+0xac>
mov    %eax,%r15d
mov    %rbx,%r9
add    $0x4,%r9
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
mov    %eax,%r8d
push   $0x1
push   %rbx
call   1060 <PKCS12_pbe_crypt@plt>
add    $0x10,%rsp
test   %eax,%eax
je     1225 <PKCS12_item_i2d_encrypt+0xc5>
cmpl   $0x0,0x14(%rsp)
je     11ee <PKCS12_item_i2d_encrypt+0x8e>
mov    0x8(%rsp),%rdi
mov    %r15d,%esi
call   1040 <OPENSSL_cleanse@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    1248 <PKCS12_item_i2d_encrypt+0xe8>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
jmp    121c <PKCS12_item_i2d_encrypt+0xbc>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
call   1050 <PKCS12err@plt>
jmp    1246 <PKCS12_item_i2d_encrypt+0xe6>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
call   1050 <PKCS12err@plt>
mov    0x8(%rsp),%rdi
call   1030 <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_4__ = 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_4__* @PKCS12_item_i2d_encrypt(i32* %0, i32* %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca %struct.TYPE_4__*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca %struct.TYPE_4__*
%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_4__* (...) @M_ASN1_OCTET_STRING_new()
store %struct.TYPE_4__* %17, %struct.TYPE_4__** %14
%18 = icmp ne %struct.TYPE_4__* %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_4__* null, %struct.TYPE_4__** %7
br label %62
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)
store %struct.TYPE_4__* null, %struct.TYPE_4__** %7
br label %62
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_4__*, %struct.TYPE_4__** %14
%40 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %39, i32 0, i32 1
%41 = load %struct.TYPE_4__*, %struct.TYPE_4__** %14
%42 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %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 %51, 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)
%49 = load i8*, i8** %15
%50 = call i32 @free(i8* %49)
store %struct.TYPE_4__* null, %struct.TYPE_4__** %7
br label %62
51: ; preds = %33
%52 = load i32, i32* %13
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = load i8*, i8** %15
%56 = load i32, i32* %16
%57 = call i32 @OPENSSL_cleanse(i8* %55, i32 %56)
br label %58
58: ; preds = %54, %51
%59 = load i8*, i8** %15
%60 = call i32 @free(i8* %59)
%61 = load %struct.TYPE_4__*, %struct.TYPE_4__** %14
store %struct.TYPE_4__* %61, %struct.TYPE_4__** %7
br label %62
62: ; preds = %58, %45, %29, %19
%63 = load %struct.TYPE_4__*, %struct.TYPE_4__** %7
ret %struct.TYPE_4__* %63
}

LLVM IR (-O2):
%struct.TYPE_4__ = 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_4__* @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_4__* (...) @M_ASN1_OCTET_STRING_new()
%10 = icmp eq %struct.TYPE_4__* %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 %42
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 %19, label %23
19: ; preds = %15
%20 = load i32, i32* @PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT
%21 = load i32, i32* @PKCS12_R_ENCODE_ERROR
%22 = call i32 @PKCS12err(i32 %20, i32 %21)
br label %42
23: ; preds = %15
%24 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 0
%26 = call i32 @PKCS12_pbe_crypt(i32* %0, i8* %2, i32 %3, i8* %17, i32 %16, i32* %24, i32* %25, i32 1)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i32, i32* @PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT
%30 = load i32, i32* @PKCS12_R_ENCRYPT_ERROR
%31 = call i32 @PKCS12err(i32 %29, i32 %30)
%32 = load i8*, i8** %7
%33 = call i32 @free(i8* %32)
br label %42
34: ; preds = %23
%35 = icmp eq i32 %5, 0
br i1 %35, label %39, label %36
36: ; preds = %34
%37 = load i8*, i8** %7
%38 = call i32 @OPENSSL_cleanse(i8* %37, i32 %16)
br label %39
39: ; preds = %36, %34
%40 = load i8*, i8** %7
%41 = call i32 @free(i8* %40)
br label %42
42: ; preds = %39, %28, %19, %11
%43 = phi %struct.TYPE_4__* [ %9, %39 ], [ null, %28 ], [ null, %19 ], [ null, %11 ]
ret %struct.TYPE_4__* %43
}