#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_F_PKCS7_FIND_DIGEST;
int /*<<< orphan*/ PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST;
int /*<<< orphan*/ PKCS7err(int /*<<< orphan*/, 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) {
      PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,
               PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
      return NULL;
    }
    BIO_get_md_ctx(bio, pmd);
    if (*pmd == NULL) {
      PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, 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   1040 <BIO_find_type@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a8 <PKCS7_find_digest+0x58>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
call   1030 <PKCS7err@plt>
movq   $0x0,-0x8(%rbp)
jmp    121b <PKCS7_find_digest+0xcb>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <BIO_get_md_ctx@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11e7 <PKCS7_find_digest+0x97>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
call   1030 <PKCS7err@plt>
movq   $0x0,-0x8(%rbp)
jmp    121b <PKCS7_find_digest+0xcb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <EVP_MD_CTX_type@plt>
cmp    -0x1c(%rbp),%eax
jne    1209 <PKCS7_find_digest+0xb9>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121b <PKCS7_find_digest+0xcb>
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   1040 <BIO_find_type@plt>
mov    0x2e42(%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   1060 <BIO_get_md_ctx@plt>
mov    (%rbx),%rdi
test   %rdi,%rdi
je     11cb <PKCS7_find_digest+0x7b>
call   1050 <EVP_MD_CTX_type@plt>
cmp    %r14d,%eax
je     11e1 <PKCS7_find_digest+0x91>
mov    %rbp,%rdi
call   1070 <BIO_next@plt>
mov    (%r12),%esi
mov    %rax,%rdi
call   1040 <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    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%esi
call   1030 <PKCS7err@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_F_PKCS7_FIND_DIGEST = 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 = %37, %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 %18
14: ; preds = %8
%15 = load i32, i32* @PKCS7_F_PKCS7_FIND_DIGEST
%16 = load i32, i32* @PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST
%17 = call i32 @PKCS7err(i32 %15, i32 %16)
store i32* null, i32** %4
br label %40
18: ; preds = %8
%19 = load i32*, i32** %6
%20 = load i32**, i32*** %5
%21 = call i32 @BIO_get_md_ctx(i32* %19, i32** %20)
%22 = load i32**, i32*** %5
%23 = load i32*, i32** %22
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = load i32, i32* @PKCS7_F_PKCS7_FIND_DIGEST
%27 = load i32, i32* @ERR_R_INTERNAL_ERROR
%28 = call i32 @PKCS7err(i32 %26, i32 %27)
store i32* null, i32** %4
br label %40
29: ; preds = %18
%30 = load i32**, i32*** %5
%31 = load i32*, i32** %30
%32 = call i32 @EVP_MD_CTX_type(i32* %31)
%33 = load i32, i32* %7
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = load i32*, i32** %6
store i32* %36, i32** %4
br label %40
37: ; preds = %29
%38 = load i32*, i32** %6
%39 = call i32* @BIO_next(i32* %38)
store i32* %39, i32** %6
br label %8
40: ; preds = %35, %25, %14
%41 = load i32*, i32** %4
ret i32* %41
}

LLVM IR (-O2):
@BIO_TYPE_MD = global i32 0
@PKCS7_F_PKCS7_FIND_DIGEST = 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 %25, 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* @PKCS7_F_PKCS7_FIND_DIGEST
%23 = load i32, i32* %21
%24 = tail call i32 @PKCS7err(i32 %22, i32 %23)
br label %25
25: ; preds = %12, %20
%26 = phi i32* [ null, %20 ], [ %8, %12 ]
ret i32* %26
}