#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 *, unsigned long, 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] (%lu bytes => %ld (0x%lX))\n",
               (void *)bio, argp, (unsigned long)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] (%lu bytes => %ld (0x%lX))\n",
               (void *)bio, argp, (unsigned long)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
movslq -0x24(%rbp),%r8
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
movslq -0x24(%rbp),%r8
mov    -0x38(%rbp),%r9
mov    -0x38(%rbp),%rax
lea    0xe1a(%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,%r15
mov    %ecx,%r13d
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r12
call   1050 <BIO_get_callback_arg@plt>
test   %rax,%rax
je     11b1 <bio_dump_callback+0x81>
mov    %rax,%rbx
mov    0x2e73(%rip),%rcx
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    (%rcx),%ecx
or     %eax,%ecx
cmp    %ebp,%ecx
jne    1177 <bio_dump_callback+0x47>
lea    0xe8b(%rip),%rsi
jmp    118b <bio_dump_callback+0x5b>
mov    0x2e5a(%rip),%rcx
or     (%rcx),%eax
cmp    %ebp,%eax
jne    11b1 <bio_dump_callback+0x81>
lea    0xea3(%rip),%rsi
movslq %r13d,%r8
mov    %r15,(%rsp)
mov    %rbx,%rdi
mov    %r12,%rdx
mov    %r14,%rcx
mov    %r15,%r9
call   1030 <BIO_printf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <BIO_dump@plt>
mov    %r15,%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 = [46 x i8] c"read from %p [%p] (%lu bytes => %ld (0x%lX))\0A\00"
@BIO_CB_WRITE = global i32 0
@.str.1 = [45 x i8] c"write to %p [%p] (%lu 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 %68
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 %44
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 = sext i32 %33 to i64
%35 = load i64, i64* %13
%36 = load i64, i64* %13
%37 = call i32 @BIO_printf(i32* %29, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %32, i64 %34, i64 %35, i64 %36)
%38 = load i32*, i32** %14
%39 = load i8*, i8** %10
%40 = load i64, i64* %13
%41 = trunc i64 %40 to i32
%42 = call i32 @BIO_dump(i32* %38, i8* %39, i32 %41)
%43 = load i64, i64* %13
store i64 %43, i64* %7
br label %68
44: ; preds = %22
%45 = load i32, i32* %9
%46 = load i32, i32* @BIO_CB_WRITE
%47 = load i32, i32* @BIO_CB_RETURN
%48 = or i32 %46, %47
%49 = icmp eq i32 %45, %48
br i1 %49, label %50, label %65
50: ; preds = %44
%51 = load i32*, i32** %14
%52 = load i32*, i32** %8
%53 = bitcast i32* %52 to i8*
%54 = load i8*, i8** %10
%55 = load i32, i32* %11
%56 = sext i32 %55 to i64
%57 = load i64, i64* %13
%58 = load i64, i64* %13
%59 = call i32 @BIO_printf(i32* %51, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %53, i8* %54, i64 %56, i64 %57, i64 %58)
%60 = load i32*, i32** %14
%61 = load i8*, i8** %10
%62 = load i64, i64* %13
%63 = trunc i64 %62 to i32
%64 = call i32 @BIO_dump(i32* %60, i8* %61, i32 %63)
br label %65
65: ; preds = %50, %44
br label %66
66: ; preds = %65
%67 = load i64, i64* %13
store i64 %67, i64* %7
br label %68
68: ; preds = %66, %28, %20
%69 = load i64, i64* %7
ret i64 %69
}

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