#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 */

/* Type definitions */
typedef int /*<<< orphan*/ iv;
typedef int /*<<< orphan*/ EVP_CIPHER_CTX;
typedef int /*<<< orphan*/ ASN1_TYPE;

/* Variables and functions */
int ASN1_TYPE_get_int_octetstring(int /*<<< orphan*/ *, long *, unsigned char *,
                                  unsigned int);
int /*<<< orphan*/ EVP_CIPHER_CTX_ctrl(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                       int, int /*<<< orphan*/ *);
unsigned int EVP_CIPHER_CTX_iv_length(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_CIPHER_CTX_set_key_length(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ EVP_CTRL_SET_RC2_KEY_BITS;
int /*<<< orphan*/ EVP_CipherInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                     unsigned char *, int);
int EVP_MAX_IV_LENGTH;
int /*<<< orphan*/ OPENSSL_assert(int);
int rc2_magic_to_meth(int);
int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) {
  long num = 0;
  int i = 0;
  int key_bits;
  unsigned int l;
  unsigned char iv[EVP_MAX_IV_LENGTH];
  if (type != NULL) {
    l = EVP_CIPHER_CTX_iv_length(c);
    OPENSSL_assert(l <= sizeof(iv));
    i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l);
    if (i != (int)l)
      return (-1);
    key_bits = rc2_magic_to_meth((int)num);
    if (!key_bits)
      return (-1);
    if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
      return -1;
    EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
    EVP_CIPHER_CTX_set_key_length(c, key_bits / 8);
  }
  return (i);
}


x86 assembly (clang, -O0):
<rc2_get_asn1_type_and_iv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12c0 <rc2_get_asn1_type_and_iv+0x150>
mov    -0x10(%rbp),%rdi
call   1040 <EVP_CIPHER_CTX_iv_length@plt>
mov    -0x58(%rbp),%rcx
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    %rcx,%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <OPENSSL_assert@plt>
mov    -0x50(%rbp),%rdx
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%ecx
lea    -0x20(%rbp),%rsi
call   1070 <ASN1_TYPE_get_int_octetstring@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     1223 <rc2_get_asn1_type_and_iv+0xb3>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12cd <rc2_get_asn1_type_and_iv+0x15d>
mov    -0x20(%rbp),%rax
mov    %eax,%edi
call   1050 <rc2_magic_to_meth@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    124e <rc2_get_asn1_type_and_iv+0xde>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12cd <rc2_get_asn1_type_and_iv+0x15d>
cmpl   $0x0,-0x24(%rbp)
jle    1291 <rc2_get_asn1_type_and_iv+0x121>
mov    -0x50(%rbp),%r8
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    $0xffffffff,%r9d
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1090 <EVP_CipherInit_ex@plt>
cmp    $0x0,%eax
jne    1291 <rc2_get_asn1_type_and_iv+0x121>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12cd <rc2_get_asn1_type_and_iv+0x15d>
mov    -0x10(%rbp),%rdi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <EVP_CIPHER_CTX_ctrl@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
call   1080 <EVP_CIPHER_CTX_set_key_length@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rc2_get_asn1_type_and_iv>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%rcx
movq   $0x0,-0x40(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%r14
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
test   %rsi,%rsi
je     1264 <rc2_get_asn1_type_and_iv+0xf4>
mov    %rsi,%r12
mov    %rcx,-0x30(%rbp)
mov    %rdi,-0x38(%rbp)
call   1040 <EVP_CIPHER_CTX_iv_length@plt>
mov    %eax,%r15d
xor    %edi,%edi
cmp    %ebx,%eax
setbe  %dil
call   1030 <OPENSSL_assert@plt>
lea    -0x40(%rbp),%rsi
mov    %r12,%rdi
mov    %r14,%rdx
mov    %r15d,%ecx
call   1070 <ASN1_TYPE_get_int_octetstring@plt>
mov    $0xffffffff,%r13d
cmp    %r15d,%eax
jne    1269 <rc2_get_asn1_type_and_iv+0xf9>
mov    -0x40(%rbp),%edi
call   1050 <rc2_magic_to_meth@plt>
test   %eax,%eax
mov    -0x30(%rbp),%rcx
je     126d <rc2_get_asn1_type_and_iv+0xfd>
mov    %eax,%r12d
test   %r15d,%r15d
jle    122a <rc2_get_asn1_type_and_iv+0xba>
mov    $0xffffffff,%r13d
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xffffffff,%r9d
call   1090 <EVP_CipherInit_ex@plt>
mov    -0x30(%rbp),%rcx
test   %eax,%eax
je     126d <rc2_get_asn1_type_and_iv+0xfd>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rbx
mov    %rbx,%rdi
mov    %r12d,%edx
xor    %ecx,%ecx
call   1060 <EVP_CIPHER_CTX_ctrl@plt>
lea    0x7(%r12),%esi
test   %r12d,%r12d
cmovns %r12d,%esi
sar    $0x3,%esi
mov    %rbx,%rdi
call   1080 <EVP_CIPHER_CTX_set_key_length@plt>
mov    -0x30(%rbp),%rcx
mov    %r15d,%r13d
jmp    126d <rc2_get_asn1_type_and_iv+0xfd>
xor    %r13d,%r13d
jmp    126d <rc2_get_asn1_type_and_iv+0xfd>
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_MAX_IV_LENGTH = global i32 0
@EVP_CTRL_SET_RC2_KEY_BITS = global i32 0
define i32 @rc2_get_asn1_type_and_iv(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 0, i64* %6
store i32 0, i32* %7
%13 = load i32, i32* @EVP_MAX_IV_LENGTH
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %10
%16 = alloca i8, i64 %14
store i64 %14, i64* %11
%17 = load i32*, i32** %5
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %58
19: ; preds = %2
%20 = load i32*, i32** %4
%21 = call i32 @EVP_CIPHER_CTX_iv_length(i32* %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = zext i32 %22 to i64
%24 = icmp ule i64 %23, %14
%25 = zext i1 %24 to i32
%26 = call i32 @OPENSSL_assert(i32 %25)
%27 = load i32*, i32** %5
%28 = load i32, i32* %9
%29 = call i32 @ASN1_TYPE_get_int_octetstring(i32* %27, i64* %6, i8* %16, i32 %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = load i32, i32* %9
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %19
store i32 -1, i32* %3
store i32 1, i32* %12
br label %60
34: ; preds = %19
%35 = load i64, i64* %6
%36 = trunc i64 %35 to i32
%37 = call i32 @rc2_magic_to_meth(i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %34
store i32 -1, i32* %3
store i32 1, i32* %12
br label %60
41: ; preds = %34
%42 = load i32, i32* %7
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i32*, i32** %4
%46 = call i32 @EVP_CipherInit_ex(i32* %45, i32* null, i32* null, i32* null, i8* %16, i32 -1)
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %44
store i32 -1, i32* %3
store i32 1, i32* %12
br label %60
49: ; preds = %44, %41
%50 = load i32*, i32** %4
%51 = load i32, i32* @EVP_CTRL_SET_RC2_KEY_BITS
%52 = load i32, i32* %8
%53 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %50, i32 %51, i32 %52, i32* null)
%54 = load i32*, i32** %4
%55 = load i32, i32* %8
%56 = sdiv i32 %55, 8
%57 = call i32 @EVP_CIPHER_CTX_set_key_length(i32* %54, i32 %56)
br label %58
58: ; preds = %49, %2
%59 = load i32, i32* %7
store i32 %59, i32* %3
store i32 1, i32* %12
br label %60
60: ; preds = %58, %48, %40, %33
%61 = load i8*, i8** %10
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@EVP_MAX_IV_LENGTH = global i32 0
@EVP_CTRL_SET_RC2_KEY_BITS = global i32 0
define i32 @rc2_get_asn1_type_and_iv(i32* %0, i32* %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%5 = load i32, i32* @EVP_MAX_IV_LENGTH
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = icmp eq i32* %1, null
br i1 %9, label %32, label %10
10: ; preds = %2
%11 = tail call i32 @EVP_CIPHER_CTX_iv_length(i32* %0)
%12 = icmp ule i32 %11, %5
%13 = zext i1 %12 to i32
%14 = tail call i32 @OPENSSL_assert(i32 %13)
%15 = call i32 @ASN1_TYPE_get_int_octetstring(i32* %1, i64* %3, i8* %8, i32 %11)
%16 = icmp eq i32 %15, %11
br i1 %16, label %17, label %32
17: ; preds = %10
%18 = load i64, i64* %3
%19 = trunc i64 %18 to i32
%20 = call i32 @rc2_magic_to_meth(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %32, label %22
22: ; preds = %17
%23 = icmp sgt i32 %11, 0
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = call i32 @EVP_CipherInit_ex(i32* %0, i32* null, i32* null, i32* null, i8* %8, i32 -1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %24, %22
%28 = load i32, i32* @EVP_CTRL_SET_RC2_KEY_BITS
%29 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %0, i32 %28, i32 %20, i32* null)
%30 = sdiv i32 %20, 8
%31 = call i32 @EVP_CIPHER_CTX_set_key_length(i32* %0, i32 %30)
br label %32
32: ; preds = %2, %27, %24, %17, %10
%33 = phi i32 [ -1, %10 ], [ -1, %17 ], [ -1, %24 ], [ %11, %27 ], [ 0, %2 ]
ret i32 %33
}