#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_15__ TYPE_6__;
typedef struct TYPE_14__ TYPE_5__;
typedef struct TYPE_13__ TYPE_4__;
typedef struct TYPE_12__ TYPE_3__;
typedef struct TYPE_11__ TYPE_2__;
typedef struct TYPE_10__ TYPE_1__;

/* Type definitions */
struct TYPE_15__ {
  unsigned char *data;
  int /*<<< orphan*/ length;
};
struct TYPE_11__ {
  TYPE_6__ *octet_string;
};
struct TYPE_14__ {
  scalar_t__ type;
  TYPE_2__ value;
};
struct TYPE_10__ {
  TYPE_5__ *other;
  TYPE_3__ *sign;
};
struct TYPE_13__ {
  TYPE_1__ d;
  int /*<<< orphan*/ type;
};
struct TYPE_12__ {
  TYPE_4__ *contents;
};
typedef int /*<<< orphan*/ TS_TST_INFO;
typedef TYPE_3__ PKCS7_SIGNED;
typedef TYPE_4__ PKCS7;
typedef TYPE_5__ ASN1_TYPE;
typedef TYPE_6__ ASN1_OCTET_STRING;

/* Variables and functions */
scalar_t__ NID_id_smime_ct_TSTInfo;
scalar_t__ OBJ_obj2nid(int /*<<< orphan*/);
scalar_t__ PKCS7_get_detached(TYPE_4__ *);
int /*<<< orphan*/ PKCS7_type_is_signed(TYPE_4__ *);
int /*<<< orphan*/ TS_R_BAD_PKCS7_TYPE;
int /*<<< orphan*/ TS_R_BAD_TYPE;
int /*<<< orphan*/ TS_R_DETACHED_CONTENT;
int /*<<< orphan*/ TSerror(int /*<<< orphan*/);
scalar_t__ V_ASN1_OCTET_STRING;
int /*<<< orphan*/ *d2i_TS_TST_INFO(int /*<<< orphan*/ *,
                                    unsigned char const **, int /*<<< orphan*/);
TS_TST_INFO *PKCS7_to_TS_TST_INFO(PKCS7 *token) {
  PKCS7_SIGNED *pkcs7_signed;
  PKCS7 *enveloped;
  ASN1_TYPE *tst_info_wrapper;
  ASN1_OCTET_STRING *tst_info_der;
  const unsigned char *p;
  if (!PKCS7_type_is_signed(token)) {
    TSerror(TS_R_BAD_PKCS7_TYPE);
    return NULL;
  }
  if (PKCS7_get_detached(token)) {
    TSerror(TS_R_DETACHED_CONTENT);
    return NULL;
  }
  pkcs7_signed = token->d.sign;
  enveloped = pkcs7_signed->contents;
  if (OBJ_obj2nid(enveloped->type) != NID_id_smime_ct_TSTInfo) {
    TSerror(TS_R_BAD_PKCS7_TYPE);
    return NULL;
  }
  tst_info_wrapper = enveloped->d.other;
  if (tst_info_wrapper->type != V_ASN1_OCTET_STRING) {
    TSerror(TS_R_BAD_TYPE);
    return NULL;
  }
  tst_info_der = tst_info_wrapper->value.octet_string;
  p = tst_info_der->data;
  return d2i_TS_TST_INFO(NULL, &p, tst_info_der->length);
}


x86 assembly (clang, -O0):
<PKCS7_to_TS_TST_INFO>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <PKCS7_type_is_signed@plt>
cmp    $0x0,%eax
jne    1189 <PKCS7_to_TS_TST_INFO+0x39>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1070 <TSerror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1271 <PKCS7_to_TS_TST_INFO+0x121>
mov    -0x10(%rbp),%rdi
call   1060 <PKCS7_get_detached@plt>
cmp    $0x0,%rax
je     11b7 <PKCS7_to_TS_TST_INFO+0x67>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1070 <TSerror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1271 <PKCS7_to_TS_TST_INFO+0x121>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
call   1040 <OBJ_obj2nid@plt>
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
je     1205 <PKCS7_to_TS_TST_INFO+0xb5>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1070 <TSerror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1271 <PKCS7_to_TS_TST_INFO+0x121>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%rax
je     1242 <PKCS7_to_TS_TST_INFO+0xf2>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1070 <TSerror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1271 <PKCS7_to_TS_TST_INFO+0x121>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x38(%rbp),%rsi
call   1050 <d2i_TS_TST_INFO@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PKCS7_to_TS_TST_INFO>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1030 <PKCS7_type_is_signed@plt>
test   %eax,%eax
je     11c1 <PKCS7_to_TS_TST_INFO+0x71>
mov    %rbx,%rdi
call   1060 <PKCS7_get_detached@plt>
test   %rax,%rax
je     1177 <PKCS7_to_TS_TST_INFO+0x27>
mov    0x2e5b(%rip),%rax
jmp    11c8 <PKCS7_to_TS_TST_INFO+0x78>
mov    0x8(%rbx),%rax
mov    (%rax),%rbx
mov    0x10(%rbx),%edi
call   1040 <OBJ_obj2nid@plt>
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
jne    11c1 <PKCS7_to_TS_TST_INFO+0x71>
mov    (%rbx),%rax
mov    (%rax),%rcx
mov    0x2e21(%rip),%rdx
cmp    (%rdx),%rcx
jne    11d7 <PKCS7_to_TS_TST_INFO+0x87>
mov    0x8(%rax),%rax
mov    (%rax),%rcx
mov    %rcx,0x8(%rsp)
mov    0x8(%rax),%edx
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1050 <d2i_TS_TST_INFO@plt>
jmp    11d1 <PKCS7_to_TS_TST_INFO+0x81>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1070 <TSerror@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e0a(%rip),%rax
jmp    11c8 <PKCS7_to_TS_TST_INFO+0x78>

LLVM IR (-O0):
%struct.TYPE_13__ = type { %struct.TYPE_10__, i32 }
%struct.TYPE_10__ = type { %struct.TYPE_14__*, %struct.TYPE_12__* }
%struct.TYPE_14__ = type { i64, %struct.TYPE_11__ }
%struct.TYPE_11__ = type { %struct.TYPE_15__* }
%struct.TYPE_15__ = type { i8*, i32 }
%struct.TYPE_12__ = type { %struct.TYPE_13__* }
@TS_R_BAD_PKCS7_TYPE = global i32 0
@TS_R_DETACHED_CONTENT = global i32 0
@NID_id_smime_ct_TSTInfo = global i64 0
@V_ASN1_OCTET_STRING = global i64 0
@TS_R_BAD_TYPE = global i32 0
define i32* @PKCS7_to_TS_TST_INFO(%struct.TYPE_13__* %0) {
%2 = alloca i32*
%3 = alloca %struct.TYPE_13__*
%4 = alloca %struct.TYPE_12__*
%5 = alloca %struct.TYPE_13__*
%6 = alloca %struct.TYPE_14__*
%7 = alloca %struct.TYPE_15__*
%8 = alloca i8*
store %struct.TYPE_13__* %0, %struct.TYPE_13__** %3
%9 = load %struct.TYPE_13__*, %struct.TYPE_13__** %3
%10 = call i32 @PKCS7_type_is_signed(%struct.TYPE_13__* %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i32, i32* @TS_R_BAD_PKCS7_TYPE
%14 = call i32 @TSerror(i32 %13)
store i32* null, i32** %2
br label %64
15: ; preds = %1
%16 = load %struct.TYPE_13__*, %struct.TYPE_13__** %3
%17 = call i64 @PKCS7_get_detached(%struct.TYPE_13__* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @TS_R_DETACHED_CONTENT
%21 = call i32 @TSerror(i32 %20)
store i32* null, i32** %2
br label %64
22: ; preds = %15
%23 = load %struct.TYPE_13__*, %struct.TYPE_13__** %3
%24 = getelementptr inbounds %struct.TYPE_13__, %struct.TYPE_13__* %23, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %24, i32 0, i32 1
%26 = load %struct.TYPE_12__*, %struct.TYPE_12__** %25
store %struct.TYPE_12__* %26, %struct.TYPE_12__** %4
%27 = load %struct.TYPE_12__*, %struct.TYPE_12__** %4
%28 = getelementptr inbounds %struct.TYPE_12__, %struct.TYPE_12__* %27, i32 0, i32 0
%29 = load %struct.TYPE_13__*, %struct.TYPE_13__** %28
store %struct.TYPE_13__* %29, %struct.TYPE_13__** %5
%30 = load %struct.TYPE_13__*, %struct.TYPE_13__** %5
%31 = getelementptr inbounds %struct.TYPE_13__, %struct.TYPE_13__* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i64 @OBJ_obj2nid(i32 %32)
%34 = load i64, i64* @NID_id_smime_ct_TSTInfo
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %22
%37 = load i32, i32* @TS_R_BAD_PKCS7_TYPE
%38 = call i32 @TSerror(i32 %37)
store i32* null, i32** %2
br label %64
39: ; preds = %22
%40 = load %struct.TYPE_13__*, %struct.TYPE_13__** %5
%41 = getelementptr inbounds %struct.TYPE_13__, %struct.TYPE_13__* %40, i32 0, i32 0
%42 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %41, i32 0, i32 0
%43 = load %struct.TYPE_14__*, %struct.TYPE_14__** %42
store %struct.TYPE_14__* %43, %struct.TYPE_14__** %6
%44 = load %struct.TYPE_14__*, %struct.TYPE_14__** %6
%45 = getelementptr inbounds %struct.TYPE_14__, %struct.TYPE_14__* %44, i32 0, i32 0
%46 = load i64, i64* %45
%47 = load i64, i64* @V_ASN1_OCTET_STRING
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %39
%50 = load i32, i32* @TS_R_BAD_TYPE
%51 = call i32 @TSerror(i32 %50)
store i32* null, i32** %2
br label %64
52: ; preds = %39
%53 = load %struct.TYPE_14__*, %struct.TYPE_14__** %6
%54 = getelementptr inbounds %struct.TYPE_14__, %struct.TYPE_14__* %53, i32 0, i32 1
%55 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %54, i32 0, i32 0
%56 = load %struct.TYPE_15__*, %struct.TYPE_15__** %55
store %struct.TYPE_15__* %56, %struct.TYPE_15__** %7
%57 = load %struct.TYPE_15__*, %struct.TYPE_15__** %7
%58 = getelementptr inbounds %struct.TYPE_15__, %struct.TYPE_15__* %57, i32 0, i32 0
%59 = load i8*, i8** %58
store i8* %59, i8** %8
%60 = load %struct.TYPE_15__*, %struct.TYPE_15__** %7
%61 = getelementptr inbounds %struct.TYPE_15__, %struct.TYPE_15__* %60, i32 0, i32 1
%62 = load i32, i32* %61
%63 = call i32* @d2i_TS_TST_INFO(i32* null, i8** %8, i32 %62)
store i32* %63, i32** %2
br label %64
64: ; preds = %52, %49, %36, %19, %12
%65 = load i32*, i32** %2
ret i32* %65
}

LLVM IR (-O2):
%struct.TYPE_13__ = type { %struct.TYPE_10__, i32 }
%struct.TYPE_10__ = type { %struct.TYPE_14__*, %struct.TYPE_12__* }
%struct.TYPE_14__ = type { i64, %struct.TYPE_11__ }
%struct.TYPE_11__ = type { %struct.TYPE_15__* }
%struct.TYPE_15__ = type { i8*, i32 }
%struct.TYPE_12__ = type { %struct.TYPE_13__* }
@TS_R_BAD_PKCS7_TYPE = global i32 0
@TS_R_DETACHED_CONTENT = global i32 0
@NID_id_smime_ct_TSTInfo = global i64 0
@V_ASN1_OCTET_STRING = global i64 0
@TS_R_BAD_TYPE = global i32 0
define i32* @PKCS7_to_TS_TST_INFO(%struct.TYPE_13__* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call i32 @PKCS7_type_is_signed(%struct.TYPE_13__* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @TS_R_BAD_PKCS7_TYPE
%8 = tail call i32 @TSerror(i32 %7)
br label %46
9: ; preds = %1
%10 = tail call i64 @PKCS7_get_detached(%struct.TYPE_13__* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = load i32, i32* @TS_R_DETACHED_CONTENT
%14 = tail call i32 @TSerror(i32 %13)
br label %46
15: ; preds = %9
%16 = getelementptr inbounds %struct.TYPE_13__, %struct.TYPE_13__* %0, i64 0, i32 0, i32 1
%17 = load %struct.TYPE_12__*, %struct.TYPE_12__** %16
%18 = getelementptr inbounds %struct.TYPE_12__, %struct.TYPE_12__* %17, i64 0, i32 0
%19 = load %struct.TYPE_13__*, %struct.TYPE_13__** %18
%20 = getelementptr inbounds %struct.TYPE_13__, %struct.TYPE_13__* %19, i64 0, i32 1
%21 = load i32, i32* %20
%22 = tail call i64 @OBJ_obj2nid(i32 %21)
%23 = load i64, i64* @NID_id_smime_ct_TSTInfo
%24 = icmp eq i64 %22, %23
br i1 %24, label %28, label %25
25: ; preds = %15
%26 = load i32, i32* @TS_R_BAD_PKCS7_TYPE
%27 = tail call i32 @TSerror(i32 %26)
br label %46
28: ; preds = %15
%29 = getelementptr inbounds %struct.TYPE_13__, %struct.TYPE_13__* %19, i64 0, i32 0, i32 0
%30 = load %struct.TYPE_14__*, %struct.TYPE_14__** %29
%31 = getelementptr inbounds %struct.TYPE_14__, %struct.TYPE_14__* %30, i64 0, i32 0
%32 = load i64, i64* %31
%33 = load i64, i64* @V_ASN1_OCTET_STRING
%34 = icmp eq i64 %32, %33
br i1 %34, label %38, label %35
35: ; preds = %28
%36 = load i32, i32* @TS_R_BAD_TYPE
%37 = tail call i32 @TSerror(i32 %36)
br label %46
38: ; preds = %28
%39 = getelementptr inbounds %struct.TYPE_14__, %struct.TYPE_14__* %30, i64 0, i32 1, i32 0
%40 = load %struct.TYPE_15__*, %struct.TYPE_15__** %39
%41 = getelementptr inbounds %struct.TYPE_15__, %struct.TYPE_15__* %40, i64 0, i32 0
%42 = load i8*, i8** %41
store i8* %42, i8** %2
%43 = getelementptr inbounds %struct.TYPE_15__, %struct.TYPE_15__* %40, i64 0, i32 1
%44 = load i32, i32* %43
%45 = call i32* @d2i_TS_TST_INFO(i32* null, i8** %2, i32 %44)
br label %46
46: ; preds = %38, %35, %25, %12, %6
%47 = phi i32* [ null, %12 ], [ null, %25 ], [ null, %35 ], [ %45, %38 ], [ null, %6 ]
ret i32* %47
}