#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;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EVBUFFER_EOL_LF;
struct evbuffer *bufferevent_get_input(struct bufferevent *);
struct evbuffer *bufferevent_get_output(struct bufferevent *);
int /*<<< orphan*/ evbuffer_add(struct evbuffer *, char *, int);
int evbuffer_get_length(struct evbuffer *);
char *evbuffer_readln(struct evbuffer *, size_t *, int /*<<< orphan*/);
int evbuffer_remove(struct evbuffer *, char *, int);
int /*<<< orphan*/ free(char *);
void on_read(struct bufferevent *bev, void *ctx) {
  struct evbuffer *input, *output;
  char *line;
  size_t n;
  int i;
  input = bufferevent_get_input(bev);
  output = bufferevent_get_output(bev);
  while ((line = evbuffer_readln(input, &n, EVBUFFER_EOL_LF))) {
    evbuffer_add(output, line, n);
    evbuffer_add(output, "\n", 1);
    free(line);
  }
  if (evbuffer_get_length(input) >= 16384) {
    char buf[1024];
    while (evbuffer_get_length(input)) {
      int n = evbuffer_remove(input, buf, sizeof(buf));
      evbuffer_add(output, buf, n);
    }
    evbuffer_add(output, "\n", 1);
  }
}


x86 assembly (clang, -O0):
<on_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <bufferevent_get_input@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <bufferevent_get_output@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rsi
call   1090 <evbuffer_readln@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11f7 <on_read+0x87>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
call   1070 <evbuffer_add@plt>
mov    -0x20(%rbp),%rdi
lea    0xe21(%rip),%rsi
mov    $0x1,%edx
call   1070 <evbuffer_add@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    119d <on_read+0x2d>
mov    -0x18(%rbp),%rdi
call   1080 <evbuffer_get_length@plt>
cmp    $0x4000,%eax
jl     126d <on_read+0xfd>
jmp    1210 <on_read+0xa0>
mov    -0x18(%rbp),%rdi
call   1080 <evbuffer_get_length@plt>
cmp    $0x0,%eax
je     1258 <on_read+0xe8>
mov    -0x18(%rbp),%rdi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
call   1040 <evbuffer_remove@plt>
mov    %eax,-0x444(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x440(%rbp),%rsi
mov    -0x444(%rbp),%edx
call   1070 <evbuffer_add@plt>
jmp    1210 <on_read+0xa0>
mov    -0x20(%rbp),%rdi
lea    0xd9d(%rip),%rsi
mov    $0x1,%edx
call   1070 <evbuffer_add@plt>
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<on_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%rbx
call   1060 <bufferevent_get_input@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <bufferevent_get_output@plt>
mov    %rax,%r13
mov    0x2e4a(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1090 <evbuffer_readln@plt>
test   %rax,%rax
je     120d <on_read+0x9d>
mov    %rax,%rbx
lea    0xe43(%rip),%r15
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rsp),%edx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <evbuffer_add@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <evbuffer_add@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x0(%rbp),%edx
mov    %r14,%rdi
mov    %r12,%rsi
call   1090 <evbuffer_readln@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d0 <on_read+0x60>
mov    %r14,%rdi
call   1080 <evbuffer_get_length@plt>
cmp    $0x4000,%eax
jl     126d <on_read+0xfd>
mov    %r14,%rdi
call   1080 <evbuffer_get_length@plt>
test   %eax,%eax
je     1259 <on_read+0xe9>
lea    0x10(%rsp),%rbx
nopl   (%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <evbuffer_remove@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <evbuffer_add@plt>
mov    %r14,%rdi
call   1080 <evbuffer_get_length@plt>
test   %eax,%eax
jne    1230 <on_read+0xc0>
lea    0xda0(%rip),%rsi
mov    %r13,%rdi
mov    $0x1,%edx
call   1070 <evbuffer_add@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@EVBUFFER_EOL_LF = global i32 0
@.str = [2 x i8] c"\0A\00"
define void @on_read(%struct.bufferevent* %0, i8* %1) {
%3 = alloca %struct.bufferevent*
%4 = alloca i8*
%5 = alloca %struct.evbuffer*
%6 = alloca %struct.evbuffer*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca i32
store %struct.bufferevent* %0, %struct.bufferevent** %3
store i8* %1, i8** %4
%12 = load %struct.bufferevent*, %struct.bufferevent** %3
%13 = call %struct.evbuffer* @bufferevent_get_input(%struct.bufferevent* %12)
store %struct.evbuffer* %13, %struct.evbuffer** %5
%14 = load %struct.bufferevent*, %struct.bufferevent** %3
%15 = call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %14)
store %struct.evbuffer* %15, %struct.evbuffer** %6
br label %16
16: ; preds = %21, %2
%17 = load %struct.evbuffer*, %struct.evbuffer** %5
%18 = load i32, i32* @EVBUFFER_EOL_LF
%19 = call i8* @evbuffer_readln(%struct.evbuffer* %17, i64* %8, i32 %18)
store i8* %19, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %31
21: ; preds = %16
%22 = load %struct.evbuffer*, %struct.evbuffer** %6
%23 = load i8*, i8** %7
%24 = load i64, i64* %8
%25 = trunc i64 %24 to i32
%26 = call i32 @evbuffer_add(%struct.evbuffer* %22, i8* %23, i32 %25)
%27 = load %struct.evbuffer*, %struct.evbuffer** %6
%28 = call i32 @evbuffer_add(%struct.evbuffer* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%29 = load i8*, i8** %7
%30 = call i32 @free(i8* %29)
br label %16
31: ; preds = %16
%32 = load %struct.evbuffer*, %struct.evbuffer** %5
%33 = call i32 @evbuffer_get_length(%struct.evbuffer* %32)
%34 = icmp sge i32 %33, 16384
br i1 %34, label %35, label %51
35: ; preds = %31
br label %36
36: ; preds = %40, %35
%37 = load %struct.evbuffer*, %struct.evbuffer** %5
%38 = call i32 @evbuffer_get_length(%struct.evbuffer* %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %36
%41 = load %struct.evbuffer*, %struct.evbuffer** %5
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%43 = call i32 @evbuffer_remove(%struct.evbuffer* %41, i8* %42, i32 1024)
store i32 %43, i32* %11
%44 = load %struct.evbuffer*, %struct.evbuffer** %6
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%46 = load i32, i32* %11
%47 = call i32 @evbuffer_add(%struct.evbuffer* %44, i8* %45, i32 %46)
br label %36
48: ; preds = %36
%49 = load %struct.evbuffer*, %struct.evbuffer** %6
%50 = call i32 @evbuffer_add(%struct.evbuffer* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
br label %51
51: ; preds = %48, %31
ret void
}

LLVM IR (-O2):
%struct.bufferevent = type { i32 }
%struct.evbuffer = type { i32 }
@EVBUFFER_EOL_LF = global i32 0
@.str = [2 x i8] c"\0A\00"
define void @on_read(%struct.bufferevent* %0, i8* nocapture readnone %1) {
%3 = alloca i64
%4 = alloca [1024 x i8]
%5 = bitcast i64* %3 to i8*
%6 = tail call %struct.evbuffer* @bufferevent_get_input(%struct.bufferevent* %0)
%7 = tail call %struct.evbuffer* @bufferevent_get_output(%struct.bufferevent* %0)
%8 = load i32, i32* @EVBUFFER_EOL_LF
%9 = call i8* @evbuffer_readln(%struct.evbuffer* %6, i64* %3, i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %21, label %11
11: ; preds = %2, %11
%12 = phi i8* [ %19, %11 ], [ %9, %2 ]
%13 = load i64, i64* %3
%14 = trunc i64 %13 to i32
%15 = call i32 @evbuffer_add(%struct.evbuffer* %7, i8* %12, i32 %14)
%16 = call i32 @evbuffer_add(%struct.evbuffer* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%17 = call i32 @free(i8* %12)
%18 = load i32, i32* @EVBUFFER_EOL_LF
%19 = call i8* @evbuffer_readln(%struct.evbuffer* %6, i64* %3, i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %11
21: ; preds = %11, %2
%22 = call i32 @evbuffer_get_length(%struct.evbuffer* %6)
%23 = icmp sgt i32 %22, 16383
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%26 = call i32 @evbuffer_get_length(%struct.evbuffer* %6)
%27 = icmp eq i32 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %24, %28
%29 = call i32 @evbuffer_remove(%struct.evbuffer* %6, i8* %25, i32 1024)
%30 = call i32 @evbuffer_add(%struct.evbuffer* %7, i8* %25, i32 %29)
%31 = call i32 @evbuffer_get_length(%struct.evbuffer* %6)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %28
33: ; preds = %28, %24
%34 = call i32 @evbuffer_add(%struct.evbuffer* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
br label %35
35: ; preds = %33, %21
ret void
}