#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_12__ TYPE_7__;
typedef struct TYPE_11__ TYPE_4__;
typedef struct TYPE_10__ TYPE_3__;
typedef struct TYPE_9__ TYPE_2__;
typedef struct TYPE_8__ TYPE_1__;

/* Type definitions */
struct TYPE_12__ {
  int /*<<< orphan*/ type;
};
struct TYPE_10__ {
  TYPE_2__ *digest;
};
struct TYPE_11__ {
  TYPE_3__ d;
};
struct TYPE_9__ {
  TYPE_1__ *md;
};
struct TYPE_8__ {
  int /*<<< orphan*/ algorithm;
  TYPE_7__ *parameter;
};
typedef TYPE_4__ PKCS7;
typedef int /*<<< orphan*/ EVP_MD;

/* Variables and functions */
TYPE_7__ *ASN1_TYPE_new();
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ EVP_MD_nid(int /*<<< orphan*/ const *);
int /*<<< orphan*/ OBJ_nid2obj(int /*<<< orphan*/);
int /*<<< orphan*/ PKCS7_R_WRONG_CONTENT_TYPE;
scalar_t__ PKCS7_type_is_digest(TYPE_4__ *);
int /*<<< orphan*/ PKCS7error(int /*<<< orphan*/);
int /*<<< orphan*/ V_ASN1_NULL;
int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md) {
  if (PKCS7_type_is_digest(p7)) {
    if (!(p7->d.digest->md->parameter = ASN1_TYPE_new())) {
      PKCS7error(ERR_R_MALLOC_FAILURE);
      return 0;
    }
    p7->d.digest->md->parameter->type = V_ASN1_NULL;
    p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
    return 1;
  }
  PKCS7error(PKCS7_R_WRONG_CONTENT_TYPE);
  return 1;
}


x86 assembly (clang, -O0):
<PKCS7_set_digest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <PKCS7_type_is_digest@plt>
cmp    $0x0,%rax
je     11ef <PKCS7_set_digest+0x9f>
mov    $0x0,%al
call   1030 <ASN1_TYPE_new@plt>
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%rcx
mov    %rax,0x8(%rcx)
cmp    $0x0,%rax
jne    11ac <PKCS7_set_digest+0x5c>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1070 <PKCS7error@plt>
movl   $0x0,-0x4(%rbp)
jmp    1204 <PKCS7_set_digest+0xb4>
mov    0x2e25(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
call   1060 <EVP_MD_nid@plt>
mov    %eax,%edi
call   1050 <OBJ_nid2obj@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1204 <PKCS7_set_digest+0xb4>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1070 <PKCS7error@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PKCS7_set_digest>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <PKCS7_type_is_digest@plt>
test   %rax,%rax
je     11a0 <PKCS7_set_digest+0x50>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <ASN1_TYPE_new@plt>
mov    (%rbx),%rcx
mov    (%rcx),%rcx
mov    %rax,0x8(%rcx)
test   %rax,%rax
je     11b5 <PKCS7_set_digest+0x65>
mov    0x2e55(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    %r14,%rdi
call   1060 <EVP_MD_nid@plt>
mov    %eax,%edi
call   1050 <OBJ_nid2obj@plt>
mov    (%rbx),%rcx
mov    (%rcx),%rcx
mov    %eax,(%rcx)
jmp    11ae <PKCS7_set_digest+0x5e>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1070 <PKCS7error@plt>
mov    $0x1,%ebp
jmp    11c3 <PKCS7_set_digest+0x73>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1070 <PKCS7error@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_11__ = type { %struct.TYPE_10__ }
%struct.TYPE_10__ = type { %struct.TYPE_9__* }
%struct.TYPE_9__ = type { %struct.TYPE_8__* }
%struct.TYPE_8__ = type { i32, %struct.TYPE_12__* }
%struct.TYPE_12__ = type { i32 }
@ERR_R_MALLOC_FAILURE = global i32 0
@V_ASN1_NULL = global i32 0
@PKCS7_R_WRONG_CONTENT_TYPE = global i32 0
define i32 @PKCS7_set_digest(%struct.TYPE_11__* %0, i32* %1) {
%3 = alloca i32
%4 = alloca %struct.TYPE_11__*
%5 = alloca i32*
store %struct.TYPE_11__* %0, %struct.TYPE_11__** %4
store i32* %1, i32** %5
%6 = load %struct.TYPE_11__*, %struct.TYPE_11__** %4
%7 = call i64 @PKCS7_type_is_digest(%struct.TYPE_11__* %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %43
9: ; preds = %2
%10 = call %struct.TYPE_12__* (...) @ASN1_TYPE_new()
%11 = load %struct.TYPE_11__*, %struct.TYPE_11__** %4
%12 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %11, i32 0, i32 0
%13 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %12, i32 0, i32 0
%14 = load %struct.TYPE_9__*, %struct.TYPE_9__** %13
%15 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %14, i32 0, i32 0
%16 = load %struct.TYPE_8__*, %struct.TYPE_8__** %15
%17 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %16, i32 0, i32 1
store %struct.TYPE_12__* %10, %struct.TYPE_12__** %17
%18 = icmp ne %struct.TYPE_12__* %10, null
br i1 %18, label %22, label %19
19: ; preds = %9
%20 = load i32, i32* @ERR_R_MALLOC_FAILURE
%21 = call i32 @PKCS7error(i32 %20)
store i32 0, i32* %3
br label %46
22: ; preds = %9
%23 = load i32, i32* @V_ASN1_NULL
%24 = load %struct.TYPE_11__*, %struct.TYPE_11__** %4
%25 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %24, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %25, i32 0, i32 0
%27 = load %struct.TYPE_9__*, %struct.TYPE_9__** %26
%28 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %27, i32 0, i32 0
%29 = load %struct.TYPE_8__*, %struct.TYPE_8__** %28
%30 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %29, i32 0, i32 1
%31 = load %struct.TYPE_12__*, %struct.TYPE_12__** %30
%32 = getelementptr inbounds %struct.TYPE_12__, %struct.TYPE_12__* %31, i32 0, i32 0
store i32 %23, i32* %32
%33 = load i32*, i32** %5
%34 = call i32 @EVP_MD_nid(i32* %33)
%35 = call i32 @OBJ_nid2obj(i32 %34)
%36 = load %struct.TYPE_11__*, %struct.TYPE_11__** %4
%37 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %36, i32 0, i32 0
%38 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %37, i32 0, i32 0
%39 = load %struct.TYPE_9__*, %struct.TYPE_9__** %38
%40 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %39, i32 0, i32 0
%41 = load %struct.TYPE_8__*, %struct.TYPE_8__** %40
%42 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %41, i32 0, i32 0
store i32 %35, i32* %42
store i32 1, i32* %3
br label %46
43: ; preds = %2
%44 = load i32, i32* @PKCS7_R_WRONG_CONTENT_TYPE
%45 = call i32 @PKCS7error(i32 %44)
store i32 1, i32* %3
br label %46
46: ; preds = %43, %22, %19
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.TYPE_11__ = type { %struct.TYPE_10__ }
%struct.TYPE_10__ = type { %struct.TYPE_9__* }
%struct.TYPE_9__ = type { %struct.TYPE_8__* }
%struct.TYPE_8__ = type { i32, %struct.TYPE_12__* }
%struct.TYPE_12__ = type { i32 }
@ERR_R_MALLOC_FAILURE = global i32 0
@V_ASN1_NULL = global i32 0
@PKCS7_R_WRONG_CONTENT_TYPE = global i32 0
define i32 @PKCS7_set_digest(%struct.TYPE_11__* %0, i32* %1) {
%3 = tail call i64 @PKCS7_type_is_digest(%struct.TYPE_11__* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %25, label %5
5: ; preds = %2
%6 = tail call %struct.TYPE_12__* (...) @ASN1_TYPE_new()
%7 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %0, i64 0, i32 0, i32 0
%8 = load %struct.TYPE_9__*, %struct.TYPE_9__** %7
%9 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %8, i64 0, i32 0
%10 = load %struct.TYPE_8__*, %struct.TYPE_8__** %9
%11 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %10, i64 0, i32 1
store %struct.TYPE_12__* %6, %struct.TYPE_12__** %11
%12 = icmp eq %struct.TYPE_12__* %6, null
br i1 %12, label %13, label %16
13: ; preds = %5
%14 = load i32, i32* @ERR_R_MALLOC_FAILURE
%15 = tail call i32 @PKCS7error(i32 %14)
br label %28
16: ; preds = %5
%17 = load i32, i32* @V_ASN1_NULL
%18 = getelementptr inbounds %struct.TYPE_12__, %struct.TYPE_12__* %6, i64 0, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @EVP_MD_nid(i32* %1)
%20 = tail call i32 @OBJ_nid2obj(i32 %19)
%21 = load %struct.TYPE_9__*, %struct.TYPE_9__** %7
%22 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %21, i64 0, i32 0
%23 = load %struct.TYPE_8__*, %struct.TYPE_8__** %22
%24 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %23, i64 0, i32 0
store i32 %20, i32* %24
br label %28
25: ; preds = %2
%26 = load i32, i32* @PKCS7_R_WRONG_CONTENT_TYPE
%27 = tail call i32 @PKCS7error(i32 %26)
br label %28
28: ; preds = %25, %16, %13
%29 = phi i32 [ 1, %16 ], [ 0, %13 ], [ 1, %25 ]
ret i32 %29
}