#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*/ BIO;

/* Variables and functions */
int BIO_CB_READ;
int BIO_CB_RETURN;
int BIO_CB_WRITE;
int /*<<< orphan*/ BIO_dump(int /*<<< orphan*/ *, char const *, int);
scalar_t__ BIO_get_callback_arg(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_printf(int /*<<< orphan*/ *, char *,
                              int /*<<< orphan*/ *, char const *, int, long,
                              long);
long bio_dump_cb(BIO *bio, int cmd, const char *argp, int argi, long argl,
                 long ret) {
  BIO *out;
  out = (BIO *)BIO_get_callback_arg(bio);
  if (out == NULL)
    return (ret);
  if (cmd == (BIO_CB_READ | BIO_CB_RETURN)) {
    BIO_printf(out, "read from %08X [%08lX] (%d bytes => %ld (0x%X))\n", bio,
               argp, argi, ret, ret);
    BIO_dump(out, argp, (int)ret);
    return (ret);
  } else if (cmd == (BIO_CB_WRITE | BIO_CB_RETURN)) {
    BIO_printf(out, "write to %08X [%08lX] (%d bytes => %ld (0x%X))\n", bio,
               argp, argi, ret, ret);
    BIO_dump(out, argp, (int)ret);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<bio_dump_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <BIO_get_callback_arg@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1173 <bio_dump_cb+0x43>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123d <bio_dump_cb+0x10d>
mov    -0x14(%rbp),%eax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e62(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
jne    11d8 <bio_dump_cb+0xa8>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
mov    -0x38(%rbp),%r9
mov    -0x38(%rbp),%rax
lea    0xe51(%rip),%rsi
mov    %rax,(%rsp)
call   1030 <BIO_printf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    %eax,%edx
call   1040 <BIO_dump@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123d <bio_dump_cb+0x10d>
mov    -0x14(%rbp),%eax
mov    0x2df6(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2dfd(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
jne    1230 <bio_dump_cb+0x100>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
mov    -0x38(%rbp),%r9
mov    -0x38(%rbp),%rax
lea    0xe1d(%rip),%rsi
mov    %rax,(%rsp)
call   1030 <BIO_printf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    %eax,%edx
call   1040 <BIO_dump@plt>
jmp    1235 <bio_dump_cb+0x105>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bio_dump_cb>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%rbx
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %esi,%r13d
mov    %rdi,%r12
call   1050 <BIO_get_callback_arg@plt>
test   %rax,%rax
je     11b3 <bio_dump_cb+0x83>
mov    %rax,%rbp
mov    0x2e72(%rip),%rcx
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    (%rcx),%ecx
or     %eax,%ecx
cmp    %r13d,%ecx
jne    1179 <bio_dump_cb+0x49>
lea    0xe89(%rip),%rsi
jmp    118e <bio_dump_cb+0x5e>
mov    0x2e58(%rip),%rcx
or     (%rcx),%eax
cmp    %r13d,%eax
jne    11b3 <bio_dump_cb+0x83>
lea    0xea3(%rip),%rsi
mov    %rbx,(%rsp)
mov    %rbp,%rdi
mov    %r12,%rdx
mov    %r14,%rcx
mov    %r15d,%r8d
mov    %rbx,%r9
call   1030 <BIO_printf@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <BIO_dump@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BIO_CB_READ = global i32 0
@BIO_CB_RETURN = global i32 0
@.str = [49 x i8] c"read from %08X [%08lX] (%d bytes => %ld (0x%X))\0A\00"
@BIO_CB_WRITE = global i32 0
@.str.1 = [48 x i8] c"write to %08X [%08lX] (%d bytes => %ld (0x%X))\0A\00"
define i64 @bio_dump_cb(i32* %0, i32 %1, i8* %2, i32 %3, i64 %4, i64 %5) {
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32*
store i32* %0, i32** %8
store i32 %1, i32* %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i64 %4, i64* %12
store i64 %5, i64* %13
%15 = load i32*, i32** %8
%16 = call i64 @BIO_get_callback_arg(i32* %15)
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** %14
%18 = load i32*, i32** %14
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %6
%21 = load i64, i64* %13
store i64 %21, i64* %7
br label %64
22: ; preds = %6
%23 = load i32, i32* %9
%24 = load i32, i32* @BIO_CB_READ
%25 = load i32, i32* @BIO_CB_RETURN
%26 = or i32 %24, %25
%27 = icmp eq i32 %23, %26
br i1 %27, label %28, label %42
28: ; preds = %22
%29 = load i32*, i32** %14
%30 = load i32*, i32** %8
%31 = load i8*, i8** %10
%32 = load i32, i32* %11
%33 = load i64, i64* %13
%34 = load i64, i64* %13
%35 = call i32 @BIO_printf(i32* %29, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32* %30, i8* %31, i32 %32, i64 %33, i64 %34)
%36 = load i32*, i32** %14
%37 = load i8*, i8** %10
%38 = load i64, i64* %13
%39 = trunc i64 %38 to i32
%40 = call i32 @BIO_dump(i32* %36, i8* %37, i32 %39)
%41 = load i64, i64* %13
store i64 %41, i64* %7
br label %64
42: ; preds = %22
%43 = load i32, i32* %9
%44 = load i32, i32* @BIO_CB_WRITE
%45 = load i32, i32* @BIO_CB_RETURN
%46 = or i32 %44, %45
%47 = icmp eq i32 %43, %46
br i1 %47, label %48, label %61
48: ; preds = %42
%49 = load i32*, i32** %14
%50 = load i32*, i32** %8
%51 = load i8*, i8** %10
%52 = load i32, i32* %11
%53 = load i64, i64* %13
%54 = load i64, i64* %13
%55 = call i32 @BIO_printf(i32* %49, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i32* %50, i8* %51, i32 %52, i64 %53, i64 %54)
%56 = load i32*, i32** %14
%57 = load i8*, i8** %10
%58 = load i64, i64* %13
%59 = trunc i64 %58 to i32
%60 = call i32 @BIO_dump(i32* %56, i8* %57, i32 %59)
br label %61
61: ; preds = %48, %42
br label %62
62: ; preds = %61
%63 = load i64, i64* %13
store i64 %63, i64* %7
br label %64
64: ; preds = %62, %28, %20
%65 = load i64, i64* %7
ret i64 %65
}

LLVM IR (-O2):
@BIO_CB_READ = global i32 0
@BIO_CB_RETURN = global i32 0
@.str = [49 x i8] c"read from %08X [%08lX] (%d bytes => %ld (0x%X))\0A\00"
@BIO_CB_WRITE = global i32 0
@.str.1 = [48 x i8] c"write to %08X [%08lX] (%d bytes => %ld (0x%X))\0A\00"
define i64 @bio_dump_cb(i32* %0, i32 %1, i8* %2, i32 %3, i64 %4, i64 returned %5) {
%7 = tail call i64 @BIO_get_callback_arg(i32* %0)
%8 = inttoptr i64 %7 to i32*
%9 = icmp eq i64 %7, 0
br i1 %9, label %24, label %10
10: ; preds = %6
%11 = load i32, i32* @BIO_CB_READ
%12 = load i32, i32* @BIO_CB_RETURN
%13 = or i32 %12, %11
%14 = icmp eq i32 %13, %1
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = load i32, i32* @BIO_CB_WRITE
%17 = or i32 %16, %12
%18 = icmp eq i32 %17, %1
br i1 %18, label %19, label %24
19: ; preds = %15, %10
%20 = phi i8* [ getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), %10 ], [ getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), %15 ]
%21 = tail call i32 @BIO_printf(i32* %8, i8* %20, i32* %0, i8* %2, i32 %3, i64 %5, i64 %5)
%22 = trunc i64 %5 to i32
%23 = tail call i32 @BIO_dump(i32* %8, i8* %2, i32 %22)
br label %24
24: ; preds = %19, %15, %6
ret i64 %5
}