#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_9__ {
  TYPE_3__ *digest;
};
struct TYPE_11__ {
  TYPE_2__ d;
};
struct TYPE_10__ {
  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_F_PKCS7_SET_DIGEST;
int /*<<< orphan*/ PKCS7_R_WRONG_CONTENT_TYPE;
scalar_t__ PKCS7_type_is_digest(TYPE_4__ *);
int /*<<< orphan*/ PKCS7err(int /*<<< orphan*/, 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())) {
      PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, 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;
  }
  PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, 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     11f8 <PKCS7_set_digest+0xa8>
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    11b5 <PKCS7_set_digest+0x65>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
movl   $0x0,-0x4(%rbp)
jmp    1216 <PKCS7_set_digest+0xc6>
mov    0x2e14(%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   1070 <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    1216 <PKCS7_set_digest+0xc6>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@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     11be <PKCS7_set_digest+0x6e>
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    %r14,%rdi
call   1070 <EVP_MD_nid@plt>
mov    %eax,%edi
call   1050 <OBJ_nid2obj@plt>
mov    (%rbx),%rcx
mov    (%rcx),%rcx
mov    %eax,(%rcx)
jmp    11b7 <PKCS7_set_digest+0x67>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
mov    $0x1,%ebp
jmp    11d5 <PKCS7_set_digest+0x85>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_11__ = type { %struct.TYPE_9__ }
%struct.TYPE_9__ = type { %struct.TYPE_10__* }
%struct.TYPE_10__ = type { %struct.TYPE_8__* }
%struct.TYPE_8__ = type { i32, %struct.TYPE_12__* }
%struct.TYPE_12__ = type { i32 }
@PKCS7_F_PKCS7_SET_DIGEST = global i32 0
@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 %44
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_9__, %struct.TYPE_9__* %12, i32 0, i32 0
%14 = load %struct.TYPE_10__*, %struct.TYPE_10__** %13
%15 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %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 %23, label %19
19: ; preds = %9
%20 = load i32, i32* @PKCS7_F_PKCS7_SET_DIGEST
%21 = load i32, i32* @ERR_R_MALLOC_FAILURE
%22 = call i32 @PKCS7err(i32 %20, i32 %21)
store i32 0, i32* %3
br label %48
23: ; preds = %9
%24 = load i32, i32* @V_ASN1_NULL
%25 = load %struct.TYPE_11__*, %struct.TYPE_11__** %4
%26 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %25, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %26, i32 0, i32 0
%28 = load %struct.TYPE_10__*, %struct.TYPE_10__** %27
%29 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %28, i32 0, i32 0
%30 = load %struct.TYPE_8__*, %struct.TYPE_8__** %29
%31 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %30, i32 0, i32 1
%32 = load %struct.TYPE_12__*, %struct.TYPE_12__** %31
%33 = getelementptr inbounds %struct.TYPE_12__, %struct.TYPE_12__* %32, i32 0, i32 0
store i32 %24, i32* %33
%34 = load i32*, i32** %5
%35 = call i32 @EVP_MD_nid(i32* %34)
%36 = call i32 @OBJ_nid2obj(i32 %35)
%37 = load %struct.TYPE_11__*, %struct.TYPE_11__** %4
%38 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %37, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %38, i32 0, i32 0
%40 = load %struct.TYPE_10__*, %struct.TYPE_10__** %39
%41 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %40, i32 0, i32 0
%42 = load %struct.TYPE_8__*, %struct.TYPE_8__** %41
%43 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %42, i32 0, i32 0
store i32 %36, i32* %43
store i32 1, i32* %3
br label %48
44: ; preds = %2
%45 = load i32, i32* @PKCS7_F_PKCS7_SET_DIGEST
%46 = load i32, i32* @PKCS7_R_WRONG_CONTENT_TYPE
%47 = call i32 @PKCS7err(i32 %45, i32 %46)
store i32 1, i32* %3
br label %48
48: ; preds = %44, %23, %19
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.TYPE_11__ = type { %struct.TYPE_9__ }
%struct.TYPE_9__ = type { %struct.TYPE_10__* }
%struct.TYPE_10__ = type { %struct.TYPE_8__* }
%struct.TYPE_8__ = type { i32, %struct.TYPE_12__* }
%struct.TYPE_12__ = type { i32 }
@PKCS7_F_PKCS7_SET_DIGEST = global i32 0
@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 %26, 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_10__*, %struct.TYPE_10__** %7
%9 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %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 %17
13: ; preds = %5
%14 = load i32, i32* @PKCS7_F_PKCS7_SET_DIGEST
%15 = load i32, i32* @ERR_R_MALLOC_FAILURE
%16 = tail call i32 @PKCS7err(i32 %14, i32 %15)
br label %30
17: ; preds = %5
%18 = load i32, i32* @V_ASN1_NULL
%19 = getelementptr inbounds %struct.TYPE_12__, %struct.TYPE_12__* %6, i64 0, i32 0
store i32 %18, i32* %19
%20 = tail call i32 @EVP_MD_nid(i32* %1)
%21 = tail call i32 @OBJ_nid2obj(i32 %20)
%22 = load %struct.TYPE_10__*, %struct.TYPE_10__** %7
%23 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %22, i64 0, i32 0
%24 = load %struct.TYPE_8__*, %struct.TYPE_8__** %23
%25 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %24, i64 0, i32 0
store i32 %21, i32* %25
br label %30
26: ; preds = %2
%27 = load i32, i32* @PKCS7_F_PKCS7_SET_DIGEST
%28 = load i32, i32* @PKCS7_R_WRONG_CONTENT_TYPE
%29 = tail call i32 @PKCS7err(i32 %27, i32 %28)
br label %30
30: ; preds = %26, %17, %13
%31 = phi i32 [ 1, %17 ], [ 0, %13 ], [ 1, %26 ]
ret i32 %31
}