#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 */
struct evbuffer {
  int dummy;
};
struct bufferevent {
  int dummy;
};

/* Variables and functions */
struct evbuffer *bufferevent_get_input(struct bufferevent *);
struct evbuffer *bufferevent_get_output(struct bufferevent *);
int /*<<< orphan*/ evbuffer_add_buffer(struct evbuffer *, struct evbuffer *);
int /*<<< orphan*/ evbuffer_copyout(struct evbuffer *, char *, size_t);
size_t evbuffer_get_length(struct evbuffer *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, char *);
void echo_read_cb(struct bufferevent *bev, void *ctx) {
  struct evbuffer *input = bufferevent_get_input(bev);
  struct evbuffer *output = bufferevent_get_output(bev);
  size_t length = evbuffer_get_length(input);
  char *data = malloc(length);
  evbuffer_copyout(input, data, length);
  printf("data: %s\n", data);
  evbuffer_add_buffer(output, input);
  free(data);
}


x86 assembly (clang, -O0):
<echo_read_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <bufferevent_get_input@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <bufferevent_get_output@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <evbuffer_get_length@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   10a0 <evbuffer_copyout@plt>
mov    -0x30(%rbp),%rsi
lea    0xe20(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <evbuffer_add_buffer@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_read_cb>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1070 <bufferevent_get_input@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <bufferevent_get_output@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1090 <evbuffer_get_length@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
mov    %r12,%rdx
call   10a0 <evbuffer_copyout@plt>
lea    0xe37(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <evbuffer_add_buffer@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@.str = [10 x i8] c"data: %s\0A\00"
define void @echo_read_cb(%struct.bufferevent* %0, i8* %1) {
%3 = alloca %struct.bufferevent*
%4 = alloca i8*
%5 = alloca %struct.evbuffer*
%6 = alloca %struct.evbuffer*
%7 = alloca i64
%8 = alloca i8*
store %struct.bufferevent* %0, %struct.bufferevent** %3
store i8* %1, i8** %4
%9 = load %struct.bufferevent*, %struct.bufferevent** %3
%10 = call %struct.evbuffer* @bufferevent_get_input(%struct.bufferevent* %9)
store %struct.evbuffer* %10, %struct.evbuffer** %5
%11 = load %struct.bufferevent*, %struct.bufferevent** %3
%12 = call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %11)
store %struct.evbuffer* %12, %struct.evbuffer** %6
%13 = load %struct.evbuffer*, %struct.evbuffer** %5
%14 = call i64 @evbuffer_get_length(%struct.evbuffer* %13)
store i64 %14, i64* %7
%15 = load i64, i64* %7
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %8
%17 = load %struct.evbuffer*, %struct.evbuffer** %5
%18 = load i8*, i8** %8
%19 = load i64, i64* %7
%20 = call i32 @evbuffer_copyout(%struct.evbuffer* %17, i8* %18, i64 %19)
%21 = load i8*, i8** %8
%22 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load %struct.evbuffer*, %struct.evbuffer** %6
%24 = load %struct.evbuffer*, %struct.evbuffer** %5
%25 = call i32 @evbuffer_add_buffer(%struct.evbuffer* %23, %struct.evbuffer* %24)
%26 = load i8*, i8** %8
%27 = call i32 @free(i8* %26)
ret void
}

LLVM IR (-O2):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@.str = [10 x i8] c"data: %s\0A\00"
define void @echo_read_cb(%struct.bufferevent* %0, i8* nocapture readnone %1) {
%3 = tail call %struct.evbuffer* @bufferevent_get_input(%struct.bufferevent* %0)
%4 = tail call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %0)
%5 = tail call i64 @evbuffer_get_length(%struct.evbuffer* %3)
%6 = tail call i8* @malloc(i64 %5)
%7 = tail call i32 @evbuffer_copyout(%struct.evbuffer* %3, i8* %6, i64 %5)
%8 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %6)
%9 = tail call i32 @evbuffer_add_buffer(%struct.evbuffer* %4, %struct.evbuffer* %3)
%10 = tail call i32 @free(i8* %6)
ret void
}