#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*/ EVP_MD_CTX;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_TYPE_MD;
int /*<<< orphan*/ *BIO_find_type(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_get_md_ctx(int /*<<< orphan*/ *, int /*<<< orphan*/ **);
int /*<<< orphan*/ *BIO_next(int /*<<< orphan*/ *);
int /*<<< orphan*/ ERR_R_INTERNAL_ERROR;
int EVP_MD_CTX_type(int /*<<< orphan*/ *);
int /*<<< orphan*/ PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST;
int /*<<< orphan*/ PKCS7error(int /*<<< orphan*/);
BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid) {
  for (;;) {
    bio = BIO_find_type(bio, BIO_TYPE_MD);
    if (bio == NULL) {
      PKCS7error(PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
      return NULL;
    }
    BIO_get_md_ctx(bio, pmd);
    if (*pmd == NULL) {
      PKCS7error(ERR_R_INTERNAL_ERROR);
      return NULL;
    }
    if (EVP_MD_CTX_type(*pmd) == nid)
      return bio;
    bio = BIO_next(bio);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<PKCS7_find_digest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
call   1030 <BIO_find_type@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119f <PKCS7_find_digest+0x4f>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1060 <PKCS7error@plt>
movq   $0x0,-0x8(%rbp)
jmp    1209 <PKCS7_find_digest+0xb9>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <BIO_get_md_ctx@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11d5 <PKCS7_find_digest+0x85>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1060 <PKCS7error@plt>
movq   $0x0,-0x8(%rbp)
jmp    1209 <PKCS7_find_digest+0xb9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <EVP_MD_CTX_type@plt>
cmp    -0x1c(%rbp),%eax
jne    11f7 <PKCS7_find_digest+0xa7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1209 <PKCS7_find_digest+0xb9>
mov    -0x18(%rbp),%rdi
call   1070 <BIO_next@plt>
mov    %rax,-0x18(%rbp)
jmp    1163 <PKCS7_find_digest+0x13>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PKCS7_find_digest>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rax
mov    %rdi,%rbx
mov    0x2e85(%rip),%r12
mov    (%r12),%esi
mov    %rax,%rdi
call   1030 <BIO_find_type@plt>
mov    0x2e4a(%rip),%r15
test   %rax,%rax
je     11ce <PKCS7_find_digest+0x7e>
mov    %rax,%rbp
mov    0x2e53(%rip),%r13
nopl   (%rax)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <BIO_get_md_ctx@plt>
mov    (%rbx),%rdi
test   %rdi,%rdi
je     11cb <PKCS7_find_digest+0x7b>
call   1040 <EVP_MD_CTX_type@plt>
cmp    %r14d,%eax
je     11d8 <PKCS7_find_digest+0x88>
mov    %rbp,%rdi
call   1070 <BIO_next@plt>
mov    (%r12),%esi
mov    %rax,%rdi
call   1030 <BIO_find_type@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1190 <PKCS7_find_digest+0x40>
jmp    11ce <PKCS7_find_digest+0x7e>
mov    %r13,%r15
mov    (%r15),%edi
call   1060 <PKCS7error@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BIO_TYPE_MD = global i32 0
@PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST = global i32 0
@ERR_R_INTERNAL_ERROR = global i32 0
define i32* @PKCS7_find_digest(i32** %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32**
%6 = alloca i32*
%7 = alloca i32
store i32** %0, i32*** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
br label %8
8: ; preds = %35, %3
%9 = load i32*, i32** %6
%10 = load i32, i32* @BIO_TYPE_MD
%11 = call i32* @BIO_find_type(i32* %9, i32 %10)
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = load i32, i32* @PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST
%16 = call i32 @PKCS7error(i32 %15)
store i32* null, i32** %4
br label %38
17: ; preds = %8
%18 = load i32*, i32** %6
%19 = load i32**, i32*** %5
%20 = call i32 @BIO_get_md_ctx(i32* %18, i32** %19)
%21 = load i32**, i32*** %5
%22 = load i32*, i32** %21
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = load i32, i32* @ERR_R_INTERNAL_ERROR
%26 = call i32 @PKCS7error(i32 %25)
store i32* null, i32** %4
br label %38
27: ; preds = %17
%28 = load i32**, i32*** %5
%29 = load i32*, i32** %28
%30 = call i32 @EVP_MD_CTX_type(i32* %29)
%31 = load i32, i32* %7
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = load i32*, i32** %6
store i32* %34, i32** %4
br label %38
35: ; preds = %27
%36 = load i32*, i32** %6
%37 = call i32* @BIO_next(i32* %36)
store i32* %37, i32** %6
br label %8
38: ; preds = %33, %24, %14
%39 = load i32*, i32** %4
ret i32* %39
}

LLVM IR (-O2):
@BIO_TYPE_MD = global i32 0
@PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST = global i32 0
@ERR_R_INTERNAL_ERROR = global i32 0
define i32* @PKCS7_find_digest(i32** %0, i32* %1, i32 %2) {
%4 = load i32, i32* @BIO_TYPE_MD
%5 = tail call i32* @BIO_find_type(i32* %1, i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %20, label %7
7: ; preds = %3, %15
%8 = phi i32* [ %18, %15 ], [ %5, %3 ]
%9 = tail call i32 @BIO_get_md_ctx(i32* %8, i32** %0)
%10 = load i32*, i32** %0
%11 = icmp eq i32* %10, null
br i1 %11, label %20, label %12
12: ; preds = %7
%13 = tail call i32 @EVP_MD_CTX_type(i32* %10)
%14 = icmp eq i32 %13, %2
br i1 %14, label %24, label %15
15: ; preds = %12
%16 = tail call i32* @BIO_next(i32* %8)
%17 = load i32, i32* @BIO_TYPE_MD
%18 = tail call i32* @BIO_find_type(i32* %16, i32 %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %7
20: ; preds = %7, %15, %3
%21 = phi i32* [ @PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST, %3 ], [ @PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST, %15 ], [ @ERR_R_INTERNAL_ERROR, %7 ]
%22 = load i32, i32* %21
%23 = tail call i32 @PKCS7error(i32 %22)
br label %24
24: ; preds = %12, %20
%25 = phi i32* [ null, %20 ], [ %8, %12 ]
ret i32* %25
}