#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_write(int /*<<< orphan*/ *, char const *, int);
int /*<<< orphan*/ EVP_DigestUpdate(int /*<<< orphan*/ *, unsigned char const *,
                                    unsigned int);
int md_write(BIO *b, const char *in, int inl) {
  int ret = 0;
  EVP_MD_CTX *ctx;
  BIO *next;
  if ((in == NULL) || (inl <= 0))
    return 0;
  ctx = BIO_get_data(b);
  next = BIO_next(b);
  if ((ctx != NULL) && (next != NULL))
    ret = BIO_write(next, in, inl);
  if (BIO_get_init(b)) {
    if (ret > 0) {
      if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
                            (unsigned int)ret)) {
        BIO_clear_flags(b, ((0x01 | 0x02 | 0x04) | 0x08));
        return 0;
      }
    }
  }
  if (next != NULL) {
    BIO_clear_flags(b, ((0x01 | 0x02 | 0x04) | 0x08));
    BIO_copy_next_retry(b);
  }
  return ret;
}


x86 assembly (clang, -O0):
<md_write>:
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)
je     119f <md_write+0x2f>
cmpl   $0x0,-0x1c(%rbp)
jg     11ab <md_write+0x3b>
movl   $0x0,-0x4(%rbp)
jmp    1270 <md_write+0x100>
mov    -0x10(%rbp),%rdi
call   1060 <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     11ee <md_write+0x7e>
cmpq   $0x0,-0x30(%rbp)
je     11ee <md_write+0x7e>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <BIO_write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <BIO_get_init@plt>
cmp    $0x0,%rax
je     1248 <md_write+0xd8>
cmpl   $0x0,-0x20(%rbp)
jle    1243 <md_write+0xd3>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <EVP_DigestUpdate@plt>
cmp    $0x0,%eax
jne    123e <md_write+0xce>
mov    -0x10(%rbp),%rdi
mov    $0xf,%esi
call   1050 <BIO_clear_flags@plt>
movl   $0x0,-0x4(%rbp)
jmp    1270 <md_write+0x100>
jmp    1243 <md_write+0xd3>
jmp    1248 <md_write+0xd8>
cmpq   $0x0,-0x30(%rbp)
je     126a <md_write+0xfa>
mov    -0x10(%rbp),%rdi
mov    $0xf,%esi
call   1050 <BIO_clear_flags@plt>
mov    -0x10(%rbp),%rdi
call   1070 <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_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebp,%ebp
test   %rsi,%rsi
je     1211 <md_write+0xa1>
mov    %edx,%ebx
test   %edx,%edx
jle    1211 <md_write+0xa1>
mov    %rsi,%r13
mov    %rdi,%r14
call   1060 <BIO_get_data@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1090 <BIO_next@plt>
mov    %rax,%r15
xor    %ebp,%ebp
test   %r12,%r12
je     11c4 <md_write+0x54>
test   %r15,%r15
je     11c4 <md_write+0x54>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1080 <BIO_write@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <BIO_get_init@plt>
test   %rax,%rax
je     11e6 <md_write+0x76>
test   %ebp,%ebp
jle    11e6 <md_write+0x76>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1030 <EVP_DigestUpdate@plt>
test   %eax,%eax
je     1202 <md_write+0x92>
test   %r15,%r15
je     1211 <md_write+0xa1>
mov    %r14,%rdi
mov    $0xf,%esi
call   1050 <BIO_clear_flags@plt>
mov    %r14,%rdi
call   1070 <BIO_copy_next_retry@plt>
jmp    1211 <md_write+0xa1>
mov    %r14,%rdi
mov    $0xf,%esi
call   1050 <BIO_clear_flags@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @md_write(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 %16, label %13
13: ; preds = %3
%14 = load i32, i32* %7
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13, %3
store i32 0, i32* %4
br label %60
17: ; preds = %13
%18 = load i32*, i32** %5
%19 = call i32* @BIO_get_data(i32* %18)
store i32* %19, i32** %9
%20 = load i32*, i32** %5
%21 = call i32* @BIO_next(i32* %20)
store i32* %21, i32** %10
%22 = load i32*, i32** %9
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %32
24: ; preds = %17
%25 = load i32*, i32** %10
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32*, i32** %10
%29 = load i8*, i8** %6
%30 = load i32, i32* %7
%31 = call i32 @BIO_write(i32* %28, i8* %29, i32 %30)
store i32 %31, i32* %8
br label %32
32: ; preds = %27, %24, %17
%33 = load i32*, i32** %5
%34 = call i64 @BIO_get_init(i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %50
36: ; preds = %32
%37 = load i32, i32* %8
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %49
39: ; preds = %36
%40 = load i32*, i32** %9
%41 = load i8*, i8** %6
%42 = load i32, i32* %8
%43 = call i32 @EVP_DigestUpdate(i32* %40, i8* %41, i32 %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %39
%46 = load i32*, i32** %5
%47 = call i32 @BIO_clear_flags(i32* %46, i32 15)
store i32 0, i32* %4
br label %60
48: ; preds = %39
br label %49
49: ; preds = %48, %36
br label %50
50: ; preds = %49, %32
%51 = load i32*, i32** %10
%52 = icmp ne i32* %51, null
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = load i32*, i32** %5
%55 = call i32 @BIO_clear_flags(i32* %54, i32 15)
%56 = load i32*, i32** %5
%57 = call i32 @BIO_copy_next_retry(i32* %56)
br label %58
58: ; preds = %53, %50
%59 = load i32, i32* %8
store i32 %59, i32* %4
br label %60
60: ; preds = %58, %45, %16
%61 = load i32, i32* %4
ret i32 %61
}

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