#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_clear_flags(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ BIO_copy_next_retry(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_get_data(int /*<<< orphan*/ *);
scalar_t__ BIO_get_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_next(int /*<<< orphan*/ *);
int BIO_read(int /*<<< orphan*/ *, char *, int);
scalar_t__ EVP_DigestUpdate(int /*<<< orphan*/ *, unsigned char *,
                            unsigned int);
int md_read(BIO *b, char *out, int outl) {
  int ret = 0;
  EVP_MD_CTX *ctx;
  BIO *next;
  if (out == NULL)
    return (0);
  ctx = BIO_get_data(b);
  next = BIO_next(b);
  if ((ctx == NULL) || (next == NULL))
    return (0);
  ret = BIO_read(next, out, outl);
  if (BIO_get_init(b)) {
    if (ret > 0) {
      if (EVP_DigestUpdate(ctx, (unsigned char *)out, (unsigned int)ret) <= 0)
        return (-1);
    }
  }
  BIO_clear_flags(b, ((0x01 | 0x02 | 0x04) | 0x08));
  BIO_copy_next_retry(b);
  return (ret);
}


x86 assembly (clang, -O0):
<md_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a1 <md_read+0x31>
movl   $0x0,-0x4(%rbp)
jmp    125a <md_read+0xea>
mov    -0x10(%rbp),%rdi
call   1070 <BIO_get_data@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <BIO_next@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11d1 <md_read+0x61>
cmpq   $0x0,-0x30(%rbp)
jne    11dd <md_read+0x6d>
movl   $0x0,-0x4(%rbp)
jmp    125a <md_read+0xea>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <BIO_read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <BIO_get_init@plt>
cmp    $0x0,%rax
je     123d <md_read+0xcd>
cmpl   $0x0,-0x20(%rbp)
jle    1238 <md_read+0xc8>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <EVP_DigestUpdate@plt>
cmp    $0x0,%rax
jg     1233 <md_read+0xc3>
movl   $0xffffffff,-0x4(%rbp)
jmp    125a <md_read+0xea>
jmp    1238 <md_read+0xc8>
jmp    123d <md_read+0xcd>
mov    -0x10(%rbp),%rdi
mov    $0xf,%esi
call   1060 <BIO_clear_flags@plt>
mov    -0x10(%rbp),%rdi
call   1080 <BIO_copy_next_retry@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<md_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r14d,%r14d
test   %rsi,%rsi
je     11f6 <md_read+0x86>
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %rdi,%rbx
call   1070 <BIO_get_data@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1090 <BIO_next@plt>
test   %r15,%r15
je     11f6 <md_read+0x86>
test   %rax,%rax
je     11f6 <md_read+0x86>
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1030 <BIO_read@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1050 <BIO_get_init@plt>
test   %rax,%rax
je     11d9 <md_read+0x69>
test   %r14d,%r14d
jle    11d9 <md_read+0x69>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <EVP_DigestUpdate@plt>
test   %rax,%rax
jle    11f0 <md_read+0x80>
mov    %rbx,%rdi
mov    $0xf,%esi
call   1060 <BIO_clear_flags@plt>
mov    %rbx,%rdi
call   1080 <BIO_copy_next_retry@plt>
jmp    11f6 <md_read+0x86>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @md_read(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 0, i32* %4
br label %51
14: ; preds = %3
%15 = load i32*, i32** %5
%16 = call i32* @BIO_get_data(i32* %15)
store i32* %16, i32** %9
%17 = load i32*, i32** %5
%18 = call i32* @BIO_next(i32* %17)
store i32* %18, i32** %10
%19 = load i32*, i32** %9
%20 = icmp eq i32* %19, null
br i1 %20, label %24, label %21
21: ; preds = %14
%22 = load i32*, i32** %10
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %21, %14
store i32 0, i32* %4
br label %51
25: ; preds = %21
%26 = load i32*, i32** %10
%27 = load i8*, i8** %6
%28 = load i32, i32* %7
%29 = call i32 @BIO_read(i32* %26, i8* %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32*, i32** %5
%31 = call i64 @BIO_get_init(i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %45
33: ; preds = %25
%34 = load i32, i32* %8
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %33
%37 = load i32*, i32** %9
%38 = load i8*, i8** %6
%39 = load i32, i32* %8
%40 = call i64 @EVP_DigestUpdate(i32* %37, i8* %38, i32 %39)
%41 = icmp sle i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 -1, i32* %4
br label %51
43: ; preds = %36
br label %44
44: ; preds = %43, %33
br label %45
45: ; preds = %44, %25
%46 = load i32*, i32** %5
%47 = call i32 @BIO_clear_flags(i32* %46, i32 15)
%48 = load i32*, i32** %5
%49 = call i32 @BIO_copy_next_retry(i32* %48)
%50 = load i32, i32* %8
store i32 %50, i32* %4
br label %51
51: ; preds = %45, %42, %24, %13
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
define i32 @md_read(i32* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %23, label %5
5: ; preds = %3
%6 = tail call i32* @BIO_get_data(i32* %0)
%7 = tail call i32* @BIO_next(i32* %0)
%8 = icmp eq i32* %6, null
%9 = icmp eq i32* %7, null
%10 = select i1 %8, i1 true, i1 %9
br i1 %10, label %23, label %11
11: ; preds = %5
%12 = tail call i32 @BIO_read(i32* %7, i8* %1, i32 %2)
%13 = tail call i64 @BIO_get_init(i32* %0)
%14 = icmp ne i64 %13, 0
%15 = icmp sgt i32 %12, 0
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i64 @EVP_DigestUpdate(i32* %6, i8* %1, i32 %12)
%19 = icmp slt i64 %18, 1
br i1 %19, label %23, label %20
20: ; preds = %17, %11
%21 = tail call i32 @BIO_clear_flags(i32* %0, i32 15)
%22 = tail call i32 @BIO_copy_next_retry(i32* %0)
br label %23
23: ; preds = %17, %5, %3, %20
%24 = phi i32 [ %12, %20 ], [ 0, %3 ], [ 0, %5 ], [ -1, %17 ]
ret i32 %24
}