#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_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int /*<<< orphan*/ algorithm;
};
typedef TYPE_1__ X509_ALGOR;
typedef int /*<<< orphan*/ EVP_MD;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_f_md();
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_set_md(int /*<<< orphan*/ *, int /*<<< orphan*/ const *);
int /*<<< orphan*/ ERR_R_BIO_LIB;
int /*<<< orphan*/ *EVP_get_digestbyobj(int /*<<< orphan*/);
int /*<<< orphan*/ PKCS7_F_PKCS7_BIO_ADD_DIGEST;
int /*<<< orphan*/ PKCS7_R_UNKNOWN_DIGEST_TYPE;
int /*<<< orphan*/ PKCS7err(int /*<<< orphan*/, int /*<<< orphan*/);
int PKCS7_bio_add_digest(BIO **pbio, X509_ALGOR *alg) {
  BIO *btmp;
  const EVP_MD *md;
  if ((btmp = BIO_new(BIO_f_md())) == NULL) {
    PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
    goto err;
  }
  md = EVP_get_digestbyobj(alg->algorithm);
  if (md == NULL) {
    PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, PKCS7_R_UNKNOWN_DIGEST_TYPE);
    goto err;
  }
  BIO_set_md(btmp, md);
  if (*pbio == NULL)
    *pbio = btmp;
  else if (!BIO_push(*pbio, btmp)) {
    PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
    goto err;
  }
  btmp = NULL;
  return 1;
err:
  if (btmp)
    BIO_free(btmp);
  return 0;
}


x86 assembly (clang, -O0):
<PKCS7_bio_add_digest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1030 <BIO_f_md@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b8 <PKCS7_bio_add_digest+0x48>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
jmp    1267 <PKCS7_bio_add_digest+0xf7>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <EVP_get_digestbyobj@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ee <PKCS7_bio_add_digest+0x7e>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
jmp    1267 <PKCS7_bio_add_digest+0xf7>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <BIO_set_md@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1219 <PKCS7_bio_add_digest+0xa9>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    1253 <PKCS7_bio_add_digest+0xe3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <BIO_push@plt>
cmp    $0x0,%eax
jne    124e <PKCS7_bio_add_digest+0xde>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
jmp    1267 <PKCS7_bio_add_digest+0xf7>
jmp    1253 <PKCS7_bio_add_digest+0xe3>
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x4(%rbp)
jmp    1282 <PKCS7_bio_add_digest+0x112>
cmpq   $0x0,-0x20(%rbp)
je     127b <PKCS7_bio_add_digest+0x10b>
mov    -0x20(%rbp),%rdi
call   1090 <BIO_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PKCS7_bio_add_digest>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <BIO_f_md@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
test   %rax,%rax
je     11cf <PKCS7_bio_add_digest+0x5f>
mov    %rax,%rbx
mov    (%r15),%edi
call   1060 <EVP_get_digestbyobj@plt>
test   %rax,%rax
je     11e8 <PKCS7_bio_add_digest+0x78>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <BIO_set_md@plt>
mov    (%r14),%rdi
test   %rdi,%rdi
je     120b <PKCS7_bio_add_digest+0x9b>
mov    %rbx,%rsi
call   1050 <BIO_push@plt>
mov    $0x1,%ebp
test   %eax,%eax
jne    1213 <PKCS7_bio_add_digest+0xa3>
mov    0x2e03(%rip),%rax
jmp    11ef <PKCS7_bio_add_digest+0x7f>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
jmp    1213 <PKCS7_bio_add_digest+0xa3>
mov    0x2df1(%rip),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%edi
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
mov    %rbx,%rdi
call   1090 <BIO_free@plt>
xor    %ebp,%ebp
jmp    1213 <PKCS7_bio_add_digest+0xa3>
mov    %rbx,(%r14)
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
@PKCS7_F_PKCS7_BIO_ADD_DIGEST = global i32 0
@ERR_R_BIO_LIB = global i32 0
@PKCS7_R_UNKNOWN_DIGEST_TYPE = global i32 0
define i32 @PKCS7_bio_add_digest(i32** %0, %struct.TYPE_3__* %1) {
%3 = alloca i32
%4 = alloca i32**
%5 = alloca %struct.TYPE_3__*
%6 = alloca i32*
%7 = alloca i32*
store i32** %0, i32*** %4
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %5
%8 = call i32 (...) @BIO_f_md()
%9 = call i32* @BIO_new(i32 %8)
store i32* %9, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @PKCS7_F_PKCS7_BIO_ADD_DIGEST
%13 = load i32, i32* @ERR_R_BIO_LIB
%14 = call i32 @PKCS7err(i32 %12, i32 %13)
br label %48
15: ; preds = %2
%16 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%17 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32* @EVP_get_digestbyobj(i32 %18)
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %26
22: ; preds = %15
%23 = load i32, i32* @PKCS7_F_PKCS7_BIO_ADD_DIGEST
%24 = load i32, i32* @PKCS7_R_UNKNOWN_DIGEST_TYPE
%25 = call i32 @PKCS7err(i32 %23, i32 %24)
br label %48
26: ; preds = %15
%27 = load i32*, i32** %6
%28 = load i32*, i32** %7
%29 = call i32 @BIO_set_md(i32* %27, i32* %28)
%30 = load i32**, i32*** %4
%31 = load i32*, i32** %30
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = load i32*, i32** %6
%35 = load i32**, i32*** %4
store i32* %34, i32** %35
br label %47
36: ; preds = %26
%37 = load i32**, i32*** %4
%38 = load i32*, i32** %37
%39 = load i32*, i32** %6
%40 = call i32 @BIO_push(i32* %38, i32* %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %36
%43 = load i32, i32* @PKCS7_F_PKCS7_BIO_ADD_DIGEST
%44 = load i32, i32* @ERR_R_BIO_LIB
%45 = call i32 @PKCS7err(i32 %43, i32 %44)
br label %48
46: ; preds = %36
br label %47
47: ; preds = %46, %33
store i32* null, i32** %6
store i32 1, i32* %3
br label %55
48: ; preds = %42, %22, %11
%49 = load i32*, i32** %6
%50 = icmp ne i32* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32*, i32** %6
%53 = call i32 @BIO_free(i32* %52)
br label %54
54: ; preds = %51, %48
store i32 0, i32* %3
br label %55
55: ; preds = %54, %47
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
@PKCS7_F_PKCS7_BIO_ADD_DIGEST = global i32 0
@ERR_R_BIO_LIB = global i32 0
@PKCS7_R_UNKNOWN_DIGEST_TYPE = global i32 0
define i32 @PKCS7_bio_add_digest(i32** nocapture %0, %struct.TYPE_3__* nocapture %1) {
%3 = tail call i32 (...) @BIO_f_md()
%4 = tail call i32* @BIO_new(i32 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %19, label %6
6: ; preds = %2
%7 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i32* @EVP_get_digestbyobj(i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %23, label %11
11: ; preds = %6
%12 = tail call i32 @BIO_set_md(i32* %4, i32* %9)
%13 = load i32*, i32** %0
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %11
store i32* %4, i32** %0
br label %29
16: ; preds = %11
%17 = tail call i32 @BIO_push(i32* %13, i32* %4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %23, label %29
19: ; preds = %2
%20 = load i32, i32* @PKCS7_F_PKCS7_BIO_ADD_DIGEST
%21 = load i32, i32* @ERR_R_BIO_LIB
%22 = tail call i32 @PKCS7err(i32 %20, i32 %21)
br label %29
23: ; preds = %16, %6
%24 = phi i32* [ @PKCS7_R_UNKNOWN_DIGEST_TYPE, %6 ], [ @ERR_R_BIO_LIB, %16 ]
%25 = load i32, i32* @PKCS7_F_PKCS7_BIO_ADD_DIGEST
%26 = load i32, i32* %24
%27 = tail call i32 @PKCS7err(i32 %25, i32 %26)
%28 = tail call i32 @BIO_free(i32* %4)
br label %29
29: ; preds = %19, %23, %15, %16
%30 = phi i32 [ 1, %16 ], [ 1, %15 ], [ 0, %23 ], [ 0, %19 ]
ret i32 %30
}