#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_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*/ PKCS12error(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())) {
    PKCS12error(ERR_R_MALLOC_FAILURE);
    return NULL;
  }
  inlen = ASN1_item_i2d(obj, &in, it);
  if (!in) {
    PKCS12error(PKCS12_R_ENCODE_ERROR);
    goto err;
  }
  if (!PKCS12_pbe_crypt(algor, pass, passlen, in, inlen, &oct->data,
                        &oct->length, 1)) {
    PKCS12error(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    11c7 <PKCS12_item_i2d_encrypt+0x57>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1060 <PKCS12error@plt>
movq   $0x0,-0x8(%rbp)
jmp    128b <PKCS12_item_i2d_encrypt+0x11b>
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    11f9 <PKCS12_item_i2d_encrypt+0x89>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1060 <PKCS12error@plt>
jmp    1271 <PKCS12_item_i2d_encrypt+0x101>
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    1245 <PKCS12_item_i2d_encrypt+0xd5>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1060 <PKCS12error@plt>
jmp    1271 <PKCS12_item_i2d_encrypt+0x101>
cmpl   $0x0,-0x34(%rbp)
je     125b <PKCS12_item_i2d_encrypt+0xeb>
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    128b <PKCS12_item_i2d_encrypt+0x11b>
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     121a <PKCS12_item_i2d_encrypt+0xaa>
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     1223 <PKCS12_item_i2d_encrypt+0xb3>
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    1245 <PKCS12_item_i2d_encrypt+0xd5>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1060 <PKCS12error@plt>
jmp    1243 <PKCS12_item_i2d_encrypt+0xd3>
mov    0x2db7(%rip),%rax
jmp    122a <PKCS12_item_i2d_encrypt+0xba>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1060 <PKCS12error@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 }
@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 %22, label %19
19: ; preds = %6
%20 = load i32, i32* @ERR_R_MALLOC_FAILURE
%21 = call i32 @PKCS12error(i32 %20)
store %struct.TYPE_5__* null, %struct.TYPE_5__** %7
br label %62
22: ; preds = %6
%23 = load i8*, i8** %12
%24 = load i32*, i32** %9
%25 = call i32 @ASN1_item_i2d(i8* %23, i8** %15, i32* %24)
store i32 %25, i32* %16
%26 = load i8*, i8** %15
%27 = icmp ne i8* %26, null
br i1 %27, label %31, label %28
28: ; preds = %22
%29 = load i32, i32* @PKCS12_R_ENCODE_ERROR
%30 = call i32 @PKCS12error(i32 %29)
br label %57
31: ; preds = %22
%32 = load i32*, i32** %8
%33 = load i8*, i8** %10
%34 = load i32, i32* %11
%35 = load i8*, i8** %15
%36 = load i32, i32* %16
%37 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%38 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %37, i32 0, i32 1
%39 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%40 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %39, i32 0, i32 0
%41 = call i32 @PKCS12_pbe_crypt(i32* %32, i8* %33, i32 %34, i8* %35, i32 %36, i32* %38, i32* %40, i32 1)
%42 = icmp ne i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %31
%44 = load i32, i32* @PKCS12_R_ENCRYPT_ERROR
%45 = call i32 @PKCS12error(i32 %44)
br label %57
46: ; preds = %31
%47 = load i32, i32* %13
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i8*, i8** %15
%51 = load i32, i32* %16
%52 = call i32 @explicit_bzero(i8* %50, i32 %51)
br label %53
53: ; preds = %49, %46
%54 = load i8*, i8** %15
%55 = call i32 @free(i8* %54)
%56 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
store %struct.TYPE_5__* %56, %struct.TYPE_5__** %7
br label %62
57: ; preds = %43, %28
%58 = load i8*, i8** %15
%59 = call i32 @free(i8* %58)
%60 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%61 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_5__* %60)
store %struct.TYPE_5__* null, %struct.TYPE_5__** %7
br label %62
62: ; preds = %57, %53, %19
%63 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
ret %struct.TYPE_5__* %63
}

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