#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 len = evbuffer_get_length(input);
  char *data = malloc(len);
  evbuffer_copyout(input, data, len);
  if (data[len - 1] == '\n') {
    data[len - 1] = '\0';
  }
  printf("data: %s\n", data);
  free(data);
  evbuffer_add_buffer(output, input);
}


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),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11fe <echo_read_cb+0x7e>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rsi
lea    0xdf7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <evbuffer_add_buffer@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,%r15
mov    %rbx,%rdi
call   1060 <bufferevent_get_output@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1090 <evbuffer_get_length@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r12,%rdx
call   10a0 <evbuffer_copyout@plt>
cmpb   $0xa,-0x1(%r12,%rbx,1)
jne    11d0 <echo_read_cb+0x50>
movb   $0x0,-0x1(%rbx,%r12,1)
lea    0xe29(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <evbuffer_add_buffer@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 = load i64, i64* %7
%23 = sub i64 %22, 1
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 10
br i1 %27, label %28, label %33
28: ; preds = %2
%29 = load i8*, i8** %8
%30 = load i64, i64* %7
%31 = sub i64 %30, 1
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %28, %2
%34 = load i8*, i8** %8
%35 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %34)
%36 = load i8*, i8** %8
%37 = call i32 @free(i8* %36)
%38 = load %struct.evbuffer*, %struct.evbuffer** %6
%39 = load %struct.evbuffer*, %struct.evbuffer** %5
%40 = call i32 @evbuffer_add_buffer(%struct.evbuffer* %38, %struct.evbuffer* %39)
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 = add i64 %5, -1
%9 = getelementptr inbounds i8, i8* %6, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 10
br i1 %11, label %12, label %13
12: ; preds = %2
store i8 0, i8* %9
br label %13
13: ; preds = %12, %2
%14 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %6)
%15 = tail call i32 @free(i8* %6)
%16 = tail call i32 @evbuffer_add_buffer(%struct.evbuffer* %4, %struct.evbuffer* %3)
ret void
}