#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 *, void *,
                              char const *, int, long, long);
long bio_dump_callback(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 %p [%p] (%d bytes => %ld (0x%lX))\n",
               (void *)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 %p [%p] (%d bytes => %ld (0x%lX))\n", (void *)bio,
               argp, argi, ret, ret);
    BIO_dump(out, argp, (int)ret);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<bio_dump_callback>:
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_callback+0x43>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123d <bio_dump_callback+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_callback+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_callback+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_callback+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    0xe19(%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_callback+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_callback>:
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_callback+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_callback+0x49>
lea    0xe89(%rip),%rsi
jmp    118e <bio_dump_callback+0x5e>
mov    0x2e58(%rip),%rcx
or     (%rcx),%eax
cmp    %r13d,%eax
jne    11b3 <bio_dump_callback+0x83>
lea    0xe9f(%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 = [45 x i8] c"read from %p [%p] (%d bytes => %ld (0x%lX))\0A\00"
@BIO_CB_WRITE = global i32 0
@.str.1 = [44 x i8] c"write to %p [%p] (%d bytes => %ld (0x%lX))\0A\00"
define i64 @bio_dump_callback(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 %66
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 %43
28: ; preds = %22
%29 = load i32*, i32** %14
%30 = load i32*, i32** %8
%31 = bitcast i32* %30 to i8*
%32 = load i8*, i8** %10
%33 = load i32, i32* %11
%34 = load i64, i64* %13
%35 = load i64, i64* %13
%36 = call i32 @BIO_printf(i32* %29, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %32, i32 %33, i64 %34, i64 %35)
%37 = load i32*, i32** %14
%38 = load i8*, i8** %10
%39 = load i64, i64* %13
%40 = trunc i64 %39 to i32
%41 = call i32 @BIO_dump(i32* %37, i8* %38, i32 %40)
%42 = load i64, i64* %13
store i64 %42, i64* %7
br label %66
43: ; preds = %22
%44 = load i32, i32* %9
%45 = load i32, i32* @BIO_CB_WRITE
%46 = load i32, i32* @BIO_CB_RETURN
%47 = or i32 %45, %46
%48 = icmp eq i32 %44, %47
br i1 %48, label %49, label %63
49: ; preds = %43
%50 = load i32*, i32** %14
%51 = load i32*, i32** %8
%52 = bitcast i32* %51 to i8*
%53 = load i8*, i8** %10
%54 = load i32, i32* %11
%55 = load i64, i64* %13
%56 = load i64, i64* %13
%57 = call i32 @BIO_printf(i32* %50, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %52, i8* %53, i32 %54, i64 %55, i64 %56)
%58 = load i32*, i32** %14
%59 = load i8*, i8** %10
%60 = load i64, i64* %13
%61 = trunc i64 %60 to i32
%62 = call i32 @BIO_dump(i32* %58, i8* %59, i32 %61)
br label %63
63: ; preds = %49, %43
br label %64
64: ; preds = %63
%65 = load i64, i64* %13
store i64 %65, i64* %7
br label %66
66: ; preds = %64, %28, %20
%67 = load i64, i64* %7
ret i64 %67
}

LLVM IR (-O2):
@BIO_CB_READ = global i32 0
@BIO_CB_RETURN = global i32 0
@.str = [45 x i8] c"read from %p [%p] (%d bytes => %ld (0x%lX))\0A\00"
@BIO_CB_WRITE = global i32 0
@.str.1 = [44 x i8] c"write to %p [%p] (%d bytes => %ld (0x%lX))\0A\00"
define i64 @bio_dump_callback(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 %25, 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 %25
19: ; preds = %15, %10
%20 = phi i8* [ getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), %10 ], [ getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), %15 ]
%21 = bitcast i32* %0 to i8*
%22 = tail call i32 @BIO_printf(i32* %8, i8* %20, i8* %21, i8* %2, i32 %3, i64 %5, i64 %5)
%23 = trunc i64 %5 to i32
%24 = tail call i32 @BIO_dump(i32* %8, i8* %2, i32 %23)
br label %25
25: ; preds = %19, %15, %6
ret i64 %5
}